summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-05 19:27:59 +0200
committerPaul Buetow <paul@buetow.org>2026-03-05 19:27:59 +0200
commita44f6ee30c11963552b5b90a19698873aa9b6b6d (patch)
tree68f80b09a7436c0059d97186f9a493c0d4b9b508 /internal
parentc3adb86fda302eeb90c2b07e56aae5abaea469ce (diff)
refactor(tui): simplify viewport sizing defaults
Diffstat (limited to 'internal')
-rw-r--r--internal/tui/common/viewport.go22
-rw-r--r--internal/tui/common/viewport_test.go49
2 files changed, 50 insertions, 21 deletions
diff --git a/internal/tui/common/viewport.go b/internal/tui/common/viewport.go
index e1729db..099a4e1 100644
--- a/internal/tui/common/viewport.go
+++ b/internal/tui/common/viewport.go
@@ -1,33 +1,13 @@
package common
-import (
- "os"
-
- xterm "github.com/charmbracelet/x/term"
-)
-
const (
defaultViewportWidth = 80
defaultViewportHeight = 24
)
// EffectiveViewport returns a usable terminal viewport size. Missing or invalid
-// dimensions are resolved from the active terminal when possible.
+// dimensions fall back to defaults.
func EffectiveViewport(width, height int) (int, int) {
- if width > 0 && height > 0 {
- return width, height
- }
-
- termWidth, termHeight, err := xterm.GetSize(os.Stdout.Fd())
- if err == nil {
- if width <= 0 && termWidth > 0 {
- width = termWidth
- }
- if height <= 0 && termHeight > 0 {
- height = termHeight
- }
- }
-
if width <= 0 {
width = defaultViewportWidth
}
diff --git a/internal/tui/common/viewport_test.go b/internal/tui/common/viewport_test.go
new file mode 100644
index 0000000..c90f046
--- /dev/null
+++ b/internal/tui/common/viewport_test.go
@@ -0,0 +1,49 @@
+package common
+
+import "testing"
+
+func TestEffectiveViewport(t *testing.T) {
+ tests := []struct {
+ name string
+ width int
+ height int
+ wantWidth int
+ wantHeight int
+ }{
+ {
+ name: "provided dimensions",
+ width: 120,
+ height: 40,
+ wantWidth: 120,
+ wantHeight: 40,
+ },
+ {
+ name: "both missing use defaults",
+ width: 0,
+ height: 0,
+ wantWidth: defaultViewportWidth,
+ wantHeight: defaultViewportHeight,
+ },
+ {
+ name: "missing height uses default",
+ width: 100,
+ height: 0,
+ wantWidth: 100,
+ wantHeight: defaultViewportHeight,
+ },
+ {
+ name: "missing width uses default",
+ width: -1,
+ height: 30,
+ wantWidth: defaultViewportWidth,
+ wantHeight: 30,
+ },
+ }
+
+ for _, tt := range tests {
+ gotWidth, gotHeight := EffectiveViewport(tt.width, tt.height)
+ if gotWidth != tt.wantWidth || gotHeight != tt.wantHeight {
+ t.Fatalf("%s: got (%d,%d), want (%d,%d)", tt.name, gotWidth, gotHeight, tt.wantWidth, tt.wantHeight)
+ }
+ }
+}