diff --git a/Makefile b/Makefile index 1a2df2c..98427a8 100644 --- a/Makefile +++ b/Makefile @@ -97,5 +97,12 @@ bump-patch: ## Bump patch version and create git tag git tag "v$$NEW"; \ echo "Bumped version: $$OLD -> $$NEW (tagged v$$NEW)" -push: ## Push commits and tags to origin - git push origin main --tags +push: ## Push commits and current tag to origin + @TAG=$$(git describe --tags --exact-match 2>/dev/null); \ + git push origin main; \ + if [ -n "$$TAG" ]; then \ + echo "Pushing tag $$TAG..."; \ + git push origin "$$TAG"; \ + else \ + echo "No tag on current commit"; \ + fi diff --git a/src/webterm/docker_exec_session.py b/src/webterm/docker_exec_session.py index bb95467..e2b66d7 100644 --- a/src/webterm/docker_exec_session.py +++ b/src/webterm/docker_exec_session.py @@ -29,9 +29,15 @@ DEFAULT_SCREEN_WIDTH = 132 DEFAULT_SCREEN_HEIGHT = 45 # Pattern to filter out terminal device attribute responses that cause display issues -# These are responses to queries that shouldn't be displayed as text +# These are responses to queries that shouldn't be displayed as text. +# Matches complete DA1/DA2 responses like \x1b[?1;10;0c or \x1b[?64;1;2;...c DA_RESPONSE_PATTERN = re.compile(rb'\x1b\[\?[\d;]+c') +# Pattern to detect partial DA responses at end of data (incomplete escape sequence) +# Matches: \x1b, \x1b[, \x1b[?, \x1b[?1, \x1b[?1;, \x1b[?1;10, etc. +# These need to be held back until more data arrives to see if they complete +DA_PARTIAL_PATTERN = re.compile(rb'\x1b(?:\[(?:\?[\d;]*)?)?$') + @dataclass(frozen=True) class DockerExecSpec: @@ -69,6 +75,8 @@ class DockerExecSession(Session): self._last_snapshot_counter = 0 self._exec_id: str | None = None self._pending_output = b"" + # Buffer for handling escape sequences split across socket reads + self._escape_buffer = b"" def __repr__(self) -> str: return ( @@ -302,11 +310,25 @@ class DockerExecSession(Session): data = await queue.get() if not data: break - # Filter out device attribute responses that can cause display issues - # when split across socket reads + # Prepend any buffered partial escape sequence from previous read + if self._escape_buffer: + data = self._escape_buffer + data + self._escape_buffer = b"" + + # Filter out complete DA1/DA2 responses (e.g., \x1b[?1;10;0c) data = DA_RESPONSE_PATTERN.sub(b'', data) if not data: continue + + # Check for partial escape sequence at end that might be a DA response + # Hold it back until we get more data to see if it completes + match = DA_PARTIAL_PATTERN.search(data) + if match: + self._escape_buffer = data[match.start():] + data = data[:match.start()] + if not data: + continue + await self._add_to_replay_buffer(data) await self._update_screen(data) if self._connector: diff --git a/src/webterm/static/js/terminal.js b/src/webterm/static/js/terminal.js index 6ad0b9b..8bd4ffa 100644 --- a/src/webterm/static/js/terminal.js +++ b/src/webterm/static/js/terminal.js @@ -10,7 +10,7 @@ Example: For tests, pass a Ghostty instance directly: import { Ghostty, Terminal } from "ghostty-web"; const ghostty = await Ghostty.load(); - const term = new Terminal({ ghostty });`);return s}var W0='ui-monospace, "SFMono-Regular", "FiraCode Nerd Font", "FiraMono Nerd Font", "Fira Code", "Roboto Mono", Menlo, Monaco, Consolas, "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace',F={tango:{background:"#000000",foreground:"#d3d7cf",cursor:"#d3d7cf",cursorAccent:"#000000",selectionBackground:"#555753",black:"#2e3436",red:"#cc0000",green:"#4e9a06",yellow:"#c4a000",blue:"#3465a4",magenta:"#75507b",cyan:"#06989a",white:"#d3d7cf",brightBlack:"#555753",brightRed:"#ef2929",brightGreen:"#8ae234",brightYellow:"#fce94f",brightBlue:"#729fcf",brightMagenta:"#ad7fa8",brightCyan:"#34e2e2",brightWhite:"#eeeeec"},xterm:{background:"#000000",foreground:"#e5e5e5",cursor:"#e5e5e5",cursorAccent:"#000000",selectionBackground:"#d4d4d4",black:"#000000",red:"#cd0000",green:"#00cd00",yellow:"#cdcd00",blue:"#0000cd",magenta:"#cd00cd",cyan:"#00cdcd",white:"#e5e5e5",brightBlack:"#4d4d4d",brightRed:"#ff0000",brightGreen:"#00ff00",brightYellow:"#ffff00",brightBlue:"#0000ff",brightMagenta:"#ff00ff",brightCyan:"#00ffff",brightWhite:"#ffffff"},monokai:{background:"#2d2a2e",foreground:"#fcfcfa",cursor:"#fcfcfa",cursorAccent:"#2d2a2e",selectionBackground:"#5b595c",black:"#403e41",red:"#ff6188",green:"#a9dc76",yellow:"#ffd866",blue:"#fc9867",magenta:"#ab9df2",cyan:"#78dce8",white:"#fcfcfa",brightBlack:"#727072",brightRed:"#ff6188",brightGreen:"#a9dc76",brightYellow:"#ffd866",brightBlue:"#fc9867",brightMagenta:"#ab9df2",brightCyan:"#78dce8",brightWhite:"#fcfcfa"},ristretto:{background:"#2d2525",foreground:"#fff1f3",cursor:"#fff1f3",cursorAccent:"#2d2525",selectionBackground:"#403838",black:"#2c2525",red:"#fd6883",green:"#adda78",yellow:"#f9cc6c",blue:"#f38d70",magenta:"#a8a9eb",cyan:"#85dacc",white:"#f9f8f5",brightBlack:"#655761",brightRed:"#fd6883",brightGreen:"#adda78",brightYellow:"#f9cc6c",brightBlue:"#f38d70",brightMagenta:"#a8a9eb",brightCyan:"#85dacc",brightWhite:"#f9f8f5"},dark:{background:"#1e1e1e",foreground:"#d4d4d4",cursor:"#aeafad",cursorAccent:"#1e1e1e",selectionBackground:"#264f78",black:"#000000",red:"#cd3131",green:"#0dbc79",yellow:"#e5e510",blue:"#2472c8",magenta:"#bc3fbc",cyan:"#11a8cd",white:"#e5e5e5",brightBlack:"#666666",brightRed:"#f14c4c",brightGreen:"#23d18b",brightYellow:"#f5f543",brightBlue:"#3b8eea",brightMagenta:"#d670d6",brightCyan:"#29b8db",brightWhite:"#ffffff"},light:{background:"#ffffff",foreground:"#383a42",cursor:"#526eff",cursorAccent:"#ffffff",selectionBackground:"#add6ff",black:"#000000",red:"#e45649",green:"#50a14f",yellow:"#c18401",blue:"#4078f2",magenta:"#a626a4",cyan:"#0184bc",white:"#a0a1a7",brightBlack:"#5c6370",brightRed:"#e06c75",brightGreen:"#98c379",brightYellow:"#d19a66",brightBlue:"#61afef",brightMagenta:"#c678dd",brightCyan:"#56b6c2",brightWhite:"#ffffff"},dracula:{background:"#282a36",foreground:"#f8f8f2",cursor:"#f8f8f2",cursorAccent:"#282a36",selectionBackground:"#44475a",black:"#21222c",red:"#ff5555",green:"#50fa7b",yellow:"#f1fa8c",blue:"#bd93f9",magenta:"#ff79c6",cyan:"#8be9fd",white:"#f8f8f2",brightBlack:"#6272a4",brightRed:"#ff6e6e",brightGreen:"#69ff94",brightYellow:"#ffffa5",brightBlue:"#d6acff",brightMagenta:"#ff92df",brightCyan:"#a4ffff",brightWhite:"#ffffff"},catppuccin:{background:"#1e1e2e",foreground:"#cdd6f4",cursor:"#f5e0dc",cursorAccent:"#1e1e2e",selectionBackground:"#45475a",black:"#45475a",red:"#f38ba8",green:"#a6e3a1",yellow:"#f9e2af",blue:"#89b4fa",magenta:"#f5c2e7",cyan:"#94e2d5",white:"#bac2de",brightBlack:"#585b70",brightRed:"#f38ba8",brightGreen:"#a6e3a1",brightYellow:"#f9e2af",brightBlue:"#89b4fa",brightMagenta:"#f5c2e7",brightCyan:"#94e2d5",brightWhite:"#a6adc8"},nord:{background:"#2e3440",foreground:"#d8dee9",cursor:"#d8dee9",cursorAccent:"#2e3440",selectionBackground:"#434c5e",black:"#3b4252",red:"#bf616a",green:"#a3be8c",yellow:"#ebcb8b",blue:"#81a1c1",magenta:"#b48ead",cyan:"#88c0d0",white:"#e5e9f0",brightBlack:"#4c566a",brightRed:"#bf616a",brightGreen:"#a3be8c",brightYellow:"#ebcb8b",brightBlue:"#81a1c1",brightMagenta:"#b48ead",brightCyan:"#8fbcbb",brightWhite:"#eceff4"},gruvbox:{background:"#282828",foreground:"#ebdbb2",cursor:"#ebdbb2",cursorAccent:"#282828",selectionBackground:"#504945",black:"#282828",red:"#cc241d",green:"#98971a",yellow:"#d79921",blue:"#458588",magenta:"#b16286",cyan:"#689d6a",white:"#a89984",brightBlack:"#928374",brightRed:"#fb4934",brightGreen:"#b8bb26",brightYellow:"#fabd2f",brightBlue:"#83a598",brightMagenta:"#d3869b",brightCyan:"#8ec07c",brightWhite:"#ebdbb2"},solarized:{background:"#002b36",foreground:"#839496",cursor:"#839496",cursorAccent:"#002b36",selectionBackground:"#073642",black:"#073642",red:"#dc322f",green:"#859900",yellow:"#b58900",blue:"#268bd2",magenta:"#d33682",cyan:"#2aa198",white:"#eee8d5",brightBlack:"#586e75",brightRed:"#cb4b16",brightGreen:"#586e75",brightYellow:"#657b83",brightBlue:"#839496",brightMagenta:"#6c71c4",brightCyan:"#93a1a1",brightWhite:"#fdf6e3"},tokyo:{background:"#1a1b26",foreground:"#a9b1d6",cursor:"#c0caf5",cursorAccent:"#1a1b26",selectionBackground:"#33467c",black:"#15161e",red:"#f7768e",green:"#9ece6a",yellow:"#e0af68",blue:"#7aa2f7",magenta:"#bb9af7",cyan:"#7dcfff",white:"#a9b1d6",brightBlack:"#414868",brightRed:"#f7768e",brightGreen:"#9ece6a",brightYellow:"#e0af68",brightBlue:"#7aa2f7",brightMagenta:"#bb9af7",brightCyan:"#7dcfff",brightWhite:"#c0caf5"}};function z0(V){console.log("[webterm:parseConfig] Parsing config from element");let Z={};if(V.dataset.fontFamily)Z.fontFamily=V.dataset.fontFamily,console.log(`[webterm:parseConfig] fontFamily: "${Z.fontFamily}"`);if(V.dataset.fontSize)Z.fontSize=parseInt(V.dataset.fontSize,10),console.log(`[webterm:parseConfig] fontSize: ${Z.fontSize}`);if(V.dataset.scrollback)Z.scrollback=parseInt(V.dataset.scrollback,10),console.log(`[webterm:parseConfig] scrollback: ${Z.scrollback}`);if(V.dataset.theme){let j=V.dataset.theme.toLowerCase();if(console.log(`[webterm:parseConfig] theme attribute: "${V.dataset.theme}" -> normalized: "${j}"`),console.log(`[webterm:parseConfig] Available themes: ${Object.keys(F).join(", ")}`),console.log(`[webterm:parseConfig] Theme "${j}" in THEMES? ${j in F}`),j in F)Z.theme=F[j],console.log(`[webterm:parseConfig] Using built-in theme "${j}":`,JSON.stringify(Z.theme,null,2));else{console.log("[webterm:parseConfig] Theme not found in THEMES, trying JSON parse...");try{Z.theme=JSON.parse(V.dataset.theme),console.log("[webterm:parseConfig] Parsed custom JSON theme:",Z.theme)}catch($){console.warn(`[webterm:parseConfig] Unknown theme "${V.dataset.theme}", JSON parse failed:`,$)}}}else console.log("[webterm:parseConfig] No theme attribute found on element");return console.log("[webterm:parseConfig] Final config:",Z),Z}function H0(){let V=document.querySelectorAll('script[src*="terminal.js"]');if(V.length>0){let Z=V[0].src;return Z.substring(0,Z.lastIndexOf("/")+1)+"ghostty-vt.wasm"}return"/static/js/ghostty-vt.wasm"}function G0(){return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)||"ontouchstart"in window&&navigator.maxTouchPoints>0}class m{terminal;fitAddon;socket=null;element;wsUrl;reconnectAttempts=0;maxReconnectAttempts=5;reconnectDelay=1000;messageQueue=[];lastValidSize=null;mobileInput=null;mobileKeybar=null;ctrlActive=!1;shiftActive=!1;fontFamily;fontSize;constructor(V,Z,j,$,P,X){this.element=V,this.wsUrl=Z,this.terminal=j,this.fitAddon=$,this.fontFamily=P,this.fontSize=X}static async create(V,Z,j){console.log("[webterm:create] WebTerminal.create() called"),console.log("[webterm:create] Container:",V),console.log("[webterm:create] wsUrl:",Z),console.log("[webterm:create] Config received:",JSON.stringify(j,null,2));let $=H0();console.log("[webterm:create] WASM path:",$),console.log("[webterm:create] Loading Ghostty WASM...");let P=await v.load($);console.log("[webterm:create] Ghostty loaded:",P);let X=j.theme??F.tango;console.log("[webterm:create] Theme to use (config.theme ?? THEMES.xterm):",JSON.stringify(X,null,2));let R=j.fontFamily?.trim()||W0,L=j.fontSize??16,K={fontFamily:R,fontSize:L,scrollback:j.scrollback??1000,cursorBlink:!0,cursorStyle:"block",theme:X,ghostty:P};console.log("[webterm:create] Full ITerminalOptions:",JSON.stringify(K,null,2)),console.log("[webterm:create] Creating ghostty-web Terminal instance...");let Y=new w(K);console.log("[webterm:create] Terminal created:",Y),console.log("[webterm:create] Terminal.options:",Y.options),console.log("[webterm:create] Creating FitAddon...");let _=new h;console.log("[webterm:create] Loading FitAddon into terminal..."),Y.loadAddon(_),console.log("[webterm:create] Calling terminal.open(container)..."),Y.open(V),console.log("[webterm:create] terminal.open() completed");let z=Y;if(console.log("[webterm:create] Terminal internal keys:",Object.keys(z)),z.renderer){console.log("[webterm:create] Renderer exists:",z.renderer);let W=z.renderer;if(console.log("[webterm:create] Renderer keys:",Object.keys(W)),W.theme)console.log("[webterm:create] Renderer.theme:",W.theme);if(W.palette)console.log("[webterm:create] Renderer.palette:",W.palette)}let u=new m(V,Z,Y,_,R,L);return console.log("[webterm:create] WebTerminal instance created"),u.initialize(),console.log("[webterm:create] WebTerminal initialized"),u}initialize(){console.log("[webterm:init] initialize() called");let V=this.element.querySelector("canvas");if(console.log("[webterm:init] Canvas element:",V),V)console.log("[webterm:init] Canvas dimensions:",{width:V.width,height:V.height,clientWidth:V.clientWidth,clientHeight:V.clientHeight,style:V.style.cssText});if(console.log("[webterm:init] Container dimensions:",{clientWidth:this.element.clientWidth,clientHeight:this.element.clientHeight}),this.waitForFonts().then(()=>{console.log("[webterm:init] Fonts loaded, reapplying font family and fitting..."),this.terminal.options.fontFamily=this.fontFamily;let Z=this.terminal.renderer;if(Z)Z.setFontFamily(this.fontFamily),Z.remeasureFont(),console.log("[webterm:init] Font family updated via renderer");this.fit(),console.log("[webterm:init] fit() completed");let j=this.element.querySelector("canvas");if(j)console.log("[webterm:init] Canvas after fit:",{width:j.width,height:j.height,clientWidth:j.clientWidth,clientHeight:j.clientHeight})}),this.setupResizeObserver(),window.addEventListener("resize",()=>{this.fit()}),this.terminal.onData((Z)=>{this.send(["stdin",Z])}),this.terminal.onResize((Z)=>{if(this.isValidSize(Z.cols,Z.rows))this.lastValidSize={cols:Z.cols,rows:Z.rows},this.send(["resize",{width:Z.cols,height:Z.rows}])}),this.setupMobileKeyboard(),G0())this.setupMobileKeybar();this.connect()}setupMobileKeyboard(){let V=document.createElement("textarea");V.setAttribute("autocapitalize","off"),V.setAttribute("autocomplete","off"),V.setAttribute("autocorrect","off"),V.setAttribute("spellcheck","false"),V.setAttribute("inputmode","text"),V.setAttribute("enterkeyhint","send"),V.style.cssText=` + const term = new Terminal({ ghostty });`);return s}var W0='ui-monospace, "SFMono-Regular", "FiraCode Nerd Font", "FiraMono Nerd Font", "Fira Code", "Roboto Mono", Menlo, Monaco, Consolas, "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace',F={tango:{background:"#000000",foreground:"#d3d7cf",cursor:"#d3d7cf",cursorAccent:"#000000",selectionBackground:"#555753",black:"#2e3436",red:"#cc0000",green:"#4e9a06",yellow:"#c4a000",blue:"#3465a4",magenta:"#75507b",cyan:"#06989a",white:"#d3d7cf",brightBlack:"#555753",brightRed:"#ef2929",brightGreen:"#8ae234",brightYellow:"#fce94f",brightBlue:"#729fcf",brightMagenta:"#ad7fa8",brightCyan:"#34e2e2",brightWhite:"#eeeeec"},xterm:{background:"#000000",foreground:"#e5e5e5",cursor:"#e5e5e5",cursorAccent:"#000000",selectionBackground:"#d4d4d4",black:"#000000",red:"#cd0000",green:"#00cd00",yellow:"#cdcd00",blue:"#0000cd",magenta:"#cd00cd",cyan:"#00cdcd",white:"#e5e5e5",brightBlack:"#4d4d4d",brightRed:"#ff0000",brightGreen:"#00ff00",brightYellow:"#ffff00",brightBlue:"#0000ff",brightMagenta:"#ff00ff",brightCyan:"#00ffff",brightWhite:"#ffffff"},monokai:{background:"#2d2a2e",foreground:"#fcfcfa",cursor:"#fcfcfa",cursorAccent:"#2d2a2e",selectionBackground:"#5b595c",black:"#403e41",red:"#ff6188",green:"#a9dc76",yellow:"#ffd866",blue:"#fc9867",magenta:"#ab9df2",cyan:"#78dce8",white:"#fcfcfa",brightBlack:"#727072",brightRed:"#ff6188",brightGreen:"#a9dc76",brightYellow:"#ffd866",brightBlue:"#fc9867",brightMagenta:"#ab9df2",brightCyan:"#78dce8",brightWhite:"#fcfcfa"},ristretto:{background:"#2d2525",foreground:"#fff1f3",cursor:"#fff1f3",cursorAccent:"#2d2525",selectionBackground:"#403838",black:"#2c2525",red:"#fd6883",green:"#adda78",yellow:"#f9cc6c",blue:"#f38d70",magenta:"#a8a9eb",cyan:"#85dacc",white:"#f9f8f5",brightBlack:"#655761",brightRed:"#fd6883",brightGreen:"#adda78",brightYellow:"#f9cc6c",brightBlue:"#f38d70",brightMagenta:"#a8a9eb",brightCyan:"#85dacc",brightWhite:"#f9f8f5"},dark:{background:"#1e1e1e",foreground:"#d4d4d4",cursor:"#aeafad",cursorAccent:"#1e1e1e",selectionBackground:"#264f78",black:"#000000",red:"#cd3131",green:"#0dbc79",yellow:"#e5e510",blue:"#2472c8",magenta:"#bc3fbc",cyan:"#11a8cd",white:"#e5e5e5",brightBlack:"#666666",brightRed:"#f14c4c",brightGreen:"#23d18b",brightYellow:"#f5f543",brightBlue:"#3b8eea",brightMagenta:"#d670d6",brightCyan:"#29b8db",brightWhite:"#ffffff"},light:{background:"#ffffff",foreground:"#383a42",cursor:"#526eff",cursorAccent:"#ffffff",selectionBackground:"#add6ff",black:"#000000",red:"#e45649",green:"#50a14f",yellow:"#c18401",blue:"#4078f2",magenta:"#a626a4",cyan:"#0184bc",white:"#a0a1a7",brightBlack:"#5c6370",brightRed:"#e06c75",brightGreen:"#98c379",brightYellow:"#d19a66",brightBlue:"#61afef",brightMagenta:"#c678dd",brightCyan:"#56b6c2",brightWhite:"#ffffff"},dracula:{background:"#282a36",foreground:"#f8f8f2",cursor:"#f8f8f2",cursorAccent:"#282a36",selectionBackground:"#44475a",black:"#21222c",red:"#ff5555",green:"#50fa7b",yellow:"#f1fa8c",blue:"#bd93f9",magenta:"#ff79c6",cyan:"#8be9fd",white:"#f8f8f2",brightBlack:"#6272a4",brightRed:"#ff6e6e",brightGreen:"#69ff94",brightYellow:"#ffffa5",brightBlue:"#d6acff",brightMagenta:"#ff92df",brightCyan:"#a4ffff",brightWhite:"#ffffff"},catppuccin:{background:"#1e1e2e",foreground:"#cdd6f4",cursor:"#f5e0dc",cursorAccent:"#1e1e2e",selectionBackground:"#45475a",black:"#45475a",red:"#f38ba8",green:"#a6e3a1",yellow:"#f9e2af",blue:"#89b4fa",magenta:"#f5c2e7",cyan:"#94e2d5",white:"#bac2de",brightBlack:"#585b70",brightRed:"#f38ba8",brightGreen:"#a6e3a1",brightYellow:"#f9e2af",brightBlue:"#89b4fa",brightMagenta:"#f5c2e7",brightCyan:"#94e2d5",brightWhite:"#a6adc8"},nord:{background:"#2e3440",foreground:"#d8dee9",cursor:"#d8dee9",cursorAccent:"#2e3440",selectionBackground:"#434c5e",black:"#3b4252",red:"#bf616a",green:"#a3be8c",yellow:"#ebcb8b",blue:"#81a1c1",magenta:"#b48ead",cyan:"#88c0d0",white:"#e5e9f0",brightBlack:"#4c566a",brightRed:"#bf616a",brightGreen:"#a3be8c",brightYellow:"#ebcb8b",brightBlue:"#81a1c1",brightMagenta:"#b48ead",brightCyan:"#8fbcbb",brightWhite:"#eceff4"},gruvbox:{background:"#282828",foreground:"#ebdbb2",cursor:"#ebdbb2",cursorAccent:"#282828",selectionBackground:"#504945",black:"#282828",red:"#cc241d",green:"#98971a",yellow:"#d79921",blue:"#458588",magenta:"#b16286",cyan:"#689d6a",white:"#a89984",brightBlack:"#928374",brightRed:"#fb4934",brightGreen:"#b8bb26",brightYellow:"#fabd2f",brightBlue:"#83a598",brightMagenta:"#d3869b",brightCyan:"#8ec07c",brightWhite:"#ebdbb2"},solarized:{background:"#002b36",foreground:"#839496",cursor:"#839496",cursorAccent:"#002b36",selectionBackground:"#073642",black:"#073642",red:"#dc322f",green:"#859900",yellow:"#b58900",blue:"#268bd2",magenta:"#d33682",cyan:"#2aa198",white:"#eee8d5",brightBlack:"#586e75",brightRed:"#cb4b16",brightGreen:"#586e75",brightYellow:"#657b83",brightBlue:"#839496",brightMagenta:"#6c71c4",brightCyan:"#93a1a1",brightWhite:"#fdf6e3"},tokyo:{background:"#1a1b26",foreground:"#a9b1d6",cursor:"#c0caf5",cursorAccent:"#1a1b26",selectionBackground:"#33467c",black:"#15161e",red:"#f7768e",green:"#9ece6a",yellow:"#e0af68",blue:"#7aa2f7",magenta:"#bb9af7",cyan:"#7dcfff",white:"#a9b1d6",brightBlack:"#414868",brightRed:"#f7768e",brightGreen:"#9ece6a",brightYellow:"#e0af68",brightBlue:"#7aa2f7",brightMagenta:"#bb9af7",brightCyan:"#7dcfff",brightWhite:"#c0caf5"}};function z0(V){console.log("[webterm:parseConfig] Parsing config from element");let Z={};if(V.dataset.fontFamily)Z.fontFamily=V.dataset.fontFamily,console.log(`[webterm:parseConfig] fontFamily: "${Z.fontFamily}"`);if(V.dataset.fontSize)Z.fontSize=parseInt(V.dataset.fontSize,10),console.log(`[webterm:parseConfig] fontSize: ${Z.fontSize}`);if(V.dataset.scrollback)Z.scrollback=parseInt(V.dataset.scrollback,10),console.log(`[webterm:parseConfig] scrollback: ${Z.scrollback}`);if(V.dataset.theme){let j=V.dataset.theme.toLowerCase();if(console.log(`[webterm:parseConfig] theme attribute: "${V.dataset.theme}" -> normalized: "${j}"`),console.log(`[webterm:parseConfig] Available themes: ${Object.keys(F).join(", ")}`),console.log(`[webterm:parseConfig] Theme "${j}" in THEMES? ${j in F}`),j in F)Z.theme=F[j],console.log(`[webterm:parseConfig] Using built-in theme "${j}":`,JSON.stringify(Z.theme,null,2));else{console.log("[webterm:parseConfig] Theme not found in THEMES, trying JSON parse...");try{Z.theme=JSON.parse(V.dataset.theme),console.log("[webterm:parseConfig] Parsed custom JSON theme:",Z.theme)}catch($){console.warn(`[webterm:parseConfig] Unknown theme "${V.dataset.theme}", JSON parse failed:`,$)}}}else console.log("[webterm:parseConfig] No theme attribute found on element");return console.log("[webterm:parseConfig] Final config:",Z),Z}function H0(){let V=document.querySelectorAll('script[src*="terminal.js"]');if(V.length>0){let Z=V[0].src;return Z.substring(0,Z.lastIndexOf("/")+1)+"ghostty-vt.wasm"}return"/static/js/ghostty-vt.wasm"}function G0(){return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)||"ontouchstart"in window&&navigator.maxTouchPoints>0}class m{terminal;fitAddon;socket=null;element;wsUrl;reconnectAttempts=0;maxReconnectAttempts=5;reconnectDelay=1000;messageQueue=[];lastValidSize=null;mobileInput=null;mobileKeybar=null;ctrlActive=!1;shiftActive=!1;fontFamily;fontSize;constructor(V,Z,j,$,P,X){this.element=V,this.wsUrl=Z,this.terminal=j,this.fitAddon=$,this.fontFamily=P,this.fontSize=X}static async create(V,Z,j){console.log("[webterm:create] WebTerminal.create() called"),console.log("[webterm:create] Container:",V),console.log("[webterm:create] wsUrl:",Z),console.log("[webterm:create] Config received:",JSON.stringify(j,null,2));let $=H0();console.log("[webterm:create] WASM path:",$),console.log("[webterm:create] Loading Ghostty WASM...");let P=await v.load($);console.log("[webterm:create] Ghostty loaded:",P);let X=j.theme??F.tango;console.log("[webterm:create] Theme to use (config.theme ?? THEMES.xterm):",JSON.stringify(X,null,2));let R=j.fontFamily?.trim()||W0,L=j.fontSize??16,K={fontFamily:R,fontSize:L,scrollback:j.scrollback??1000,cursorBlink:!0,cursorStyle:"block",theme:X,ghostty:P};console.log("[webterm:create] Full ITerminalOptions:",JSON.stringify(K,null,2)),console.log("[webterm:create] Creating ghostty-web Terminal instance...");let Y=new w(K);console.log("[webterm:create] Terminal created:",Y),console.log("[webterm:create] Terminal.options:",Y.options),console.log("[webterm:create] Creating FitAddon...");let _=new h;console.log("[webterm:create] Loading FitAddon into terminal..."),Y.loadAddon(_),console.log("[webterm:create] Calling terminal.open(container)..."),Y.open(V),console.log("[webterm:create] terminal.open() completed");let z=Y;if(console.log("[webterm:create] Terminal internal keys:",Object.keys(z)),z.renderer){console.log("[webterm:create] Renderer exists:",z.renderer);let W=z.renderer;if(console.log("[webterm:create] Renderer keys:",Object.keys(W)),W.theme)console.log("[webterm:create] Renderer.theme:",W.theme);if(W.palette)console.log("[webterm:create] Renderer.palette:",W.palette)}let u=new m(V,Z,Y,_,R,L);return console.log("[webterm:create] WebTerminal instance created"),u.initialize(),console.log("[webterm:create] WebTerminal initialized"),u}initialize(){console.log("[webterm:init] initialize() called");let V=this.element.querySelector("canvas");if(console.log("[webterm:init] Canvas element:",V),V)console.log("[webterm:init] Canvas dimensions:",{width:V.width,height:V.height,clientWidth:V.clientWidth,clientHeight:V.clientHeight,style:V.style.cssText});if(console.log("[webterm:init] Container dimensions:",{clientWidth:this.element.clientWidth,clientHeight:this.element.clientHeight}),this.waitForFonts().then(()=>{if(console.log("[webterm:init] Fonts loaded, triggering font reload..."),typeof this.terminal.loadFonts==="function")this.terminal.loadFonts(),console.log("[webterm:init] terminal.loadFonts() called");this.fit(),console.log("[webterm:init] fit() completed");let Z=this.element.querySelector("canvas");if(Z)console.log("[webterm:init] Canvas after fit:",{width:Z.width,height:Z.height,clientWidth:Z.clientWidth,clientHeight:Z.clientHeight})}),this.setupResizeObserver(),window.addEventListener("resize",()=>{this.fit()}),this.terminal.onData((Z)=>{this.send(["stdin",Z])}),this.terminal.onResize((Z)=>{if(this.isValidSize(Z.cols,Z.rows))this.lastValidSize={cols:Z.cols,rows:Z.rows},this.send(["resize",{width:Z.cols,height:Z.rows}])}),this.setupMobileKeyboard(),G0())this.setupMobileKeybar();this.connect()}setupMobileKeyboard(){let V=document.createElement("textarea");V.setAttribute("autocapitalize","off"),V.setAttribute("autocomplete","off"),V.setAttribute("autocorrect","off"),V.setAttribute("spellcheck","false"),V.setAttribute("inputmode","text"),V.setAttribute("enterkeyhint","send"),V.style.cssText=` position: absolute; left: 0; top: 0; diff --git a/src/webterm/static/js/terminal.ts b/src/webterm/static/js/terminal.ts index 8282ac3..36c581c 100644 --- a/src/webterm/static/js/terminal.ts +++ b/src/webterm/static/js/terminal.ts @@ -513,19 +513,31 @@ class WebTerminal { }); // Wait for fonts to load before fitting to ensure correct measurements + // + // FONT INITIALIZATION (ghostty-web): + // ----------------------------------- + // The font stack is set in two places: + // 1. At Terminal construction time via ITerminalOptions.fontFamily + // - This sets the initial font for the renderer + // 2. After web fonts load via terminal.loadFonts() + // - This re-measures font metrics and triggers a full re-render + // + // The loadFonts() method (added in ghostty-web commit feab41f9a8e4491f): + // - Calls renderer.remeasureFont() to recalculate cell dimensions + // - Calls handleFontChange() to resize canvas and re-render + // + // DO NOT manually set terminal.options.fontFamily or call renderer methods + // directly - use the public loadFonts() API which handles the full chain. + // + // See: https://github.com/rcarmo/ghostty-web/commit/feab41f9a8e4491f04688a6620974c3f7762a3d9 this.waitForFonts().then(() => { - console.log("[webterm:init] Fonts loaded, reapplying font family and fitting..."); - // IMPORTANT: Font updates require BOTH steps to work correctly: - // 1. Set terminal.options.fontFamily - stores the font stack for future reference - // 2. Call renderer.setFontFamily() + remeasureFont() - applies the font and recalculates metrics - // Without step 1, the font stack is lost and defaults are used on re-render. - // Without step 2, the renderer doesn't know about the new fonts. - this.terminal.options.fontFamily = this.fontFamily; - const renderer = (this.terminal as unknown as { renderer?: { setFontFamily: (family: string) => void; remeasureFont: () => void } }).renderer; - if (renderer) { - renderer.setFontFamily(this.fontFamily); - renderer.remeasureFont(); - console.log("[webterm:init] Font family updated via renderer"); + console.log("[webterm:init] Fonts loaded, triggering font reload..."); + // Use the public loadFonts() API which properly handles font re-measurement + // and triggers handleFontChange() internally. This is the correct approach + // per ghostty-web commit feab41f9a8e4491f04688a6620974c3f7762a3d9 + if (typeof (this.terminal as unknown as { loadFonts?: () => void }).loadFonts === "function") { + (this.terminal as unknown as { loadFonts: () => void }).loadFonts(); + console.log("[webterm:init] terminal.loadFonts() called"); } this.fit(); console.log("[webterm:init] fit() completed");