-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathsixel.go
69 lines (59 loc) · 1.49 KB
/
sixel.go
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 (
"log"
"os"
"strconv"
"github.com/gdamore/tcell/v2"
)
const (
gSixelBegin = "\033P"
)
type sixelScreen struct {
lastFile string
lastSxW int
lastSxH int
lastWinW int
lastWinH int
}
func (sxs *sixelScreen) unlockSixel(win *win, screen tcell.Screen, filePath string) {
if filePath != "" && (filePath != sxs.lastFile || win.w != sxs.lastWinW || win.h != sxs.lastWinH) {
screen.LockRegion(win.x, win.y, sxs.lastSxW, sxs.lastSxH, false)
}
}
func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
if reg.path == sxs.lastFile && win.w == sxs.lastWinW && win.h == sxs.lastWinH {
return
}
if reg.sixel == nil {
sxs.lastFile = ""
return
}
tty, ok := screen.Tty()
if !ok {
log.Printf("returning underlying tty failed during sixel render")
return
}
ti, err := tcell.LookupTerminfo(os.Getenv("TERM"))
if err != nil {
log.Printf("terminal lookup failed during sixel render %s", err)
return
}
v, _ := tty.WindowSize()
w, h := v.CellDimensions()
matches := reSixelSize.FindStringSubmatch(*reg.sixel)
if matches == nil {
log.Printf("sixel dimensions cannot be looked up")
return
}
iw, _ := strconv.Atoi(matches[1])
ih, _ := strconv.Atoi(matches[2])
// width and height are -1 to avoid showing half filled sixels
screen.LockRegion(win.x, win.y, iw/w-1, ih/h-1, true)
ti.TPuts(tty, ti.TGoto(win.x, win.y))
ti.TPuts(tty, *reg.sixel)
sxs.lastFile = reg.path
sxs.lastSxW = iw
sxs.lastSxH = ih
sxs.lastWinW = win.w
sxs.lastWinH = win.h
}