Skip to content
0magnetPublic

About

Go/wasm port of xterm.js — terminal emulator library with a pure-Go VT core

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

74 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xterm-go

A Go port of xterm.js 6.0.0 — a terminal emulator library, compiled to WebAssembly for the browser. The VT core is pure Go and also works headless on any platform.

Live demo — a wasm terminal with an in-browser echo shell.

xterm-go in the browser

xterm.js is the front-end terminal component used by VS Code, Hyper and Theia — and by skywire's web pty. This port reimplements it as a Go library so Go/wasm applications can embed a full terminal without JavaScript dependencies.

Features

  • Terminal apps just work: the full escape-sequence machinery of xterm.js is ported — cursor addressing, scroll regions, the alternate screen buffer, insert/delete, SGR text styling (16/256/truecolor, underline styles), charsets, device reports (DA/DSR/DECRQM/DECRQSS, XTVERSION, CSI 14/16/18 t, OSC 4/10/11/12 color queries, focus mode 1004) and mouse tracking (X10/VT200/DRAG/ANY with SGR encoding) for curses apps. CSI 14/16 t answer only when enabled in Options.WindowOptions. Terminal.OnReply receives these replies apart from what the person typed, so an embedder running a shell can keep them out of its line editor.
  • Headless-capable core: the vt subpackage (parser, buffers, input handler) is pure Go with zero dependencies — it builds natively, so terminal semantics are tested with plain go test, no browser needed. Use it standalone to interpret pty output server-side.
  • Rich Unicode support: CJK wide characters, combining characters, wcwidth tables ported from the UnicodeV6 provider, and opt-in grapheme clustering (mode 2027, UAX #29 on Unicode 17 tables) for ZWJ emoji, flags and conjuncts.
  • Scrollback: ring-buffer scrollback with reflow on resize, native scrollbar viewport in the browser layer.
  • Self-contained: no JS dependencies; styles are injected automatically.
  • GPU-accelerated: an optional WebGL2 renderer (the addon-webgl equivalent) draws the grid as instanced quads sampling a glyph texture atlas, with pixel-perfect procedural box drawing, block, shade and powerline glyphs. Enable with term.EnableWebGL(); it falls back to the DOM renderer when WebGL2 is unavailable.
  • IME support: composition events (CJK input methods, dead keys) are handled with an in-place composition view like xterm.js.
  • Pictures: inline images (AddImage, RemoveImage, ClearImages, CellSize), laid over the screen for an embedder to place (the protocol parsing is the embedder's, through SetApcHandler for APC sequences such as kitty graphics), and sixel (Options.Sixel, on by default; Terminal.OnSixel receives each picture, and DA1 then answers 62;4;9;22).
  • Kitty keyboard protocol: vt.KittyKey encodes a key event under the flags in effect (KittyFlags; ResetKittyKeyboard).
  • Shell integration: OSC 133 prompt marks. ScrollToPreviousPrompt and ScrollToNextPrompt move between prompts and LastCommandOutput returns the last command's text and exit status.
  • Pointer, fonts and touch: OSC 22 pointer shapes set the CSS cursor over the screen. SetFont, SetFontFamily and SetFontSize re-measure the cells and rebuild the renderer. Touch scrolls and taps.
  • Screen reader mode: Options.ScreenReaderMode builds an accessibility tree and a live region that announces output.
  • Small: ~1.1 MB wasm with TinyGo (the demo) (≈4 MB with standard Go).

What xterm-go is not

  • xterm-go is not a terminal application you can download and use — it is a library for embedding terminals into (wasm) applications.
  • xterm-go is not bash. Connect it to a real process over a WebSocket (see Attach) or feed it output yourself.

Getting Started

go get github.com/0magnet/xterm-go

Create a <div id="terminal"></div> in your page, then in your wasm program:

//go:build js && wasm

package main

import (
	"syscall/js"

	xterm "github.com/0magnet/xterm-go"
	"github.com/0magnet/xterm-go/vt"
)

func main() {
	term := xterm.New(nil) // nil = default options
	term.Open(js.Global().Get("document").Call("getElementById", "terminal"))
	term.Fit() // size to the parent element

	term.WriteString("Hello from \x1b[1;3;31mxterm-go\x1b[0m $ ")
	term.Core.OnData = func(data string) {
		term.WriteString(data) // echo
	}

	select {}
}

Build with standard Go or TinyGo:

GOOS=js GOARCH=wasm go build -o main.wasm .
# or ~6x smaller:
tinygo build -target wasm -no-debug -o main.wasm .

Options

xterm.New takes *vt.Options (pass nil for defaults): dimensions, scrollback length, fonts, cursor style/blink, a Theme with the standard 16 colors (set Theme.Generate256 to derive colors 16–255 from them in CIELAB), Sixel, ScreenReaderMode, WindowOptions and more — mirroring the xterm.js options relevant to the port.

opts := vt.NewOptions()
opts.Scrollback = 5000
opts.FontSize = 14
opts.Theme.Background = "#1e1e2e"
term := xterm.New(opts)

Connecting to a pty (attach)

The equivalent of @xterm/addon-attach is built in — hand it a WebSocket carrying pty data:

ws := js.Global().Get("WebSocket").New("wss://example.com/pty")
term.Attach(ws, true) // bidirectional: keystrokes go back over the socket

Fitting (fit addon)

term.Fit() resizes the terminal to fill its parent element; call it from a window resize listener.

Headless use

The vt package runs anywhere Go runs:

term := vt.NewTerminal(nil)
term.WriteString("ls\r\n\x1b[1;34mdocs\x1b[0m  main.go\r\n")
line := term.Buffer().Lines.Get(1).TranslateToString(true, 0, -1)
// "docs  main.go"

Real-world uses

  • Building the web terminal for skywire's wasm hypervisor UI, replacing the bundled xterm.js + fit/attach/webgl addons.

WebGL renderer

if err := term.EnableWebGL(); err != nil {
	// WebGL2 unavailable — the DOM renderer stays active
}
// term.DisableWebGL() switches back

Differences from xterm.js

  • Parser handlers are synchronous (Go has no reason for the async parse-stack machinery).
  • The WebGL renderer uses a single 2048² atlas page (cleared and lazily rebuilt on overflow) instead of the multi-page grow/merge machinery, and does not port the selection/decoration/ligature-joiner model overrides or the minimum-contrast-ratio option.
  • The deprecated canvas renderer is not ported (DOM and WebGL are).
  • Link decorations and the link provider are not ported: vt registers OSC 8 hyperlinks (OscLinkService) but the browser layer does not underline or open them. Selection is the port's own cell selection (selection.go, drawn under WebGL too) rather than the selection service.
  • Windows conpty wrapping heuristics are not ported.

Contributing

Issues and PRs welcome at github.com/0magnet/xterm-go.

License

Copyright (c) 2017-2022, The xterm.js authors (MIT License)
Copyright (c) 2014-2016, SourceLair Private Company (MIT License)
Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
Go port copyright (c) 2026 (MIT License)

Related projects

Other browser terminal emulators and terminal emulation libraries:

  • ghostty-web — Ghostty's terminal emulation compiled to WebAssembly behind an xterm.js-compatible API (MIT)
  • wterm — a DOM-rendered web terminal with a Zig/WebAssembly or Ghostty core
  • go-te — a VT100–VT520 terminal emulation library in Go, with pyte and esctest conformance (MIT)

Dependency Graph

Made with goda:

# GOOS=js: the import edges of a wasm program live in js/wasm-tagged
# files and are invisible to a host-context run
GOOS=js GOARCH=wasm go run github.com/loov/goda@latest graph github.com/0magnet/xterm-go/... | dot -Tsvg -o docs/xterm-go-goda-graph.svg

Dependency Graph

Lines of Code

Made with gocloc (excludes vendor/, node_modules/, .git/):

gocloc --not-match-d='(vendor|node_modules|\.git)' .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                              68           1965           2880          18712
Plain Text                       2              1              0            799
JavaScript                       1             51             44            452
Markdown                         1             52              0            131
Makefile                         1             21             52            111
YAML                             1              0              7             98
HTML                             1              0              4             45
Bourne Shell                     2              9             21             37
JSON                             1              0              0              8
XML                              1              0              0              4
-------------------------------------------------------------------------------
TOTAL                           79           2099           3008          20397
-------------------------------------------------------------------------------

About

Go/wasm port of xterm.js — terminal emulator library with a pure-Go VT core

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages