diff --git a/examples/go.mod b/examples/go.mod index fd99e64..522e6ee 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -6,7 +6,7 @@ replace github.com/charmbracelet/bubbletea => ../ require ( github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80 - github.com/charmbracelet/bubbletea v0.6.4-0.20200525234836-3b8b011b5a26 + github.com/charmbracelet/bubbletea v0.8.1 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 ) diff --git a/examples/go.sum b/examples/go.sum index bc9e93e..a5ea61f 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -8,7 +8,6 @@ github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tW github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/muesli/termenv v0.5.2 h1:N1Y1dHRtx6OizOgaIQXd8SkJl4T/cCOV+YyWXiuLUEA= github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I= github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI= diff --git a/examples/pager/main.go b/examples/pager/main.go index 8bcb804..514031b 100644 --- a/examples/pager/main.go +++ b/examples/pager/main.go @@ -49,19 +49,7 @@ func main() { } } -type terminalSizeMsg struct { - width int - height int - err error -} - -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 ready bool viewport viewport.Model @@ -70,11 +58,8 @@ type model struct { func initialize(content string) func() (tea.Model, tea.Cmd) { return func() (tea.Model, tea.Cmd) { return model{ - content: content, // keep content in the model - }, tea.Batch( - getTerminalSize(), - listenForResize(), - ) + content: content, // keep content in the model + }, nil } } @@ -92,31 +77,22 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { return m, tea.Quit } - case terminalSizeMsg: - if msg.Error() != nil { - m.err = msg.Error() - break - } - + case tea.WindowSizeMsg: viewportVerticalMargins := viewportTopMargin + viewportBottomMargin - w, h := msg.Size() if !m.ready { - m.viewport = viewport.NewModel(w, h-viewportVerticalMargins) + m.viewport = viewport.NewModel(msg.Width, msg.Height-viewportVerticalMargins) m.viewport.YPosition = viewportTopMargin m.viewport.HighPerformanceRendering = true m.viewport.SetContent(m.content) m.ready = true } else { - m.viewport.Width = w - m.viewport.Height = h - viewportBottomMargin + m.viewport.Width = msg.Width + m.viewport.Height = msg.Height - viewportBottomMargin } // Render (or re-render) the whole viewport cmds = append(cmds, viewport.Sync(m.viewport)) - - case resizeMsg: - return m, tea.Batch(getTerminalSize(), listenForResize()) } // Because we're using the viewport's default update function (with pager- @@ -131,29 +107,18 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { } func view(mdl tea.Model) string { - m, _ := mdl.(model) - if m.err != nil { - return "\nError:" + m.err.Error() - } else if m.ready { - - return viewport.View(m.viewport) - return fmt.Sprintf( - "── Mr. Pager ──\n\n%s\n\n── %3.f%% ──", - viewport.View(m.viewport), - m.viewport.ScrollPercent()*100, - ) + m, ok := mdl.(model) + if !ok { + return "\n Error: could not perform assertion on model in view." } - return "\nInitalizing..." -} -func getTerminalSize() tea.Cmd { - return tea.GetTerminalSize(func(w, h int, err error) tea.TerminalSizeMsg { - return terminalSizeMsg{width: w, height: h, err: err} - }) -} + if !m.ready { + return "\n Initalizing..." + } -func listenForResize() tea.Cmd { - return tea.OnResize(func() tea.Msg { - return resizeMsg{} - }) + return fmt.Sprintf( + "── Mr. Pager ──\n\n%s\n\n── %3.f%% ──", + viewport.View(m.viewport), + m.viewport.ScrollPercent()*100, + ) }