Skip to content
Myr

Just listen for tea.WindowSizeMsg to get terminal dimensions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 116ab34 · Browse files · 3 files+18 added−54 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+18 added−54 removed

  • other:+18 added−54 removed

3 files+18 added−54 removed

modified:examples/pager/main.go+17 added-52 removed
47 unmodified lines
48 os.Exit(1)49 }50}5152type terminalSizeMsg struct {53 width int54 height int55 err error56}5758func (t terminalSizeMsg) Size() (int, int) { return t.width, t.height }59func (t terminalSizeMsg) Error() error { return t.err }6061type resizeMsg struct{}6252type model struct {64 err error53 content string54 ready bool55 viewport viewport.Model56}5758func initialize(content string) func() (tea.Model, tea.Cmd) {59 return func() (tea.Model, tea.Cmd) {60 return model{73 content: content, // keep content in the model74 }, tea.Batch(75 getTerminalSize(),76 listenForResize(),77 )61 content: content, // keep content in the model62 }, nil63 }64}6566func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
9 unmodified lines
76 if msg.Type == tea.KeyCtrlC {77 return m, tea.Quit78 }7995 case terminalSizeMsg:96 if msg.Error() != nil {97 m.err = msg.Error()98 break99 }10080 case tea.WindowSizeMsg:81 viewportVerticalMargins := viewportTopMargin + viewportBottomMargin82103 w, h := msg.Size()83 if !m.ready {105 m.viewport = viewport.NewModel(w, h-viewportVerticalMargins)84 m.viewport = viewport.NewModel(msg.Width, msg.Height-viewportVerticalMargins)85 m.viewport.YPosition = viewportTopMargin86 m.viewport.HighPerformanceRendering = true87 m.viewport.SetContent(m.content)88 m.ready = true89 } else {111 m.viewport.Width = w112 m.viewport.Height = h - viewportBottomMargin90 m.viewport.Width = msg.Width91 m.viewport.Height = msg.Height - viewportBottomMargin92 }9394 // Render (or re-render) the whole viewport95 cmds = append(cmds, viewport.Sync(m.viewport))117118 case resizeMsg:119 return m, tea.Batch(getTerminalSize(), listenForResize())96 }9798 // Because we're using the viewport's default update function (with pager-99 // style navigation) it's important that the viewport's update function:
6 unmodified lines
106 return m, tea.Batch(cmds...)107}108109func view(mdl tea.Model) string {134 m, _ := mdl.(model)135 if m.err != nil {136 return "\nError:" + m.err.Error()137 } else if m.ready {110 m, ok := mdl.(model)111 if !ok {112 return "\n Error: could not perform assertion on model in view."113 }114139 return viewport.View(m.viewport)140 return fmt.Sprintf(141 "── Mr. Pager ──\n\n%s\n\n── %3.f%% ──",142 viewport.View(m.viewport),143 m.viewport.ScrollPercent()*100,144 )115 if !m.ready {116 return "\n Initalizing..."117 }146 return "\nInitalizing..."147}118149func getTerminalSize() tea.Cmd {150 return tea.GetTerminalSize(func(w, h int, err error) tea.TerminalSizeMsg {151 return terminalSizeMsg{width: w, height: h, err: err}152 })119 return fmt.Sprintf(120 "── Mr. Pager ──\n\n%s\n\n── %3.f%% ──",121 viewport.View(m.viewport),122 m.viewport.ScrollPercent()*100,123 )124}154155func listenForResize() tea.Cmd {156 return tea.OnResize(func() tea.Msg {157 return resizeMsg{}158 })159}
modified:examples/go.mod+1 added-1 removed
4 unmodified lines
5replace github.com/charmbracelet/bubbletea => ../67require (8 github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f809 github.com/charmbracelet/bubbletea v0.6.4-0.20200525234836-3b8b011b5a269 github.com/charmbracelet/bubbletea v0.8.110 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177611 github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c012)
modified:examples/go.sum-1 removed
6 unmodified lines
7github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=8github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=9github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=10github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=11github.com/muesli/termenv v0.5.2 h1:N1Y1dHRtx6OizOgaIQXd8SkJl4T/cCOV+YyWXiuLUEA=11github.com/muesli/termenv v0.5.2/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=12github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I=13github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=14github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0 h1:IASsj5pQ478TEN9TLiuhP+ugUQEotZ1QdzQvcJUQ1dM=
14 unmodified lines