From 7de0811eafde44cad863182b3820b5e2520e8fbb Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Fri, 4 Jun 2021 17:46:03 +0530 Subject: [PATCH] No need to "Refresh" explicitly Closes: https://github.com/sayanarijit/xplr/issues/207 --- src/app.rs | 102 +++++++++++++++++++-------------------------------- src/init.lua | 71 +++++++++++------------------------ 2 files changed, 59 insertions(+), 114 deletions(-) diff --git a/src/app.rs b/src/app.rs index b76209c..0223d04 100644 --- a/src/app.rs +++ b/src/app.rs @@ -961,27 +961,25 @@ impl ExplorerConfig { pub enum ExternalMsg { /// Explore the present working directory and register the filtered nodes. /// This operation is expensive. So, try to avoid using it too often. - /// Once exploration is done, it will auto `Refresh` the state. ExplorePwd, /// Explore the present working directory and register the filtered nodes asynchronously. /// This operation happens asynchronously. That means, the xplr directory buffers won't be updated /// immediately. Hence, it needs to be used with care and probably with special checks in place. /// To explore `$PWD` synchronously, use `ExplorePwd` instead. - /// Once exploration is done, it will auto `Refresh` the state. ExplorePwdAsync, /// Explore the present working directory along with its parents and register the filtered nodes. /// This operation happens asynchronously. That means, the xplr directory buffers won't be updated /// immediately. Hence, it needs to be used with care and probably with special checks in place. /// To explore just the `$PWD` synchronously, use `ExplorePwd` instead. - /// Once exploration is done, it will auto `Refresh` the state. ExploreParentsAsync, - /// Refresh the app state (uncluding UI). + /// Refresh the UI. /// But it will not re-explore the directory if the working directory is the same. /// If there is some change in the working directory and you want to re-explore it, /// use the `Explore` message instead. + /// Also, it will not clear the screen. Use `ClearScreen` for that. Refresh, /// Clears the screen. @@ -1105,7 +1103,6 @@ pub enum ExternalMsg { PopMode, /// Switch layout. - /// This will call `Refresh` automatically. /// /// > **NOTE:** To be specific about which layout to switch to, use `SwitchLayoutBuiltin` or /// `SwitchLayoutCustom` instead. @@ -1114,13 +1111,11 @@ pub enum ExternalMsg { SwitchLayout(String), /// Switch to a builtin layout. - /// This will call `Refresh` automatically. /// /// **Example:** `SwitchLayoutBuiltin: default` SwitchLayoutBuiltin(String), /// Switch to a custom layout. - /// This will call `Refresh` automatically. /// /// **Example:** `SwitchLayoutCustom: my_custom_layout` SwitchLayoutCustom(String), @@ -1129,7 +1124,7 @@ pub enum ExternalMsg { /// Note that the arguments will be shell-escaped. /// So to read the variables, the `-c` option of the shell /// can be used. - /// You may need to pass `Refresh` or `Explore` depening on the expectation. + /// You may need to pass `ExplorePwd` depening on the expectation. /// /// **Example:** `Call: {command: bash, args: ["-c", "read -p test"]}` Call(Command), @@ -1613,10 +1608,11 @@ impl App { } pub fn handle_task(self, task: Task) -> Result { - match task.msg { - MsgIn::Internal(msg) => self.handle_internal(msg), - MsgIn::External(msg) => self.handle_external(msg, task.key), - } + let app = match task.msg { + MsgIn::Internal(msg) => self.handle_internal(msg)?, + MsgIn::External(msg) => self.handle_external(msg, task.key)?, + }; + app.refresh() } fn handle_internal(self, msg: InternalMsg) -> Result { @@ -1752,7 +1748,6 @@ impl App { ExternalMsg::LogWarning( "Key map not found. Let's calm down, escape, and try again.".into(), ), - ExternalMsg::Refresh, ] }); @@ -1808,10 +1803,8 @@ impl App { self.history = history.push(n.absolute_path().clone()) } } - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_last(mut self) -> Result { @@ -1826,19 +1819,15 @@ impl App { if let Some(n) = dir.focused_node() { self.history = history.push(n.absolute_path().clone()); } - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_previous(mut self) -> Result { if let Some(dir) = self.directory_buffer_mut() { dir.focus = dir.focus.max(1) - 1; - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_previous_by_relative_index(mut self, index: usize) -> Result { @@ -1852,10 +1841,8 @@ impl App { if let Some(n) = self.focused_node() { self.history = history.push(n.absolute_path().clone()); } - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_previous_by_relative_index_from_input(self) -> Result { @@ -1869,10 +1856,8 @@ impl App { fn focus_next(mut self) -> Result { if let Some(dir) = self.directory_buffer_mut() { dir.focus = (dir.focus + 1).min(dir.total.max(1) - 1); - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_next_by_relative_index(mut self, index: usize) -> Result { @@ -1886,10 +1871,8 @@ impl App { if let Some(n) = self.focused_node() { self.history = history.push(n.absolute_path().clone()); } - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_next_by_relative_index_from_input(self) -> Result { @@ -1966,7 +1949,7 @@ impl App { .map(|s| self.clone().change_directory(s, false)) .unwrap_or(Ok(self)) } else { - self.clone().focus_path(target, false)?.refresh() + self.clone().focus_path(target, false) } } else { Ok(self) @@ -1980,7 +1963,7 @@ impl App { self.input_buffer = Some(input.to_owned()); }; self.logs_hidden = true; - self.refresh() + Ok(self) } fn buffer_input_from_key(self, key: Option) -> Result { @@ -1994,7 +1977,7 @@ impl App { fn set_input_buffer(mut self, string: String) -> Result { self.input_buffer = Some(string); self.logs_hidden = true; - self.refresh() + Ok(self) } fn remove_input_buffer_last_character(mut self) -> Result { @@ -2002,10 +1985,8 @@ impl App { buf.pop(); self.input_buffer = Some(buf); self.logs_hidden = true; - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn remove_input_buffer_last_word(mut self) -> Result { @@ -2023,15 +2004,13 @@ impl App { self.input_buffer = Some(buf); self.logs_hidden = true; - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn reset_input_buffer(mut self) -> Result { self.input_buffer = None; - self.refresh() + Ok(self) } fn focus_by_index(mut self, index: usize) -> Result { @@ -2041,10 +2020,8 @@ impl App { if let Some(n) = self.focused_node() { self.history = history.push(n.absolute_path().clone()); } - self.refresh() - } else { - Ok(self) - } + }; + Ok(self) } fn focus_by_index_from_input(self) -> Result { @@ -2077,7 +2054,7 @@ impl App { self.history = history.push(n.absolute_path().clone()); } } - self.refresh() + Ok(self) } else { self.log_error(format!("{} not found in $PWD", name)) } @@ -2171,7 +2148,7 @@ impl App { fn switch_layout(mut self, layout: &str) -> Result { if let Some(l) = self.config().layouts().get(layout) { self.layout = l.to_owned(); - self.refresh() + Ok(self) } else { self.log_error(format!("Layout not found: {}", layout)) } @@ -2180,7 +2157,7 @@ impl App { fn switch_layout_builtin(mut self, layout: &str) -> Result { if let Some(l) = self.config().layouts().get_builtin(layout) { self.layout = l.to_owned(); - self.refresh() + Ok(self) } else { self.log_error(format!("Builtin layout not found: {}", layout)) } @@ -2189,7 +2166,7 @@ impl App { fn switch_layout_custom(mut self, layout: &str) -> Result { if let Some(l) = self.config().layouts().get_custom(layout) { self.layout = l.to_owned(); - self.refresh() + Ok(self) } else { self.log_error(format!("Custom layout not found: {}", layout)) } @@ -2235,13 +2212,13 @@ impl App { pub fn add_directory(mut self, parent: String, dir: DirectoryBuffer) -> Result { self.directory_buffers.insert(parent, dir); - self.refresh() + Ok(self) } fn select(mut self) -> Result { if let Some(n) = self.focused_node().map(|n| n.to_owned()) { self.selection.insert(n); - self.refresh() + Ok(self) } else { Ok(self) } @@ -2253,7 +2230,7 @@ impl App { let filename = path.file_name().map(|p| p.to_string_lossy().to_string()); if let (Some(p), Some(n)) = (parent, filename) { self.selection.insert(Node::new(p, n)); - self.refresh() + Ok(self) } else { Ok(self) } @@ -2264,7 +2241,6 @@ impl App { d.nodes.clone().into_iter().for_each(|n| { self.selection.insert(n); }); - self.msg_out.push_back(MsgOut::Refresh); }; Ok(self) @@ -2273,14 +2249,12 @@ impl App { fn un_select(mut self) -> Result { if let Some(n) = self.focused_node().map(|n| n.to_owned()) { self.selection.retain(|s| s != &n); - self.msg_out.push_back(MsgOut::Refresh); } Ok(self) } fn un_select_path(mut self, path: String) -> Result { self.selection.retain(|n| n.absolute_path != path); - self.msg_out.push_back(MsgOut::Refresh); Ok(self) } @@ -2289,7 +2263,6 @@ impl App { d.nodes.clone().into_iter().for_each(|n| { self.selection.retain(|s| s != &n); }); - self.msg_out.push_back(MsgOut::Refresh); }; Ok(self) @@ -2328,7 +2301,6 @@ impl App { fn clear_selection(mut self) -> Result { self.selection.clear(); - self.msg_out.push_back(MsgOut::Refresh); Ok(self) } diff --git a/src/init.lua b/src/init.lua index 9552914..2d19947 100644 --- a/src/init.lua +++ b/src/init.lua @@ -581,7 +581,6 @@ xplr.config.modes.builtin.default = { { SwitchModeBuiltin = "action" }, - "Refresh" } }, ["?"] = { @@ -597,7 +596,7 @@ xplr.config.modes.builtin.default = { }, ["G"] = { help = "go to bottom", - messages = {"PopMode", "FocusLast", "Refresh"} + messages = {"PopMode", "FocusLast"} }, ["ctrl-a"] = { help = "select/unselect all", @@ -626,7 +625,7 @@ xplr.config.modes.builtin.default = { }, ["ctrl-r"] = { help = "refresh screen", - messages = {"ClearScreen", "Refresh"} + messages = {"ClearScreen"} }, ["ctrl-u"] = { help = "clear selection", @@ -638,7 +637,6 @@ xplr.config.modes.builtin.default = { { SwitchModeBuiltin = "switch_layout" }, - "Refresh" } }, ["d"] = { @@ -648,7 +646,6 @@ xplr.config.modes.builtin.default = { { SwitchModeBuiltin = "delete" }, - "Refresh" } }, down = { @@ -668,7 +665,6 @@ xplr.config.modes.builtin.default = { messages = { "PopMode", { SwitchModeBuiltin = "filter" }, - "Refresh" } }, ["g"] = { @@ -676,7 +672,6 @@ xplr.config.modes.builtin.default = { messages = { "PopMode", { SwitchModeBuiltin = "go_to" }, - "Refresh" } }, left = { @@ -697,7 +692,6 @@ xplr.config.modes.builtin.default = { echo SetInputBuffer: "'"$(basename "${XPLR_FOCUS_PATH}")"'" >> "${XPLR_PIPE_MSG_IN:?}" ]===] }, - "Refresh" } }, right = { @@ -709,7 +703,6 @@ xplr.config.modes.builtin.default = { messages = { "PopMode", { SwitchModeBuiltin = "sort" }, - "Refresh" } }, space = { @@ -767,7 +760,7 @@ xplr.config.modes.builtin.recover = { }, esc = { help = "escape", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} } }, on_alphabet = nil, @@ -805,7 +798,6 @@ xplr.config.modes.builtin.selection_ops = { ]===] }, "PopMode", - "Refresh" } }, ["ctrl-c"] = { @@ -814,7 +806,7 @@ xplr.config.modes.builtin.selection_ops = { }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, ["m"] = { help = "move here", @@ -833,7 +825,6 @@ xplr.config.modes.builtin.selection_ops = { ]===] }, "PopMode", - "Refresh" } }, ["x"] = { @@ -858,7 +849,6 @@ xplr.config.modes.builtin.selection_ops = { }, "ClearScreen", "PopMode", - "Refresh" } } }, @@ -894,7 +884,7 @@ xplr.config.modes.builtin.create = { }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, ["f"] = { help = "create file", @@ -957,7 +947,6 @@ xplr.config.modes.builtin.create_directory = { && echo FocusByFileName: "'"$PTH"'" >> "${XPLR_PIPE_MSG_IN:?}" else echo PopMode >> "${XPLR_PIPE_MSG_IN:?}" - echo Refresh >> "${XPLR_PIPE_MSG_IN:?}" fi ]===] } @@ -965,7 +954,7 @@ xplr.config.modes.builtin.create_directory = { }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} } }, on_alphabet = nil, @@ -1019,7 +1008,6 @@ xplr.config.modes.builtin.create_file = { && echo FocusByFileName: "'"$PTH"'" >> "${XPLR_PIPE_MSG_IN:?}" else echo PopMode >> "${XPLR_PIPE_MSG_IN:?}" - echo Refresh >> "${XPLR_PIPE_MSG_IN:?}" fi ]===] } @@ -1027,7 +1015,7 @@ xplr.config.modes.builtin.create_file = { }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} } }, on_alphabet = nil, @@ -1069,19 +1057,19 @@ xplr.config.modes.builtin.number = { }, down = { help = "to down", - messages = {"FocusNextByRelativeIndexFromInput", "PopMode", "Refresh"} + messages = {"FocusNextByRelativeIndexFromInput", "PopMode"} }, enter = { help = "to index", - messages = {"FocusByIndexFromInput", "PopMode", "Refresh"} + messages = {"FocusByIndexFromInput", "PopMode"} }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, up = { help = "to up", - messages = {"FocusPreviousByRelativeIndexFromInput", "PopMode", "Refresh"} + messages = {"FocusPreviousByRelativeIndexFromInput", "PopMode"} } }, on_alphabet = nil, @@ -1110,15 +1098,15 @@ xplr.config.modes.builtin.go_to = { }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, ["f"] = { help = "follow symlink", - messages = {"FollowSymlink", "PopMode", "Refresh"} + messages = {"FollowSymlink", "PopMode"} }, ["g"] = { help = "top", - messages = {"FocusFirst", "PopMode", "Refresh"} + messages = {"FocusFirst", "PopMode"} }, ["x"] = { help = "open in gui", @@ -1140,7 +1128,6 @@ xplr.config.modes.builtin.go_to = { }, "ClearScreen", "PopMode", - "Refresh" } } }, @@ -1192,12 +1179,11 @@ xplr.config.modes.builtin.rename = { ]===] }, "PopMode", - "Refresh" } }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} } }, on_alphabet = nil, @@ -1234,7 +1220,6 @@ xplr.config.modes.builtin.delete = { ]===] }, "PopMode", - "Refresh" } }, ["ctrl-c"] = { @@ -1266,12 +1251,11 @@ xplr.config.modes.builtin.delete = { ]===] }, "PopMode", - "Refresh" } }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} } }, on_alphabet = nil, @@ -1299,7 +1283,6 @@ xplr.config.modes.builtin.action = { }, "ExplorePwdAsync", "PopMode", - "Refresh" } }, ["c"] = { @@ -1309,7 +1292,6 @@ xplr.config.modes.builtin.action = { { SwitchModeBuiltin = "create" }, - "Refresh" } }, ["ctrl-c"] = { @@ -1325,12 +1307,11 @@ xplr.config.modes.builtin.action = { ]===] }, "PopMode", - "Refresh" } }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, ["l"] = { help = "logs", @@ -1342,7 +1323,6 @@ xplr.config.modes.builtin.action = { ]===] }, "PopMode", - "Refresh" } }, ["s"] = { @@ -1352,7 +1332,6 @@ xplr.config.modes.builtin.action = { { SwitchModeBuiltin = "selection_ops" }, - "Refresh" } }, ["q"] = { @@ -1442,7 +1421,6 @@ xplr.config.modes.builtin.search = { }, "PopMode", "ExplorePwdAsync", - "Refresh" } }, left = { @@ -1543,7 +1521,7 @@ xplr.config.modes.builtin.filter = { }, enter = { help = "done", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, ["r"] = { help = "relative does contain", @@ -1624,7 +1602,7 @@ xplr.config.modes.builtin.relative_path_does_contain = { }, enter = { help = "apply filter", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, esc = { help = "cancel", @@ -1634,7 +1612,6 @@ xplr.config.modes.builtin.relative_path_does_contain = { }, "PopMode", "ExplorePwdAsync", - "Refresh" } } }, @@ -1711,7 +1688,7 @@ xplr.config.modes.builtin.relative_path_does_not_contain = { }, enter = { help = "apply filter", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, esc = { help = "cancel", @@ -1856,7 +1833,7 @@ xplr.config.modes.builtin.sort = { }, enter = { help = "done", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} }, ["m"] = { help = "by canonical mime essence", @@ -1942,7 +1919,6 @@ xplr.config.modes.builtin.switch_layout = { SwitchLayoutBuiltin = "default" }, "PopMode", - "Refresh" } }, ["2"] = { @@ -1952,7 +1928,6 @@ xplr.config.modes.builtin.switch_layout = { SwitchLayoutBuiltin = "no_help" }, "PopMode", - "Refresh" } }, ["3"] = { @@ -1962,7 +1937,6 @@ xplr.config.modes.builtin.switch_layout = { SwitchLayoutBuiltin = "no_selection" }, "PopMode", - "Refresh" } }, ["4"] = { @@ -1972,7 +1946,6 @@ xplr.config.modes.builtin.switch_layout = { SwitchLayoutBuiltin = "no_help_no_selection" }, "PopMode", - "Refresh" } }, ["ctrl-c"] = { @@ -1981,7 +1954,7 @@ xplr.config.modes.builtin.switch_layout = { }, esc = { help = "cancel", - messages = {"PopMode", "Refresh"} + messages = {"PopMode"} } }, on_alphabet = nil,