| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "image" |
| 5 | "image/color" |
| 6 | "image/draw" |
| 7 | "image/png" |
| 8 | "log" |
| 9 | "os" |
| 10 | |
| 11 | "github.com/ddevault/go-libvterm" |
| 12 | "golang.org/x/image/font" |
| 13 | "golang.org/x/image/font/basicfont" |
| 14 | "golang.org/x/image/math/fixed" |
| 15 | ) |
| 16 | |
| 17 | func drawChar(img *image.RGBA, x, y int, c color.Color, text string) { |
| 18 | point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)} |
| 19 | d := &font.Drawer{ |
| 20 | Dst: img, |
| 21 | Src: image.NewUniform(c), |
| 22 | Face: basicfont.Face7x13, |
| 23 | Dot: point, |
| 24 | } |
| 25 | d.DrawString(text) |
| 26 | } |
| 27 | |
| 28 | func main() { |
| 29 | vt := vterm.New(25, 80) |
| 30 | defer vt.Close() |
| 31 | |
| 32 | vt.SetUTF8(true) |
| 33 | |
| 34 | screen := vt.ObtainScreen() |
| 35 | screen.Reset(true) |
| 36 | state := vt.ObtainState() |
| 37 | |
| 38 | _, err := vt.Write([]byte("\033[31mHello \033[32mGolang\033[0m")) |
| 39 | if err != nil { |
| 40 | log.Fatal(err) |
| 41 | } |
| 42 | screen.Flush() |
| 43 | |
| 44 | rows, cols := vt.Size() |
| 45 | img := image.NewRGBA(image.Rect(0, 0, cols*7, rows*13)) |
| 46 | draw.Draw(img, img.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src) |
| 47 | |
| 48 | for row := 0; row < rows; row++ { |
| 49 | for col := 0; col < cols; col++ { |
| 50 | cell, err := screen.GetCellAt(row, col) |
| 51 | if err != nil { |
| 52 | log.Fatal(err) |
| 53 | } |
| 54 | chars := cell.Chars() |
| 55 | if len(chars) > 0 && chars[0] != 0 { |
| 56 | drawChar(img, (col+1)*7, (row+1)*13, state.ConvertVTermColorToRGB(cell.Fg()), string(chars)) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | f, err := os.Create("output.png") |
| 61 | if err != nil { |
| 62 | log.Fatal(err) |
| 63 | } |
| 64 | defer f.Close() |
| 65 | err = png.Encode(f, img) |
| 66 | if err != nil { |
| 67 | log.Fatal(err) |
| 68 | } |
| 69 | } |