Make sixel output flicker free (#1943)
* Make sixel output flicker free * Address pr comments * Lock regions based on sixel dimensions This fixes issues related to clearing the previous sixel data * name changes and extra error handeling * Apply force clearing changes and change log messages * Delete missleading comment * check pixel dimensions * correct cell dimensions checking * Add force clearing on image update --------- Co-authored-by: Mariusz Kuchta <mariusz@kuchta.dev>
This commit is contained in:
1 parent
3d205f05e3
commit
7a89808688
6 files changed
+71
-64
No files matched your search
@@ -435,6 +435,9 @@ func (app *app) loop() {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
if r.path == curr.path {
|
if r.path == curr.path {
|
||||||
app.ui.regPrev = r
|
app.ui.regPrev = r
|
||||||
|
if gOpts.sixel {
|
||||||
|
app.ui.sxScreen.forceClear = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,9 +91,6 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
if app.nav.height != app.ui.wins[0].h {
|
if app.nav.height != app.ui.wins[0].h {
|
||||||
app.nav.height = app.ui.wins[0].h
|
app.nav.height = app.ui.wins[0].h
|
||||||
clear(app.nav.regCache)
|
clear(app.nav.regCache)
|
||||||
if gOpts.sixel {
|
|
||||||
app.ui.sxScreen.lastFile = ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
}
|
}
|
||||||
@@ -157,6 +154,9 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
err = errors.New("preview: 'ratios' should consist of at least two numbers before enabling 'preview'")
|
err = errors.New("preview: 'ratios' should consist of at least two numbers before enabling 'preview'")
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
if gOpts.sixel {
|
||||||
|
app.ui.sxScreen.forceClear = true
|
||||||
|
}
|
||||||
gOpts.preview = preview
|
gOpts.preview = preview
|
||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
}
|
}
|
||||||
@@ -174,6 +174,9 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
err = applyBoolOpt(&gOpts.showbinds, e)
|
err = applyBoolOpt(&gOpts.showbinds, e)
|
||||||
case "sixel", "nosixel", "sixel!":
|
case "sixel", "nosixel", "sixel!":
|
||||||
err = applyBoolOpt(&gOpts.sixel, e)
|
err = applyBoolOpt(&gOpts.sixel, e)
|
||||||
|
clear(app.nav.regCache)
|
||||||
|
app.ui.sxScreen.forceClear = true
|
||||||
|
app.ui.loadFile(app, true)
|
||||||
case "smartcase", "nosmartcase", "smartcase!":
|
case "smartcase", "nosmartcase", "smartcase!":
|
||||||
err = applyBoolOpt(&gOpts.smartcase, e)
|
err = applyBoolOpt(&gOpts.smartcase, e)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -350,7 +353,6 @@ func (e *setExpr) eval(app *app, args []string) {
|
|||||||
app.ui.wins = getWins(app.ui.screen)
|
app.ui.wins = getWins(app.ui.screen)
|
||||||
if gOpts.sixel {
|
if gOpts.sixel {
|
||||||
clear(app.nav.regCache)
|
clear(app.nav.regCache)
|
||||||
app.ui.sxScreen.lastFile = ""
|
|
||||||
}
|
}
|
||||||
app.ui.loadFile(app, true)
|
app.ui.loadFile(app, true)
|
||||||
case "scrolloff":
|
case "scrolloff":
|
||||||
@@ -1433,7 +1435,7 @@ func (e *callExpr) eval(app *app, args []string) {
|
|||||||
}
|
}
|
||||||
if gOpts.sixel {
|
if gOpts.sixel {
|
||||||
clear(app.nav.regCache)
|
clear(app.nav.regCache)
|
||||||
app.ui.sxScreen.lastFile = ""
|
app.ui.sxScreen.forceClear = true
|
||||||
}
|
}
|
||||||
for _, dir := range app.nav.dirs {
|
for _, dir := range app.nav.dirs {
|
||||||
dir.boundPos(app.nav.height)
|
dir.boundPos(app.nav.height)
|
||||||
|
|||||||
@@ -325,8 +325,9 @@ func getFileExtension(file fs.FileInfo) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
|
reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
|
||||||
reRulerSub = regexp.MustCompile(`%[apmcsfithd]|%\{[^}]+\}`)
|
reRulerSub = regexp.MustCompile(`%[apmcsfithd]|%\{[^}]+\}`)
|
||||||
|
reSixelSize = regexp.MustCompile(`"1;1;(\d+);(\d+)`)
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -1,69 +1,73 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const gSixelBegin = "\033P"
|
||||||
gSixelBegin = "\033P"
|
|
||||||
|
|
||||||
// The filler character should be:
|
|
||||||
// - rarely used: the filler is used to trick tcell into redrawing, if the
|
|
||||||
// filler character appears in the user's preview, that cell might not
|
|
||||||
// be cleaned up properly
|
|
||||||
// - ideally renders as empty space: the filler alternates between bold
|
|
||||||
// and regular, using a non-space would look weird to the user.
|
|
||||||
gSixelFiller = '\u2000'
|
|
||||||
)
|
|
||||||
|
|
||||||
type sixelScreen struct {
|
type sixelScreen struct {
|
||||||
xprev, yprev int
|
lastFile string
|
||||||
sixel *string
|
lastWin win
|
||||||
altFill bool
|
forceClear bool
|
||||||
lastFile string // TODO maybe use hash of sixels instead to flip altFill
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sxs *sixelScreen) fillerStyle(filePath string) tcell.Style {
|
func (sxs *sixelScreen) clearSixel(win *win, screen tcell.Screen, filePath string) {
|
||||||
if sxs.lastFile != filePath {
|
if sxs.lastFile != "" && (filePath != sxs.lastFile || *win != sxs.lastWin || sxs.forceClear) {
|
||||||
sxs.altFill = !sxs.altFill
|
screen.LockRegion(sxs.lastWin.x, sxs.lastWin.y, sxs.lastWin.w, sxs.lastWin.h, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sxs.altFill {
|
|
||||||
return tcell.StyleDefault.Bold(true)
|
|
||||||
}
|
|
||||||
return tcell.StyleDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sxs *sixelScreen) showSixels() {
|
|
||||||
if sxs.sixel == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX: workaround for bug where quitting lf might leave the terminal in bold
|
|
||||||
fmt.Fprint(os.Stderr, "\033[0m")
|
|
||||||
|
|
||||||
fmt.Fprint(os.Stderr, "\0337") // Save cursor position
|
|
||||||
fmt.Fprintf(os.Stderr, "\033[%d;%dH", sxs.yprev, sxs.xprev) // Move cursor to position
|
|
||||||
fmt.Fprint(os.Stderr, *sxs.sixel) //
|
|
||||||
fmt.Fprint(os.Stderr, "\0338") // Restore cursor position
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
|
func (sxs *sixelScreen) printSixel(win *win, screen tcell.Screen, reg *reg) {
|
||||||
if reg.sixel == nil {
|
if reg.path == sxs.lastFile && *win == sxs.lastWin && !sxs.forceClear {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACK: fillers are used to control when tcell redraws the region where a sixel image is drawn.
|
if reg.sixel == nil {
|
||||||
// alternating between bold and regular is to clear the image before drawing a new one.
|
sxs.lastFile = ""
|
||||||
st := sxs.fillerStyle(reg.path)
|
return
|
||||||
for y := range win.h {
|
|
||||||
st = win.print(screen, 0, y, st, strings.Repeat(string(gSixelFiller), win.w))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sxs.xprev, sxs.yprev = win.x+1, win.y+1
|
ti, err := tcell.LookupTerminfo(os.Getenv("TERM"))
|
||||||
sxs.sixel = reg.sixel
|
if err != nil {
|
||||||
|
log.Printf("sixel: failed to look up term into %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tty, ok := screen.Tty()
|
||||||
|
if !ok {
|
||||||
|
log.Printf("sixel: failed to get tty")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ws, err := tty.WindowSize()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("sixel: failed to get window size %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cw, ch := ws.CellDimensions()
|
||||||
|
if cw <= 0 || ch <= 0 {
|
||||||
|
log.Printf("sixel: cell dimensions should not be 0")
|
||||||
|
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])
|
||||||
|
|
||||||
|
screen.LockRegion(win.x, win.y, iw/cw, ih/ch, true)
|
||||||
|
ti.TPuts(tty, ti.TGoto(win.x, win.y))
|
||||||
|
ti.TPuts(tty, *reg.sixel)
|
||||||
|
|
||||||
|
sxs.lastFile = reg.path
|
||||||
|
sxs.lastWin = *win
|
||||||
|
sxs.forceClear = false
|
||||||
}
|
}
|
||||||
@@ -1020,7 +1020,6 @@ func (ui *ui) draw(nav *nav) {
|
|||||||
context := dirContext{selections: nav.selections, saves: nav.saves, tags: nav.tags}
|
context := dirContext{selections: nav.selections, saves: nav.saves, tags: nav.tags}
|
||||||
|
|
||||||
ui.screen.Clear()
|
ui.screen.Clear()
|
||||||
ui.sxScreen.sixel = nil
|
|
||||||
|
|
||||||
ui.drawPromptLine(nav)
|
ui.drawPromptLine(nav)
|
||||||
|
|
||||||
@@ -1058,14 +1057,15 @@ func (ui *ui) draw(nav *nav) {
|
|||||||
ui.screen.ShowCursor(ui.msgWin.x+runeSliceWidth(prefix)+runeSliceWidth(left), ui.msgWin.y)
|
ui.screen.ShowCursor(ui.msgWin.x+runeSliceWidth(prefix)+runeSliceWidth(left), ui.msgWin.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gOpts.preview {
|
curr, err := nav.currFile()
|
||||||
curr, err := nav.currFile()
|
if err == nil {
|
||||||
if err == nil {
|
preview := ui.wins[len(ui.wins)-1]
|
||||||
preview := ui.wins[len(ui.wins)-1]
|
ui.sxScreen.clearSixel(preview, ui.screen, curr.path)
|
||||||
|
if gOpts.preview {
|
||||||
if curr.Mode().IsRegular() || (curr.IsDir() && gOpts.dirpreviews) {
|
if curr.Mode().IsRegular() || (curr.IsDir() && gOpts.dirpreviews) {
|
||||||
preview.printReg(ui.screen, ui.regPrev, nav.previewLoading, &ui.sxScreen)
|
preview.printReg(ui.screen, ui.regPrev, nav.previewLoading, &ui.sxScreen)
|
||||||
} else if curr.IsDir() {
|
} else if curr.IsDir() {
|
||||||
|
ui.sxScreen.lastFile = ""
|
||||||
preview.printDir(ui, ui.dirPrev, &context,
|
preview.printDir(ui, ui.dirPrev, &context,
|
||||||
&dirStyle{colors: ui.styles, icons: ui.icons, role: Preview})
|
&dirStyle{colors: ui.styles, icons: ui.icons, role: Preview})
|
||||||
}
|
}
|
||||||
@@ -1097,10 +1097,6 @@ func (ui *ui) draw(nav *nav) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ui.screen.Show()
|
ui.screen.Show()
|
||||||
if ui.menu == "" && ui.cmdPrefix == "" && ui.sxScreen.sixel != nil {
|
|
||||||
ui.sxScreen.lastFile = ui.regPrev.path
|
|
||||||
ui.sxScreen.showSixels()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findBinds(keys map[string]expr, prefix string) (binds map[string]expr, ok bool) {
|
func findBinds(keys map[string]expr, prefix string) (binds map[string]expr, ok bool) {
|
||||||
@@ -1522,6 +1518,7 @@ func (ui *ui) readExpr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ui *ui) suspend() error {
|
func (ui *ui) suspend() error {
|
||||||
|
ui.sxScreen.forceClear = true
|
||||||
return ui.screen.Suspend()
|
return ui.screen.Suspend()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user