Skip to content

Commit e21e29f

Browse files
committed
Add input validation for switches
1 parent 5f38409 commit e21e29f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lua/neogit/lib/popup/builder.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ local M = {}
5454
---@field type string
5555
---@field user_input boolean
5656
---@field value string?
57+
---@field validate fun(string): boolean
58+
---@field validate_help string|nil
5759

5860
---@class PopupConfig
5961
---@field id string
@@ -90,6 +92,8 @@ local M = {}
9092
---@field value? string Allows for pre-building cli flags that can be customized by user input
9193
---@field user_input? boolean If true, allows user to customize the value of the cli flag
9294
---@field dependent? string[] other switches/options with a state dependency on this one
95+
---@field validate? fun(string): boolean
96+
---@field validate_help? string
9397

9498
---@class PopupOptionOpts
9599
---@field key_prefix? string Allows overwriting the default '=' to set option
@@ -252,6 +256,10 @@ function M:switch(key, cli, description, opts)
252256
internal = opts.internal,
253257
cli_prefix = opts.cli_prefix,
254258
user_input = opts.user_input,
259+
validate = opts.validate or function()
260+
return true
261+
end,
262+
validate_help = opts.validate_help,
255263
cli_suffix = opts.cli_suffix,
256264
options = opts.options,
257265
incompatible = util.build_reverse_lookup(opts.incompatible),

lua/neogit/lib/popup/init.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,17 @@ function M:toggle_switch(switch)
119119
if switch.user_input then
120120
if switch.enabled then
121121
local value = input.get_user_input(switch.cli_prefix .. switch.cli_base, { separator = "" })
122-
if value then
123-
switch.cli = switch.cli_base .. value
122+
if value and value ~= "" then
123+
if switch.validate(value) then
124+
switch.cli = switch.cli_base .. value
125+
else
126+
switch.enabled = false
127+
switch.cli = switch.cli_base
128+
notification.warn(switch.validate_help)
129+
end
130+
else
131+
switch.enabled = false
132+
switch.cli = switch.cli_base
124133
end
125134
else
126135
switch.cli = switch.cli_base

lua/neogit/popups/log/init.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ function M.create()
1919
:option("F", "grep", "", "Search messages", { key_prefix = "-" })
2020
:switch("G", "G", "Search changes", { user_input = true, cli_prefix = "-" })
2121
:switch("S", "S", "Search occurrences", { user_input = true, cli_prefix = "-" })
22-
:switch("L", "L", "Trace line evolution", { user_input = true, cli_prefix = "-" })
22+
:switch("L", "L", "Trace line evolution", {
23+
user_input = true,
24+
cli_prefix = "-",
25+
validate_help = "Input must match 'start,end:file' or ':funcname:file'",
26+
validate = function(input)
27+
return (input:match("^%d+,%d+:.-$") ~= nil) or (input:match("^:.-:.-$") ~= nil)
28+
end,
29+
})
2330
:option("s", "since", "", "Limit to commits since", { key_prefix = "-" })
2431
:option("u", "until", "", "Limit to commits until", { key_prefix = "-" })
2532
:switch("m", "no-merges", "Omit merges", { key_prefix = "=" })

0 commit comments

Comments
 (0)