| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"os" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/gdamore/tcell/v3" |
| 11 |
) |
| 12 |
|
| 13 |
type sixelScreen struct { |
| 14 |
lastFile string |
| 15 |
lastWin win |
| 16 |
forceClear bool |
| 17 |
} |
| 18 |
|
| 19 |
func (sxs *sixelScreen) clearSixel(win *win, screen tcell.Screen, filePath string) { |
| 20 |
if sxs.lastFile != "" && (filePath != sxs.lastFile || *win != sxs.lastWin || sxs.forceClear) { |
| 21 |
screen.LockRegion(sxs.lastWin.x, sxs.lastWin.y, sxs.lastWin.w, sxs.lastWin.h, false) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) { |
| 26 |
if reg.path == sxs.lastFile && *win == sxs.lastWin && !sxs.forceClear { |
| 27 |
return |
| 28 |
} |
| 29 |
|
| 30 |
cw, ch, err := cellSize(screen) |
| 31 |
if err != nil { |
| 32 |
log.Printf("sixel: %s", err) |
| 33 |
return |
| 34 |
} |
| 35 |
|
| 36 |
y := win.y |
| 37 |
var b strings.Builder |
| 38 |
|
| 39 |
for _, line := range reg.lines { |
| 40 |
if !strings.HasPrefix(line, "\033P") { |
| 41 |
if y >= win.y+win.h { |
| 42 |
break |
| 43 |
} |
| 44 |
|
| 45 |
line = sanitizePreview(line) |
| 46 |
screen.LockRegion(win.x, y, printLength(line), 1, true) |
| 47 |
fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1) |
| 48 |
b.WriteString(line) |
| 49 |
y++ |
| 50 |
continue |
| 51 |
} |
| 52 |
|
| 53 |
matches := reSixelSize.FindStringSubmatch(line) |
| 54 |
if matches == nil { |
| 55 |
log.Print("sixel: failed to get image size") |
| 56 |
continue |
| 57 |
} |
| 58 |
|
| 59 |
iw, _ := strconv.Atoi(matches[1]) |
| 60 |
ih, _ := strconv.Atoi(matches[2]) |
| 61 |
|
| 62 |
sw := (iw + cw - 1) / cw |
| 63 |
sh := (ih + ch - 1) / ch |
| 64 |
|
| 65 |
if y+sh-1 >= win.y+win.h { |
| 66 |
break |
| 67 |
} |
| 68 |
|
| 69 |
screen.LockRegion(win.x, y, sw, sh, true) |
| 70 |
fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1) |
| 71 |
b.WriteString(line) |
| 72 |
y += sh |
| 73 |
} |
| 74 |
|
| 75 |
fmt.Fprint(os.Stderr, "\033[?2026h") // Begin synchronized update |
| 76 |
fmt.Fprint(os.Stderr, "\0337") // Save cursor position |
| 77 |
fmt.Fprint(os.Stderr, b.String()) // Write data |
| 78 |
fmt.Fprint(os.Stderr, "\0338") // Restore cursor position |
| 79 |
fmt.Fprint(os.Stderr, "\033[?2026l") // End synchronized update |
| 80 |
|
| 81 |
sxs.lastFile = reg.path |
| 82 |
sxs.lastWin = *win |
| 83 |
sxs.forceClear = false |
| 84 |
} |
| 85 |
|
| 86 |
func cellSize(screen tcell.Screen) (int, int, error) { |
| 87 |
tty, ok := screen.Tty() |
| 88 |
if !ok { |
| 89 |
return -1, -1, fmt.Errorf("failed to get tty") |
| 90 |
} |
| 91 |
|
| 92 |
ws, err := tty.WindowSize() |
| 93 |
if err != nil { |
| 94 |
return -1, -1, fmt.Errorf("failed to get window size: %w", err) |
| 95 |
} |
| 96 |
|
| 97 |
cw, ch := ws.CellDimensions() |
| 98 |
if cw <= 0 || ch <= 0 { |
| 99 |
// fallback for Windows Terminal |
| 100 |
return 10, 20, nil |
| 101 |
} |
| 102 |
|
| 103 |
return cw, ch, nil |
| 104 |
} |