]> git.proxmox.com Git - mirror_xterm.js.git/blame - demo/main.js
Fix #488: Implement configurable tab width
[mirror_xterm.js.git] / demo / main.js
CommitLineData
e0c0fb69
DI
1var term,
2 protocol,
3 socketURL,
f57a40ee 4 socket,
0f8fc63a
PK
5 pid,
6 charWidth,
7 charHeight;
8
9var terminalContainer = document.getElementById('terminal-container'),
10 optionElements = {
5a932b2a 11 cursorBlink: document.querySelector('#option-cursor-blink'),
f4293a6d
PK
12 scrollback: document.querySelector('#option-scrollback'),
13 tabstopwidth: document.querySelector('#option-tabstopwidth')
0f8fc63a
PK
14 },
15 colsElement = document.getElementById('cols'),
16 rowsElement = document.getElementById('rows');
17
18function setTerminalSize () {
19 var cols = parseInt(colsElement.value),
20 rows = parseInt(rowsElement.value),
21 width = (cols * charWidth).toString() + 'px',
22 height = (rows * charHeight).toString() + 'px';
23
24 terminalContainer.style.width = width;
25 terminalContainer.style.height = height;
26 term.resize(cols, rows);
27}
e0c0fb69 28
0f8fc63a
PK
29colsElement.addEventListener('change', setTerminalSize);
30rowsElement.addEventListener('change', setTerminalSize);
e0c0fb69 31
7679475b
DI
32optionElements.cursorBlink.addEventListener('change', function () {
33 term.setOption('cursorBlink', optionElements.cursorBlink.checked);
34});
5a932b2a 35optionElements.scrollback.addEventListener('change', function () {
f4293a6d
PK
36 term.setOption('scrollback', parseInt(optionElements.scrollback.value, 10));
37});
38optionElements.tabstopwidth.addEventListener('change', function () {
39 term.setOption('tabStopWidth', parseInt(optionElements.tabstopwidth.value));
40 term.setupStops();
5a932b2a 41});
e0c0fb69
DI
42
43createTerminal();
44
45function createTerminal() {
f57a40ee 46 // Clean terminal
e0c0fb69
DI
47 while (terminalContainer.children.length) {
48 terminalContainer.removeChild(terminalContainer.children[0]);
49 }
50 term = new Terminal({
5a932b2a 51 cursorBlink: optionElements.cursorBlink.checked,
f4293a6d
PK
52 scrollback: parseInt(optionElements.scrollback.value, 10),
53 tabStopWidth: parseInt(optionElements.tabstopwidth.value)
e0c0fb69 54 });
f57a40ee
PK
55 term.on('resize', function (size) {
56 if (!pid) {
57 return;
58 }
0f8fc63a
PK
59 var cols = size.cols,
60 rows = size.rows,
61 url = '/terminals/' + pid + '/size?cols=' + cols + '&rows=' + rows;
62
63 fetch(url, {method: 'POST'});
f57a40ee 64 });
e0c0fb69 65 protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://';
f57a40ee 66 socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + '/terminals/';
e0c0fb69
DI
67
68 term.open(terminalContainer);
0f8fc63a 69 term.fit();
e0c0fb69 70
f57a40ee
PK
71 var initialGeometry = term.proposeGeometry(),
72 cols = initialGeometry.cols,
73 rows = initialGeometry.rows;
74
0f8fc63a
PK
75 colsElement.value = cols;
76 rowsElement.value = rows;
77
f57a40ee 78 fetch('/terminals?cols=' + cols + '&rows=' + rows, {method: 'POST'}).then(function (res) {
0f8fc63a
PK
79
80 charWidth = Math.ceil(term.element.offsetWidth / cols);
81 charHeight = Math.ceil(term.element.offsetHeight / rows);
82
f57a40ee
PK
83 res.text().then(function (pid) {
84 window.pid = pid;
85 socketURL += pid;
86 socket = new WebSocket(socketURL);
87 socket.onopen = runRealTerminal;
88 socket.onclose = runFakeTerminal;
89 socket.onerror = runFakeTerminal;
90 });
91 });
e0c0fb69 92}
dc3946f6 93
53e8df40
PK
94function runRealTerminal() {
95 term.attach(socket);
96 term._initialized = true;
97}
c5ae1cea 98
53e8df40
PK
99function runFakeTerminal() {
100 if (term._initialized) {
101 return;
102 }
103
104 term._initialized = true;
105
106 var shellprompt = '$ ';
107
108 term.prompt = function () {
109 term.write('\r\n' + shellprompt);
110 };
c5ae1cea 111
53e8df40
PK
112 term.writeln('Welcome to xterm.js');
113 term.writeln('This is a local terminal emulation, without a real terminal in the back-end.');
114 term.writeln('Type some keys and commands to play around.');
115 term.writeln('');
116 term.prompt();
46bb446e 117
53e8df40
PK
118 term.on('key', function (key, ev) {
119 var printable = (
120 !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey
121 );
122
123 if (ev.keyCode == 13) {
124 term.prompt();
125 } else if (ev.keyCode == 8) {
0f8fc63a 126 // Do not delete the prompt
53e8df40
PK
127 if (term.x > 2) {
128 term.write('\b \b');
129 }
130 } else if (printable) {
131 term.write(key);
567b39dc 132 }
53e8df40
PK
133 });
134
135 term.on('paste', function (data, ev) {
136 term.write(data);
137 });
138}