Fix broken hostory

Fixes: https://github.com/sayanarijit/xplr/issues/598
pull/599/head
Arijit Basu 1 year ago
parent c6ef149f84
commit 2ce093f121
No known key found for this signature in database
GPG Key ID: 0F8EF5258DC38077

@ -101,23 +101,67 @@ pub struct History {
}
impl History {
fn loc_exists(&self) -> bool {
self.peek()
.map(|p| PathBuf::from(p).exists())
.unwrap_or(false)
}
fn cleanup(mut self) -> Self {
while self.loc != 0
&& self
.paths
.get(self.loc.saturating_sub(1))
.and_then(|p1| self.peek().map(|p2| p1 == p2))
.unwrap_or(false)
{
self.paths.remove(self.loc);
self.loc = self.loc.saturating_sub(1);
}
while self.loc != self.paths.len().saturating_sub(1)
&& self
.paths
.get(self.loc.saturating_add(1))
.and_then(|p1| self.peek().map(|p2| p1 == p2))
.unwrap_or(false)
{
self.paths.remove(self.loc.saturating_add(1));
}
self
}
fn push(mut self, path: String) -> Self {
if self.peek() != Some(&path) {
self.paths = self.paths.into_iter().take(self.loc + 1).collect();
self.paths.push(path);
self.loc = self.paths.len().max(1) - 1;
self.loc = self.paths.len().saturating_sub(1);
}
self
}
fn visit_last(mut self) -> Self {
self.loc = self.loc.max(1) - 1;
self
self.loc = self.loc.saturating_sub(1);
while self.loc != 0 && !self.loc_exists() {
self.paths.remove(self.loc);
self.loc = self.loc.saturating_sub(1);
}
self.cleanup()
}
fn visit_next(mut self) -> Self {
self.loc = (self.loc + 1).min(self.paths.len().max(1) - 1);
self
self.loc = self
.loc
.saturating_add(1)
.min(self.paths.len().saturating_sub(1));
while self.loc != self.paths.len().saturating_sub(1) && !self.loc_exists() {
self.paths.remove(self.loc);
}
self.cleanup()
}
fn peek(&self) -> Option<&String> {
@ -696,7 +740,7 @@ impl App {
history = history.push(n.absolute_path.clone());
}
dir.focus = dir.total.max(1) - 1;
dir.focus = dir.total.saturating_sub(1);
if let Some(n) = dir.focused_node() {
self.history = history.push(n.absolute_path.clone());
@ -713,10 +757,10 @@ impl App {
if bounded {
dir.focus
} else {
dir.total.max(1) - 1
dir.total.saturating_sub(1)
}
} else {
dir.focus.max(1) - 1
dir.focus.saturating_sub(1)
};
};
Ok(self)
@ -740,10 +784,10 @@ impl App {
if bounded {
idx
} else {
total.max(1) - 1
total.saturating_sub(1)
}
} else {
idx.max(1) - 1
idx.saturating_sub(1)
};
if let Some(p) = self
.selection
@ -769,7 +813,7 @@ impl App {
history = history.push(n.absolute_path.clone());
}
dir.focus = dir.focus.max(index) - index;
dir.focus = dir.focus.saturating_sub(index);
if let Some(n) = self.focused_node() {
self.history = history.push(n.absolute_path.clone());
}
@ -854,7 +898,11 @@ impl App {
history = history.push(n.absolute_path.clone());
}
dir.focus = (dir.focus + index).min(dir.total.max(1) - 1);
dir.focus = dir
.focus
.saturating_add(index)
.min(dir.total.saturating_sub(1));
if let Some(n) = self.focused_node() {
self.history = history.push(n.absolute_path.clone());
}
@ -946,9 +994,9 @@ impl App {
match env::set_current_dir(&dir) {
Ok(()) => {
let pwd = self.pwd.clone();
let lwd = self.pwd.clone();
let focus = self.focused_node().map(|n| n.relative_path.clone());
self = self.add_last_focus(pwd, focus)?;
self = self.add_last_focus(lwd, focus)?;
self.pwd = dir.to_string_lossy().to_string();
self.explorer_config.searcher = None;
if save_history {
@ -1099,7 +1147,7 @@ impl App {
fn focus_by_index(mut self, index: usize) -> Result<Self> {
let history = self.history.clone();
if let Some(dir) = self.directory_buffer_mut() {
dir.focus = index.min(dir.total.max(1) - 1);
dir.focus = index.min(dir.total.saturating_sub(1));
if let Some(n) = self.focused_node() {
self.history = history.push(n.absolute_path.clone());
}

@ -56,7 +56,7 @@ pub(crate) fn explore_sync(
.enumerate()
.find(|(_, n)| n.relative_path == focus_str)
.map(|(i, _)| i)
.unwrap_or_else(|| fallback_focus.min(nodes.len().max(1) - 1))
.unwrap_or_else(|| fallback_focus.min(nodes.len().saturating_sub(1)))
} else {
0
};

@ -157,11 +157,11 @@ xplr.config.general.logs.error.style = { fg = "Red" }
-- * format: nullable string
-- * style: [Style](https://xplr.dev/en/style)
xplr.config.general.table.header.cols = {
{ format = " index", style = {} },
{ format = " index", style = {} },
{ format = "╭─── path", style = {} },
{ format = "perm", style = {} },
{ format = "size", style = {} },
{ format = "modified", style = {} },
{ format = "perm", style = {} },
{ format = "size", style = {} },
{ format = "modified", style = {} },
}
-- Style of the table header.
@ -478,7 +478,7 @@ xplr.config.general.sort_and_filter_ui.search_identifiers = {
--
-- Type: nullable string
xplr.config.general.sort_and_filter_ui.search_direction_identifiers.ordered.format =
""
""
-- The shape of unordered indicator for search ordering identifiers in Sort & filter panel.
--
@ -676,7 +676,7 @@ xplr.config.general.panel_ui.sort_and_filter.border_style = {}
-- Type: nullable list of [Node Sorter](https://xplr.dev/en/sorting#node-sorter-applicable)
xplr.config.general.initial_sorting = {
{ sorter = "ByCanonicalIsDir", reverse = true },
{ sorter = "ByIRelativePath", reverse = false },
{ sorter = "ByIRelativePath", reverse = false },
}
-- The name of one of the modes to use when xplr loads.
@ -1285,19 +1285,21 @@ xplr.config.modes.builtin.default = {
}
xplr.config.modes.builtin.default.key_bindings.on_key["v"] =
xplr.config.modes.builtin.default.key_bindings.on_key["space"]
xplr.config.modes.builtin.default.key_bindings.on_key["space"]
xplr.config.modes.builtin.default.key_bindings.on_key["V"] =
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-a"]
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-a"]
xplr.config.modes.builtin.default.key_bindings.on_key["/"] =
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-f"]
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-f"]
xplr.config.modes.builtin.default.key_bindings.on_key["h"] =
xplr.config.modes.builtin.default.key_bindings.on_key["left"]
xplr.config.modes.builtin.default.key_bindings.on_key["left"]
xplr.config.modes.builtin.default.key_bindings.on_key["j"] =
xplr.config.modes.builtin.default.key_bindings.on_key["down"]
xplr.config.modes.builtin.default.key_bindings.on_key["down"]
xplr.config.modes.builtin.default.key_bindings.on_key["k"] =
xplr.config.modes.builtin.default.key_bindings.on_key["up"]
xplr.config.modes.builtin.default.key_bindings.on_key["up"]
xplr.config.modes.builtin.default.key_bindings.on_key["l"] =
xplr.config.modes.builtin.default.key_bindings.on_key["right"]
xplr.config.modes.builtin.default.key_bindings.on_key["right"]
xplr.config.modes.builtin.default.key_bindings.on_key["tab"] =
xplr.config.modes.builtin.default.key_bindings.on_key["ctrl-i"] -- compatibility workaround
-- The builtin debug error mode.
--
@ -1755,9 +1757,9 @@ xplr.config.modes.builtin.number = {
}
xplr.config.modes.builtin.number.key_bindings.on_key["j"] =
xplr.config.modes.builtin.number.key_bindings.on_key["down"]
xplr.config.modes.builtin.number.key_bindings.on_key["down"]
xplr.config.modes.builtin.number.key_bindings.on_key["k"] =
xplr.config.modes.builtin.number.key_bindings.on_key["up"]
xplr.config.modes.builtin.number.key_bindings.on_key["up"]
-- The builtin go to mode.
--
@ -2223,9 +2225,9 @@ xplr.config.modes.builtin.search = {
}
xplr.config.modes.builtin.search.key_bindings.on_key["ctrl-n"] =
xplr.config.modes.builtin.search.key_bindings.on_key["down"]
xplr.config.modes.builtin.search.key_bindings.on_key["down"]
xplr.config.modes.builtin.search.key_bindings.on_key["ctrl-p"] =
xplr.config.modes.builtin.search.key_bindings.on_key["up"]
xplr.config.modes.builtin.search.key_bindings.on_key["up"]
-- The builtin filter mode.
--
@ -2930,14 +2932,14 @@ xplr.fn.builtin.fmt_general_table_row_cols_2 = function(m)
local T = xplr.util.paint("T", { fg = "Red" })
return xplr.util
.permissions_rwx(m.permissions)
:gsub("r", r)
:gsub("w", w)
:gsub("x", x)
:gsub("s", s)
:gsub("S", S)
:gsub("t", t)
:gsub("T", T)
.permissions_rwx(m.permissions)
:gsub("r", r)
:gsub("w", w)
:gsub("x", x)
:gsub("s", s)
:gsub("S", S)
:gsub("t", t)
:gsub("T", T)
end
-- Renders the fourth column in the table

Loading…
Cancel
Save