fix(tty): fall back to default dimensions when terminal size is 0x0 (#1836)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by adam magued · committed by GitHub · parent 33f6a40 · Browse files · 3 files·+231 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: AdamMagued <adammagued.business@gmail.com>
Diff stats
3 files+231 added−1 removed
- other:+231 added−1 removed
Provenance
- Commit
96d69d2- Tree
2a99ba4- Author
adam magued <adamismailmageud@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
33f6a40
3 files changed+231 added−1 removed
modified:tea.go+1 added-1 removed
1055 unmodified lines1056 if err != nil {1057 return p.initialModel, fmt.Errorf("bubbletea: error getting terminal size: %w", err)1058 }1059 1060- width, height = w, h1060+ width, height = p.fallbackDimensions(w, h)1061 }1062 1063 p.width, p.height = width, height1064 resizeMsg := WindowSizeMsg{Width: p.width, Height: p.height}402 unmodified linesadded:tty_test.go+190 added
1+package tea2+3+import (4+ "bytes"5+ "testing"6+)7+8+type windowSizeModel struct {9+ windowSize WindowSizeMsg10+ quit bool11+}12+13+func (m *windowSizeModel) Init() Cmd {14+ return nil15+}16+17+func (m *windowSizeModel) Update(msg Msg) (Model, Cmd) {18+ switch msg := msg.(type) {19+ case WindowSizeMsg:20+ m.windowSize = msg21+ m.quit = true22+ return m, Quit23+ }24+ return m, nil25+}26+27+func (m *windowSizeModel) View() View {28+ return NewView("")29+}30+31+func TestFallbackDimensions(t *testing.T) {32+ t.Run("default fallback to 80x24", func(t *testing.T) {33+ t.Setenv("COLUMNS", "")34+ t.Setenv("LINES", "")35+36+ p := NewProgram(nil)37+ w, h := p.fallbackDimensions(0, 0)38+ if w != defaultWidth || h != defaultHeight {39+ t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)40+ }41+42+ w, h = p.fallbackDimensions(-10, -5)43+ if w != defaultWidth || h != defaultHeight {44+ t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)45+ }46+ })47+48+ t.Run("preserve positive dimensions", func(t *testing.T) {49+ p := NewProgram(nil)50+ w, h := p.fallbackDimensions(120, 40)51+ if w != 120 || h != 40 {52+ t.Errorf("expected 120x40, got %dx%d", w, h)53+ }54+ })55+56+ t.Run("mixed non-positive dimensions", func(t *testing.T) {57+ t.Setenv("COLUMNS", "")58+ t.Setenv("LINES", "")59+60+ p := NewProgram(nil)61+ w, h := p.fallbackDimensions(0, 50)62+ if w != defaultWidth || h != 50 {63+ t.Errorf("expected %dx50, got %dx%d", defaultWidth, w, h)64+ }65+66+ w, h = p.fallbackDimensions(100, 0)67+ if w != 100 || h != defaultHeight {68+ t.Errorf("expected 100x%d, got %dx%d", defaultHeight, w, h)69+ }70+ })71+72+ t.Run("fallback to COLUMNS and LINES from os env", func(t *testing.T) {73+ t.Setenv("COLUMNS", "132")74+ t.Setenv("LINES", "43")75+76+ p := NewProgram(nil)77+ w, h := p.fallbackDimensions(0, 0)78+ if w != 132 || h != 43 {79+ t.Errorf("expected 132x43, got %dx%d", w, h)80+ }81+ })82+83+ t.Run("fallback to COLUMNS and LINES from program environment", func(t *testing.T) {84+ t.Setenv("COLUMNS", "200")85+ t.Setenv("LINES", "100")86+87+ p := NewProgram(nil, WithEnvironment([]string{"COLUMNS=150", "LINES=60"}))88+ w, h := p.fallbackDimensions(0, 0)89+ if w != 150 || h != 60 {90+ t.Errorf("expected 150x60, got %dx%d", w, h)91+ }92+ })93+94+ t.Run("program environment without COLUMNS and LINES falls back to 80x24 skipping os env", func(t *testing.T) {95+ t.Setenv("COLUMNS", "200")96+ t.Setenv("LINES", "100")97+98+ p := NewProgram(nil, WithEnvironment([]string{"OTHER_VAR=1"}))99+ w, h := p.fallbackDimensions(0, 0)100+ if w != defaultWidth || h != defaultHeight {101+ t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)102+ }103+ })104+105+ t.Run("invalid or non-positive COLUMNS and LINES fall back to defaults", func(t *testing.T) {106+ t.Setenv("COLUMNS", "invalid")107+ t.Setenv("LINES", "-10")108+109+ p := NewProgram(nil)110+ w, h := p.fallbackDimensions(0, 0)111+ if w != defaultWidth || h != defaultHeight {112+ t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)113+ }114+115+ t.Setenv("COLUMNS", "0")116+ t.Setenv("LINES", "0")117+118+ w, h = p.fallbackDimensions(0, 0)119+ if w != defaultWidth || h != defaultHeight {120+ t.Errorf("expected %dx%d, got %dx%d", defaultWidth, defaultHeight, w, h)121+ }122+ })123+}124+125+func TestProgramNonTTYDimensions(t *testing.T) {126+ t.Run("non-tty output does not receive fallback dimensions", func(t *testing.T) {127+ t.Setenv("COLUMNS", "110")128+ t.Setenv("LINES", "35")129+130+ var buf bytes.Buffer131+ var in bytes.Buffer132+ m := &windowSizeModel{}133+134+ p := NewProgram(m,135+ WithInput(&in),136+ WithOutput(&buf),137+ )138+139+ if _, err := p.Run(); err != nil {140+ t.Fatalf("unexpected error: %v", err)141+ }142+143+ if m.windowSize.Width != 0 || m.windowSize.Height != 0 {144+ t.Errorf("expected WindowSizeMsg 0x0 for non-TTY output, got %dx%d",145+ m.windowSize.Width, m.windowSize.Height)146+ }147+ })148+149+ t.Run("non-tty output with program environment does not receive fallback dimensions", func(t *testing.T) {150+ var buf bytes.Buffer151+ var in bytes.Buffer152+ m := &windowSizeModel{}153+154+ p := NewProgram(m,155+ WithInput(&in),156+ WithOutput(&buf),157+ WithEnvironment([]string{"COLUMNS=150", "LINES=60"}),158+ )159+160+ if _, err := p.Run(); err != nil {161+ t.Fatalf("unexpected error: %v", err)162+ }163+164+ if m.windowSize.Width != 0 || m.windowSize.Height != 0 {165+ t.Errorf("expected WindowSizeMsg 0x0 for non-TTY output, got %dx%d",166+ m.windowSize.Width, m.windowSize.Height)167+ }168+ })169+170+ t.Run("non-tty output preserves WithWindowSize when specified", func(t *testing.T) {171+ var buf bytes.Buffer172+ var in bytes.Buffer173+ m := &windowSizeModel{}174+175+ p := NewProgram(m,176+ WithInput(&in),177+ WithOutput(&buf),178+ WithWindowSize(120, 50),179+ )180+181+ if _, err := p.Run(); err != nil {182+ t.Fatalf("unexpected error: %v", err)183+ }184+185+ if m.windowSize.Width != 120 || m.windowSize.Height != 50 {186+ t.Errorf("expected WindowSizeMsg 120x50, got %dx%d",187+ m.windowSize.Width, m.windowSize.Height)188+ }189+ })190+}modified:tty.go+40 added
1 unmodified line2 3 import (4 "fmt"5 "os"6+ "strconv"7 "time"8 9 uv "github.com/charmbracelet/ultraviolet"10 "github.com/charmbracelet/x/term"93 unmodified lines104 // though it was not able to cancel the read.105 }106 }107 108+const (109+ defaultWidth = 80110+ defaultHeight = 24111+)112+113+// fallbackDimensions returns width and height, falling back to COLUMNS/LINES114+// environment variables or 80x24 defaults when dimensions are non-positive.115+func (p *Program) fallbackDimensions(width, height int) (int, int) {116+ if width <= 0 {117+ var cols string118+ if p != nil && p.environ != nil {119+ cols = p.environ.Getenv("COLUMNS")120+ } else {121+ cols = os.Getenv("COLUMNS")122+ }123+ if c, err := strconv.Atoi(cols); err == nil && c > 0 {124+ width = c125+ } else {126+ width = defaultWidth127+ }128+ }129+ if height <= 0 {130+ var lines string131+ if p != nil && p.environ != nil {132+ lines = p.environ.Getenv("LINES")133+ } else {134+ lines = os.Getenv("LINES")135+ }136+ if l, err := strconv.Atoi(lines); err == nil && l > 0 {137+ height = l138+ } else {139+ height = defaultHeight140+ }141+ }142+ return width, height143+}144+145 // checkResize detects the current size of the output and informs the program146 // via a WindowSizeMsg.147 func (p *Program) checkResize() {148 if p.ttyOutput == nil {10 unmodified lines159 160 return161 }162 163+ w, h = p.fallbackDimensions(w, h)164+165 p.width, p.height = w, h166 p.Send(WindowSizeMsg{Width: w, Height: h})167 }168 8 unmodified lines