2022-03-03 02:34:19 +03:00
|
|
|
import { Terminal, ITerminalOptions } from 'xterm';
|
|
|
|
import { FitAddon } from 'xterm-addon-fit';
|
2022-04-14 17:13:09 +03:00
|
|
|
import { debounce } from 'lodash';
|
2022-03-03 02:34:19 +03:00
|
|
|
import bel from './lib/bel';
|
|
|
|
import api from './api';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Belt, pokeTask, pokeBelt
|
|
|
|
} from '@urbit/api/term';
|
|
|
|
import { Session } from './state';
|
|
|
|
import { useCallback, useEffect, useRef } from 'react';
|
|
|
|
import useTermState from './state';
|
|
|
|
import React from 'react';
|
|
|
|
import { Box, Col } from '@tlon/indigo-react';
|
|
|
|
import { makeTheme } from './lib/theme';
|
2022-03-25 17:38:56 +03:00
|
|
|
import { showBlit, csi, hasBell } from './lib/blit';
|
2022-04-19 13:41:40 +03:00
|
|
|
import { DEFAULT_SESSION, RESIZE_DEBOUNCE_MS, RESIZE_THRESHOLD_PX } from './constants';
|
2022-03-11 07:54:30 +03:00
|
|
|
import { retry } from './lib/retry';
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
const termConfig: ITerminalOptions = {
|
|
|
|
logLevel: 'warn',
|
|
|
|
//
|
|
|
|
convertEol: true,
|
|
|
|
//
|
|
|
|
rows: 24,
|
|
|
|
cols: 80,
|
|
|
|
scrollback: 10000,
|
|
|
|
//
|
|
|
|
fontFamily: '"Source Code Pro", "Roboto mono", "Courier New", monospace',
|
|
|
|
fontWeight: 400,
|
|
|
|
// NOTE theme colors configured dynamically
|
|
|
|
//
|
|
|
|
bellStyle: 'sound',
|
|
|
|
bellSound: bel,
|
|
|
|
//
|
|
|
|
// allows text selection by holding modifier (option, or shift)
|
2022-04-13 18:47:30 +03:00
|
|
|
macOptionClickForcesSelection: true,
|
|
|
|
// prevent insertion of simulated arrow keys on-altclick
|
|
|
|
altClickMovesCursor: false
|
2022-03-03 02:34:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const readInput = (term: Terminal, e: string): Belt[] => {
|
|
|
|
const belts: Belt[] = [];
|
|
|
|
let strap = '';
|
|
|
|
|
|
|
|
while (e.length > 0) {
|
|
|
|
let c = e.charCodeAt(0);
|
|
|
|
|
|
|
|
// text input
|
|
|
|
//
|
|
|
|
if (c >= 32 && c !== 127) {
|
|
|
|
strap += e[0];
|
2022-05-15 02:13:54 +03:00
|
|
|
e = e.slice(1); //TODO revisit wrt (list @c) & unicode characters
|
2022-03-03 02:34:19 +03:00
|
|
|
continue;
|
|
|
|
} else if ('' !== strap) {
|
|
|
|
belts.push({ txt: strap.split('') });
|
|
|
|
strap = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// special keys/characters
|
|
|
|
//
|
|
|
|
if (0 === c) {
|
|
|
|
term.write('\x07'); // bel
|
|
|
|
} else if (8 === c || 127 === c) {
|
|
|
|
belts.push({ bac: null });
|
|
|
|
} else if (13 === c) {
|
|
|
|
belts.push({ ret: null });
|
|
|
|
} else if (c <= 26) {
|
2022-03-10 01:25:10 +03:00
|
|
|
const k = String.fromCharCode(96 + c);
|
2022-03-03 02:34:19 +03:00
|
|
|
//NOTE prevent remote shut-downs
|
|
|
|
if ('d' !== k) {
|
|
|
|
belts.push({ mod: { mod: 'ctl', key: k } });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequences
|
|
|
|
//
|
|
|
|
if (27 === c) { // ESC
|
|
|
|
e = e.slice(1);
|
|
|
|
c = e.charCodeAt(0);
|
|
|
|
if (91 === c || 79 === c) { // [ or O
|
|
|
|
e = e.slice(1);
|
|
|
|
c = e.charCodeAt(0);
|
|
|
|
/* eslint-disable max-statements-per-line */
|
|
|
|
switch (c) {
|
|
|
|
case 65: belts.push({ aro: 'u' }); break;
|
|
|
|
case 66: belts.push({ aro: 'd' }); break;
|
|
|
|
case 67: belts.push({ aro: 'r' }); break;
|
|
|
|
case 68: belts.push({ aro: 'l' }); break;
|
|
|
|
//
|
|
|
|
case 77: {
|
|
|
|
const m = e.charCodeAt(1) - 31;
|
|
|
|
if (1 === m) {
|
|
|
|
const c = e.charCodeAt(2) - 32;
|
|
|
|
const r = e.charCodeAt(3) - 32;
|
2022-04-03 22:38:09 +03:00
|
|
|
belts.push({ hit: { y: r - 1, x: c - 1 } });
|
2022-03-03 02:34:19 +03:00
|
|
|
}
|
|
|
|
e = e.slice(3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
default: term.write('\x07'); break; // bel
|
|
|
|
}
|
|
|
|
} else if (c >= 97 && c <= 122) { // a <= c <= z
|
|
|
|
belts.push({ mod: { mod: 'met', key: e[0] } });
|
|
|
|
} else if (c === 46) { // .
|
|
|
|
belts.push({ mod: { mod: 'met', key: '.' } });
|
|
|
|
} else if (c === 8 || c === 127) {
|
|
|
|
belts.push({ mod: { mod: 'met', key: { bac: null } } });
|
|
|
|
} else {
|
|
|
|
term.write('\x07'); break; // bel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e = e.slice(1);
|
|
|
|
}
|
|
|
|
if ('' !== strap) {
|
2022-05-15 02:13:54 +03:00
|
|
|
if (1 === strap.length) {
|
|
|
|
belts.push(strap);
|
|
|
|
} else {
|
|
|
|
belts.push({ txt: strap.split('') });
|
|
|
|
}
|
2022-03-03 02:34:19 +03:00
|
|
|
strap = '';
|
|
|
|
}
|
|
|
|
return belts;
|
|
|
|
};
|
|
|
|
|
2022-04-14 17:13:09 +03:00
|
|
|
const onResize = async (name: string, session: Session) => {
|
|
|
|
if (session) {
|
|
|
|
session.fit.fit();
|
2022-07-16 11:35:48 +03:00
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name].pending++;
|
|
|
|
});
|
|
|
|
api.poke(pokeTask(name, { blew: { w: session.term.cols, h: session.term.rows } })).then(() => {
|
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name].pending--;
|
|
|
|
});
|
|
|
|
});
|
2022-04-14 17:13:09 +03:00
|
|
|
}
|
2022-03-03 02:34:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const onInput = (name: string, session: Session, e: string) => {
|
|
|
|
if (!session) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const term = session.term;
|
|
|
|
const belts = readInput(term, e);
|
2022-07-17 07:14:29 +03:00
|
|
|
belts.forEach((b) => {
|
2022-07-16 11:35:48 +03:00
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name].pending++;
|
|
|
|
});
|
|
|
|
api.poke(pokeBelt(name, b)).then(() => {
|
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name].pending--;
|
|
|
|
});
|
|
|
|
});
|
2022-03-03 02:34:19 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
interface BufferProps {
|
|
|
|
name: string,
|
|
|
|
selected: boolean,
|
2022-03-04 02:10:54 +03:00
|
|
|
dark: boolean,
|
2022-03-03 02:34:19 +03:00
|
|
|
}
|
|
|
|
|
2022-03-04 02:10:54 +03:00
|
|
|
export default function Buffer({ name, selected, dark }: BufferProps) {
|
2022-04-14 17:13:09 +03:00
|
|
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
const session: Session = useTermState(s => s.sessions[name]);
|
|
|
|
|
|
|
|
const initSession = useCallback(async (name: string, dark: boolean) => {
|
2022-03-04 03:11:37 +03:00
|
|
|
console.log('setting up', name === DEFAULT_SESSION ? 'default' : name);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
// set up xterm terminal
|
|
|
|
//
|
|
|
|
const term = new Terminal(termConfig);
|
2022-04-14 14:52:43 +03:00
|
|
|
term.options.theme = makeTheme(dark);
|
2022-03-03 02:34:19 +03:00
|
|
|
const fit = new FitAddon();
|
|
|
|
term.loadAddon(fit);
|
|
|
|
fit.fit();
|
|
|
|
term.focus();
|
|
|
|
|
|
|
|
// start mouse reporting
|
|
|
|
//
|
|
|
|
term.write(csi('?9h'));
|
|
|
|
|
2022-07-16 11:35:48 +03:00
|
|
|
const ses: Session = {
|
|
|
|
term,
|
|
|
|
fit,
|
|
|
|
hasBell: false,
|
|
|
|
pending: 0,
|
|
|
|
subscriptionId: null
|
|
|
|
};
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
// set up event handlers
|
|
|
|
//
|
2022-07-08 21:44:21 +03:00
|
|
|
term.attachCustomKeyEventHandler((e: KeyboardEvent) => {
|
|
|
|
//NOTE ctrl+shift keypresses never make it into term.onData somehow,
|
|
|
|
// so we handle them specially ourselves.
|
|
|
|
// we may be able to remove this once xterm.js fixes #3382 & co.
|
|
|
|
if (e.shiftKey
|
|
|
|
&& e.ctrlKey
|
|
|
|
&& e.type === 'keydown'
|
|
|
|
&& e.key.length === 1
|
|
|
|
) {
|
|
|
|
api.poke(pokeBelt(name, { mod: { mod: 'ctl', key: e.key } }));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2022-03-03 02:34:19 +03:00
|
|
|
term.onData(e => onInput(name, ses, e));
|
|
|
|
term.onBinary(e => onInput(name, ses, e));
|
|
|
|
|
|
|
|
// open subscription
|
|
|
|
//
|
2022-03-10 09:03:27 +03:00
|
|
|
const initSubscription = async () => {
|
|
|
|
const subscriptionId = await api.subscribe({
|
|
|
|
app: 'herm', path: '/session/' + name + '/view',
|
|
|
|
event: (e) => {
|
|
|
|
showBlit(ses.term, e);
|
2022-03-25 17:38:56 +03:00
|
|
|
//NOTE getting selected from state because selected prop is stale
|
|
|
|
if (hasBell(e) && (useTermState.getState().selected !== name)) {
|
2022-03-10 09:03:27 +03:00
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name].hasBell = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//TODO should handle %bye on this higher level though, for deletion
|
|
|
|
},
|
|
|
|
err: (e, id) => {
|
|
|
|
console.log(`subscription error, id ${id}:`, e);
|
|
|
|
},
|
|
|
|
quit: async () => { // quit
|
2022-03-15 03:34:19 +03:00
|
|
|
console.error('quit, reconnecting...');
|
2022-03-11 07:54:30 +03:00
|
|
|
try {
|
|
|
|
const newSubscriptionId = await retry(initSubscription, () => {
|
|
|
|
console.log('attempting to reconnect ...');
|
|
|
|
}, 5);
|
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name].subscriptionId = newSubscriptionId;
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log('unable to reconnect', error);
|
|
|
|
}
|
2022-03-03 02:34:19 +03:00
|
|
|
}
|
2022-03-10 09:03:27 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return subscriptionId;
|
|
|
|
};
|
|
|
|
|
|
|
|
ses.subscriptionId = await initSubscription();
|
2022-03-10 01:25:10 +03:00
|
|
|
|
2022-03-03 02:34:19 +03:00
|
|
|
useTermState.getState().set((state) => {
|
|
|
|
state.sessions[name] = ses;
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2022-04-19 13:41:40 +03:00
|
|
|
const shouldResize = useCallback(() => {
|
|
|
|
if(!session) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const containerHeight = document.querySelector('.buffer-container')?.clientHeight || Infinity;
|
|
|
|
const terminalHeight = session.term.element?.clientHeight || 0;
|
|
|
|
|
|
|
|
return (containerHeight - terminalHeight) >= RESIZE_THRESHOLD_PX;
|
|
|
|
}, [session]);
|
|
|
|
|
2022-04-14 17:13:09 +03:00
|
|
|
const onSelect = useCallback(async () => {
|
2022-04-19 13:41:40 +03:00
|
|
|
if (session && selected && shouldResize()) {
|
2022-04-14 17:13:09 +03:00
|
|
|
session.fit.fit();
|
|
|
|
await api.poke(pokeTask(name, { blew: { w: session.term.cols, h: session.term.rows } }));
|
2022-04-19 13:41:40 +03:00
|
|
|
session.term.focus();
|
2022-04-14 17:13:09 +03:00
|
|
|
}
|
2022-09-17 11:36:55 +03:00
|
|
|
}, [session?.term, selected]);
|
2022-04-14 17:13:09 +03:00
|
|
|
|
|
|
|
// Effects
|
2022-03-03 02:34:19 +03:00
|
|
|
// init session
|
|
|
|
useEffect(() => {
|
|
|
|
if(session) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initSession(name, dark);
|
|
|
|
}, [name]);
|
|
|
|
|
2022-04-14 17:13:09 +03:00
|
|
|
// attach to DOM when ref is available
|
|
|
|
useEffect(() => {
|
|
|
|
if(session && containerRef.current && !session.term.element) {
|
|
|
|
session.term.open(containerRef.current);
|
2022-03-03 02:34:19 +03:00
|
|
|
}
|
2022-04-14 17:13:09 +03:00
|
|
|
}, [session, containerRef]);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
2022-04-19 13:41:40 +03:00
|
|
|
// initialize resize listeners
|
2022-03-03 02:34:19 +03:00
|
|
|
//
|
|
|
|
useEffect(() => {
|
2022-04-14 17:13:09 +03:00
|
|
|
if(!session) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-19 13:41:40 +03:00
|
|
|
// TODO: use ResizeObserver for improved performance?
|
2022-04-14 17:13:09 +03:00
|
|
|
const debouncedResize = debounce(() => onResize(name, session), RESIZE_DEBOUNCE_MS);
|
|
|
|
window.addEventListener('resize', debouncedResize);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
return () => {
|
2022-04-14 17:13:09 +03:00
|
|
|
window.removeEventListener('resize', debouncedResize);
|
2022-03-03 02:34:19 +03:00
|
|
|
};
|
2022-04-14 17:13:09 +03:00
|
|
|
}, [session]);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
// on dark mode change, change terminals' theme
|
|
|
|
//
|
|
|
|
useEffect(() => {
|
|
|
|
const theme = makeTheme(dark);
|
|
|
|
if (session) {
|
2022-04-14 14:52:43 +03:00
|
|
|
session.term.options.theme = theme;
|
2022-03-03 02:34:19 +03:00
|
|
|
}
|
2022-04-14 17:13:09 +03:00
|
|
|
if (containerRef.current) {
|
|
|
|
containerRef.current.style.backgroundColor = theme.background || '';
|
2022-03-03 02:34:19 +03:00
|
|
|
}
|
2022-03-04 03:11:37 +03:00
|
|
|
}, [session, dark]);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
2022-04-14 17:13:09 +03:00
|
|
|
// On select, resize, focus, and poke herm with updated cols and rows
|
2022-03-03 02:34:19 +03:00
|
|
|
useEffect(() => {
|
2022-04-14 17:13:09 +03:00
|
|
|
onSelect();
|
|
|
|
}, [onSelect]);
|
2022-03-03 02:34:19 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
!session && !selected ?
|
|
|
|
<p>Loading...</p>
|
2022-04-14 17:13:09 +03:00
|
|
|
:
|
2022-03-03 02:34:19 +03:00
|
|
|
<Box
|
|
|
|
width='100%'
|
|
|
|
height='100%'
|
|
|
|
bg='white'
|
|
|
|
fontFamily='mono'
|
|
|
|
overflow='hidden'
|
2022-04-19 13:41:40 +03:00
|
|
|
className="terminal-container"
|
|
|
|
style={selected ? { zIndex: 999 } : {}}
|
2022-03-03 02:34:19 +03:00
|
|
|
>
|
2022-04-14 17:13:09 +03:00
|
|
|
<Col
|
|
|
|
width='100%'
|
|
|
|
height='100%'
|
|
|
|
minHeight='0'
|
|
|
|
px={['0', '2']}
|
|
|
|
pb={['0', '2']}
|
|
|
|
ref={containerRef}
|
|
|
|
>
|
|
|
|
</Col>
|
|
|
|
</Box>
|
2022-03-03 02:34:19 +03:00
|
|
|
);
|
|
|
|
}
|