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

+41 -27
View File
@@ -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]