diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c9e559..38d9671 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # 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 First release. diff --git a/README.md b/README.md index a7b13af..c4668a7 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Download the file for your system from the | Linux, ARM64 (e.g. Pi) | `nscan-linux-arm64` | ```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 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 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 -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 enter for everything nscan knows about a device. That includes all of its Bonjour services and the open ports from the last port scan. @@ -280,7 +282,7 @@ named `nscan--`, with the version from `version.txt` compiled in. A pl [mgsh](https://git.micw.org/mike/mgsh), which attaches everything in `./bin`: ```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. diff --git a/docs/list.png b/docs/list.png index 4b7cd12..977cba6 100644 Binary files a/docs/list.png and b/docs/list.png differ diff --git a/internal/tui/model_test.go b/internal/tui/model_test.go index 7534bde..77cdeda 100644 --- a/internal/tui/model_test.go +++ b/internal/tui/model_test.go @@ -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) + } +} diff --git a/internal/tui/view.go b/internal/tui/view.go index 5ce731d..597089a 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -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] diff --git a/version.go b/version.go index a75bd83..754479e 100644 --- a/version.go +++ b/version.go @@ -3,4 +3,4 @@ package main // 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 // `go build`; the version actually built is the one in version.txt. -var Version = "1.0.0" +var Version = "1.0.1" diff --git a/version.txt b/version.txt index 3eefcb9..7dea76e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 +1.0.1