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 80e6f97
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl ResolvedNode {
}
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, Hash, Serialize, Deserialize)]
pub struct Node {
pub parent: String,
pub relative_path: String,
Expand Down Expand Up @@ -366,12 +366,19 @@ 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))
}
}

impl PartialEq<Self> for Node {
fn eq(&self, other: &Self) -> bool {
self.absolute_path == other.absolute_path
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct DirectoryBuffer {
pub parent: String,
Expand Down Expand Up @@ -2322,7 +2329,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 +2348,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 +2369,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 80e6f97

Please sign in to comment.