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:
1 parent
52781ac4a8
commit
60257239d7
7 files changed
+111
-32
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
+41
-27
@@ -29,12 +29,9 @@ const (
|
||||
|
||||
var colTitles = [numCols]string{"Name", "IP address", "MAC address", "Vendor", "Last seen", "Ping", "Services"}
|
||||
|
||||
// Fixed-width columns; the others share the remaining space by weight.
|
||||
var (
|
||||
colFixed = map[column]int{colIP: 15, colMAC: 17, colLastSeen: 10, colRTT: 8}
|
||||
colMin = map[column]int{colName: 14, colVendor: 10, colServices: 10}
|
||||
colWeight = map[column]int{colName: 5, colVendor: 3, colServices: 3}
|
||||
)
|
||||
// colMin lists the columns that give up width when the table does not fit,
|
||||
// and how narrow they may get. The others always get what their content needs.
|
||||
var colMin = map[column]int{colName: 14, colVendor: 10, colServices: 10}
|
||||
|
||||
var sortOrder = []column{colName, colIP, colMAC, colVendor, colLastSeen, colRTT}
|
||||
|
||||
@@ -45,40 +42,57 @@ func nextSortColumn(c column) column {
|
||||
|
||||
const rowPrefix = 6 // "▸ ●⚑✓ "
|
||||
|
||||
func (m *model) layout() ([]column, []int) {
|
||||
// layout sizes every visible column to its widest cell. When the table does not
|
||||
// fit, the widest of the shrinkable columns are cut back first, so a long
|
||||
// service list does not squeeze a short name. Space left over goes to the last
|
||||
// column.
|
||||
func (m *model) layout(now time.Time) ([]column, []int) {
|
||||
var cols []column
|
||||
for c := range numCols {
|
||||
if m.cols[c] {
|
||||
cols = append(cols, c)
|
||||
}
|
||||
}
|
||||
avail := m.w - rowPrefix - 2*max(0, len(cols)-1)
|
||||
fixed, weights := 0, 0
|
||||
for _, c := range cols {
|
||||
if w, ok := colFixed[c]; ok {
|
||||
fixed += w
|
||||
} else {
|
||||
weights += colWeight[c]
|
||||
}
|
||||
}
|
||||
flex := max(0, avail-fixed)
|
||||
widths := make([]int, len(cols))
|
||||
lastFlex, used := -1, 0
|
||||
used := 0
|
||||
for i, c := range cols {
|
||||
if w, ok := colFixed[c]; ok {
|
||||
widths[i] = w
|
||||
continue
|
||||
w := runewidth.StringWidth(colTitles[c])
|
||||
if slices.Contains(sortOrder, c) {
|
||||
w += 2 // room for the sort arrow, so columns do not move with it
|
||||
}
|
||||
widths[i] = max(colMin[c], flex*colWeight[c]/weights)
|
||||
used += widths[i]
|
||||
lastFlex = i
|
||||
for _, d := range m.list {
|
||||
w = max(w, runewidth.StringWidth(m.cellText(d, c, now)))
|
||||
}
|
||||
widths[i] = w
|
||||
used += w
|
||||
}
|
||||
if lastFlex >= 0 && used < flex {
|
||||
widths[lastFlex] += flex - used
|
||||
avail := m.w - rowPrefix - 2*max(0, len(cols)-1)
|
||||
if used > avail {
|
||||
shrink(cols, widths, used-avail)
|
||||
} else if len(widths) > 0 {
|
||||
widths[len(widths)-1] += avail - used
|
||||
}
|
||||
return cols, widths
|
||||
}
|
||||
|
||||
// shrink takes over cells away from the shrinkable columns, one at a time from
|
||||
// the widest (the rightmost of equals, so names are cut last), and never below
|
||||
// colMin. If that is not enough, the rows are clipped at the terminal edge.
|
||||
func shrink(cols []column, widths []int, over int) {
|
||||
for ; over > 0; over-- {
|
||||
widest := -1
|
||||
for i, c := range cols {
|
||||
if lo, ok := colMin[c]; ok && widths[i] > lo && (widest < 0 || widths[i] >= widths[widest]) {
|
||||
widest = i
|
||||
}
|
||||
}
|
||||
if widest < 0 {
|
||||
return
|
||||
}
|
||||
widths[widest]--
|
||||
}
|
||||
}
|
||||
|
||||
// fit truncates or pads s to exactly w cells.
|
||||
func fit(s string, w int) string {
|
||||
if w <= 0 {
|
||||
@@ -192,7 +206,7 @@ func (m *model) viewList() string {
|
||||
}
|
||||
lines = append(lines, " "+status, " "+m.scanStatus(now), "")
|
||||
|
||||
cols, widths := m.layout()
|
||||
cols, widths := m.layout(now)
|
||||
header := strings.Repeat(" ", rowPrefix)
|
||||
for i, c := range cols {
|
||||
title := colTitles[c]
|
||||
|
||||
Reference in new issue
Block a user