File: main.go

package info (click to toggle)
golang-github-ddevault-go-libvterm 0.0~git20190526.b7d861d-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 752 kB
  • sloc: ansic: 5,945; perl: 194; makefile: 113
file content (69 lines) | stat: -rw-r--r-- 1,419 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main

import (
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"log"
	"os"

	"github.com/ddevault/go-libvterm"
	"golang.org/x/image/font"
	"golang.org/x/image/font/basicfont"
	"golang.org/x/image/math/fixed"
)

func drawChar(img *image.RGBA, x, y int, c color.Color, text string) {
	point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
	d := &font.Drawer{
		Dst:  img,
		Src:  image.NewUniform(c),
		Face: basicfont.Face7x13,
		Dot:  point,
	}
	d.DrawString(text)
}

func main() {
	vt := vterm.New(25, 80)
	defer vt.Close()

	vt.SetUTF8(true)

	screen := vt.ObtainScreen()
	screen.Reset(true)
	state := vt.ObtainState()

	_, err := vt.Write([]byte("\033[31mHello \033[32mGolang\033[0m"))
	if err != nil {
		log.Fatal(err)
	}
	screen.Flush()

	rows, cols := vt.Size()
	img := image.NewRGBA(image.Rect(0, 0, cols*7, rows*13))
	draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)

	for row := 0; row < rows; row++ {
		for col := 0; col < cols; col++ {
			cell, err := screen.GetCellAt(row, col)
			if err != nil {
				log.Fatal(err)
			}
			chars := cell.Chars()
			if len(chars) > 0 && chars[0] != 0 {
				drawChar(img, (col+1)*7, (row+1)*13, state.ConvertVTermColorToRGB(cell.Fg()), string(chars))
			}
		}
	}
	f, err := os.Create("output.png")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	err = png.Encode(f, img)
	if err != nil {
		log.Fatal(err)
	}
}