Repository navigation
Expand file tree
/
Copy pathapp.terminal.js
More file actions
798 lines (749 loc) · 29.8 KB
/
Copy pathapp.terminal.js
File metadata and controls
798 lines (749 loc) · 29.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
'use strict';
/* ============================================================================
* SerialCanvas - terminal mode (part 2 of 3)
* xterm.js based terminal interaction mode: raw byte passthrough, optional
* local echo/line editing, skins, fonts and view switching. Continues the
* IIFE opened in app.core.js.
* ==========================================================================*/
function isTerminalViewActive() {
return state.ui.monitorView === 'terminal';
}
/* Printable-run buffer for the paste debounce. Runtime-only, never persisted
and never part of terminalSession (that is the local-echo line, which is a
different thing). */
const TERMINAL_KEY_FLUSH_MS = 40;
let terminalKeyBuffer = '';
let terminalKeyFlushTimer = 0;
const TERMINAL_SKINS = {
green: {
background: '#0b0f0b',
foreground: '#33ff66',
cursor: '#33ff66',
cursorAccent: '#0b0f0b',
selectionBackground: 'rgba(51, 255, 102, 0.28)'
},
'black-white': {
background: '#000000',
foreground: '#e8e8ea',
cursor: '#e8e8ea',
cursorAccent: '#000000',
selectionBackground: 'rgba(232, 232, 234, 0.28)'
},
dracula: {
background: '#282a36',
foreground: '#f8f8f2',
cursor: '#f8f8f2',
cursorAccent: '#282a36',
selectionBackground: '#44475a'
},
'solarized-dark': {
background: '#002b36',
foreground: '#839496',
cursor: '#93a1a1',
cursorAccent: '#002b36',
selectionBackground: '#073642'
},
'solarized-light': {
background: '#fdf6e3',
foreground: '#657b83',
cursor: '#586e75',
cursorAccent: '#fdf6e3',
selectionBackground: '#eee8d5'
},
'one-dark': {
background: '#282c34',
foreground: '#abb2bf',
cursor: '#528bff',
cursorAccent: '#282c34',
selectionBackground: '#3e4451'
}
};
function getTerminalTheme() {
const skin = TERMINAL_SKINS[refs.termSkin?.value || ''];
if (skin) {
return { ...skin };
}
const style = getComputedStyle(document.body);
const read = (name, fallback) => style.getPropertyValue(name).trim() || fallback;
// The console keeps its own palette rather than borrowing the page's:
// --bg-terminal is dark in both themes, so reading --text here paired a
// dark foreground with a dark ground in light mode (contrast 1.06).
return {
background: read('--bg-terminal', '#1b1918'),
foreground: read('--terminal-text', '#f4f1ef'),
cursor: read('--terminal-cursor', '#ff9783'),
cursorAccent: read('--bg-terminal', '#1b1918'),
selectionBackground: read('--terminal-selection', 'rgba(255, 151, 131, 0.28)')
};
}
function updateTerminalTheme() {
const theme = getTerminalTheme();
if (refs.termOutput) {
refs.termOutput.style.background = theme.background;
}
if (terminalRuntime.term) {
terminalRuntime.term.options.theme = theme;
}
}
function getTerminalFontFamily() {
const value = refs.termFont?.value || '';
if (value) return value;
return getComputedStyle(document.body).getPropertyValue('--font-mono').trim() || 'Consolas, monospace';
}
function clampTerminalFontSize(value) {
const parsed = Number(value);
if (!Number.isFinite(parsed)) return TERMINAL_FONT_SIZE_DEFAULT;
return Math.min(TERMINAL_FONT_SIZE_MAX, Math.max(TERMINAL_FONT_SIZE_MIN, Math.round(parsed)));
}
function getTerminalFontSize() {
return clampTerminalFontSize(refs.monoSizeInput?.value);
}
/* xterm scrollback, settable from the toolbar. Cheap compared with the DOM
log: only text nodes are retained and changing it does not re-render. */
function getTerminalScrollback() {
var size = Math.round(Number(state.settings.terminalScrollback));
if (!Number.isFinite(size)) return TERMINAL_SCROLLBACK_DEFAULT;
return Math.min(TERMINAL_SCROLLBACK_MAX, Math.max(TERMINAL_SCROLLBACK_MIN, size));
}
function setTerminalScrollback(value) {
var size = Math.round(Number(value));
if (!Number.isFinite(size)) return;
size = Math.min(TERMINAL_SCROLLBACK_MAX, Math.max(TERMINAL_SCROLLBACK_MIN, size));
state.settings.terminalScrollback = size;
const term = terminalRuntime.term;
if (term) term.options.scrollback = size;
}
function terminalHostVisible() {
return Boolean(refs.termOutput && refs.termOutput.offsetParent !== null);
}
// xterm measures its cell size from the live host element, so a font or size
// change made while the terminal is hidden (manual view, expanded layout) is
// lost: the metrics come back as 0 and FitAddon quietly no-ops. Park the
// change and re-apply it from applyMonitorView() once the host is shown.
function applyTerminalMetrics() {
const term = terminalRuntime.term;
if (!term) return;
const fontFamily = getTerminalFontFamily();
const fontSize = getTerminalFontSize();
const stale = term.options.fontFamily !== fontFamily || term.options.fontSize !== fontSize;
if (stale) {
term.options.fontFamily = fontFamily;
term.options.fontSize = fontSize;
}
if (!terminalHostVisible()) {
terminalRuntime.pendingApply = true;
return;
}
const wasPending = terminalRuntime.pendingApply;
terminalRuntime.pendingApply = false;
if (wasPending) {
// The options setter only re-measures on an actual value change, so the
// cached 0 metrics survive a hidden edit; one step up and back forces it.
term.options.fontSize = fontSize + 1;
term.options.fontSize = fontSize;
}
if (stale || wasPending) {
try {
term.refresh(0, term.rows - 1);
} catch (error) {}
}
refitTerminal();
}
function updateTerminalFont() {
applyMonoSize();
applyTerminalMetrics();
}
/* One size control feeds three surfaces: xterm, the manual log and the parse
preview. The two CSS tokens stay separate on purpose (see style.css) but
both follow this value. */
function applyMonoSize() {
const size = getTerminalFontSize();
document.documentElement.style.setProperty('--mono-size', size + 'px');
document.documentElement.style.setProperty('--log-size', size + 'px');
}
function setTerminalFontSize(value) {
const size = clampTerminalFontSize(value);
// The combo input is the single font-size control now: the wireCombo
// helper has already written the clamped value back into it.
if (refs.monoSizeInput && refs.monoSizeInput.value !== String(size)) {
refs.monoSizeInput.value = String(size);
}
applyMonoSize();
applyTerminalMetrics();
}
function handleTerminalWheel(event) {
if (!event.ctrlKey) return;
event.preventDefault();
const direction = event.deltaY < 0 ? 1 : -1;
const next = getTerminalFontSize() + direction;
if (clampTerminalFontSize(next) === getTerminalFontSize()) return;
setTerminalFontSize(next);
scheduleLocalPrefsSave();
}
// Every offered stack ends in `monospace`, so an uninstalled family renders
// exactly like the default and the switch looks broken. Say so instead.
function applyTerminalFontAvailability() {
const select = refs.termFont;
if (!select) return;
Array.from(select.options).forEach((option) => {
if (!option.value) {
option.disabled = false;
option.removeAttribute('title');
return;
}
const available = fontStackAvailable(option.value);
option.disabled = !available;
if (available) option.removeAttribute('title');
else option.setAttribute('title', '未检测到该字体,已回退为默认等宽字体');
});
syncCustomSelectShell(
select.parentElement?.classList.contains('custom-select') ? select.parentElement : null,
select
);
}
function ensureTerminalInstance() {
if (terminalRuntime.term || typeof Terminal === 'undefined' || typeof FitAddon === 'undefined') {
return terminalRuntime.term;
}
const host = refs.termOutput;
if (!host) return null;
const term = new Terminal({
fontFamily: getTerminalFontFamily(),
fontSize: getTerminalFontSize(),
lineHeight: 1.25,
cursorBlink: true,
cursorStyle: 'block',
scrollback: getTerminalScrollback(),
convertEol: false,
theme: getTerminalTheme()
});
const fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
term.open(host);
term.onData(queueTerminalKeyChunk);
host.addEventListener('wheel', handleTerminalWheel, { passive: false });
try {
fitAddon.fit();
} catch (error) {}
terminalRuntime.term = term;
terminalRuntime.fitAddon = fitAddon;
if (typeof ResizeObserver !== 'undefined') {
terminalRuntime.resizeObserver = new ResizeObserver(() => refitTerminal());
terminalRuntime.resizeObserver.observe(host);
}
return term;
}
function refitTerminal() {
const { term, fitAddon } = terminalRuntime;
if (!term || !fitAddon || !isTerminalViewActive()) return;
try {
fitAddon.fit();
} catch (error) {}
}
function termEchoText(text) {
if (!text || !refs.termLocalEcho?.checked) return;
terminalRuntime.term?.write(text);
}
function appendTermRx(text) {
const term = terminalRuntime.term;
if (!term || !text) return;
// Normalize a bare \n to \r\n for xterm; keep a lone \r (devices may use it for in-place redraws)
term.write(String(text).replace(/\r?\n/g, '\r\n'));
termEchoText(terminalSession.buffer);
}
function getTermLineEnding() {
const mode = refs.termNewline?.value || 'rn';
return mode === 'none' ? '' : mode === 'r' ? '\r' : mode === 'n' ? '\n' : '\r\n';
}
function clearTerminalOutput() {
const term = terminalRuntime.term;
if (!term) return;
term.clear();
term.write('\x1b[2K');
}
function handleTermData(data) {
if (!data) return;
if (!refs.termLocalEcho?.checked) {
// Passthrough mode: forward raw keystrokes to the device like a real terminal (Enter mapped to the selected line ending)
const payload = data === '\r'
? (getTermLineEnding() || '\r')
: data;
sendTerminalBytes(payload);
return;
}
// Local echo / line-editing mode (for devices that do not echo themselves)
if (data === '\r') {
commitTerminalLine();
return;
}
if (data === '\x7f') {
if (terminalSession.buffer.length) {
terminalSession.buffer = terminalSession.buffer.slice(0, -1);
termEchoText('\b \b');
}
return;
}
if (data === '\x15') {
terminalSession.buffer = '';
termEchoText('\r\x1b[2K');
return;
}
if (data === '\x1b[A') {
navigateTermHistory('up');
return;
}
if (data === '\x1b[B') {
navigateTermHistory('down');
return;
}
if (data.length === 1 && data.charCodeAt(0) < 32 && data !== '\t') {
// Other control keys (Ctrl+C etc.) are sent raw, with a local ^X marker displayed
const label = `^${String.fromCharCode(data.charCodeAt(0) + 64)}`;
sendTerminalBytes(data);
terminalSession.buffer = '';
termEchoText(`${label}\r\n`);
return;
}
if (data.startsWith('\x1b')) {
sendTerminalBytes(data);
return;
}
terminalSession.buffer += data;
termEchoText(String(data).replace(/\r\n|\r|\n/g, '\r\n'));
}
/* Byte sequences for the toolbar buttons.
These are the exact sequences xterm reports through onData for the same
keys, so a button and a keypress take the identical path through
handleTermData(). Nothing here re-implements the echo/passthrough split or
the line editor.
The Ctrl entries are single bytes, which is what makes a binding like ^R
meaningful over a serial line: it reaches the device as 0x12 and is
interpreted by whatever readline is running there — including fzf, where
^R is reverse-i-search and ^T/^N/^P drive the selection. These are the
bytes, not browser keybindings, so the browser never sees them.
Every sequence below was read out of vendor/xterm/xterm.js rather than
assumed, because a wrong guess would send bytes the device never sees:
Ctrl-arrows o.key = ESC + "[1;" + (modifier + 1) + "D" -> 1;5
Shift-Tab case 9: if (e.shiftKey) o.key = ESC + "[Z"
Insert ESC + "[2~"
F1-F4 ESC + "OP" / "OQ" / "OR" / "OS"
Enter case 13: o.key = e.altKey ? ESC + CR : CR
That last line is also why there is no Ctrl-Enter binding: xterm's case 13
never consults the ctrl flag, so Ctrl+Enter and Enter produce identical
bytes. A button for it would claim a distinction that does not exist. */
const TERM_KEY_SEQUENCES = {
// Submission. ^M and ^J are the raw CR/LF bytes: Enter follows the
// newline-mode setting, so with the mode at CR there is no way to send a
// bare LF — and parser testing regularly wants exactly one byte.
enter: '\r',
'alt-enter': '\x1b\r',
'ctrl-m': '\r',
'ctrl-j': '\n',
// Navigation
up: '\x1b[A',
down: '\x1b[B',
right: '\x1b[C',
left: '\x1b[D',
'ctrl-right': '\x1b[1;5C',
'ctrl-left': '\x1b[1;5D',
home: '\x1b[H',
end: '\x1b[F',
pageup: '\x1b[5~',
pagedown: '\x1b[6~',
insert: '\x1b[2~',
delete: '\x1b[3~',
'shift-tab': '\x1b[Z',
'alt-b': '\x1bb',
'alt-f': '\x1bf',
f1: '\x1bOP',
f2: '\x1bOQ',
f3: '\x1bOR',
f4: '\x1bOS',
// F5-F12: the standard xterm modifier-less codes. xterm already emits
// these for the physical keys; the buttons exist so the bar shows them
// and touch/trackpad users can reach them.
f5: '\x1b[15~',
f6: '\x1b[17~',
f7: '\x1b[18~',
f8: '\x1b[19~',
f9: '\x1b[20~',
f10: '\x1b[21~',
f11: '\x1b[23~',
f12: '\x1b[24~',
// Editing. backspace sends DEL 0x7f; ^H sends BS 0x08 — some firmware
// honours only one of the two, so both are bound.
backspace: '\x7f',
'ctrl-h': '\x08',
'ctrl-a': '\x01',
'ctrl-e': '\x05',
'ctrl-b': '\x02',
'ctrl-f': '\x06',
'ctrl-k': '\x0b',
'ctrl-u': '\x15',
'ctrl-w': '\x17',
'alt-backspace': '\x1b\x7f',
'alt-d': '\x1bd',
'ctrl-t': '\x14',
'ctrl-y': '\x19',
'ctrl-d': '\x04',
// History and completion — the fzf-facing group
'ctrl-r': '\x12',
'ctrl-p': '\x10',
'ctrl-n': '\x0e',
'ctrl-o': '\x0f',
'ctrl-l': '\x0c',
// Control bytes. ^\ joins ^C/^D/^Z/^G as a real interrupt (SIGQUIT);
// ^S/^Q are flow control, deliberately NOT here — the amber styling
// marks bytes that interrupt something, and freezing output is not that.
'ctrl-c': '\x03',
'ctrl-d': '\x04',
'ctrl-z': '\x1a',
'ctrl-g': '\x07',
'ctrl-backslash': '\x1c',
'ctrl-s': '\x13',
'ctrl-q': '\x11',
'ctrl-v': '\x16',
'ctrl-x': '\x18',
tab: '\t',
escape: '\x1b'
};
/* Button grouping for the key bar.
Grouped by what the key DOES rather than by what it is called, which the
previous five groups did not:
- 历史 ↑↓ sat in "nav" while ^P/^N — the same function — sat in
"history" on the other row, with the whole edit group between them;
- ^T 转置字符 sat in "edit" while ^X 切换转置 sat in "control";
- ^A/^E/^B/^F are movement but were filed under "edit" between Bksp and
^K, because the Emacs letter made them look like editing keys;
- "control" carried ^V, ^X, Tab and Esc, none of which interrupt
anything, while that group's amber styling exists precisely to mark the
bytes that do. Four of its eight keys were false alarms.
Order is reading order: submit first, because Enter is the only key that
sends the line; then recall, movement, editing; then the screen/function
keys; and the interrupts last, so muscle memory reaching for an editing
key cannot land on ^C. Within a group the order is one the user can
predict: history is arrows then their Emacs equivalents then search;
movement goes character → word → line → Emacs → Alt; editing goes by
deletion granularity, smallest first, with ^X beside ^T because both are
transpose. */
const TERM_KEY_GROUPS = [
{
id: 'submit',
buttons: [
{ key: 'enter', label: 'Enter ⏎', tip: 'Enter 回车(遵循换行设置)', wide: true },
{ key: 'alt-enter', label: 'Alt-⏎', tip: 'Alt-Enter 发送 ESC + CR' },
{ key: 'ctrl-m', label: '^M', tip: '^M 裸 CR(0x0D,不受换行设置影响)' },
{ key: 'ctrl-j', label: '^J', tip: '^J 裸 LF(0x0A,不受换行设置影响)' }
]
},
{
id: 'history',
buttons: [
{ key: 'up', label: '↑', tip: '↑ 上一个历史' },
{ key: 'down', label: '↓', tip: '↓ 下一个历史' },
{ key: 'ctrl-p', label: '^P', tip: '^P 上一条历史' },
{ key: 'ctrl-n', label: '^N', tip: '^N 下一条历史' },
{ key: 'ctrl-r', label: '^R', tip: '^R 反向增量搜索(fzf 常用)' },
{ key: 'ctrl-o', label: '^O', tip: '^O 轮换历史' },
{ key: 'ctrl-l', label: '^L', tip: '^L 清屏' }
]
},
{
id: 'move',
buttons: [
{ key: 'left', label: '←', tip: '← 光标左移' },
{ key: 'right', label: '→', tip: '→ 光标右移' },
{ key: 'ctrl-left', label: '^←', tip: '^← 按词左移' },
{ key: 'ctrl-right', label: '^→', tip: '^→ 按词右移' },
{ key: 'home', label: 'Home', tip: 'Home 行首' },
{ key: 'end', label: 'End', tip: 'End 行尾' },
{ key: 'ctrl-b', label: '^B', tip: '^B 左移一字符' },
{ key: 'ctrl-f', label: '^F', tip: '^F 右移一字符' },
{ key: 'ctrl-a', label: '^A', tip: '^A 行首' },
{ key: 'ctrl-e', label: '^E', tip: '^E 行尾' },
{ key: 'alt-b', label: 'Alt-B', tip: 'Alt-B 按词左移(经典绑定)' },
{ key: 'alt-f', label: 'Alt-F', tip: 'Alt-F 按词右移(经典绑定)' }
]
},
{
id: 'edit',
buttons: [
{ key: 'backspace', label: 'Bksp', tip: 'Backspace 退格(DEL 0x7F)' },
{ key: 'ctrl-h', label: '^H', tip: '^H 退格(BS 0x08,部分固件只认这个字节)' },
{ key: 'delete', label: 'Del', tip: 'Delete 删除' },
{ key: 'ctrl-k', label: '^K', tip: '^K 删到行尾' },
{ key: 'ctrl-u', label: '^U', tip: '^U 删到行首' },
{ key: 'ctrl-w', label: '^W', tip: '^W 删前一个词' },
{ key: 'alt-backspace', label: 'Alt-⌫', tip: 'Alt-Backspace 删前一个词(ESC + DEL)' },
{ key: 'alt-d', label: 'Alt-D', tip: 'Alt-D 删后一个词(ESC + d)' },
{ key: 'ctrl-t', label: '^T', tip: '^T 转置字符' },
{ key: 'ctrl-x', label: '^X', tip: '^X 切换转置' },
{ key: 'ctrl-y', label: '^Y', tip: '^Y 召回' },
{ key: 'ctrl-v', label: '^V', tip: '^V 引号插入' }
]
},
{
id: 'page',
buttons: [
{ key: 'pageup', label: 'PgUp', tip: 'PgUp 上翻一屏' },
{ key: 'pagedown', label: 'PgDn', tip: 'PgDn 下翻一屏' },
{ key: 'insert', label: 'Ins', tip: 'Insert 插入模式' },
{ key: 'shift-tab', label: '⇧Tab', tip: 'Shift-Tab 反向补全(越过补全菜单)' },
{ key: 'tab', label: 'Tab', tip: 'Tab 制表符' },
{ key: 'escape', label: 'Esc', tip: 'Esc ESC' }
]
},
{
id: 'flow',
buttons: [
{ key: 'ctrl-s', label: '^S', tip: '^S XOFF 暂停输出(0x13)——误按后屏幕冻结就用它' },
{ key: 'ctrl-q', label: '^Q', tip: '^Q XON 恢复输出(0x11)' }
]
},
{
id: 'control',
buttons: [
{ key: 'ctrl-c', label: '^C', tip: '^C 中断前台进程(0x03)' },
{ key: 'ctrl-d', label: '^D', tip: '^D EOF / 注销(0x04)' },
{ key: 'ctrl-z', label: '^Z', tip: '^Z 挂起到后台(0x1A)' },
{ key: 'ctrl-g', label: '^G', tip: '^G 中止' },
{ key: 'ctrl-backslash', label: '^\\', tip: '^\\ SIGQUIT(0x1C)' }
]
},
{
id: 'fkeys',
buttons: [
{ key: 'f1', label: 'F1', tip: 'F1(部分固件菜单绑定)' },
{ key: 'f2', label: 'F2', tip: 'F2' },
{ key: 'f3', label: 'F3', tip: 'F3' },
{ key: 'f4', label: 'F4', tip: 'F4' },
{ key: 'f5', label: 'F5', tip: 'F5' },
{ key: 'f6', label: 'F6', tip: 'F6' },
{ key: 'f7', label: 'F7', tip: 'F7' },
{ key: 'f8', label: 'F8', tip: 'F8' },
{ key: 'f9', label: 'F9', tip: 'F9' },
{ key: 'f10', label: 'F10', tip: 'F10' },
{ key: 'f11', label: 'F11', tip: 'F11' },
{ key: 'f12', label: 'F12', tip: 'F12' }
]
}
];
/* Which row of the key bar each group lands on.
60 keys in one row wrapped wherever the window happened to end, so the bar
changed height as the window resized and shoved the terminal around. Fixed
rows keep the bar's height constant.
Three rows, measured at 60 keys: line input and navigation on top, text
and screen manipulation in the middle, and the device-affecting keys last
— flow, the amber interrupts, and the function keys the firmware eats.
Amber at the very end of the bar is the point: the "read twice before
pressing" keys should not sit where an editing reflex lands. audit/
verify-keybar.mjs holds the row split and balance. */
const TERM_KEY_ROWS = [
['submit', 'history', 'move'],
['edit', 'page'],
['flow', 'control', 'fkeys']
];
/* Buttons must work without the terminal holding focus, so this dispatches
through handleTermData() rather than relying on xterm's onData. */
function sendTerminalKey(key) {
const sequence = TERM_KEY_SEQUENCES[key];
if (!sequence) return;
flushTerminalKeyBuffer();
handleTermData(sequence);
scheduleLocalPrefsSave();
}
/* Key bar collapse.
The bar is ~44 buttons and takes real vertical space in a panel whose
height the user is usually trying to give to the terminal, so it has to be
collapsible. Open by default: the keys are the reason to be in this view,
and hiding them behind a control that starts closed reads as a missing
feature. Persisted, because the choice is per-user, not per-session. */
function applyTermKeyBarOpen(open = Boolean(state.ui.termKeyBarOpen)) {
state.ui.termKeyBarOpen = Boolean(open);
const body = refs.termKeyBarBody;
if (!body) return;
body.classList.toggle('hidden', !state.ui.termKeyBarOpen);
refs.termKeyBarToggle?.setAttribute('aria-expanded', String(state.ui.termKeyBarOpen));
const label = refs.termKeyBarToggle?.querySelector('.term-key-bar-toggle-text');
if (label) label.textContent = state.ui.termKeyBarOpen ? t('收起') : t('展开');
}
/* Builds the key bar from TERM_KEY_GROUPS.
One source for both the byte table and the buttons: a binding cannot exist
in the bar without a byte, or send bytes without a way to press them. The
control group gets the amber treatment because those bytes interrupt the
foreground process on the device — they are the ones worth reading twice
before clicking. */
function renderTerminalKeyBar() {
const bar = refs.termKeyBar;
if (!bar) return;
// Everything is generated, so clear both rows unconditionally. Keeping the
// label is no longer needed: the head is static markup in index.html.
const rows = Array.from(bar.querySelectorAll('[data-term-row]'));
rows.forEach((row) => { row.replaceChildren(); });
const rowFor = new Map();
TERM_KEY_ROWS.forEach((groupIds, index) => {
groupIds.forEach((id) => rowFor.set(id, index));
});
TERM_KEY_GROUPS.forEach((group) => {
const groupEl = document.createElement('div');
groupEl.className = 'term-key-bar-group';
if (group.id === 'control') groupEl.classList.add('is-control');
groupEl.dataset.termGroup = group.id;
group.buttons.forEach((spec) => {
if (!TERM_KEY_SEQUENCES[spec.key]) return;
const button = document.createElement('button');
button.type = 'button';
button.className = 'term-key';
if (group.id === 'control') button.classList.add('term-key-control');
if (group.id === 'submit') button.classList.add('term-key-submit');
if (spec.wide) button.classList.add('term-key-wide');
button.dataset.termKey = spec.key;
// Translated here rather than in the constant: TERM_KEY_GROUPS is
// evaluated once at load, so a t() call inside it would keep the
// language it was built in.
button.dataset.tip = t(spec.tip);
button.textContent = spec.label;
groupEl.appendChild(button);
});
// A group missing from TERM_KEY_ROWS is a table bug, not a layout one:
// dropping it silently would hide bindings the byte table still has.
const rowIndex = rowFor.get(group.id);
const row = Number.isInteger(rowIndex) ? rows[rowIndex] : null;
if (!row) {
console.warn(`[keybar] group "${group.id}" has no row ${rowIndex} in the markup — buttons dropped`);
return;
}
if (row.childElementCount) {
const divider = document.createElement('span');
divider.className = 'term-key-bar-divider';
divider.setAttribute('aria-hidden', 'true');
row.appendChild(divider);
}
row.appendChild(groupEl);
});
applyTermKeyBarOpen();
}
/* ---- Paste debounce ---------------------------------------------------
xterm's onData fires once per keystroke, so a 500-character paste becomes
500 serial writes. Printable runs accumulate and flush after 40ms, so a
burst becomes a handful of frames instead. Control characters and escape
sequences bypass the buffer so they stay immediate, and the buffer is
flushed before any of them so ordering is preserved. */
function flushTerminalKeyBuffer() {
if (!terminalKeyFlushTimer) return;
clearTimeout(terminalKeyFlushTimer);
terminalKeyFlushTimer = 0;
const buffered = terminalKeyBuffer;
terminalKeyBuffer = '';
if (buffered) sendTerminalBytes(buffered);
}
function queueTerminalKeyChunk(data) {
if (!data || typeof data !== 'string') return;
// Local echo is a line editor: every keystroke must reach handleTermData so
// terminalSession.buffer and the echo stay in step. Debouncing there would
// break backspace, history and the draft. So the buffer only applies to
// passthrough mode, which is the one that writes straight to the wire.
if (refs.termLocalEcho?.checked) {
flushTerminalKeyBuffer();
handleTermData(data);
return;
}
// Anything that is not plain printable text goes straight out, so control
// bytes and escape sequences keep their immediate, ordered behaviour.
if (data.length !== 1 || data.charCodeAt(0) < 32 || data.charCodeAt(0) === 127) {
flushTerminalKeyBuffer();
handleTermData(data);
return;
}
terminalKeyBuffer += data;
if (terminalKeyFlushTimer) return;
terminalKeyFlushTimer = window.setTimeout(flushTerminalKeyBuffer, TERMINAL_KEY_FLUSH_MS);
}
function sendTerminalBytes(data) {
if (!data) return Promise.resolve(false);
let bytes;
try {
bytes = encodeText(data, state.settings.textEncoding);
} catch (error) {
bytes = new TextEncoder().encode(data);
}
return performSerialWrite(bytes).then((sent) => {
if (sent) {
state.session.txBytes += bytes.length;
renderStats();
} else {
toast('发送失败', '串口未连接,无法发送终端输入。', 'warn', [], { key: 'term-send' });
}
return sent;
});
}
async function commitTerminalLine() {
const text = terminalSession.buffer;
const ending = getTermLineEnding();
terminalSession.buffer = '';
terminalSession.historyIndex = -1;
terminalSession.draft = '';
if (text) {
terminalSession.history.push(text);
if (terminalSession.history.length > TERMINAL_HISTORY_MAX) terminalSession.history.shift();
}
if (refs.termLocalEcho?.checked) {
terminalRuntime.term?.write('\r\n');
}
const payload = text + ending;
if (!payload) return;
await sendTerminalBytes(payload);
}
function navigateTermHistory(direction) {
const term = terminalRuntime.term;
if (!term || !terminalSession.history.length) return;
const redraw = (value) => {
terminalSession.buffer = value;
termEchoText(`\r\x1b[2K${value}`);
};
if (direction === 'up') {
if (terminalSession.historyIndex === -1) {
terminalSession.draft = terminalSession.buffer;
terminalSession.historyIndex = terminalSession.history.length - 1;
} else if (terminalSession.historyIndex > 0) {
terminalSession.historyIndex -= 1;
}
} else {
if (terminalSession.historyIndex === -1) return;
if (terminalSession.historyIndex < terminalSession.history.length - 1) {
terminalSession.historyIndex += 1;
} else {
terminalSession.historyIndex = -1;
redraw(terminalSession.draft || '');
return;
}
}
redraw(terminalSession.history[terminalSession.historyIndex] || '');
}
function applyMonitorView() {
const terminal = isTerminalViewActive();
refs.monitorPanel?.classList.toggle('view-terminal', terminal);
refs.monitorViewSwitch?.classList.toggle('term-active', terminal);
refs.monitorViewTabs?.forEach((btn) => {
btn.classList.toggle('active', btn.dataset.monitorView === state.ui.monitorView);
});
if (terminal) {
const term = ensureTerminalInstance();
if (term && !terminalSession.hintShown) {
terminalSession.hintShown = true;
term.writeln(`\x1b[2m${t(TERMINAL_READY_HINT_TEXT)}\x1b[0m`);
}
applyTerminalMetrics();
requestAnimationFrame(() => {
refitTerminal();
term?.focus();
});
}
}
function setMonitorView(view) {
const next = view === 'terminal' ? 'terminal' : 'manual';
if (state.ui.monitorView === next) return;
state.ui.monitorView = next;
applyMonitorView();
scheduleLocalPrefsSave();
}