-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·733 lines (607 loc) · 19.1 KB
/
index.js
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
#!/usr/bin/env node
const { stdin, stdout } = process
const { getCtlSeqs } = require('./ctlseqs')
const { createSubTerminal } = require('./subterminal')
const log = require('./log')
// const { performance } = require('./perf')
function initWorkspace (shells = []) {
return {
focussed_shell: 0,
layout: 0,
scroll_position: 0,
shells,
start_last_shell_index: 0,
start_size_pct: 50
}
}
const reduce = (state = {
mode: true,
show_help: false,
workspaces: [initWorkspace()],
focussed_workspace: 0
}, action) => {
const canUpdateWorkspaces = state.mode || action.type === 'FOCUS_SHELL_ABSOLUTE'
return {
mode: reduceMode(state, action),
workspaces: canUpdateWorkspaces ? reduceWorkspaces(state, action) : state.workspaces,
focussed_workspace: state.mode ? reduceFocussedWorkspace(state, action) : state.focussed_workspace,
show_help:
state.mode
? (
action.type === 'HELP_TOGGLE'
? !state.show_help
: state.show_help
)
: state.show_help
}
}
function reduceFocussedWorkspace ({ focussed_workspace }, action) {
const result = {
CURRENT_WORKSPACE_SELECT: () => action.destination
}[action.type] || (() => focussed_workspace)
return result()
}
function reduceMode ({ mode }, action) {
return action.type === 'MODE_TOGGLE' ? !mode : mode
}
function rotate (total, current, direction) {
if (total === 0) return 0
return (total + current + direction) % total
}
function limit (value, lower, upper) {
if (value < lower) return lower
if (value > upper) return upper
return value
}
function newShell () {
const sts = createSubTerminal(drawBuffer)
sts.forEach(st => {
subTerminals[st.id] = st
})
return sts.map(st => ({ id: st.id }))
}
function reduceCurrentWorkspace (state, action) {
const { shells, focussed_shell, start_size_pct, start_last_shell_index } = state
const rotatedTarget = action.direction
? rotate(shells.length, focussed_shell, action.direction)
: 0
let newState = {}
switch (action.type) {
case 'FOCUS_SHELL_ABSOLUTE':
const ix = shells.findIndex(st => st.id === action.targetId)
if ( ix !== -1 ) {
newState = { focussed_shell: ix }
}
break
case 'FOCUS_SHELL':
newState = { focussed_shell: rotatedTarget }
break
case 'SWAP_SHELL':
newState = {
shells: shells.map((s, index) =>
({
[focussed_shell]: shells[rotatedTarget],
[rotatedTarget]: shells[focussed_shell]
}[index] || s)
),
focussed_shell: rotatedTarget
}
break
case 'LAUNCH_SHELL':
newState = {
shells: [...shells, ...newShell()],
focussed_shell: shells.length
}
break
case 'CLOSE_FOCUSSED_SHELL':
newState = {
shells:
shells.length === 1
? [...newShell()]
: shells.filter((s, index) => index !== focussed_shell),
focussed_shell: focussed_shell === 0 ? 0 : rotate(shells.length - 1, focussed_shell, -1)
}
break
case 'START_SIZE_CHANGE':
newState = {
start_size_pct: limit(start_size_pct + action.direction * 10, 0, 100)
}
break
case 'START_SHELL_INDEX_CHANGE':
newState = {
start_last_shell_index: limit(start_last_shell_index + action.direction, 0, shells.length - 1)
}
break
case 'LAYOUT_ROTATE':
newState = {
layout: rotate(2, state.layout, 1)
}
break
}
return {
...state,
...newState
}
}
function reduceWorkspaces (state, action) {
const { workspaces, focussed_workspace } = state
const focussedWorkspace = workspaces[focussed_workspace]
const { focussed_shell } = focussedWorkspace
const newWorkspaces = workspaces
.map((workspace, index) => {
// This complexity indicates this could be better...
if (action.destination === focussed_workspace) return workspace
if (typeof action.destination === 'undefined' || action.type !== 'SHELL_WORKSPACE_MOVE') return workspace
if (typeof focussedWorkspace.shells[focussed_shell] === 'undefined') return workspace
if (index === focussed_workspace) {
return {
...workspace,
shells: workspace.shells.filter((s, i) => i !== focussed_shell),
focussed_shell: rotate(workspace.shells.length - 1, focussed_shell, -1),
start_last_shell_index: limit(workspace.start_last_shell_index, 0, workspace.shells.length - 1)
}
} else if (index === action.destination) {
return {
...workspace,
shells: [
...workspace.shells,
focussedWorkspace.shells[focussed_shell]
]
}
}
return workspace
})
// Selecting an empty workspace populates it with a shell
if (action.type === 'CURRENT_WORKSPACE_SELECT' && !newWorkspaces[action.destination]) {
newWorkspaces[action.destination] = initWorkspace([...newShell()])
}
return newWorkspaces.map(
(workspace, index) => index === focussed_workspace
? reduceCurrentWorkspace(workspace, action)
: workspace
)
}
let state
function applyAction (action) {
state = reduce(state, action)
// Render borders, set sub terminal areas
render()
}
const subTerminals = {}
function clearScreen () {
stdout.write('\u001b[2J\u001b[H')
}
function exit () {
clearScreen()
// Show the cursor
stdout.write('\u001b[?25h')
// Normal buffer
stdout.write('\u001b[?47l')
// Disable mouse tracking
stdout.write('\u001b[?1000l')
process.exit()
}
function mapKeyToAction (key) {
return {
m: { type: 'FOCUS_SHELL' },
k: { type: 'FOCUS_SHELL', direction: +1 },
j: { type: 'FOCUS_SHELL', direction: -1 },
'\u000d': { type: 'SWAP_SHELL' },
K: { type: 'SWAP_SHELL', direction: +1 },
J: { type: 'SWAP_SHELL', direction: -1 },
// shift-tab : ^[[Z
'\u001b\u005bZ': { type: 'MODE_TOGGLE' },
H: { type: 'HELP_TOGGLE' },
C: { type: 'CLOSE_FOCUSSED_SHELL' },
'!': { type: 'SHELL_WORKSPACE_MOVE', destination: 0 },
'@': { type: 'SHELL_WORKSPACE_MOVE', destination: 1 },
'£': { type: 'SHELL_WORKSPACE_MOVE', destination: 2 },
$: { type: 'SHELL_WORKSPACE_MOVE', destination: 3 },
'%': { type: 'SHELL_WORKSPACE_MOVE', destination: 4 },
'^': { type: 'SHELL_WORKSPACE_MOVE', destination: 5 },
'&': { type: 'SHELL_WORKSPACE_MOVE', destination: 6 },
'*': { type: 'SHELL_WORKSPACE_MOVE', destination: 7 },
'(': { type: 'SHELL_WORKSPACE_MOVE', destination: 8 },
')': { type: 'SHELL_WORKSPACE_MOVE', destination: 9 },
1: { type: 'CURRENT_WORKSPACE_SELECT', destination: 0 },
2: { type: 'CURRENT_WORKSPACE_SELECT', destination: 1 },
3: { type: 'CURRENT_WORKSPACE_SELECT', destination: 2 },
4: { type: 'CURRENT_WORKSPACE_SELECT', destination: 3 },
5: { type: 'CURRENT_WORKSPACE_SELECT', destination: 4 },
6: { type: 'CURRENT_WORKSPACE_SELECT', destination: 5 },
7: { type: 'CURRENT_WORKSPACE_SELECT', destination: 6 },
8: { type: 'CURRENT_WORKSPACE_SELECT', destination: 7 },
9: { type: 'CURRENT_WORKSPACE_SELECT', destination: 8 },
0: { type: 'CURRENT_WORKSPACE_SELECT', destination: 9 },
// space
'\u0020': { type: 'LAYOUT_ROTATE' },
l: { type: 'START_SIZE_CHANGE', direction: +1 },
h: { type: 'START_SIZE_CHANGE', direction: -1 },
',': { type: 'START_SHELL_INDEX_CHANGE', direction: +1 },
'.': { type: 'START_SHELL_INDEX_CHANGE', direction: -1 },
Q: { type: 'QUIT' },
q: { type: 'RESTART' },
// backspace
'\u007f': { type: 'LAUNCH_SHELL' }
}[key]
}
const VIEW_FRAME = {
CORNER: {
T: { L: '\u250c', R: '\u2510' },
B: { L: '\u2514', R: '\u2518' }
},
EDGE: { V: '\u2502', H: '\u2500' },
POINT: {
V: {
R: '\u2502',
L: '\u2502'
// R: '\u2523', L: '\u252b'
},
H: { T: '\u253b', B: '\u2533' },
M: '\u254b'
}
}
function drawEdgeH (x, y, w, top, join) {
const corner = join
? VIEW_FRAME.POINT.V
: (top ? VIEW_FRAME.CORNER.T : VIEW_FRAME.CORNER.B)
let c
for (let i = 0; i < w; i++) {
c = VIEW_FRAME.EDGE.H
if (i === 0) {
c = corner.L
} else if (i === w - 1) {
c = corner.R
}
stdout.cursorTo(x + i, y)
stdout.write(c)
}
}
function viewTransform (c) {
const space = {
x: stdout.columns,
y: stdout.rows,
y2: stdout.rows,
w: stdout.columns,
h: stdout.rows
}
const transform = (k, v) => limit(Math.floor(v * space[k] / 100), 0, space[k])
const transformed = Object.keys(c)
.map((k) => ({ [k]: transform(k, c[k]) }))
.reduce((t, n) => ({ ...t, ...n }), {})
return transformed
}
function drawBox (x, y, w, h, isTop) {
const { x: viewX, y: viewY, w: viewW, h: viewH } = viewTransform({ x, y, w, h })
drawBoxView(viewX, viewY, viewW, viewH, { isTop })
}
function wrapLine (s, w) {
if (s.length < w) return [s]
if (w <= 0) return [s]
const res = []
let rest = s
while (rest.length > w) {
let wrapPoint = rest.slice(0, w).lastIndexOf(' ') + 1
if (wrapPoint <= 0) {
wrapPoint = rest.length
}
const l = rest.slice(0, wrapPoint)
rest = rest.slice(wrapPoint)
res.push(l)
}
res.push(rest)
return res
}
function drawBoxView (viewX, viewY, viewW, viewH, { isTop, lines, shouldWrap = true }) {
const [x, y, w, h] = [viewX, viewY, viewW, viewH].map(Math.round)
for (let i = 0; i < (h - 1); i++) {
stdout.cursorTo(x, y + 1 + i)
stdout.write(VIEW_FRAME.EDGE.V)
stdout.cursorTo(x + w - 1, y + 1 + i)
stdout.write(VIEW_FRAME.EDGE.V)
}
drawEdgeH(x, y, w, true, !isTop)
drawEdgeH(x, y + h, w, false)
if (lines) {
const blankLine = Array(w - 2).fill(' ').join('')
let wrappedLines = shouldWrap
? lines
.map(l => wrapLine(l, w - 6))
.reduce((ls, acc) => ls.concat(acc), [])
: lines
if (wrappedLines.length > h - 1) {
wrappedLines[h - 2] = '...'
}
if (wrappedLines.length < h - 1) {
wrappedLines = wrappedLines.concat(Array((h - 1) - wrappedLines.length).fill(''))
}
const blob = wrappedLines.slice(0, h - 1).map(
(l, ix) =>
'\u001b[' + [y + 2 + ix, x + 2].join(';') + 'H' + blankLine +
'\u001b[' + [y + 2 + ix, x + 4].join(';') + 'H' + l
).join('')
stdout.write(blob)
}
}
const prevLines = {}
function lineHasChanged (y, x, l) {
if (!prevLines[y]) return true
if (!prevLines[y][x]) return true
return prevLines[y][x] !== l
}
function recordLines (y, lines) {
lines.forEach((l, ix) => {
prevLines[y + ix] = l
})
}
const help = [
'',
'welcome to nomad :)',
'',
'nomad-term is a terminal multiplexer that provides a similar interface to the tiling window manager, xmonad. It uses node-pty to fork each child process within a virtual pseudoterminal, maintaining an in-memory visual representation of program output.',
'',
'',
'controls',
'----',
'',
'shift-tab toggle between input/movement',
'backspace create a new terminal',
'Q quit nomad',
'H toggle help',
'',
'',
'movement',
'----',
'',
'j select next terminal',
'k select previous terminal',
'shift-j swap current terminal with next terminal',
'shift-k swap current terminal with previous terminal',
'shift-c close selected terminal',
'h decrease main terminal size',
'l increase main terminal size',
', increase number of primary terminals',
'. decrease number of secondary terminals',
'␣ (space) select next layout: normal / full',
'[0-9] select workspace 0 - 9',
'shift-[0-9] move selected terminal to workspace 0 - 9'
]
// TODO: User-controlled buffer scrolling
function drawBuffer (shell_id) {
if (!areas[shell_id]) return
const { x, y, w, h } = areas[shell_id]
const st = subTerminals[shell_id]
const fw = state.workspaces[state.focussed_workspace]
const isFocussed = fw.shells && fw.shells.length && shell_id === fw.shells[fw.focussed_shell].id
const showGuide = state.mode > 0 && isFocussed
// performance.mark('mark1')
const lines = st.drawSubTerminal(w - 2, h - 1, { isFocussed, highlight: false })
// performance.mark('mark2')
// performance.measure('DRAW_SUB_TERMINAL', 'mark1', 'mark2')
const blob = lines.map(
(l, ix) =>
lineHasChanged(y, x, l)
? '\u001b[' + [y + 2 + ix, x + 1].join(';') + 'H' + l
: ''
).join('')
stdout.write(blob)
recordLines(y, lines)
if (showGuide) {
drawGuide(x, y, w, h)
}
// performance.mark('mark3')
// performance.measure('OUTPUT_LINES', 'mark2', 'mark3')
}
function drawGuide (x, y, w, h) {
const fw = state.workspaces[state.focussed_workspace]
if (state.show_help) {
const hbY = Math.round(y + h * 0.05)
const hbW = Math.max(Math.round(2 * w / 3), 64)
const hbX = Math.round(x + w / 2 - hbW / 2)
const hbH = Math.min(Math.round(h - h * 0.1), 45)
const helpBoxRect = [hbX, hbY, hbW, hbH]
drawBoxView(...helpBoxRect, { isTop: true, lines: help })
stdout.cursorTo(hbX + 2, hbY)
stdout.write(
' nomad - help // space:' + (state.focussed_workspace + 1) +
' term:' + (fw.focussed_shell + 1) +
' view:' + ({ 0: 'normal', 1: 'full' }[fw.layout]) + ' '
)
stdout.cursorTo(hbX + hbW - 21, hbY)
stdout.write('(press H to toggle)')
} else {
const hbW = Math.min(Math.max(Math.round(w * 0.6), w - 5), 40)
const hbH = h < 10 ? 5 : 7
const hbX = Math.round(x + w * 0.5 - hbW / 2)
const hbY = Math.round(y + h * 0.5 - hbH / 2)
const lines = hbH === 7 ? [
'',
'help H select jk (^ to move)',
'quit Q new backspace',
'close C toggle ^-tab',
'resize hl layout . ␣ ,'
] : [
'',
'help H, quit Q, select [^]jk',
'close C, new <-, toggle ^-tab'
]
const helpBoxRect = [hbX, hbY, hbW, hbH]
drawBoxView(...helpBoxRect, { isTop: true, lines, shouldWrap: false })
stdout.cursorTo(hbX + 2, hbY)
stdout.write(
' nomad // ' + (state.focussed_workspace + 1) +
':' + (fw.focussed_shell + 1) +
' // ' + ({ 0: 'normal', 1: 'full' }[fw.layout]) + ' '
)
}
}
function drawBoxesH (x, w, h, shells) {
if (shells.length === 0) return
const divisions = shells.length
const divisionH = Math.floor(h / divisions)
drawBox(x, 0, w, h, true)
const { h: totalViewH } = viewTransform({ h })
let viewY = 0
for (let i = 0; i < divisions; i++) {
const y = (i + 1) * divisionH
const {
x: viewX,
y: newViewY,
w: viewW
} = viewTransform({ x, y, w })
let viewH = newViewY - viewY
if (i === divisions - 1) {
viewH = totalViewH - viewY - 1
}
if (i > 0) {
drawEdgeH(viewX, viewY, viewW, false, true)
}
setBufferArea(viewX + 1, viewY, viewW, viewH, shells[i].id)
viewY = newViewY
}
}
let areas = {}
function setBufferArea (x, y, w, h, id) {
areas[id] = { x, y, w, h }
if (!subTerminals[id]) return
// This is a side-effect, TODO - move this somewhere else
subTerminals[id].resize(w - 2, h - 1)
subTerminals[id].position(x, y + 1)
subTerminals[id].render()
}
function render () {
// Render Layout 0
if (!state) return
const fw = state.workspaces[state.focussed_workspace]
stdout.cursorTo(1, 1)
// TODO: Column layout
if (fw.layout === 1) {
renderFullScreen(fw)
} else if (fw.layout === 0) {
renderTwoPanes(fw)
}
}
function renderFullScreen (fw) {
const { x: viewX, y: viewY, w: viewW, h: viewH } =
viewTransform({ x: 0, y: 0, w: 100, h: 100 })
const shellId = fw.shells[fw.focussed_shell].id
// TODO: eugh. Should reset this more carefully
areas = {}
setBufferArea(viewX + 1, viewY, viewW, viewH - 1, shellId)
drawBuffer(shellId)
drawBox(0, 0, 100, 100, true)
}
function renderTwoPanes (fw) {
const startIndex = limit(fw.start_last_shell_index, 0, fw.shells.length - 1)
const startDivisions = startIndex + 1
const endDivisions = fw.shells.length - startDivisions
let w = 0
if (endDivisions === 0) {
w = 100
} else if (startDivisions === 0) {
w = 0
} else {
w = fw.start_size_pct
}
const h = 100
// Reset all buffer areas
areas = {}
if (w !== 0) {
drawBoxesH(0, w, h, fw.shells.slice(0, startDivisions))
}
if (w !== 100) {
drawBoxesH(w, 100 - w, h, fw.shells.slice(startDivisions))
}
// Render all sub terminals
Object.keys(subTerminals).forEach(drawBuffer)
}
function onData (data) {
const action = mapKeyToAction(data)
if (action) {
// Stateful actions
applyAction(action)
switch (action.type) {
// Don't send the escape sequence to the program, otherwise
// it could cause unexpected results with it
case 'MODE_TOGGLE':
return
case 'QUIT':
if (state && state.mode) {
exit()
}
break
case 'RESTART':
log.info({ unimplemented: 'RESTART' })
break
}
}
const seqs = getCtlSeqs(data.toString('utf8')).outs
seqs.forEach(seq => {
if (seq.code === 'nml_tracking') {
Object.keys(subTerminals).forEach(id => {
const st = subTerminals[id]
const subPos = st.translateMouse(seq.chars)
if (subPos.col < 0 || subPos.col > st.size.cols) return
if (subPos.row < 0 || subPos.row > st.size.rows) return
applyAction({
type: 'FOCUS_SHELL_ABSOLUTE',
targetId: id
})
})
}
})
if (!(state && state.mode)) {
const fw = state.workspaces[state.focussed_workspace]
// TODO: This is a potential attack vector - if an attacking program
// can control which shell is focussed, it could potentially redirect
// user input to another shell, e.g. when the user is entering a password
//
// One mitigation would be to reduce the posibility of other state changing
// when state.mode is enabled. This is currently done explicitly for each
// part of reduced state
const focussedShell = fw.shells[fw.focussed_shell]
const focussedShellId = focussedShell && focussedShell.id
subTerminals[focussedShellId] && subTerminals[focussedShellId].writeToProc(data)
}
}
function start () {
/// ///////////////////////////////////////////////////////////
//
// nomad-term
//
// i like xmonad, ok.
//
/// ///////////////////////////////////////////////////////////
//
// 1. View based on state. Assume empty buffers in each pane.
// 2. Plain text buffering into panes, scrolling to old output.
// 3. Handling certain colour control sequences
// 4. Handling control sequences that alter cursor position,
// pinning the viewport to the most recent panes-worth of
// output when handling them.
//
stdin.setRawMode(true)
stdin.on('data', onData)
let resizeTimeout
stdout.on('resize', () => {
clearScreen()
if (resizeTimeout) clearTimeout(resizeTimeout)
resizeTimeout = setTimeout(() => render(), 200)
})
process.on('SIGINT', () => {
exit()
})
clearScreen()
// Hide cursor
stdout.write('\u001b[?25l')
// Alt buffer
stdout.write('\u001b[?47h')
// title
stdout.write('\u001b]2;nomad\u0007')
// Enable mouse tracking
stdout.write('\u001b[?1000h')
applyAction({
type: 'LAUNCH_SHELL'
})
}
start()