shot

A Go library that renders a terminal screen to PNG, SVG, HTML, ANSI or text, with an optional window frame.

tuios.dev/shot renders a terminal screen to a picture. It takes a grid of cells and writes PNG, SVG, HTML, ANSI or plain text, with an optional window frame around it. tuios uses it for screenshots.

It needs Go 1.25 or later. It is pure Go and needs no cgo, no browser and no external tool. The API reference is on pkg.go.dev.

A terminal window drawn by shot on a gradient: a prompt, go test output, a coloured label, a box corner and braille
The PNG that the quickstart program below writes, as it wrote it.

Use it to

  • Save a picture of a screen when a terminal test fails.
  • Put real terminal output in a README or in docs.
  • Post the output of a command from a bot.

What it does

  • PNG is drawn in Go. Box drawing, block elements, braille and powerline glyphs are drawn as shapes, so borders join up in any font.
  • SVG uses real <text> and <rect> elements. Design tools and browsers open it, and the text stays selectable.
  • HTML is one file with inline styles. Links stay links.
  • ANSI and text write the screen back as escape sequences or as plain lines.
  • mosaic, a sub-package, does the opposite: it draws a picture as cells of block glyphs.

Quickstart

go get tuios.dev/shot tuios.dev/vt

shot does not parse escape sequences. A terminal emulator does that, such as vt, and FromScreen reads its screen. This code writes screen.png and screen.svg:

// A terminal emulator reads what a program printed.
term := vt.NewEmulator(46, 6)
defer term.Close()
term.Write([]byte("\x1b[35m>\x1b[0m \x1b[1mmain\x1b[0m \x1b[2mon\x1b[0m ~/dev/tuios\r\n" +
	"$ go test ./shot/...\r\n" +
	"\x1b[32mok\x1b[0m  tuios.dev/shot         0.012s\r\n" +
	"\x1b[32mok\x1b[0m  tuios.dev/shot/mosaic  0.004s\r\n" +
	"\x1b[44;30m 2 packages \x1b[0m \x1b[33m╭─╮\x1b[0m \x1b[2m⣿⣶⣤⣀\x1b[0m\r\n$ "))

// The palette turns the basic and indexed colours into RGB.
p := shot.XTermPalette()
p.FG, p.BG = shot.RGB(0xcd, 0xd6, 0xf4), shot.RGB(0x1e, 0x1e, 0x2e)
p.ANSI[2], p.ANSI[3] = shot.RGB(0xa6, 0xe3, 0xa1), shot.RGB(0xf9, 0xe2, 0xaf)
p.ANSI[4], p.ANSI[5] = shot.RGB(0x89, 0xb4, 0xfa), shot.RGB(0xcb, 0xa6, 0xf7)

// FromScreen reads the screen into a grid of resolved cells.
grid := shot.FromScreen(term, p)

// BuildFrame dresses the grid as a window on a gradient.
frame := shot.BuildFrame(shot.FrameSpec{
	Frame: "window", Title: "tuios", Padding: 24, Radius: 10, Shadow: true, Scale: 2,
}, shot.FrameInputs{Palette: p})

for _, format := range []shot.Format{shot.FormatPNG, shot.FormatSVG} {
	out, err := shot.Render(format, grid, frame)
	if err != nil {
		log.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(dir, "screen."+format.Ext()), out, 0o644); err != nil {
		log.Fatal(err)
	}
}

The picture at the top of this page is its screen.png. It uses no font of its own, so shot draws the text in Go Mono. This is its screen.svg, which your browser draws with its own monospace font:

The same terminal window as SVG: text and rectangles that the browser draws

A program with its own screen model fills a grid directly with NewGrid. Each cell holds its text, its colours as RGB and its attributes.

Fonts

PNG looks for each glyph in this order, and the first face that has the glyph draws it:

  1. Frame.Font, and Frame.BoldFont for a bold cell. Without a font, Go Mono.
  2. Each face in Frame.Fallback, in order.
  3. Go Mono, which shot embeds.

A glyph in no face draws as a dotted box. MissingGlyphs lists those glyphs before you render, so you can find a font for each one and add it to Frame.Fallback.

shot parses a fallback face only when a glyph needs it, and looks up each glyph once per render. A glyph wider than its cell is centred on the cell. tuios fills the fallback list from fontconfig.

SVG and HTML name Frame.FontFamily and let the viewer pick the fonts. With Frame.EmbedFont, they also carry Frame.Font as an @font-face.

Pictures as cells: mosaic

tuios.dev/shot/mosaic does the opposite of shot. It draws a picture as cells of block glyphs, each with a foreground and a background colour. tuios uses it for an image when the terminal shows no graphics.

The tuios mascot drawn as 32 by 16 cells of half blocks
mosaic.Half: 1 by 2 sub-pixels a cell
The tuios mascot drawn as 32 by 16 cells of quadrant blocks, with finer edges
mosaic.Quadrant: 2 by 2 sub-pixels a cell

Sextant and Octant use 2 by 3 and 2 by 4 sub-pixels, for a font that has those glyphs. At 256 colours the cells are dithered. At 16 colours they are always half blocks.

How do I

  • Save a picture when a terminal test fails? Keep the vt emulator of the test, then shot.Render(shot.FormatPNG, shot.FromScreen(term, palette), nil).
  • Use my own font? Set Frame.Font and Frame.BoldFont, and add faces for missing glyphs to Frame.Fallback.
  • Get a picture with no window? Use Frame: "none" in the FrameSpec, or a nil frame.
  • Put real output in a README? Write SVG. Its text stays selectable.
  • Fill a grid from my own screen model? Use NewGrid, and set each cell's text, colours and attributes.

API overview

NameDoes
Grid, Cell, NewGridA screen of resolved cells.
FromScreen, PaletteRead a terminal screen, with its colours made RGB.
Render, FormatWrite PNG, SVG, HTML, ANSI or text.
BuildFrame, FrameThe window, padding, gradient and shadow.
MissingGlyphs, FontPick the faces.
mosaicDraw a picture as cells.

Where it fits

  • tuios uses shot for tuios screenshot and the screenshot keys, and mosaic for images on a terminal with no graphics.
  • shot reads vt screens through FromScreen.
  • The pictures of loom, plot and vt output on these library pages are PNG files from shot.

Versions

The module is at v0. The API can change between minor versions. Grid, Cell, Palette, Render, the five formats and FromScreen are settled. The fields of Frame and FrameSpec, and the mosaic package, can still change.

The pictures can change between versions. Do not compare PNG bytes in a test. Compare the grid, or the text and ANSI output.

Its tags have the folder name as a prefix, such as shot/v0.1.0. The source is in the shot folder of the tuios repository, under the MIT licence. The embedded Go Mono fonts are by Bigelow & Holmes, under the Go font licence.

tuios lives only at tuios.dev and github.com/Gaurav-Gosain/tuios. tuios has no X or Twitter account. An account that offers a tuios token or presale is not this project.

On this page