Skip to content

Commit

Permalink
Fix ToggleSelectionByPath
Browse files Browse the repository at this point in the history
Fixes: #295
  • Loading branch information
sayanarijit committed Jul 5, 2021
1 parent 6a63f5f commit 4307ba6
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ impl Ord for Node {
other.relative_path.cmp(&self.relative_path)
}
}

impl PartialOrd for Node {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
Expand Down Expand Up @@ -2322,7 +2323,12 @@ impl App {
}

fn un_select_path(mut self, path: String) -> Result<Self> {
self.selection.retain(|n| n.absolute_path != path);
let mut pathbuf = PathBuf::from(path);
if pathbuf.is_relative() {
pathbuf = PathBuf::from(self.pwd()).join(pathbuf);
}
self.selection
.retain(|n| PathBuf::from(&n.absolute_path) != pathbuf);
Ok(self)
}

Expand All @@ -2336,15 +2342,12 @@ impl App {
Ok(self)
}

fn toggle_selection(mut self) -> Result<Self> {
if let Some(n) = self.focused_node() {
if self.selection().contains(n) {
self = self.un_select()?;
} else {
self = self.select()?;
}
fn toggle_selection(self) -> Result<Self> {
if let Some(p) = self.focused_node().map(|n| n.absolute_path().clone()) {
self.toggle_selection_by_path(p)
} else {
Ok(self)
}
Ok(self)
}

fn toggle_select_all(self) -> Result<Self> {
Expand All @@ -2360,10 +2363,18 @@ impl App {
}

fn toggle_selection_by_path(self, path: String) -> Result<Self> {
if self.selection.iter().any(|n| n.absolute_path == path) {
self.select_path(path)
} else {
let mut pathbuf = PathBuf::from(&path);
if pathbuf.is_relative() {
pathbuf = PathBuf::from(self.pwd()).join(pathbuf);
}
if self
.selection
.iter()
.any(|n| PathBuf::from(&n.absolute_path) == pathbuf)
{
self.un_select_path(path)
} else {
self.select_path(path)
}
}

Expand Down

0 comments on commit 4307ba6

Please sign in to comment.