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

+6
View File
@@ -1,5 +1,11 @@
# Changelog # Changelog
## 1.0.1 – 2026-10-26
- The device list sizes its columns to their content and gives the remaining width to
the services. When the terminal is too narrow, the widest of name, vendor and services
are shortened first.
## 1.0.0 – 2026-10-26 ## 1.0.0 – 2026-10-26
First release. First release.
+5 -3
View File
@@ -59,7 +59,7 @@ Download the file for your system from the
| Linux, ARM64 (e.g. Pi) | `nscan-linux-arm64` | | Linux, ARM64 (e.g. Pi) | `nscan-linux-arm64` |
```sh ```sh
curl -fLo nscan https://git.micw.org/mike/nscan/releases/download/1.0.0/nscan-darwin-arm64 curl -fLo nscan https://git.micw.org/mike/nscan/releases/download/1.0.1/nscan-darwin-arm64
chmod +x nscan chmod +x nscan
sudo mv nscan /usr/local/bin/ sudo mv nscan /usr/local/bin/
``` ```
@@ -103,7 +103,9 @@ nscan scan # scan once, print a table, exit
The list updates while a scan runs and rescans every minute. `●` marks devices that are The list updates while a scan runs and rescans every minute. `●` marks devices that are
online, `○` those that are gone (shown in cumulative mode), `⚑` tracked devices and `✓` online, `○` those that are gone (shown in cumulative mode), `⚑` tracked devices and `✓`
whitelisted ones. New devices and tracked devices that come or go appear in the event whitelisted ones. New devices and tracked devices that come or go appear in the event
line at the bottom and ring the terminal bell. line at the bottom and ring the terminal bell. Each column is as wide as its content, and
the services get the rest of the line. In a narrow terminal, the widest of name, vendor
and services give way first.
Press <kbd>enter</kbd> for everything nscan knows about a device. That includes all of its Press <kbd>enter</kbd> for everything nscan knows about a device. That includes all of its
Bonjour services and the open ports from the last port scan. Bonjour services and the open ports from the last port scan.
@@ -280,7 +282,7 @@ named `nscan-<os>-<arch>`, with the version from `version.txt` compiled in. A pl
[mgsh](https://git.micw.org/mike/mgsh), which attaches everything in `./bin`: [mgsh](https://git.micw.org/mike/mgsh), which attaches everything in `./bin`:
```sh ```sh
./build.sh && mgsh release 1.0.1 ./build.sh && mgsh release @micw $(cat version.txt)
``` ```
See [CHANGELOG.md](CHANGELOG.md) for what changed in each version. See [CHANGELOG.md](CHANGELOG.md) for what changed in each version.
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 422 KiB

After

Width:  |  Height:  |  Size: 423 KiB

+57
View File
@@ -3,12 +3,14 @@ package tui
import ( import (
"net" "net"
"net/netip" "net/netip"
"slices"
"strings" "strings"
"testing" "testing"
"time" "time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi" "github.com/charmbracelet/x/ansi"
"github.com/mattn/go-runewidth"
"nscan/internal/oui" "nscan/internal/oui"
"nscan/internal/scanner" "nscan/internal/scanner"
@@ -177,3 +179,58 @@ func TestStoredDevicesGetVendor(t *testing.T) {
t.Error("no hint about the missing vendor database") 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
View File
@@ -29,12 +29,9 @@ const (
var colTitles = [numCols]string{"Name", "IP address", "MAC address", "Vendor", "Last seen", "Ping", "Services"} 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. // colMin lists the columns that give up width when the table does not fit,
var ( // and how narrow they may get. The others always get what their content needs.
colFixed = map[column]int{colIP: 15, colMAC: 17, colLastSeen: 10, colRTT: 8} var colMin = map[column]int{colName: 14, colVendor: 10, colServices: 10}
colMin = map[column]int{colName: 14, colVendor: 10, colServices: 10}
colWeight = map[column]int{colName: 5, colVendor: 3, colServices: 3}
)
var sortOrder = []column{colName, colIP, colMAC, colVendor, colLastSeen, colRTT} var sortOrder = []column{colName, colIP, colMAC, colVendor, colLastSeen, colRTT}
@@ -45,40 +42,57 @@ func nextSortColumn(c column) column {
const rowPrefix = 6 // "▸ ●⚑✓ " 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 var cols []column
for c := range numCols { for c := range numCols {
if m.cols[c] { if m.cols[c] {
cols = append(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)) widths := make([]int, len(cols))
lastFlex, used := -1, 0 used := 0
for i, c := range cols { for i, c := range cols {
if w, ok := colFixed[c]; ok { w := runewidth.StringWidth(colTitles[c])
if slices.Contains(sortOrder, c) {
w += 2 // room for the sort arrow, so columns do not move with it
}
for _, d := range m.list {
w = max(w, runewidth.StringWidth(m.cellText(d, c, now)))
}
widths[i] = w widths[i] = w
continue used += w
} }
widths[i] = max(colMin[c], flex*colWeight[c]/weights) avail := m.w - rowPrefix - 2*max(0, len(cols)-1)
used += widths[i] if used > avail {
lastFlex = i shrink(cols, widths, used-avail)
} } else if len(widths) > 0 {
if lastFlex >= 0 && used < flex { widths[len(widths)-1] += avail - used
widths[lastFlex] += flex - used
} }
return cols, widths 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. // fit truncates or pads s to exactly w cells.
func fit(s string, w int) string { func fit(s string, w int) string {
if w <= 0 { if w <= 0 {
@@ -192,7 +206,7 @@ func (m *model) viewList() string {
} }
lines = append(lines, " "+status, " "+m.scanStatus(now), "") lines = append(lines, " "+status, " "+m.scanStatus(now), "")
cols, widths := m.layout() cols, widths := m.layout(now)
header := strings.Repeat(" ", rowPrefix) header := strings.Repeat(" ", rowPrefix)
for i, c := range cols { for i, c := range cols {
title := colTitles[c] title := colTitles[c]
+1 -1
View File
@@ -3,4 +3,4 @@ package main
// Version is a var, not a const, so build.sh can put the built number in via // Version is a var, not a const, so build.sh can put the built number in via
// -ldflags "-X main.Version=…". The value here only shows up in a bare // -ldflags "-X main.Version=…". The value here only shows up in a bare
// `go build`; the version actually built is the one in version.txt. // `go build`; the version actually built is the one in version.txt.
var Version = "1.0.0" var Version = "1.0.1"
+1 -1
View File
@@ -1 +1 @@
1.0.0 1.0.1