Update pager example with resize listening + log to file

This commit is contained in:
Christian Rocha 2020-05-25 20:24:17 -04:00
parent 40332a36b3
commit 845a269706
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 33 additions and 5 deletions

View File

@ -18,6 +18,18 @@ func main() {
os.Exit(1)
}
// Set PAGER_LOG to a path to log to a file. For example,
// export PAGER_LOG=debug.log
if os.Getenv("PAGER_LOG") != "" {
p := os.Getenv("PAGER_LOG")
f, err := tea.LogToFile(p, "pager")
if err != nil {
fmt.Printf("Could not open file %s: %v", p, err)
os.Exit(1)
}
defer f.Close()
}
// Use the full size of the terminal in its "Alternate Screen Buffer"
tea.AltScreen()
defer tea.ExitAltScreen()
@ -41,6 +53,8 @@ type terminalSizeMsg struct {
func (t terminalSizeMsg) Size() (int, int) { return t.width, t.height }
func (t terminalSizeMsg) Error() error { return t.err }
type resizeMsg struct{}
type model struct {
err error
content string
@ -51,8 +65,16 @@ type model struct {
func initialize(content string) func() (tea.Model, tea.Cmd) {
return func() (tea.Model, tea.Cmd) {
return model{
content: content,
}, getTerminalSize()
content: content, // keep content in the model
}, tea.Batch(
// Get terminal size asynchronously
getTerminalSize(),
// We're not doing anything with it in this example, but this
// is now you'd listen for resizes.
tea.OnResize(func() tea.Msg { return resizeMsg{} }),
)
}
}
@ -65,17 +87,23 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
m.viewport, _ = viewport.Update(msg, m.viewport)
case terminalSizeMsg:
if msg.Error() != nil {
m.err = msg.Error()
break
}
w, h := msg.Size()
if !m.ready {
m.viewport = viewport.NewModel(w, h)
m.viewport.SetContent(m.content)
m.ready = true
}
case resizeMsg:
return m, getTerminalSize()
}
return m, nil
}