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 (
|
require (
|
||||||
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80
|
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/fogleman/ease v0.0.0-20170301025033-8da417bf1776
|
||||||
github.com/muesli/termenv v0.5.3-0.20200617154346-da5de72b61c0
|
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/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 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
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.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 h1:oQHK4eixHzrl+DmUKH0x7SJgbkQBiggektj3ChSuE9I=
|
||||||
github.com/muesli/termenv v0.5.3-0.20200617122104-66d435cfe924/go.mod h1:O1/I6sw+6KcrgAmcs6uiUVr7Lui+DNVbHTzt9Lm/PlI=
|
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 {
|
type model struct {
|
||||||
err error
|
|
||||||
content string
|
content string
|
||||||
ready bool
|
ready bool
|
||||||
viewport viewport.Model
|
viewport viewport.Model
|
||||||
|
@ -70,11 +58,8 @@ type model struct {
|
||||||
func initialize(content string) func() (tea.Model, tea.Cmd) {
|
func initialize(content string) func() (tea.Model, tea.Cmd) {
|
||||||
return func() (tea.Model, tea.Cmd) {
|
return func() (tea.Model, tea.Cmd) {
|
||||||
return model{
|
return model{
|
||||||
content: content, // keep content in the model
|
content: content, // keep content in the model
|
||||||
}, tea.Batch(
|
}, nil
|
||||||
getTerminalSize(),
|
|
||||||
listenForResize(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,31 +77,22 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
|
|
||||||
case terminalSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
if msg.Error() != nil {
|
|
||||||
m.err = msg.Error()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
viewportVerticalMargins := viewportTopMargin + viewportBottomMargin
|
viewportVerticalMargins := viewportTopMargin + viewportBottomMargin
|
||||||
|
|
||||||
w, h := msg.Size()
|
|
||||||
if !m.ready {
|
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.YPosition = viewportTopMargin
|
||||||
m.viewport.HighPerformanceRendering = true
|
m.viewport.HighPerformanceRendering = true
|
||||||
m.viewport.SetContent(m.content)
|
m.viewport.SetContent(m.content)
|
||||||
m.ready = true
|
m.ready = true
|
||||||
} else {
|
} else {
|
||||||
m.viewport.Width = w
|
m.viewport.Width = msg.Width
|
||||||
m.viewport.Height = h - viewportBottomMargin
|
m.viewport.Height = msg.Height - viewportBottomMargin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render (or re-render) the whole viewport
|
// Render (or re-render) the whole viewport
|
||||||
cmds = append(cmds, viewport.Sync(m.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-
|
// 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 {
|
func view(mdl tea.Model) string {
|
||||||
m, _ := mdl.(model)
|
m, ok := mdl.(model)
|
||||||
if m.err != nil {
|
if !ok {
|
||||||
return "\nError:" + m.err.Error()
|
return "\n Error: could not perform assertion on model in view."
|
||||||
} 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,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return "\nInitalizing..."
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTerminalSize() tea.Cmd {
|
if !m.ready {
|
||||||
return tea.GetTerminalSize(func(w, h int, err error) tea.TerminalSizeMsg {
|
return "\n Initalizing..."
|
||||||
return terminalSizeMsg{width: w, height: h, err: err}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func listenForResize() tea.Cmd {
|
return fmt.Sprintf(
|
||||||
return tea.OnResize(func() tea.Msg {
|
"── Mr. Pager ──\n\n%s\n\n── %3.f%% ──",
|
||||||
return resizeMsg{}
|
viewport.View(m.viewport),
|
||||||
})
|
m.viewport.ScrollPercent()*100,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue