Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input validation for switches #941

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/neogit/lib/popup/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ function M:switch(key, cli, description, opts)
internal = opts.internal,
cli_prefix = opts.cli_prefix,
user_input = opts.user_input,
validate = opts.validate or function() return true end,
validate_help = opts.validate_help,
cli_suffix = opts.cli_suffix,
options = opts.options,
incompatible = util.build_reverse_lookup(opts.incompatible),
Expand Down
13 changes: 11 additions & 2 deletions lua/neogit/lib/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,17 @@ function M:toggle_switch(switch)
if switch.user_input then
if switch.enabled then
local value = input.get_user_input(switch.cli_prefix .. switch.cli_base .. ": ")
if value then
switch.cli = switch.cli_base .. value
if value and value ~= "" then
if switch.validate(value) then
switch.cli = switch.cli_base .. value
else
switch.enabled = false
switch.cli = switch.cli_base
notification.warn(switch.validate_help)
end
else
switch.enabled = false
switch.cli = switch.cli_base
end
else
switch.cli = switch.cli_base
Expand Down
8 changes: 7 additions & 1 deletion lua/neogit/popups/log/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ function M.create()
:option("u", "until", "", "Limit to commits until")
:switch("G", "G", "Search changes", { user_input = true, cli_prefix = "-" })
:switch("S", "S", "Search occurrences", { user_input = true, cli_prefix = "-" })
:switch("L", "L", "Trace line evolution", { user_input = true, cli_prefix = "-" })
:switch("L", "L", "Trace line evolution", {
user_input = true,
cli_prefix = "-",
validate_help = "Input must match 'start,end:file' or ':funcname:file'",
validate = function(input)
return (input:match("^%d+,%d+:.-$") ~= nil) or (input:match("^:.-:.-$") ~= nil)
end })
:switch("m", "no-merges", "Omit merges", { key_prefix = "=" })
:switch("p", "first-parent", "First parent", { key_prefix = "=" })
:switch("R", "reflog", "List reflog", { key_prefix = "=" })
Expand Down
Loading