From 4c42dd4adc975f1b726e638cf5650a2d26ae1037 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 5 May 2021 02:04:51 -0400 Subject: [PATCH] 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) --- data/core/commands/core.lua | 9 +++++++++ data/core/commandview.lua | 12 ++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/data/core/commands/core.lua b/data/core/commands/core.lua index 04ad1be5..c8233062 100644 --- a/data/core/commands/core.lua +++ b/data/core/commands/core.lua @@ -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, diff --git a/data/core/commandview.lua b/data/core/commandview.lua index 14c6e98c..4d518d02 100644 --- a/data/core/commandview.lua +++ b/data/core/commandview.lua @@ -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()