2019-06-10 22:50:31 +03:00
|
|
|
package m
|
|
|
|
|
2019-06-11 19:29:30 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
2019-06-10 22:50:31 +03:00
|
|
|
// Pager is the main on-screen pager
|
2019-06-11 16:31:26 +03:00
|
|
|
type _Pager struct {
|
2019-06-10 22:50:31 +03:00
|
|
|
reader _Reader
|
2019-06-11 22:09:57 +03:00
|
|
|
screen tcell.Screen
|
2019-06-10 22:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPager creates a new Pager
|
2019-06-11 16:31:26 +03:00
|
|
|
func NewPager(r _Reader) *_Pager {
|
|
|
|
return &_Pager{
|
2019-06-10 22:50:31 +03:00
|
|
|
reader: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:09:57 +03:00
|
|
|
func (p *_Pager) _AddFooter() {
|
|
|
|
_, height := p.screen.Size()
|
2019-06-11 19:52:38 +03:00
|
|
|
|
|
|
|
for pos, char := range "Press ESC / Return to exit" {
|
2019-06-11 22:09:57 +03:00
|
|
|
p.screen.SetContent(pos, height-1, char, nil, tcell.StyleDefault)
|
2019-06-11 19:52:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:09:57 +03:00
|
|
|
func (p *_Pager) _Redraw() {
|
|
|
|
p.screen.Clear()
|
2019-06-11 19:52:38 +03:00
|
|
|
|
|
|
|
// FIXME: Ask our reader for lines and draw them
|
|
|
|
|
2019-06-11 22:09:57 +03:00
|
|
|
p._AddFooter()
|
|
|
|
p.screen.Sync()
|
2019-06-11 19:52:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 22:50:31 +03:00
|
|
|
// StartPaging brings up the pager on screen
|
2019-06-11 16:31:26 +03:00
|
|
|
func (p *_Pager) StartPaging() {
|
2019-06-11 19:29:30 +03:00
|
|
|
// This function initially inspired by
|
|
|
|
// https://github.com/gdamore/tcell/blob/master/_demos/unicode.go
|
|
|
|
s, e := tcell.NewScreen()
|
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2019-06-11 22:09:57 +03:00
|
|
|
p.screen = s
|
2019-06-11 19:29:30 +03:00
|
|
|
|
|
|
|
if e = s.Init(); e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", e)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
quit := make(chan struct{})
|
|
|
|
|
|
|
|
// Main loop
|
|
|
|
go func() {
|
2019-06-11 19:52:38 +03:00
|
|
|
s.Show()
|
2019-06-11 19:29:30 +03:00
|
|
|
for {
|
2019-06-11 22:09:57 +03:00
|
|
|
p._Redraw()
|
2019-06-11 19:52:38 +03:00
|
|
|
|
2019-06-11 19:29:30 +03:00
|
|
|
ev := s.PollEvent()
|
|
|
|
switch ev := ev.(type) {
|
|
|
|
case *tcell.EventKey:
|
|
|
|
switch ev.Key() {
|
|
|
|
case tcell.KeyEscape, tcell.KeyEnter:
|
|
|
|
close(quit)
|
|
|
|
return
|
|
|
|
case tcell.KeyCtrlL:
|
|
|
|
s.Sync()
|
|
|
|
}
|
|
|
|
case *tcell.EventResize:
|
|
|
|
s.Sync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-quit
|
2019-06-10 22:50:31 +03:00
|
|
|
|
2019-06-11 19:29:30 +03:00
|
|
|
s.Fini()
|
2019-06-10 22:50:31 +03:00
|
|
|
}
|