Compare commits
1 Commits
| Author | SHA256 | Date | |
|---|---|---|---|
| 34a46f3c40 |
@@ -1098,6 +1098,13 @@ back. Closing the window brings it back when the console is showing that
|
||||
device's shell and ends the session otherwise; reloading or closing the
|
||||
console tab ends every popped-out shell, since they run on its page.
|
||||
|
||||
**Terminal Settings** (the **Tt** button; theme and text size also sit in a
|
||||
pop-out's toolbar) are remembered per browser and apply to every open shell at
|
||||
once: a theme (Dark, Classic, PowerShell Blue, Solarized Dark, Light), the text
|
||||
size (9-28 px; in the terminal Ctrl + = / Ctrl + - or Ctrl + mouse wheel, Ctrl + 0
|
||||
resets) and the scrollback, the output lines kept above the screen (1,000 to
|
||||
25,000; 10,000 by default). Older lines drop off as new output arrives.
|
||||
|
||||
---
|
||||
|
||||
## Agent upgrades and the watchdog
|
||||
|
||||
@@ -21,7 +21,9 @@ import useApi from 'hooks/useApi';
|
||||
import useSession, { useGate } from 'hooks/useSession';
|
||||
import DeviceToolFrame, { useToolSurface } from 'sections/deviceTools/DeviceToolFrame';
|
||||
import { useIsPopOut } from 'sections/deviceTools/popOut';
|
||||
import useRemoteShell, { SHELL_KINDS, shellLabel, ShellKind, ShellState, TERMINAL_THEME } from 'sections/shell/useRemoteShell';
|
||||
import useRemoteShell, { SHELL_KINDS, shellLabel, ShellKind, ShellState } from 'sections/shell/useRemoteShell';
|
||||
import TerminalSettings from 'sections/shell/TerminalSettings';
|
||||
import { themeOf, useTerminalPrefs } from 'sections/shell/terminalPrefs';
|
||||
import { keys } from 'api/client';
|
||||
import type { DeviceDetailDto } from 'api/types';
|
||||
|
||||
@@ -96,6 +98,7 @@ function ShellView({ clientId }: { clientId: string }) {
|
||||
|
||||
const active = ACTIVE.includes(state);
|
||||
const status = STATUS[state];
|
||||
const { theme } = themeOf(useTerminalPrefs());
|
||||
|
||||
// Bringing it back from this page never needs to navigate.
|
||||
const bringBack = () => controller?.dock(() => {});
|
||||
@@ -113,6 +116,7 @@ function ShellView({ clientId }: { clientId: string }) {
|
||||
allowed && (
|
||||
<>
|
||||
{windows && controller && <ShellPicker value={shell} started={startedShell} onChange={chooseShell} />}
|
||||
<TerminalSettings />
|
||||
{poppedOut && !popout && (
|
||||
<Button variant="outlined" color="secondary" startIcon={<ImportOutlined />} onClick={bringBack}>
|
||||
Bring Back
|
||||
@@ -143,7 +147,7 @@ function ShellView({ clientId }: { clientId: string }) {
|
||||
{(state === 'failed' || state === 'noResponse') && message && (
|
||||
<Alert severity={state === 'failed' ? 'error' : 'warning'}>{message}</Alert>
|
||||
)}
|
||||
<MainCard content={false} sx={{ bgcolor: TERMINAL_THEME.background, overflow: 'hidden', ...surface.card }}>
|
||||
<MainCard content={false} sx={{ bgcolor: theme.background, overflow: 'hidden', ...surface.card }}>
|
||||
<Box sx={{ position: 'relative', p: 1.5, ...surface.body }}>
|
||||
{/* xterm sizes itself to this element (the padding lives on the parent so fitting stays exact).
|
||||
It stays mounted while popped out so closing the window can dock the terminal straight back. */}
|
||||
@@ -152,13 +156,13 @@ function ShellView({ clientId }: { clientId: string }) {
|
||||
<Stack
|
||||
sx={{ position: 'absolute', inset: 0, alignItems: 'center', justifyContent: 'center', gap: 2, p: 3, textAlign: 'center' }}
|
||||
>
|
||||
<ExportOutlined style={{ fontSize: '2rem', color: TERMINAL_THEME.cursor }} />
|
||||
<ExportOutlined style={{ fontSize: '2rem', color: theme.cursor }} />
|
||||
<Stack
|
||||
direction="row"
|
||||
sx={{
|
||||
alignItems: 'center',
|
||||
color: TERMINAL_THEME.foreground,
|
||||
'& [tabindex]': { color: TERMINAL_THEME.foreground, opacity: 0.7 }
|
||||
color: theme.foreground,
|
||||
'& [tabindex]': { color: theme.foreground, opacity: 0.7 }
|
||||
}}
|
||||
>
|
||||
<Typography variant="h5">This Shell Is In Its Own Window</Typography>
|
||||
@@ -168,7 +172,11 @@ function ShellView({ clientId }: { clientId: string }) {
|
||||
<Button variant="contained" startIcon={<ImportOutlined />} onClick={bringBack}>
|
||||
Bring Back
|
||||
</Button>
|
||||
<Button variant="outlined" onClick={focusWindow} sx={{ color: TERMINAL_THEME.foreground, borderColor: '#2a3b5c' }}>
|
||||
<Button
|
||||
variant="outlined"
|
||||
onClick={focusWindow}
|
||||
sx={{ color: theme.foreground, borderColor: theme.selectionBackground }}
|
||||
>
|
||||
Show Window
|
||||
</Button>
|
||||
</Stack>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { useNavigate } from 'react-router-dom';
|
||||
|
||||
// project imports
|
||||
import { shellLabel, shells, TERMINAL_THEME, type ShellController, type ShellState } from './shellSessions';
|
||||
import { FONT_SIZE, TERMINAL_THEMES, terminalPrefs, useTerminalPrefs } from './terminalPrefs';
|
||||
|
||||
// assets
|
||||
import ImportOutlined from '@ant-design/icons/ImportOutlined';
|
||||
@@ -55,6 +56,11 @@ const CHROME_CSS = `
|
||||
button.primary { border-color: #2a3b5c; }
|
||||
button.danger:hover { background: rgba(248, 81, 73, 0.14); color: #ff7b72; }
|
||||
.anticon { display: inline-flex; font-size: 14px; }
|
||||
.prefs { display: flex; align-items: center; gap: 2px; padding-right: 6px; margin-right: 2px; border-right: 1px solid #1f2b40; }
|
||||
.prefs button { padding: 0 8px; }
|
||||
.prefs .size { min-width: 34px; text-align: center; color: #c9d4e5; font-variant-numeric: tabular-nums; }
|
||||
select { height: 28px; border-radius: 4px; border: 1px solid #2a3b5c; background: #111a29; color: ${TERMINAL_THEME.foreground}; font: inherit; padding: 0 6px; }
|
||||
select:focus-visible { outline: 2px solid ${TERMINAL_THEME.cursor}; outline-offset: 1px; }
|
||||
`;
|
||||
|
||||
function Chrome({ controller }: { controller: ShellController }) {
|
||||
@@ -63,6 +69,7 @@ function Chrome({ controller }: { controller: ShellController }) {
|
||||
const started = shellLabel(startedShell);
|
||||
const name = hostname || controller.clientId.slice(0, 12);
|
||||
const active = ACTIVE.includes(state);
|
||||
const prefs = useTerminalPrefs();
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -80,6 +87,37 @@ function Chrome({ controller }: { controller: ShellController }) {
|
||||
)}
|
||||
</div>
|
||||
<div className="actions">
|
||||
<div className="prefs">
|
||||
<select
|
||||
aria-label="Terminal Theme"
|
||||
title="Terminal Theme"
|
||||
value={prefs.themeId}
|
||||
onChange={(e) => terminalPrefs.set({ themeId: e.target.value })}
|
||||
>
|
||||
{TERMINAL_THEMES.map((t) => (
|
||||
<option key={t.id} value={t.id}>
|
||||
{t.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
type="button"
|
||||
title="Smaller Text (Ctrl + -)"
|
||||
disabled={prefs.fontSize <= FONT_SIZE.min}
|
||||
onClick={() => terminalPrefs.zoom(-1)}
|
||||
>
|
||||
A−
|
||||
</button>
|
||||
<span className="size">{prefs.fontSize}</span>
|
||||
<button
|
||||
type="button"
|
||||
title="Larger Text (Ctrl + =)"
|
||||
disabled={prefs.fontSize >= FONT_SIZE.max}
|
||||
onClick={() => terminalPrefs.zoom(1)}
|
||||
>
|
||||
A+
|
||||
</button>
|
||||
</div>
|
||||
{active ? (
|
||||
<button type="button" className="danger" onClick={() => controller.close()}>
|
||||
<PoweroffOutlined /> Close Session
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
// material-ui
|
||||
import Box from '@mui/material/Box';
|
||||
import Button from '@mui/material/Button';
|
||||
import ButtonBase from '@mui/material/ButtonBase';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import MenuItem from '@mui/material/MenuItem';
|
||||
import Popover from '@mui/material/Popover';
|
||||
import Stack from '@mui/material/Stack';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import Typography from '@mui/material/Typography';
|
||||
|
||||
// project imports
|
||||
import HoverHelp from 'components/HoverHelp';
|
||||
import { MONO_FONT_FAMILY } from 'config';
|
||||
import { FONT_SIZE, SCROLLBACK_OPTIONS, TERMINAL_THEMES, terminalPrefs, useTerminalPrefs } from './terminalPrefs';
|
||||
|
||||
// assets
|
||||
import FontSizeOutlined from '@ant-design/icons/FontSizeOutlined';
|
||||
import MinusOutlined from '@ant-design/icons/MinusOutlined';
|
||||
import PlusOutlined from '@ant-design/icons/PlusOutlined';
|
||||
|
||||
/** Theme, text size and scrollback of every terminal in this browser. */
|
||||
export default function TerminalSettings() {
|
||||
const prefs = useTerminalPrefs();
|
||||
const [anchor, setAnchor] = useState<HTMLElement | null>(null);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Tooltip title="Terminal Settings">
|
||||
<Button
|
||||
variant="outlined"
|
||||
color="secondary"
|
||||
aria-label="Terminal Settings"
|
||||
onClick={(e) => setAnchor(e.currentTarget)}
|
||||
sx={{ minWidth: 0, px: 1.25 }}
|
||||
>
|
||||
<FontSizeOutlined />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Popover
|
||||
open={Boolean(anchor)}
|
||||
anchorEl={anchor}
|
||||
onClose={() => setAnchor(null)}
|
||||
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
|
||||
transformOrigin={{ vertical: 'top', horizontal: 'right' }}
|
||||
slotProps={{ paper: { sx: { p: 2, width: 300 } } }}
|
||||
>
|
||||
<Stack sx={{ gap: 2 }}>
|
||||
<Stack sx={{ gap: 1 }}>
|
||||
<Typography variant="subtitle2">Theme</Typography>
|
||||
<Stack direction="row" sx={{ gap: 1, flexWrap: 'wrap' }}>
|
||||
{TERMINAL_THEMES.map(({ id, label, theme }) => (
|
||||
<Tooltip key={id} title={label}>
|
||||
<ButtonBase
|
||||
aria-label={label}
|
||||
aria-pressed={prefs.themeId === id}
|
||||
onClick={() => terminalPrefs.set({ themeId: id })}
|
||||
sx={{
|
||||
width: 44,
|
||||
height: 32,
|
||||
borderRadius: 1,
|
||||
bgcolor: theme.background,
|
||||
color: theme.foreground,
|
||||
fontFamily: MONO_FONT_FAMILY,
|
||||
fontSize: 13,
|
||||
border: 2,
|
||||
borderColor: prefs.themeId === id ? 'primary.main' : 'divider'
|
||||
}}
|
||||
>
|
||||
<Box component="span" sx={{ color: theme.green ?? theme.cursor }}>
|
||||
>
|
||||
</Box>
|
||||
_
|
||||
</ButtonBase>
|
||||
</Tooltip>
|
||||
))}
|
||||
</Stack>
|
||||
</Stack>
|
||||
|
||||
<Stack sx={{ gap: 1 }}>
|
||||
<Stack direction="row" sx={{ alignItems: 'center' }}>
|
||||
<Typography variant="subtitle2">Text Size</Typography>
|
||||
<HoverHelp title="In the terminal: Ctrl + = and Ctrl + - (or Ctrl + mouse wheel) change the size, Ctrl + 0 resets it." />
|
||||
</Stack>
|
||||
<Stack direction="row" sx={{ alignItems: 'center', gap: 1 }}>
|
||||
<IconButton
|
||||
size="small"
|
||||
aria-label="Smaller"
|
||||
onClick={() => terminalPrefs.zoom(-1)}
|
||||
disabled={prefs.fontSize <= FONT_SIZE.min}
|
||||
>
|
||||
<MinusOutlined />
|
||||
</IconButton>
|
||||
<Typography sx={{ minWidth: 48, textAlign: 'center', fontVariantNumeric: 'tabular-nums' }}>{prefs.fontSize} px</Typography>
|
||||
<IconButton size="small" aria-label="Larger" onClick={() => terminalPrefs.zoom(1)} disabled={prefs.fontSize >= FONT_SIZE.max}>
|
||||
<PlusOutlined />
|
||||
</IconButton>
|
||||
<Button
|
||||
size="small"
|
||||
onClick={() => terminalPrefs.zoom(0)}
|
||||
disabled={prefs.fontSize === FONT_SIZE.default}
|
||||
sx={{ ml: 'auto' }}
|
||||
>
|
||||
Reset
|
||||
</Button>
|
||||
</Stack>
|
||||
</Stack>
|
||||
|
||||
<Stack sx={{ gap: 1 }}>
|
||||
<Stack direction="row" sx={{ alignItems: 'center' }}>
|
||||
<Typography variant="subtitle2">Scrollback</Typography>
|
||||
<HoverHelp title="Output lines kept above the screen; older lines drop off as new output arrives." />
|
||||
</Stack>
|
||||
<TextField
|
||||
select
|
||||
size="small"
|
||||
aria-label="Scrollback"
|
||||
value={prefs.scrollback}
|
||||
onChange={(e) => terminalPrefs.set({ scrollback: Number(e.target.value) })}
|
||||
>
|
||||
{SCROLLBACK_OPTIONS.map((lines) => (
|
||||
<MenuItem key={lines} value={lines}>
|
||||
{lines.toLocaleString()} Lines
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
</Stack>
|
||||
</Stack>
|
||||
</Popover>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { api, errorMessage } from 'api/client';
|
||||
import { MONO_FONT_FAMILY } from 'config';
|
||||
import { POPOUT_SIZE, popOutWindowName } from 'sections/deviceTools/deviceTools';
|
||||
import { nats, type ShellEvent } from 'messaging/nats';
|
||||
import { terminalPrefs, themeOf } from './terminalPrefs';
|
||||
|
||||
// assets
|
||||
import xtermCss from '@xterm/xterm/css/xterm.css?inline';
|
||||
@@ -72,7 +73,7 @@ function rememberShell(kind: ShellKind) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Dark terminal palette in every color scheme. */
|
||||
/** The default (dark) palette: the pop-out window's toolbar keeps it whatever the terminal's theme. */
|
||||
export const TERMINAL_THEME = { background: '#0d1420', foreground: '#e6ecf5', cursor: '#4c8dff', selectionBackground: '#2a3b5c' };
|
||||
|
||||
/** How long the agent gets to open the shell before the operator is told why it may not. */
|
||||
@@ -146,6 +147,7 @@ export class ShellController {
|
||||
private readonly fit = new FitAddon();
|
||||
private term: Terminal | null = null;
|
||||
private observer: ResizeObserver | null = null;
|
||||
private offPrefs: (() => void) | null = null;
|
||||
private session: Session | null = null;
|
||||
private snapshot: ShellSnapshot = { state: 'connecting', hostname: '', poppedOut: false, shell: rememberedShell() };
|
||||
private readonly listeners = new Set<() => void>();
|
||||
@@ -217,9 +219,18 @@ export class ShellController {
|
||||
host.appendChild(this.element);
|
||||
|
||||
if (!this.term) {
|
||||
this.term = new Terminal({ fontFamily: MONO_FONT_FAMILY, fontSize: 13, cursorBlink: true, theme: TERMINAL_THEME });
|
||||
const prefs = terminalPrefs.get();
|
||||
this.term = new Terminal({
|
||||
fontFamily: MONO_FONT_FAMILY,
|
||||
fontSize: prefs.fontSize,
|
||||
scrollback: prefs.scrollback,
|
||||
cursorBlink: true,
|
||||
theme: themeOf(prefs).theme
|
||||
});
|
||||
this.term.loadAddon(this.fit);
|
||||
this.term.open(this.element);
|
||||
this.offPrefs = terminalPrefs.subscribe(() => this.applyPrefs());
|
||||
this.bindZoom(this.term);
|
||||
this.term.onResize(({ cols, rows }) => {
|
||||
const session = this.session;
|
||||
if (session?.id && !session.closed) publish(session.id, 'command.shell.resize', { sessionId: session.id, cols, rows });
|
||||
@@ -241,6 +252,41 @@ export class ShellController {
|
||||
this.term.focus();
|
||||
}
|
||||
|
||||
/** Terminal Settings changed (here or in another window): restyle and refit. */
|
||||
private applyPrefs() {
|
||||
const term = this.term;
|
||||
if (!term) return;
|
||||
const prefs = terminalPrefs.get();
|
||||
const { theme } = themeOf(prefs);
|
||||
term.options.theme = theme;
|
||||
term.options.fontSize = prefs.fontSize;
|
||||
term.options.scrollback = prefs.scrollback;
|
||||
if (this.popup && !this.popup.closed) this.popup.document.body.style.background = theme.background;
|
||||
this.safeFit();
|
||||
}
|
||||
|
||||
/** Ctrl (or Cmd) with = / - / 0, or with the wheel, sizes the text while the terminal has focus. */
|
||||
private bindZoom(term: Terminal) {
|
||||
term.attachCustomKeyEventHandler((event) => {
|
||||
if (event.type !== 'keydown' || !(event.ctrlKey || event.metaKey) || event.altKey || event.shiftKey) return true;
|
||||
const step = event.key === '=' || event.key === '+' ? 1 : event.key === '-' ? -1 : event.key === '0' ? 0 : null;
|
||||
if (step === null) return true;
|
||||
event.preventDefault();
|
||||
terminalPrefs.zoom(step);
|
||||
return false;
|
||||
});
|
||||
this.element.addEventListener(
|
||||
'wheel',
|
||||
(event) => {
|
||||
if (!(event.ctrlKey || event.metaKey) || event.deltaY === 0) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
terminalPrefs.zoom(event.deltaY < 0 ? 1 : -1);
|
||||
},
|
||||
{ capture: true, passive: false }
|
||||
);
|
||||
}
|
||||
|
||||
private safeFit() {
|
||||
// Hidden or zero-sized hosts cannot be measured; the observer refits once visible.
|
||||
if (this.element.isConnected && this.element.clientWidth > 0) this.fit.fit();
|
||||
@@ -284,6 +330,8 @@ export class ShellController {
|
||||
host.id = 'shell-terminal';
|
||||
doc.body.append(chrome, host);
|
||||
|
||||
doc.body.style.background = themeOf(terminalPrefs.get()).theme.background;
|
||||
|
||||
this.popup = popup;
|
||||
this.chromeHost = chrome;
|
||||
this.mount(host);
|
||||
@@ -479,6 +527,8 @@ export class ShellController {
|
||||
this.session = null;
|
||||
this.closePopup();
|
||||
this.observer?.disconnect();
|
||||
this.offPrefs?.();
|
||||
this.offPrefs = null;
|
||||
this.term?.dispose();
|
||||
this.term = null;
|
||||
this.element.remove();
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
import { useSyncExternalStore } from 'react';
|
||||
|
||||
// third-party
|
||||
import type { ITheme } from '@xterm/xterm';
|
||||
|
||||
/** A terminal palette; the card and the pop-out window around the terminal take its background. */
|
||||
export interface TerminalTheme {
|
||||
id: string;
|
||||
label: string;
|
||||
theme: ITheme & Required<Pick<ITheme, 'background' | 'foreground' | 'cursor' | 'selectionBackground'>>;
|
||||
light?: boolean;
|
||||
}
|
||||
|
||||
/** Palettes offered in Terminal Settings; the first is the default. */
|
||||
export const TERMINAL_THEMES: TerminalTheme[] = [
|
||||
{
|
||||
id: 'dark',
|
||||
label: 'Dark',
|
||||
theme: { background: '#0d1420', foreground: '#e6ecf5', cursor: '#4c8dff', selectionBackground: '#2a3b5c' }
|
||||
},
|
||||
{
|
||||
id: 'classic',
|
||||
label: 'Classic',
|
||||
theme: {
|
||||
background: '#0c0c0c',
|
||||
foreground: '#cccccc',
|
||||
cursor: '#ffffff',
|
||||
selectionBackground: '#3a3a3a',
|
||||
black: '#0c0c0c',
|
||||
red: '#c50f1f',
|
||||
green: '#13a10e',
|
||||
yellow: '#c19c00',
|
||||
blue: '#0037da',
|
||||
magenta: '#881798',
|
||||
cyan: '#3a96dd',
|
||||
white: '#cccccc',
|
||||
brightBlack: '#767676',
|
||||
brightRed: '#e74856',
|
||||
brightGreen: '#16c60c',
|
||||
brightYellow: '#f9f1a5',
|
||||
brightBlue: '#3b78ff',
|
||||
brightMagenta: '#b4009e',
|
||||
brightCyan: '#61d6d6',
|
||||
brightWhite: '#f2f2f2'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'powershell',
|
||||
label: 'PowerShell Blue',
|
||||
theme: {
|
||||
background: '#012456',
|
||||
foreground: '#eeedf0',
|
||||
cursor: '#fedba9',
|
||||
selectionBackground: '#264f78',
|
||||
black: '#000000',
|
||||
red: '#c50f1f',
|
||||
green: '#13a10e',
|
||||
yellow: '#c19c00',
|
||||
blue: '#0037da',
|
||||
magenta: '#881798',
|
||||
cyan: '#3a96dd',
|
||||
white: '#cccccc',
|
||||
brightBlack: '#767676',
|
||||
brightRed: '#e74856',
|
||||
brightGreen: '#16c60c',
|
||||
brightYellow: '#f9f1a5',
|
||||
brightBlue: '#3b78ff',
|
||||
brightMagenta: '#b4009e',
|
||||
brightCyan: '#61d6d6',
|
||||
brightWhite: '#f2f2f2'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'solarized',
|
||||
label: 'Solarized Dark',
|
||||
theme: {
|
||||
background: '#002b36',
|
||||
foreground: '#93a1a1',
|
||||
cursor: '#93a1a1',
|
||||
selectionBackground: '#073642',
|
||||
black: '#073642',
|
||||
red: '#dc322f',
|
||||
green: '#859900',
|
||||
yellow: '#b58900',
|
||||
blue: '#268bd2',
|
||||
magenta: '#d33682',
|
||||
cyan: '#2aa198',
|
||||
white: '#eee8d5',
|
||||
brightBlack: '#586e75',
|
||||
brightRed: '#cb4b16',
|
||||
brightGreen: '#93a400',
|
||||
brightYellow: '#c8a000',
|
||||
brightBlue: '#4aa3e0',
|
||||
brightMagenta: '#6c71c4',
|
||||
brightCyan: '#3ec1b6',
|
||||
brightWhite: '#fdf6e3'
|
||||
}
|
||||
},
|
||||
{
|
||||
id: 'light',
|
||||
label: 'Light',
|
||||
light: true,
|
||||
theme: {
|
||||
background: '#fafbfc',
|
||||
foreground: '#1f2328',
|
||||
cursor: '#0969da',
|
||||
selectionBackground: '#b6d5ff',
|
||||
black: '#24292f',
|
||||
red: '#cf222e',
|
||||
green: '#116329',
|
||||
yellow: '#7d4e00',
|
||||
blue: '#0550ae',
|
||||
magenta: '#8250df',
|
||||
cyan: '#1b7c83',
|
||||
white: '#6e7781',
|
||||
brightBlack: '#57606a',
|
||||
brightRed: '#a40e26',
|
||||
brightGreen: '#1a7f37',
|
||||
brightYellow: '#633c01',
|
||||
brightBlue: '#218bff',
|
||||
brightMagenta: '#a475f9',
|
||||
brightCyan: '#3192aa',
|
||||
brightWhite: '#8c959f'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
export const FONT_SIZE = { min: 9, max: 28, default: 13 };
|
||||
|
||||
/** Lines kept above the screen. xterm keeps 12 bytes a cell, so 25,000 lines of 200 columns is about 60 MB. */
|
||||
export const SCROLLBACK_OPTIONS = [1_000, 5_000, 10_000, 25_000];
|
||||
const DEFAULT_SCROLLBACK = 10_000;
|
||||
|
||||
export interface TerminalPrefs {
|
||||
themeId: string;
|
||||
fontSize: number;
|
||||
scrollback: number;
|
||||
}
|
||||
|
||||
const KEY = 'executer-terminal-prefs';
|
||||
const DEFAULTS: TerminalPrefs = { themeId: TERMINAL_THEMES[0].id, fontSize: FONT_SIZE.default, scrollback: DEFAULT_SCROLLBACK };
|
||||
|
||||
function sanitize(value: Partial<TerminalPrefs> | null | undefined): TerminalPrefs {
|
||||
const themeId = TERMINAL_THEMES.some((t) => t.id === value?.themeId) ? value!.themeId! : DEFAULTS.themeId;
|
||||
const size = Number(value?.fontSize);
|
||||
const fontSize = Number.isFinite(size) ? Math.min(FONT_SIZE.max, Math.max(FONT_SIZE.min, Math.round(size))) : DEFAULTS.fontSize;
|
||||
const scrollback = SCROLLBACK_OPTIONS.includes(Number(value?.scrollback)) ? Number(value!.scrollback) : DEFAULTS.scrollback;
|
||||
return { themeId, fontSize, scrollback };
|
||||
}
|
||||
|
||||
function load(): TerminalPrefs {
|
||||
try {
|
||||
return sanitize(JSON.parse(localStorage.getItem(KEY) ?? 'null') as Partial<TerminalPrefs> | null);
|
||||
} catch {
|
||||
return DEFAULTS;
|
||||
}
|
||||
}
|
||||
|
||||
let current = load();
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
/** The terminal settings, remembered per browser; every open terminal follows changes at once. */
|
||||
export const terminalPrefs = {
|
||||
get: () => current,
|
||||
subscribe(listener: () => void) {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
},
|
||||
set(patch: Partial<TerminalPrefs>) {
|
||||
const next = sanitize({ ...current, ...patch });
|
||||
if (next.themeId === current.themeId && next.fontSize === current.fontSize && next.scrollback === current.scrollback) return;
|
||||
current = next;
|
||||
try {
|
||||
localStorage.setItem(KEY, JSON.stringify(current));
|
||||
} catch {
|
||||
// Private windows: the settings last for this page only.
|
||||
}
|
||||
listeners.forEach((listener) => listener());
|
||||
},
|
||||
/** Larger (+1), smaller (-1) or back to the default size (0). */
|
||||
zoom(step: -1 | 0 | 1) {
|
||||
terminalPrefs.set({ fontSize: step === 0 ? FONT_SIZE.default : current.fontSize + step });
|
||||
}
|
||||
};
|
||||
|
||||
export const themeOf = (prefs: TerminalPrefs) => TERMINAL_THEMES.find((t) => t.id === prefs.themeId) ?? TERMINAL_THEMES[0];
|
||||
|
||||
export function useTerminalPrefs(): TerminalPrefs {
|
||||
return useSyncExternalStore(terminalPrefs.subscribe, terminalPrefs.get);
|
||||
}
|
||||
Reference in New Issue
Block a user