Implement builtin support for sixel images, allow combining with text (#2109)
* Add function for reading lines in file * Implement sixel drawing * Remove sixel option * Fix linting error
This commit is contained in:
1 parent
aba73884e3
commit
13da770af6
11 files changed
+190
-99
No files matched your search
@@ -6,12 +6,11 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
const gSixelBegin = "\033P"
|
||||
|
||||
type sixelScreen struct {
|
||||
lastFile string
|
||||
lastWin win
|
||||
@@ -29,7 +28,7 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
|
||||
return
|
||||
}
|
||||
|
||||
if reg.sixel == nil {
|
||||
if !reg.sixel {
|
||||
sxs.lastFile = ""
|
||||
return
|
||||
}
|
||||
@@ -40,26 +39,56 @@ func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
|
||||
return
|
||||
}
|
||||
|
||||
matches := reSixelSize.FindStringSubmatch(*reg.sixel)
|
||||
if matches == nil {
|
||||
log.Printf("sixel: failed to get image size")
|
||||
return
|
||||
}
|
||||
iw, _ := strconv.Atoi(matches[1])
|
||||
ih, _ := strconv.Atoi(matches[2])
|
||||
y := win.y
|
||||
var b strings.Builder
|
||||
|
||||
if os.Getenv("TMUX") != "" {
|
||||
// tmux rounds the image height up to a multiple of 6, so we
|
||||
// need to do the same to avoid overwriting the image, as tmux
|
||||
// would remove the image if we touched it.
|
||||
ih = (ih + 5) / 6 * 6
|
||||
for _, line := range reg.lines {
|
||||
if !strings.HasPrefix(line, "\033P") {
|
||||
if y >= win.y+win.h {
|
||||
break
|
||||
}
|
||||
|
||||
screen.LockRegion(win.x, y, printLength(line), 1, true)
|
||||
fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1)
|
||||
b.WriteString(line)
|
||||
y += 1
|
||||
continue
|
||||
}
|
||||
|
||||
matches := reSixelSize.FindStringSubmatch(line)
|
||||
if matches == nil {
|
||||
log.Printf("sixel: failed to get image size")
|
||||
continue
|
||||
}
|
||||
|
||||
iw, _ := strconv.Atoi(matches[1])
|
||||
ih, _ := strconv.Atoi(matches[2])
|
||||
|
||||
if os.Getenv("TMUX") != "" {
|
||||
// tmux rounds the image height up to a multiple of 6, so we
|
||||
// need to do the same to avoid overwriting the image, as tmux
|
||||
// would remove the image if we touched it.
|
||||
ih = (ih + 5) / 6 * 6
|
||||
}
|
||||
|
||||
sw := (iw + cw - 1) / cw
|
||||
sh := (ih + ch - 1) / ch
|
||||
|
||||
if y+sh-1 >= win.y+win.h {
|
||||
break
|
||||
}
|
||||
|
||||
screen.LockRegion(win.x, y, sw, sh, true)
|
||||
fmt.Fprintf(&b, "\033[%d;%dH", y+1, win.x+1)
|
||||
b.WriteString(line)
|
||||
y += sh
|
||||
}
|
||||
|
||||
screen.LockRegion(win.x, win.y, (iw+cw-1)/cw, (ih+ch-1)/ch, true)
|
||||
fmt.Fprint(os.Stderr, "\0337") // Save cursor position
|
||||
fmt.Fprintf(os.Stderr, "\033[%d;%dH", win.y+1, win.x+1) // Move cursor to position
|
||||
fmt.Fprint(os.Stderr, *reg.sixel) // Print sixel
|
||||
fmt.Fprint(os.Stderr, "\0338") // Restore cursor position
|
||||
fmt.Fprint(os.Stderr, "\033[?2026h") // Begin synchronized update
|
||||
fmt.Fprint(os.Stderr, "\0337") // Save cursor position
|
||||
fmt.Fprint(os.Stderr, b.String()) // Write data
|
||||
fmt.Fprint(os.Stderr, "\0338") // Restore cursor position
|
||||
fmt.Fprint(os.Stderr, "\033[?2026l") // End synchronized update
|
||||
|
||||
sxs.lastFile = reg.path
|
||||
sxs.lastWin = *win
|
||||
|
||||
Reference in new issue
Block a user