Skip to content

Commit d826821

Browse files
committed
Fit the TUI form to the terminal height (#287)
The form drew all 31 lines whatever the terminal's height, so on an 80x24 terminal the top rows, or the error and key hints, were off screen. The form now records the height from the window size message. When it does not fit it drops the logo, then draws a window of field rows that contains the focused one, with "N more fields" lines above and below; the error line and key hints are always drawn. The form reopened with r after a scan is given the current size as well. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Fixes #287
1 parent 731f371 commit d826821

4 files changed

Lines changed: 194 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5252
- Tests no longer flake under load: lab end-to-end runs use a 5 s timeout with 3 attempts and fail with a clear message if the lab 2 wildcard probe times out, the resolver-pool spread test counts lookups per member instead of timing the scan (the timing view is now `BenchmarkPoolSpreadsLoad`), and CI runs tests in shuffled order (#167).
5353

5454
### Fixed
55+
- The TUI configuration form fits a terminal shorter than its 31 lines, such as the standard 80×24: the logo is dropped, then the field rows scroll to keep the focused field in view, with `↑ N more fields` and `↓ N more fields` lines for the rest. The validation error and the key hints are always shown, so a rejected ctrl+r no longer fails without a visible reason (#287).
5556
- The run-quality report counts the `-permute` pass: a target's and the run's `queries_sent`, and so `achieved_qps`, left its queries out, often most of the traffic sent. A permutation pass that went worse than the main pass (failed lookups, a spent `-max-queries` or `-max-duration` budget, a reliability abort, or a pass that could not start) now sets the target's and the run's `verdict`, with a `reason` starting `permutation pass:`, so a job gated on the verdict no longer passes on it. The report's `permutation` entry gains the pass's own `processed` and `queries_sent` (#298).
5657
- A run with `-diff` and `-o` on the same file that is interrupted (or stopped by `-max-duration` or the reliability guard) and saves a resume state now leaves that file as it was, instead of replacing it with its partial results. The resumed run compared against the partial file, so it reported names that were already known as added, exited 4 with nothing changed and could not report removals. The partial results are in the resume state, and the resumed run writes the full file; `-resume` now also refuses to continue if that file changed in between (#297).
5758
- A resumed `-recursive` scan checks each name found before the interrupt for wildcard DNS before expanding it, as a live scan does. A wildcard subzone, or one whose check was inconclusive, is skipped with the same notice instead of reporting every wordlist entry under it as a result, so an interrupted-then-resumed scan reports the same names as an uninterrupted one (#296).

‎internal/tui/form.go‎

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ type formModel struct {
216216
focus int
217217
err string
218218
loading bool // wordlist is loading in the background; ctrl+r is ignored
219-
width int
219+
width int // terminal size, 0 until the first size message
220+
height int
220221
}
221222

222223
// newFormModel creates a form, optionally pre-seeded from a saved config.
@@ -272,7 +273,7 @@ func (m *formModel) moveFocus(delta int) {
272273
func (m formModel) Update(msg tea.Msg) (formModel, tea.Cmd) {
273274
switch msg := msg.(type) {
274275
case tea.WindowSizeMsg:
275-
m.width = msg.Width
276+
m.width, m.height = msg.Width, msg.Height
276277

277278
case tea.KeyPressMsg:
278279
switch msg.String() {
@@ -311,18 +312,105 @@ func (m formModel) shiftFocus(delta int) (formModel, tea.Cmd) {
311312
return m, nil
312313
}
313314

315+
// View renders the form. On a terminal too short for all of it the logo is
316+
// dropped, then the field rows scroll to keep the focused one in view; the
317+
// error or loading line and the key hints are always drawn (#287).
314318
func (m formModel) View() string {
319+
title := titleStyle.Render("Configure Scan") + "\n"
320+
header := logo() + "\n\n" + title
321+
rows := m.rows()
322+
footer := m.footer()
323+
324+
heights := make([]int, len(rows))
325+
total := 0
326+
for i, r := range rows {
327+
heights[i] = m.lines(r)
328+
total += heights[i]
329+
}
330+
// Before the first size message the height is unknown: draw everything.
331+
if m.height <= 0 || m.lines(header)+total+m.lines(footer) <= m.height {
332+
return header + strings.Join(rows, "\n") + "\n" + footer
333+
}
334+
header = title
335+
avail := m.height - m.lines(header) - m.lines(footer)
336+
if total <= avail {
337+
return header + strings.Join(rows, "\n") + "\n" + footer
338+
}
339+
340+
// Two of the lines left say how many fields are off each end.
341+
lo, hi := rowWindow(heights, m.focus, max(1, avail-2))
315342
var b strings.Builder
343+
b.WriteString(header)
344+
if lo > 0 {
345+
b.WriteString(dimStyle.Render(fmt.Sprintf(" ↑ %d more fields", lo)))
346+
}
347+
b.WriteString("\n" + strings.Join(rows[lo:hi], "\n") + "\n")
348+
if hi < len(rows) {
349+
b.WriteString(dimStyle.Render(fmt.Sprintf(" ↓ %d more fields", len(rows)-hi)))
350+
}
351+
b.WriteString("\n" + footer)
352+
return b.String()
353+
}
354+
355+
// lines counts the terminal lines s takes, a line wider than the terminal
356+
// wrapping onto the next. A trailing newline does not start another line.
357+
func (m formModel) lines(s string) int {
358+
n := 0
359+
for _, l := range strings.Split(strings.TrimSuffix(s, "\n"), "\n") {
360+
n++
361+
if m.width > 0 {
362+
n += max(0, lipgloss.Width(l)-1) / m.width
363+
}
364+
}
365+
return n
366+
}
316367

317-
b.WriteString(logo() + "\n\n")
318-
b.WriteString(titleStyle.Render("Configure Scan") + "\n")
368+
// rowWindow picks the rows [lo, hi) to draw in avail lines so that row focus
369+
// is among them. It keeps to the top of the form while focus fits there, then
370+
// to the bottom, and otherwise grows the window around focus.
371+
func rowWindow(heights []int, focus, avail int) (lo, hi int) {
372+
used := 0
373+
for hi < len(heights) && used+heights[hi] <= avail {
374+
used += heights[hi]
375+
hi++
376+
}
377+
if focus < hi {
378+
return 0, hi
379+
}
380+
lo, hi, used = len(heights), len(heights), 0
381+
for lo > 0 && used+heights[lo-1] <= avail {
382+
lo--
383+
used += heights[lo]
384+
}
385+
if focus >= lo {
386+
return lo, hi
387+
}
388+
lo, hi, used = focus, focus+1, heights[focus]
389+
for grew := true; grew; {
390+
grew = false
391+
if lo > 0 && used+heights[lo-1] <= avail {
392+
lo--
393+
used += heights[lo]
394+
grew = true
395+
}
396+
if hi < len(heights) && used+heights[hi] <= avail {
397+
used += heights[hi]
398+
hi++
399+
grew = true
400+
}
401+
}
402+
return lo, hi
403+
}
319404

405+
// rows renders one line per form field, indexed by the field constants.
406+
func (m formModel) rows() []string {
407+
rows := make([]string, 0, fieldCount)
320408
for f, spec := range formFields {
321409
if !m.shown(f) {
322410
gate := formFields[spec.gate].name()
323411
// The label's width plus the "> " of a text field, so the hint
324412
// lines up with the values.
325-
b.WriteString(dimmedRow.Render(fmt.Sprintf(" %-22s(enable %s)", spec.name()+":", gate)) + "\n")
413+
rows = append(rows, dimmedRow.Render(fmt.Sprintf(" %-22s(enable %s)", spec.name()+":", gate)))
326414
continue
327415
}
328416
var content string
@@ -336,11 +424,18 @@ func (m formModel) View() string {
336424
}
337425
lbl := labelStyle.Render(spec.name() + ":")
338426
if m.focus == f {
339-
b.WriteString(focusedStyle.Render("▸ ") + lbl + content + "\n")
427+
rows = append(rows, focusedStyle.Render("▸ ")+lbl+content)
340428
} else {
341-
b.WriteString(" " + lbl + content + "\n")
429+
rows = append(rows, " "+lbl+content)
342430
}
343431
}
432+
return rows
433+
}
434+
435+
// footer renders what follows the field rows: the validation error or the
436+
// loading line, if any, and the key hints.
437+
func (m formModel) footer() string {
438+
var b strings.Builder
344439

345440
if m.err != "" {
346441
b.WriteString("\n" + errorStyle.Render(" ✗ "+m.err) + "\n")

‎internal/tui/form_keys_test.go‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package tui
22

33
import (
44
"context"
5+
"regexp"
56
"strings"
67
"testing"
78

89
tea "charm.land/bubbletea/v2"
10+
"charm.land/lipgloss/v2"
911
)
1012

1113
// Key handling for the form and the root model (#172).
@@ -243,3 +245,90 @@ func TestModelScanKeys(t *testing.T) {
243245
t.Fatal("r after done does not show the form")
244246
}
245247
}
248+
249+
// formLines is the form's view as the lines a terminal shows, colours removed.
250+
func formLines(m formModel) []string {
251+
ansi := regexp.MustCompile("\x1b\\[[0-9;?]*[a-zA-Z]")
252+
return strings.Split(ansi.ReplaceAllString(m.View(), ""), "\n")
253+
}
254+
255+
// On a terminal shorter than the form, the fields scroll: the focused field,
256+
// the validation error and the key hints stay on screen (#287).
257+
func TestFormFitsShortTerminal(t *testing.T) {
258+
const width, height = 80, 24
259+
m := newFormModel(savedConfig{})
260+
m, _ = m.Update(tea.WindowSizeMsg{Width: width, Height: height})
261+
m.err = "Domain is required"
262+
263+
check := func(step string) {
264+
t.Helper()
265+
lines := formLines(m)
266+
if len(lines) > height {
267+
t.Fatalf("%s: view is %d lines on a %d-line terminal", step, len(lines), height)
268+
}
269+
v := strings.Join(lines, "\n")
270+
for _, want := range []string{"▸ " + formFields[m.focus].name() + ":", "✗ Domain is required", "ctrl+r run"} {
271+
if !strings.Contains(v, want) {
272+
t.Fatalf("%s: view lacks %q:\n%s", step, want, v)
273+
}
274+
}
275+
}
276+
277+
check("start")
278+
if v := strings.Join(formLines(m), "\n"); !strings.Contains(v, "more fields") || strings.Contains(v, "Retry Inconclusive") {
279+
t.Errorf("the fields below the window are not announced:\n%s", v)
280+
}
281+
// Every field less the 2 gated ones: that many tabs are back on the domain.
282+
for range fieldCount - 2 {
283+
m, _ = m.Update(keyTab)
284+
check("tab to " + formFields[m.focus].name())
285+
}
286+
if m.focus != fieldDomain {
287+
t.Fatalf("focus = %d after a full round, want the domain", m.focus)
288+
}
289+
for range fieldCount - 2 {
290+
m, _ = m.Update(keyShiftTab)
291+
check("shift+tab to " + formFields[m.focus].name())
292+
}
293+
294+
// A value wider than the terminal wraps onto more lines; they are counted.
295+
m.focus = fieldExclude
296+
m.inputs[fieldExclude].SetValue(strings.Repeat("a.example.com,", 20))
297+
rows := 0
298+
for _, l := range formLines(m) {
299+
rows += max(1, (lipgloss.Width(l)+width-1)/width)
300+
}
301+
if rows > height {
302+
t.Errorf("with a wrapped value the view takes %d terminal lines, want at most %d", rows, height)
303+
}
304+
check("wrapped value")
305+
306+
// Much too short for any layout: the focused field is still shown.
307+
m, _ = m.Update(tea.WindowSizeMsg{Width: width, Height: 3})
308+
if v := strings.Join(formLines(m), "\n"); !strings.Contains(v, "▸ "+formFields[m.focus].name()+":") {
309+
t.Errorf("3-line terminal: the focused field is not shown:\n%s", v)
310+
}
311+
312+
// A terminal tall enough shows the whole form, as does an unknown size.
313+
want := strings.Join(formLines(newFormModel(savedConfig{})), "\n")
314+
tall, _ := newFormModel(savedConfig{}).Update(tea.WindowSizeMsg{Width: width, Height: 40})
315+
if got := strings.Join(formLines(tall), "\n"); got != want {
316+
t.Errorf("40-line terminal does not show the whole form:\n%s", got)
317+
}
318+
}
319+
320+
// The form opened by r after a scan knows the terminal size: no resize message
321+
// follows it (#287).
322+
func TestFormAfterScanKeepsTerminalSize(t *testing.T) {
323+
redirectConfig(t)
324+
m := Model{state: stateScan, scanView: newScanViewModel(80, 24, false), width: 80, height: 24}
325+
m.scanView.done = true
326+
next, _ := m.Update(typeText("r"))
327+
fm := next.(Model)
328+
if fm.state != stateForm {
329+
t.Fatalf("r after done: state=%v", fm.state)
330+
}
331+
if n := len(formLines(fm.form)); n > 24 {
332+
t.Errorf("form after a scan is %d lines on a 24-line terminal", n)
333+
}
334+
}

‎internal/tui/model.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) {
320320
saved, _ := loadSavedConfig()
321321
m.state = stateForm
322322
m.form = newFormModel(saved)
323+
// No size message follows, and the form scrolls by it (#287).
324+
m.form.width, m.form.height = m.width, m.height
323325
m.events = nil
324326
return m, m.form.initCmd()
325327
}

0 commit comments

Comments
 (0)