forked from Mirrors/bubbletea
Just listen for tea.WindowSizeMsg to get terminal dimensions
This commit is contained in:
parent
116ab34eb8
commit
8e616c4c0c
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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=
|
||||
|
|
|
@ -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
|
||||
|
@ -71,10 +59,7 @@ 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(),
|
||||
)
|
||||
}, 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 {
|
||||
m, ok := mdl.(model)
|
||||
if !ok {
|
||||
return "\n Error: could not perform assertion on model in view."
|
||||
}
|
||||
|
||||
if !m.ready {
|
||||
return "\n Initalizing..."
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
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}
|
||||
})
|
||||
}
|
||||
|
||||
func listenForResize() tea.Cmd {
|
||||
return tea.OnResize(func() tea.Msg {
|
||||
return resizeMsg{}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue