| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package main |
| 20 |
|
| 21 |
import ( |
| 22 |
"bytes" |
| 23 |
"fmt" |
| 24 |
"image" |
| 25 |
"image/png" |
| 26 |
"io" |
| 27 |
"log" |
| 28 |
"math" |
| 29 |
"os" |
| 30 |
|
| 31 |
"github.com/gdamore/tcell/v3" |
| 32 |
"github.com/gdamore/tcell/v3/color" |
| 33 |
"github.com/gdamore/tcell/v3/encoding" |
| 34 |
|
| 35 |
"github.com/mattn/go-sixel" |
| 36 |
) |
| 37 |
|
| 38 |
type imageData struct { |
| 39 |
width int |
| 40 |
height int |
| 41 |
data *bytes.Buffer |
| 42 |
} |
| 43 |
|
| 44 |
func displayHelloWorld(s tcell.Screen) { |
| 45 |
w, h := s.Size() |
| 46 |
s.Clear() |
| 47 |
style := tcell.StyleDefault.Foreground(color.CadetBlue.TrueColor()).Background(color.White) |
| 48 |
s.PutStrStyled(w/2-7, h/2, "Hello, World!", style) |
| 49 |
s.PutStr(w/2-9, h/2+1, "Press ESC to exit.") |
| 50 |
s.PutStr(w/2-18, h/2+2, "Press Enter to toggle sixel lock.") |
| 51 |
s.Show() |
| 52 |
} |
| 53 |
|
| 54 |
func displaySixel(s tcell.Screen, img *imageData, lock bool) { |
| 55 |
tty, ok := s.Tty() |
| 56 |
if !ok { |
| 57 |
s.Fini() |
| 58 |
log.Fatal("not a terminal") |
| 59 |
} |
| 60 |
ws, err := tty.WindowSize() |
| 61 |
if err != nil { |
| 62 |
s.Fini() |
| 63 |
log.Fatal(err) |
| 64 |
} |
| 65 |
|
| 66 |
cw, ch := ws.CellDimensions() |
| 67 |
if cw == 0 || ch == 0 { |
| 68 |
s.Fini() |
| 69 |
log.Fatal("terminal does not support sixel graphics") |
| 70 |
return |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
sixelWidth := int(math.Ceil(float64(img.width) / float64(cw))) |
| 76 |
sixelHeight := int(math.Ceil(float64(img.height) / float64(ch))) |
| 77 |
|
| 78 |
sixelX := ws.Width/2 - (sixelWidth / 2) |
| 79 |
sixelY := ws.Height/2 - sixelHeight - 2 |
| 80 |
if sixelY < 0 { |
| 81 |
sixelY = 0 |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
s.LockRegion(sixelX, sixelY, sixelWidth, sixelHeight, lock) |
| 86 |
|
| 87 |
s.PutStr(sixelX, sixelY, "This text is behind") |
| 88 |
s.PutStr(sixelX, sixelY+1, " the sixel") |
| 89 |
|
| 90 |
setCursorPosition = "\x1b[%[1]d;%[2]dH" |
| 91 |
|
| 92 |
|
| 93 |
io.WriteString(tty, fmt.Sprintf(setCursorPosition, sixelY+1, sixelX+1)) |
| 94 |
|
| 95 |
io.WriteString(tty, img.data.String()) |
| 96 |
|
| 97 |
s.Show() |
| 98 |
} |
| 99 |
|
| 100 |
func loadImage(path string) (image.Image, error) { |
| 101 |
f, err := os.Open(path) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
return png.Decode(f) |
| 106 |
} |
| 107 |
|
| 108 |
func main() { |
| 109 |
encoding.Register() |
| 110 |
|
| 111 |
s, e := tcell.NewScreen() |
| 112 |
if e != nil { |
| 113 |
fmt.Fprintf(os.Stderr, "%v\n", e) |
| 114 |
os.Exit(1) |
| 115 |
} |
| 116 |
if e := s.Init(); e != nil { |
| 117 |
fmt.Fprintf(os.Stderr, "%v\n", e) |
| 118 |
os.Exit(1) |
| 119 |
} |
| 120 |
|
| 121 |
defStyle := tcell.StyleDefault. |
| 122 |
Background(color.Black). |
| 123 |
Foreground(color.White) |
| 124 |
s.SetStyle(defStyle) |
| 125 |
|
| 126 |
raw, err := loadImage("./logos/tcell.png") |
| 127 |
if err != nil { |
| 128 |
s.Fini() |
| 129 |
log.Println("couldn't load image. try running from the root directory") |
| 130 |
log.Fatalf(" go run ./_demos/sixel.go") |
| 131 |
} |
| 132 |
|
| 133 |
img := &imageData{ |
| 134 |
width: raw.Bounds().Dx(), |
| 135 |
height: raw.Bounds().Dy(), |
| 136 |
data: bytes.NewBuffer(nil), |
| 137 |
} |
| 138 |
enc := sixel.NewEncoder(img.data) |
| 139 |
if err := enc.Encode(raw); err != nil { |
| 140 |
s.Fini() |
| 141 |
log.Fatal(err) |
| 142 |
} |
| 143 |
|
| 144 |
lock := true |
| 145 |
displayHelloWorld(s) |
| 146 |
displaySixel(s, img, lock) |
| 147 |
|
| 148 |
for { |
| 149 |
ev := <-s.EventQ() |
| 150 |
switch ev := ev.(type) { |
| 151 |
case *tcell.EventResize: |
| 152 |
s.Sync() |
| 153 |
displayHelloWorld(s) |
| 154 |
displaySixel(s, img, lock) |
| 155 |
case *tcell.EventKey: |
| 156 |
if ev.Key() == tcell.KeyEscape { |
| 157 |
s.Fini() |
| 158 |
os.Exit(0) |
| 159 |
} |
| 160 |
if ev.Key() == tcell.KeyEnter { |
| 161 |
lock = !lock |
| 162 |
displaySixel(s, img, lock) |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |