Skip to content

Add input validation for switches #941

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

Draft
wants to merge 4 commits 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/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ function Buffer:show()
style = "minimal",
focusable = true,
border = "rounded",
zindex = 48,
})

api.nvim_win_set_cursor(content_window, { 1, 0 })
Expand Down Expand Up @@ -349,6 +350,7 @@ function Buffer:show()
row = vim.o.lines - 2,
style = "minimal",
border = { "─", "─", "─", "", "", "", "", "" },
zindex = 49,
-- title = (" %s Actions "):format(title),
-- title_pos = "center",
})
Expand Down
8 changes: 8 additions & 0 deletions lua/neogit/lib/popup/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ local M = {}
---@field type string
---@field user_input boolean
---@field value string?
---@field validate fun(string): boolean
---@field validate_help string|nil

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

---@class PopupOptionOpts
---@field key_prefix? string Allows overwriting the default '=' to set option
Expand Down Expand Up @@ -252,6 +256,10 @@ 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 @@ -119,8 +119,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, { separator = "" })
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
11 changes: 8 additions & 3 deletions lua/neogit/popups/log/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ function M.create()
:option("F", "grep", "", "Search messages", { key_prefix = "-" })
: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,
})
:option("s", "since", "", "Limit to commits since", { key_prefix = "-" })
:option("u", "until", "", "Limit to commits until", { key_prefix = "-" })
:switch("m", "no-merges", "Omit merges", { key_prefix = "=" })
Expand Down Expand Up @@ -89,8 +96,6 @@ function M.create()
:action("r", "current", actions.reflog_current)
:action("H", "HEAD", actions.reflog_head)
:action("O", "other", actions.reflog_other)
:new_action_group("Other")
:action("s", "shortlog")
:build()

p:show()
Expand Down
Loading