Do not close command view on open-file is file is invalid or it is a directory

Added in a validation function which fires before submitting a command enter; found it incredibly irritating to try to open something, hit enter, only to be told I'd selected a directory, and then have to go through the whole process again. (#175)
This commit is contained in:
Adam 2021-05-05 02:04:51 -04:00 committed by GitHub
parent fa99d5401e
commit 4c42dd4adc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -94,6 +94,15 @@ command.add(nil, {
core.root_view:open_doc(core.open_doc(common.home_expand(text)))
end, function (text)
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
end, nil, function(text)
local path_stat, err = system.get_file_info(common.home_expand(text))
if err then
core.error("Cannot open file %q: %q", text, err)
elseif path_stat.type == 'dir' then
core.error("Cannot open %q, is a folder", text)
else
return true
end
end)
end,

View File

@ -23,6 +23,7 @@ local default_state = {
submit = noop,
suggest = noop,
cancel = noop,
validate = function() return true end
}
@ -97,13 +98,15 @@ end
function CommandView:submit()
local suggestion = self.suggestions[self.suggestion_idx]
local text = self:get_text()
local submit = self.state.submit
self:exit(true)
submit(text, suggestion)
if self.state.validate(text) then
local submit = self.state.submit
self:exit(true)
submit(text, suggestion)
end
end
function CommandView:enter(text, submit, suggest, cancel)
function CommandView:enter(text, submit, suggest, cancel, validate)
if self.state ~= default_state then
return
end
@ -111,6 +114,7 @@ function CommandView:enter(text, submit, suggest, cancel)
submit = submit or noop,
suggest = suggest or noop,
cancel = cancel or noop,
validate = validate or function() return true end
}
core.set_active_view(self)
self:update_suggestions()