Skip to content
Myr

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

3 files+231 added−1 removed

modified:tea.go+1 added-1 removed
1055 unmodified lines
1056 if err != nil {1057 return p.initialModel, fmt.Errorf("bubbletea: error getting terminal size: %w", err)1058 }10591060 width, height = w, h1060 width, height = p.fallbackDimensions(w, h)1061 }10621063 p.width, p.height = width, height1064 resizeMsg := WindowSizeMsg{Width: p.width, Height: p.height}
402 unmodified lines
added:tty_test.go+190 added
1package tea23import (4 "bytes"5 "testing"6)78type windowSizeModel struct {9 windowSize WindowSizeMsg10 quit bool11}1213func (m *windowSizeModel) Init() Cmd {14 return nil15}1617func (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}2627func (m *windowSizeModel) View() View {28 return NewView("")29}3031func TestFallbackDimensions(t *testing.T) {32 t.Run("default fallback to 80x24", func(t *testing.T) {33 t.Setenv("COLUMNS", "")34 t.Setenv("LINES", "")3536 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 }4142 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 })4748 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 })5556 t.Run("mixed non-positive dimensions", func(t *testing.T) {57 t.Setenv("COLUMNS", "")58 t.Setenv("LINES", "")5960 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 }6566 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 })7172 t.Run("fallback to COLUMNS and LINES from os env", func(t *testing.T) {73 t.Setenv("COLUMNS", "132")74 t.Setenv("LINES", "43")7576 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 })8283 t.Run("fallback to COLUMNS and LINES from program environment", func(t *testing.T) {84 t.Setenv("COLUMNS", "200")85 t.Setenv("LINES", "100")8687 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 })9394 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")9798 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 })104105 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")108109 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 }114115 t.Setenv("COLUMNS", "0")116 t.Setenv("LINES", "0")117118 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}124125func 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")129130 var buf bytes.Buffer131 var in bytes.Buffer132 m := &windowSizeModel{}133134 p := NewProgram(m,135 WithInput(&in),136 WithOutput(&buf),137 )138139 if _, err := p.Run(); err != nil {140 t.Fatalf("unexpected error: %v", err)141 }142143 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 })148149 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{}153154 p := NewProgram(m,155 WithInput(&in),156 WithOutput(&buf),157 WithEnvironment([]string{"COLUMNS=150", "LINES=60"}),158 )159160 if _, err := p.Run(); err != nil {161 t.Fatalf("unexpected error: %v", err)162 }163164 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 })169170 t.Run("non-tty output preserves WithWindowSize when specified", func(t *testing.T) {171 var buf bytes.Buffer172 var in bytes.Buffer173 m := &windowSizeModel{}174175 p := NewProgram(m,176 WithInput(&in),177 WithOutput(&buf),178 WithWindowSize(120, 50),179 )180181 if _, err := p.Run(); err != nil {182 t.Fatalf("unexpected error: %v", err)183 }184185 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 line
23import (4 "fmt"5 "os"6 "strconv"7 "time"89 uv "github.com/charmbracelet/ultraviolet"10 "github.com/charmbracelet/x/term"
93 unmodified lines
104 // though it was not able to cancel the read.105 }106}107108const (109 defaultWidth = 80110 defaultHeight = 24111)112113// fallbackDimensions returns width and height, falling back to COLUMNS/LINES114// environment variables or 80x24 defaults when dimensions are non-positive.115func (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}144145// checkResize detects the current size of the output and informs the program146// via a WindowSizeMsg.147func (p *Program) checkResize() {148 if p.ttyOutput == nil {
10 unmodified lines
159160 return161 }162163 w, h = p.fallbackDimensions(w, h)164165 p.width, p.height = w, h166 p.Send(WindowSizeMsg{Width: w, Height: h})167}168
8 unmodified lines