Skip to content

Commit

Permalink
Add resize handling to img viewer (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly authored Sep 24, 2024
1 parent 26ca163 commit 43f4880
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
8 changes: 4 additions & 4 deletions ansipixels/ansipixels.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
const BUFSIZE = 1024

type AnsiPixels struct {
fd int
FdIn int
fdOut int
Out *bufio.Writer
In io.Reader
Expand All @@ -39,15 +39,15 @@ type AnsiPixels struct {

func NewAnsiPixels() *AnsiPixels {
return &AnsiPixels{
fd: safecast.MustConvert[int](os.Stdin.Fd()),
FdIn: safecast.MustConvert[int](os.Stdin.Fd()),
fdOut: safecast.MustConvert[int](os.Stdout.Fd()),
Out: bufio.NewWriter(os.Stdout),
In: os.Stdin,
}
}

func (ap *AnsiPixels) Open() (err error) {
ap.state, err = term.MakeRaw(ap.fd)
ap.state, err = term.MakeRaw(ap.FdIn)
return
}

Expand All @@ -61,7 +61,7 @@ func (ap *AnsiPixels) Restore() {
if ap.state == nil {
return
}
err := term.Restore(ap.fd, ap.state)
err := term.Restore(ap.FdIn, ap.state)
if err != nil {
log.Fatalf("Error restoring terminal: %v", err)
}
Expand Down
32 changes: 26 additions & 6 deletions ansipixels/fps/fps.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fortio.org/cli"
"fortio.org/log"
"fortio.org/safecast"
"fortio.org/terminal"
"fortio.org/terminal/ansipixels"
)

Expand Down Expand Up @@ -93,6 +94,8 @@ func imagesViewer(ap *ansipixels.AnsiPixels, imageFiles []string) int {
i := 0
l := len(imageFiles)
showInfo := l > 1
ap.SignalChannel()
tv := terminal.TimeoutToTimeval(100 * time.Millisecond)
for {
if i >= l {
i = 0
Expand All @@ -102,27 +105,44 @@ func imagesViewer(ap *ansipixels.AnsiPixels, imageFiles []string) int {
if err != nil {
return log.FErrf("Error reading image %s: %v", imageFile, err)
}
redraw:
if err = ap.ShowImage(img, "\033[34m"); err != nil {
return log.FErrf("Error showing image: %v", err)
}
if showInfo {
ap.WriteRight(ap.H-1, "%s (%s, %d/%d)", imageFile, format, i+1, l)
ap.Out.Flush()
}
wait:
ap.Out.Flush()
_, err = ap.In.Read(ap.Data[0:1])
if err != nil {
return log.FErrf("Error with cursor position request: %v", err)
var n int
for {
select {
case s := <-ap.C:
if ap.IsResizeSignal(s) {
_ = ap.GetSize()
goto redraw
}
return 0
default:
n, err = terminal.TimeoutReader(ap.FdIn, tv, ap.Data[0:1])
if err != nil {
return log.FErrf("Error reading key: %v", err)
}
}
if n != 0 {
break
}
}
ap.Data = ap.Data[0:n]
if ap.Data[0] == '?' || ap.Data[0] == 'h' || ap.Data[0] == 'H' {
ap.WriteCentered(ap.H/2-1, "Showing %d out of %d images, hit any key to continue,", i+1, l)
ap.WriteCentered(ap.H/2, "'q' to exit, left arrow to go back, 'i' to toggle image information")
ap.Out.Flush()
_, _ = ap.In.Read(ap.Data[0:1])
goto wait
}
if ap.Data[0] == 'i' || ap.Data[0] == 'I' {
showInfo = !showInfo
continue
goto redraw
}
if ap.Data[0] == 27 {
n, _ := ap.In.Read(ap.Data[1:3])
Expand Down

0 comments on commit 43f4880

Please sign in to comment.