Skip to content

Commit 2e93b80

Browse files
committed
fix(tui): use field defaults for blank numeric inputs
1 parent 0f3b0b6 commit 2e93b80

3 files changed

Lines changed: 59 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,8 @@ To compare two results files you already have, without scanning, use `subenum di
624624

625625
No flags required. Fill in the form and press `ctrl+r` to start scanning. Last-used values are saved to `last.json` in your user config directory (`~/.config/subenum/` on Linux, `~/Library/Application Support/subenum/` on macOS, `%AppData%\subenum\` on Windows) and restored on next launch.
626626

627+
Leaving a numeric field blank uses its built-in default.
628+
627629
<img src="docs/assets/tui-form.png" alt="subenum interactive TUI" width="640"/>
628630

629631
<details>

‎internal/tui/form.go‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,14 @@ func (m *formModel) validate() (formValues, string) {
550550

551551
// The form only parses its text fields here; ranges and the resolver are
552552
// checked by scan.Options.Validate, the same validator the CLI uses (#80).
553-
num := func(f int, name string, blank int) int {
553+
// Blank values use the same defaults as remembered().
554+
num := func(f int, name string) int {
554555
s := strings.TrimSpace(text(f))
555-
if err != nil || s == "" {
556-
keep(f, strconv.Itoa(blank))
557-
return blank
556+
if err != nil {
557+
return 0
558+
}
559+
if s == "" {
560+
s = formFields[f].def
558561
}
559562
n, perr := strconv.Atoi(s)
560563
if perr != nil {
@@ -563,22 +566,22 @@ func (m *formModel) validate() (formValues, string) {
563566
keep(f, strconv.Itoa(n))
564567
return n
565568
}
566-
concurrency := num(fieldConcurrency, "Concurrency", 0)
567-
timeout := num(fieldTimeout, "Timeout", 0)
568-
attempts := num(fieldAttempts, "Attempts", 0)
569+
concurrency := num(fieldConcurrency, "Concurrency")
570+
timeout := num(fieldTimeout, "Timeout")
571+
attempts := num(fieldAttempts, "Attempts")
569572
// Hit rate only matters in simulation mode, and depth only when
570573
// recursive is on; otherwise their fields must not block the scan.
571574
hitRate := scan.DefaultHitRate
572575
if m.shown(fieldHitRate) {
573-
hitRate = num(fieldHitRate, "Hit rate", 0)
576+
hitRate = num(fieldHitRate, "Hit rate")
574577
}
575578
depth := 1
576579
if m.shown(fieldDepth) {
577-
depth = num(fieldDepth, "Depth", 1)
580+
depth = num(fieldDepth, "Depth")
578581
}
579-
rate := num(fieldRate, "Rate", 0) // blank means unlimited
580-
maxQueries := num(fieldMaxQueries, "Max queries", 0) // blank means unlimited
581-
ratePerResolver := num(fieldRatePerResolver, formFields[fieldRatePerResolver].name(), 0)
582+
rate := num(fieldRate, "Rate") // blank means unlimited
583+
maxQueries := num(fieldMaxQueries, "Max queries") // blank means unlimited
584+
ratePerResolver := num(fieldRatePerResolver, formFields[fieldRatePerResolver].name())
582585
var maxDuration time.Duration // blank means unlimited, as -max-duration 0
583586
if s := strings.TrimSpace(text(fieldMaxDuration)); err == nil && s != "" {
584587
var perr error

‎internal/tui/form_test.go‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func TestValidateNonPositiveConcurrency(t *testing.T) {
4545
}
4646
}
4747

48-
// Empty hit rate must be accepted when Simulate is OFF (it is never used),
49-
// and rejected when Simulate is ON.
48+
// Empty hit rate uses the default; explicit invalid values only matter
49+
// when Simulate is ON.
5050
func TestValidateHitRateOnlyWhenSimulate(t *testing.T) {
5151
live := newFormModel(savedConfig{})
5252
live.inputs[fieldDomain].SetValue("example.com")
@@ -64,8 +64,8 @@ func TestValidateHitRateOnlyWhenSimulate(t *testing.T) {
6464
sim.inputs[fieldDomain].SetValue("example.com")
6565
sim.inputs[fieldHitRate].SetValue("") // blank hit rate
6666
sim.toggles[fieldSimulate] = true
67-
if _, errStr := sim.validate(); errStr == "" {
68-
t.Error("simulate mode should reject blank hit rate")
67+
if vals, errStr := sim.validate(); errStr != "" || vals.HitRate != scan.DefaultHitRate {
68+
t.Errorf("simulate mode with blank hit rate: rate %d, error %q", vals.HitRate, errStr)
6969
}
7070

7171
sim.inputs[fieldHitRate].SetValue("500") // out of range
@@ -74,6 +74,44 @@ func TestValidateHitRateOnlyWhenSimulate(t *testing.T) {
7474
}
7575
}
7676

77+
func TestValidateBlankNumericFieldsUseDefaults(t *testing.T) {
78+
for _, field := range []int{fieldConcurrency, fieldTimeout, fieldAttempts, fieldHitRate,
79+
fieldDepth, fieldRate, fieldMaxQueries, fieldRatePerResolver} {
80+
t.Run(formFields[field].name(), func(t *testing.T) {
81+
for _, blank := range []string{"", " \t "} {
82+
m := newFormModel(savedConfig{Domain: "example.com", Simulate: true, Recursive: true})
83+
want, errStr := m.validate()
84+
if errStr != "" {
85+
t.Fatal(errStr)
86+
}
87+
m.inputs[field].SetValue(blank)
88+
got, errStr := m.validate()
89+
if errStr != "" {
90+
t.Errorf("blank %q: %s", blank, errStr)
91+
continue
92+
}
93+
if !reflect.DeepEqual(got.Options, want.Options) || !reflect.DeepEqual(got.saved, want.saved) {
94+
t.Errorf("blank %q changed scan or saved defaults: got %+v, want %+v", blank, got, want)
95+
}
96+
}
97+
})
98+
}
99+
}
100+
101+
func TestValidateExplicitInvalidNumericFields(t *testing.T) {
102+
for _, field := range []int{fieldConcurrency, fieldTimeout, fieldAttempts, fieldHitRate} {
103+
t.Run(formFields[field].name(), func(t *testing.T) {
104+
for _, value := range []string{"0", "-1", "not-a-number"} {
105+
m := newFormModel(savedConfig{Domain: "example.com", Simulate: true})
106+
m.inputs[field].SetValue(value)
107+
if _, errStr := m.validate(); errStr == "" {
108+
t.Errorf("explicit %q must still fail validation", value)
109+
}
110+
}
111+
})
112+
}
113+
}
114+
77115
func TestMoveFocusSkipsGatedFields(t *testing.T) {
78116
m := newFormModel(savedConfig{})
79117

0 commit comments

Comments
 (0)