summaryrefslogtreecommitdiff
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
parentc3adb86fda302eeb90c2b07e56aae5abaea469ce (diff)
refactor(tui): simplify viewport sizing defaults
-rw-r--r--go.mod2
-rw-r--r--internal/tui/common/viewport.go22
-rw-r--r--internal/tui/common/viewport_test.go49
3 files changed, 51 insertions, 22 deletions
diff --git a/go.mod b/go.mod
index 2644495..56ad791 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,6 @@ require (
charm.land/lipgloss/v2 v2.0.0
github.com/DataDog/zstd v1.5.7
github.com/aquasecurity/libbpfgo v0.6.0-libbpf-1.3.0.20240111220235-90dbffffbdab
- github.com/charmbracelet/x/term v0.2.2
github.com/magefile/mage v1.15.0
)
@@ -17,6 +16,7 @@ require (
github.com/charmbracelet/colorprofile v0.4.2 // indirect
github.com/charmbracelet/ultraviolet v0.0.0-20260205113103-524a6607adb8 // indirect
github.com/charmbracelet/x/ansi v0.11.6 // indirect
+ github.com/charmbracelet/x/term v0.2.2 // indirect
github.com/charmbracelet/x/termios v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.2.2 // indirect
github.com/clipperhouse/displaywidth v0.11.0 // indirect
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)
+ }
+ }
+}