Size table columns to their content, release 1.0.1

Each column of the device list is as wide as its widest cell and the
services get the rest of the line, instead of fixed widths and weights.
When the terminal is too narrow, the widest of name, vendor and services
are shortened first, never below their minimum.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mikeandClaude Opus 5.5 committed 2026-10-26 16:16:59 +01:00
1 parent 52781ac4a8
commit 60257239d7
7 files changed
+111 -32

No files matched your search

+57
View File
@@ -3,12 +3,14 @@ package tui
import (
"net"
"net/netip"
"slices"
"strings"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/mattn/go-runewidth"
"nscan/internal/oui"
"nscan/internal/scanner"
@@ -177,3 +179,58 @@ func TestStoredDevicesGetVendor(t *testing.T) {
t.Error("no hint about the missing vendor database")
}
}
func TestLayoutFollowsContent(t *testing.T) {
m := testModel(t)
gw := host("192.168.1.1", "3c:a6:2f:00:00:01")
gw.Gateway = true
pi := host("192.168.1.33", "b8:27:eb:00:00:03")
pi.MDNSName = "raspberrypi"
for _, typ := range []string{"_ssh._tcp", "_sftp-ssh._tcp", "_smb._tcp", "_http._tcp", "_workstation._tcp", "_rfb._tcp", "_afpovertcp._tcp", "_device-info._tcp"} {
pi.Services = append(pi.Services, scanner.Service{Type: typ, Instance: "raspberrypi"})
}
m.upsert(gw)
m.upsert(pi)
m.refresh()
width := func(cols []column, widths []int, c column) int {
return widths[slices.Index(cols, c)]
}
total := func(widths []int) int {
n := rowPrefix + 2*(len(widths)-1)
for _, w := range widths {
n += w
}
return n
}
services := runewidth.StringWidth(serviceSummary(pi.Services))
// Wide enough: every column as wide as its content, the rest for services.
m.Update(tea.WindowSizeMsg{Width: 200, Height: 30})
cols, widths := m.layout(time.Now())
if got := width(cols, widths, colName); got != len("raspberrypi") {
t.Errorf("name column %d wide, want %d", got, len("raspberrypi"))
}
if got := width(cols, widths, colIP); got != len("192.168.1.33") {
t.Errorf("IP column %d wide", got)
}
if got := width(cols, widths, colServices); got <= services {
t.Errorf("services column %d wide, want more than its content (%d)", got, services)
}
if got := total(widths); got != 200 {
t.Errorf("table %d wide, want 200", got)
}
// Too narrow: the long service list gives up width, the short name does not.
m.Update(tea.WindowSizeMsg{Width: 100, Height: 30})
cols, widths = m.layout(time.Now())
if got := width(cols, widths, colName); got != len("raspberrypi") {
t.Errorf("name column %d wide at 100 columns, want %d", got, len("raspberrypi"))
}
if got := width(cols, widths, colServices); got >= services || got < colMin[colServices] {
t.Errorf("services column %d wide at 100 columns", got)
}
if got := total(widths); got != 100 {
t.Errorf("table %d wide, want 100", got)
}
}