-
Notifications
You must be signed in to change notification settings - Fork 330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sixel output flicker free #1943
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,69 +1,69 @@ | ||||||
package main | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"log" | ||||||
"os" | ||||||
"strings" | ||||||
"strconv" | ||||||
|
||||||
"github.com/gdamore/tcell/v2" | ||||||
) | ||||||
|
||||||
const ( | ||||||
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 { | ||||||
xprev, yprev int | ||||||
sixel *string | ||||||
altFill bool | ||||||
lastFile string // TODO maybe use hash of sixels instead to flip altFill | ||||||
lastFile string | ||||||
lastSxW int | ||||||
lastSxH int | ||||||
lastWinW int | ||||||
lastWinH int | ||||||
} | ||||||
|
||||||
func (sxs *sixelScreen) fillerStyle(filePath string) tcell.Style { | ||||||
if sxs.lastFile != filePath { | ||||||
sxs.altFill = !sxs.altFill | ||||||
} | ||||||
|
||||||
if sxs.altFill { | ||||||
return tcell.StyleDefault.Bold(true) | ||||||
} | ||||||
return tcell.StyleDefault | ||||||
} | ||||||
func (sxs *sixelScreen) unlockSixel(win *win, screen tcell.Screen, filePath string) { | ||||||
if filePath != "" && (filePath != sxs.lastFile || win.w != sxs.lastWinW || win.h != sxs.lastWinH) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check should be
Suggested change
|
||||||
screen.LockRegion(win.x, win.y, sxs.lastSxW, sxs.lastSxH, false) | ||||||
|
||||||
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) { | ||||||
|
||||||
if reg.path == sxs.lastFile && win.w == sxs.lastWinW && win.h == sxs.lastWinH { | ||||||
return | ||||||
} | ||||||
if reg.sixel == nil { | ||||||
sxs.lastFile = "" | ||||||
return | ||||||
} | ||||||
|
||||||
// HACK: fillers are used to control when tcell redraws the region where a sixel image is drawn. | ||||||
// alternating between bold and regular is to clear the image before drawing a new one. | ||||||
st := sxs.fillerStyle(reg.path) | ||||||
for y := range win.h { | ||||||
st = win.print(screen, 0, y, st, strings.Repeat(string(gSixelFiller), win.w)) | ||||||
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) | ||||||
Kuchteq marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return | ||||||
} | ||||||
v, _ := tty.WindowSize() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||||||
w, h := v.CellDimensions() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use the same variable names as the sixel demo:
|
||||||
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]) | ||||||
|
||||||
sxs.xprev, sxs.yprev = win.x+1, win.y+1 | ||||||
sxs.sixel = reg.sixel | ||||||
// 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 | ||||||
Comment on lines
+65
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect, So I did some more investigation on my side, and I think what happens is this:
So I would make the following changes:
|
||||||
sxs.lastWinW = win.w | ||||||
sxs.lastWinH = win.h | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might have to stay. I tried running the
redraw
command manually (or alternatively press<c-l>
), and found that the sixel image got removed instead of being redrawn.It might be good to check what happens when you try toggling other options that affect sixel images and their sizes, such as
sixel
/preview
/ratios
/drawbox
.