-
I'm doing a simple experiment to poll the terminal for size changes and update a bordered viewport to span the full window. (It's not an ideal solution but I can't rely on package main
import (
"fmt"
"os"
"time"
"golang.org/x/term"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var borderStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder())
var textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("45"))
type tickMsg int
type model struct {
w, h int
vp viewport.Model
}
func tick() tea.Msg {
time.Sleep(time.Second / 4)
return tickMsg(1)
}
func (m *model) Init() tea.Cmd {
return tick
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tickMsg:
w, h, _ := term.GetSize(int(os.Stdout.Fd()))
if w != m.w || h != m.h {
m.updateSize(w, h)
}
return m, tick
}
return m, nil
}
func (m *model) View() string {
return m.vp.View()
}
func (m *model) updateSize(w, h int) {
m.w = w
m.h = h
m.vp = viewport.New(m.w, m.h)
m.vp.Style = borderStyle
text := fmt.Sprintf("Size: %d, %d", m.w, m.h)
rendered := textStyle.Copy().Width(m.w).Height(m.h).Render(text)
m.vp.SetContent(rendered)
}
func main() {
m := &model{w: 1, h: 1, vp: viewport.New(1, 1)}
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
fmt.Print(err)
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Okay, it looks like batching a WindowSizeMsg Cmd with my tick Cmd solves my problem. It'd be great if bubbletea comes up with a way to avoid polling for window resizes on Windows machines in the future but I'm happy with this for now. previous: func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tickMsg:
w, h, _ := term.GetSize(int(os.Stdout.Fd()))
if w != m.w || h != m.h {
m.updateSize(w, h)
}
return m, tick
}
return m, nil
} solution: func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case tickMsg:
w, h, _ := term.GetSize(int(os.Stdout.Fd()))
if w != m.w || h != m.h {
m.updateSize(w, h)
}
return m, tea.Batch(tick, func() tea.Msg { return tea.WindowSizeMsg{Width: w, Height: h} })
}
return m, nil
} |
Beta Was this translation helpful? Give feedback.
-
What issues are you having with the |
Beta Was this translation helpful? Give feedback.
-
Ah, I meant that I can't listen for Lines 3 to 6 in 6b77c8f I can fire my own |
Beta Was this translation helpful? Give feedback.
-
Ah ok thank you! Looks like this was a duplicate of #538 🤔 Thank you for reporting it! |
Beta Was this translation helpful? Give feedback.
Okay, it looks like batching a WindowSizeMsg Cmd with my tick Cmd solves my problem. It'd be great if bubbletea comes up with a way to avoid polling for window resizes on Windows machines in the future but I'm happy with this for now.
previous:
solution: