rob-revisited/ide/ui/js/terminal.js

255 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// The Terminal tool window (desktop IDE): shells in the project's folder, one tab each (xterm.js, MIT).
// Linux / macOS: the user's shell in a pseudo-terminal (PHP: JumPHP\Ide\Run\Terminals), everything as in a real
// terminal. Windows: cmd.exe (or PowerShell) on pipes: the page echoes and edits the line itself (pty: false).
import { backend } from './backend.js';
import { zoom } from './zoom.js';
import { el, clear, icon, notify, copyText, pasteText, prefs } from './ui.js';
/** xterm.js and its fit addon (index.html loads them before Monaco's AMD loader: UMD scripts). */
function loadXterm() {
if (!window.Terminal || !window.FitAddon) return Promise.reject(new Error('xterm.js is missing (run ide/tools/fetch-vendor.sh)'));
return Promise.resolve({ Terminal: window.Terminal, FitAddon: window.FitAddon.FitAddon });
}
const decode = (b64) => Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
export class TerminalPanel {
constructor(app) {
this.app = app;
this.terms = []; // {id, label, term, fit, pty, line, node, ended}
this.active = null;
this.count = 0;
this.node = el('div.terminal-panel', { dataset: { zoom: 'terminal' } });
// Ctrl + wheel / Ctrl+= / Ctrl+-: the font of every shell, then a fit (xterm's onResize tells the shell: terminal.resize)
zoom.define('terminal', { kind: 'px', label: 'Terminal', def: () => 13, apply: (v) => { for (const t of this.terms) { t.term.options.fontSize = v; t.fitW = null; } try { this.current()?.fit.fit(); } catch { /* hidden */ } } });
this.tabsNode = el('div.tw-tabs');
backend.on('terminal.output', (p) => {
const t = this.terms.find((x) => x.id === p.id);
if (!t || t.attaching) return; // while re-attaching, what came before the answer is in the buffer the answer carries
t.lastOutput = Date.now();
t.term.write(decode(p.data));
});
backend.on('terminal.exit', (p) => {
const t = this.terms.find((x) => x.id === p.id);
if (!t) return;
// a shell left with "exit" (or Ctrl+D): its tab closes, as in a terminal; a command's tab (onEnd: a container's command,
// whose output is worth reading) stays with its notice
if (!t.onEnd && prefs.get('terminalCloseOnExit', true)) { t.ended = true; this.close(t); return; }
t.ended = true;
t.term.write('\r\n\x1b[2m[process ended: Enter opens a new shell]\x1b[0m\r\n');
this.render();
t.onEnd?.();
});
let raf = 0; // one fit per frame; a fit never re-triggers itself (scrollbars flickering)
new ResizeObserver(() => { if (raf) return; raf = requestAnimationFrame(() => { raf = 0; this.fit(); }); }).observe(this.node);
}
title() { return 'Terminal'; }
headerExtra() { return this.tabsNode; }
actions() {
return [el('button', { title: 'New Terminal', onclick: () => this.open() }, icon('plus'))];
}
current() { return this.terms.find((t) => t === this.active) || null; }
/** Shown: a first shell, the size of the window, the keys. */
shown() {
if (!this.terms.length && !this.running && !this.restoring) this.open();
else { this.render(); requestAnimationFrame(() => { this.fit(); this.current()?.term.focus(); }); }
}
async open() {
let lib;
try { lib = await loadXterm(); } catch (e) { notify(e.message, 'error'); return; }
const t = this.make(lib);
this.terms.push(t);
this.active = t;
this.render();
t.term.open(t.node);
t.fit.fit();
await this.start(t);
t.term.focus();
}
/** A shell's xterm (not started, not in the list yet): the keys, the size, the input. */
make(lib, label = null) {
const style = getComputedStyle(document.documentElement);
const v = (name, fallback) => style.getPropertyValue(name).trim() || fallback;
const term = new lib.Terminal({
fontFamily: v('--mono', 'monospace'), fontSize: zoom.get('terminal'), cursorBlink: true, scrollback: 5000, allowProposedApi: false,
theme: { background: v('--bg', '#1e1f22'), foreground: v('--text', '#dfe1e5'), cursor: v('--text', '#dfe1e5'), selectionBackground: v('--selection', '#2e436e') },
});
const fit = new lib.FitAddon();
term.loadAddon(fit);
const t = { id: null, label: label || `Local${this.count++ ? ' (' + this.count + ')' : ''}`, term, fit, pty: true, line: '', node: el('div.terminal-view'), ended: false };
term.onData((data) => this.input(t, data));
term.attachCustomKeyEventHandler((e) => {
// Ctrl+Shift+C / V: copy and paste (Ctrl+C stays the shell's interrupt)
if (e.type === 'keydown' && e.ctrlKey && e.shiftKey && (e.code === 'KeyC' || e.code === 'KeyV')) {
if (e.code === 'KeyC' && term.hasSelection()) copyText(term.getSelection());
if (e.code === 'KeyV') pasteText().then((text) => text && this.input(t, text));
e.preventDefault();
return false;
}
return true;
});
term.onResize(({ cols, rows }) => { if (t.id !== null && t.pty) backend.invoke('terminal.resize', { id: t.id, cols, rows }).catch(() => {}); });
return t;
}
/** What the window layout keeps (layout.js): the shells alive, which one is shown, and the token of this IDE run. */
snapshot(rcOf = null) {
const live = this.terms.filter((t) => !t.ended && t.id !== null);
return { boot: this.boot || null, active: Math.max(0, live.indexOf(this.active)),
tabs: live.map((t) => ({ id: t.id, label: t.label, rc: rcOf ? rcOf(t) : null })) };
}
/**
* After a page reload the shells are still there (PHP holds them with their last output: terminal.list / terminal.attach); after an
* IDE restart (another boot token) they are gone: new shells in the project folder, as many as there were (a Remote Control tab is not
* started again). Returns the tabs by their saved id, for the conversations that were in Remote Control.
*/
async restoreLayout(rec, list) {
if (this.terms.length || !rec.tabs.length) return new Map();
const lib = await loadXterm();
const out = new Map();
this.restoring = true;
try {
if (list?.boot && this.boot !== list.boot) this.boot = list.boot;
const alive = new Map((list?.terminals || []).map((x) => [x.id, x]));
if (list && rec.boot && list.boot === rec.boot) {
const ids = rec.tabs.map((x) => x.id).filter((id) => alive.has(id));
for (const id of alive.keys()) if (!ids.includes(id)) ids.push(id);
for (const id of ids) {
const meta = rec.tabs.find((x) => x.id === id);
const info = alive.get(id);
const t = this.make(lib, meta?.label || null);
t.id = id; t.pty = !!info.pty; t.term.options.convertEol = !t.pty; t.attaching = true;
this.terms.push(t);
const r = await backend.invoke('terminal.attach', { id }).catch(() => null);
t.attaching = false;
if (r?.data) t.term.write(decode(r.data));
if (!r?.ok) t.ended = true;
out.set(id, t);
}
} else {
for (const x of rec.tabs.filter((x) => !x.rc)) {
const t = this.make(lib, x.label);
this.terms.push(t);
await this.start(t);
}
}
this.active = this.terms[Math.min(rec.active, this.terms.length - 1)] || this.terms[0] || null;
} finally { this.restoring = false; }
this.render();
for (const t of this.terms) { t.term.open(t.node); t.fit.fit(); }
return out;
}
/** Runs a command in a new tab (a CLI app's Run): typed into its shell, which stays there afterwards. */
async run(command, label = null) {
this.running = true; // shown() must not open an empty shell beside it
try { await this.open(); } finally { this.running = false; }
const t = this.current();
if (!t) return;
if (label) { t.label = label; this.render(); }
// typed only once the shell shows its prompt: typed earlier, the terminal echoes it, then readline again, and the
// shell's start (stty, its rc files) may swallow the line: it was shown twice and never ran
await this.ready(t);
this.input(t, command + '\r');
t.term.focus();
}
/** Resolves once the shell has printed something (its prompt) then stayed quiet 250 ms; at most 8 s. */
ready(t) {
const begun = Date.now();
return new Promise((resolve) => {
const check = () => {
const now = Date.now();
if (t.ended || now - begun > 8000 || (t.lastOutput && now - t.lastOutput >= 250)) resolve();
else setTimeout(check, 50);
};
check();
});
}
async start(t) {
t.lastOutput = 0;
t.ended = false;
try {
const r = await backend.invoke('terminal.open', { cols: t.term.cols, rows: t.term.rows });
t.id = r.id;
t.pty = r.pty;
if (!this.boot) backend.invoke('terminal.list').then((l) => { this.boot = l.boot; }).catch(() => null);
t.term.options.convertEol = !t.pty; // a pipe sends bare \n
t.label = t.label.replace(/^Local/, r.shell && !/^(bash|zsh|sh)$/.test(r.shell) ? r.shell.replace(/\.exe$/i, '') : 'Local');
if (t.queued) { const q = t.queued; t.queued = ''; this.input(t, q); }
if (!t.pty) t.term.write(`\x1b[2m${r.shell}: no pseudo-terminal here, the line is edited before it is sent\x1b[0m\r\n`);
} catch (e) {
t.ended = true;
t.term.write(`\x1b[31m${e.message || e}\x1b[0m\r\n`);
}
this.render();
}
input(t, data) {
if (t.ended) {
if (data === '\r') { t.term.reset(); this.start(t); }
return;
}
if (t.id === null) { t.queued = (t.queued || '') + data; return; } // the shell is starting
if (t.pty) { backend.invoke('terminal.write', { id: t.id, data }).catch(() => {}); return; }
// pipes (Windows): echo and edit the line here, send it whole
for (const ch of data.replace(/\r\n/g, '\r')) {
if (ch === '\r' || ch === '\n') {
t.term.write('\r\n');
backend.invoke('terminal.write', { id: t.id, data: t.line + '\r\n' }).catch(() => {});
t.line = '';
} else if (ch === '\x7f' || ch === '\b') {
if (t.line) { t.line = t.line.slice(0, -1); t.term.write('\b \b'); }
} else if (ch === '\x03') {
t.line = '';
t.term.write('^C\r\n');
} else if (ch >= ' ') {
t.line += ch;
t.term.write(ch);
}
}
}
close(t) {
if (t.id !== null && !t.ended) backend.invoke('terminal.close', { id: t.id }).catch(() => {});
t.term.dispose();
const wasEnded = t.ended;
t.ended = true;
if (!wasEnded) t.onEnd?.();
this.terms = this.terms.filter((x) => x !== t);
if (this.active === t) this.active = this.terms[this.terms.length - 1] || null;
this.render();
if (!this.terms.length) this.app.closeSide?.(this.app.tools.terminal.side);
}
fit() {
const t = this.current();
if (!t || !this.node.isConnected || !this.node.offsetWidth) return;
const w = this.node.clientWidth, h = this.node.clientHeight;
if (t.fitW === w && t.fitH === h) return; // same box: nothing to do
t.fitW = w; t.fitH = h;
try { t.fit.fit(); } catch { /* not laid out yet */ }
}
render() {
clear(this.tabsNode);
for (const t of this.terms) {
this.tabsNode.append(el(`button.tw-tab${t === this.active ? '.on' : ''}`, { onclick: () => { this.active = t; this.render(); t.term.focus(); } },
icon('terminal'), t.label + (t.ended ? ' (ended)' : ''), el('span.x', { title: 'Close', onclick: (e) => { e.stopPropagation(); this.close(t); } }, '×')));
}
for (const t of this.terms) t.node.hidden = t !== this.active;
if (!this.node.isConnected || [...this.node.children].length !== this.terms.length) {
clear(this.node);
for (const t of this.terms) this.node.append(t.node);
}
requestAnimationFrame(() => this.fit());
}
}