Skip to content

Commit

Permalink
Improve search and filter
Browse files Browse the repository at this point in the history
Concern:
Using `ResetNodeFilters` to clear the filters while searching or exiting
from search unexpectedly resets the `show hidden` mode because the
action not only removes the target filter, it resets all the other
filters as well.

Solution:
Implement `RemoveNodeFilterFromInput` to be able to clear or remove
target filters without having to reset it.
  • Loading branch information
sayanarijit committed Apr 8, 2021
1 parent e2b49ab commit fa37cd1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xplr"
version = "0.3.7" # Update app.rs and default.nix
version = "0.3.8" # Update app.rs and default.nix
authors = ["Arijit Basu <[email protected]>"]
edition = "2018"
description = "A hackable, minimal, fast TUI file explorer, stealing ideas from nnn and fzf"
Expand Down
4 changes: 2 additions & 2 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ with import <nixpkgs> { };

rustPlatform.buildRustPackage rec {
name = "xplr";
version = "0.3.7";
version = "0.3.8";
src = fetchTarball
("https://github.com/sayanarijit/xplr/archive/refs/tags/v0.3.7.tar.gz");
("https://github.com/sayanarijit/xplr/archive/refs/tags/v0.3.8.tar.gz");
buildInputs = [ cargo ];
checkPhase = "";
cargoSha256 = "0000000000000000000000000000000000000000000000000000";
Expand Down
64 changes: 41 additions & 23 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::fs;
use std::io;
use std::path::PathBuf;
use std::time::Duration;

pub const VERSION: &str = "v0.3.7"; // Update Cargo.toml and default.nix
pub const VERSION: &str = "v0.3.8"; // Update Cargo.toml and default.nix
pub const TEMPLATE_TABLE_ROW: &str = "TEMPLATE_TABLE_ROW";
pub const UNSUPPORTED_STR: &str = "???";
pub const UPGRADE_GUIDE_LINK: &str = "https://github.com/sayanarijit/xplr/wiki/Upgrade-Guide";
Expand Down Expand Up @@ -187,7 +188,7 @@ pub enum InternalMsg {
HandleKey(Key),
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum NodeFilter {
RelativePathIs,
RelativePathIsNot,
Expand Down Expand Up @@ -378,7 +379,7 @@ impl NodeFilter {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct NodeFilterApplicable {
filter: NodeFilter,
input: String,
Expand Down Expand Up @@ -409,7 +410,7 @@ pub struct NodeFilterFromInput {

#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ExplorerConfig {
filters: Vec<NodeFilterApplicable>,
filters: HashSet<NodeFilterApplicable>,
}

impl ExplorerConfig {
Expand Down Expand Up @@ -568,6 +569,11 @@ pub enum ExternalMsg {
/// Example: `AddNodeFilterFromInput: {filter: RelativePathDoesStartWith}`
AddNodeFilterFromInput(NodeFilterFromInput),

/// Remove a node filter reading the input from the buffer.
///
/// Example: `RemoveNodeFilterFromInput: {filter: RelativePathDoesStartWith}`
RemoveNodeFilterFromInput(NodeFilterFromInput),

/// Reset the node filters back to the default configuration.
ResetNodeFilters,

Expand Down Expand Up @@ -780,7 +786,7 @@ impl App {

let mut explorer_config = ExplorerConfig::default();
if !config.general.show_hidden {
explorer_config.filters.push(NodeFilterApplicable::new(
explorer_config.filters.insert(NodeFilterApplicable::new(
NodeFilter::RelativePathDoesNotStartWith,
".".into(),
Default::default(),
Expand Down Expand Up @@ -881,6 +887,7 @@ impl App {
ExternalMsg::AddNodeFilter(f) => self.add_node_filter(f),
ExternalMsg::AddNodeFilterFromInput(f) => self.add_node_filter_from_input(f),
ExternalMsg::RemoveNodeFilter(f) => self.remove_node_filter(f),
ExternalMsg::RemoveNodeFilterFromInput(f) => self.remove_node_filter_from_input(f),
ExternalMsg::ToggleNodeFilter(f) => self.toggle_node_filter(f),
ExternalMsg::ResetNodeFilters => self.reset_node_filters(),
ExternalMsg::LogInfo(l) => self.log_info(l),
Expand Down Expand Up @@ -1175,34 +1182,45 @@ impl App {
}

fn add_node_filter(mut self, filter: NodeFilterApplicable) -> Result<Self> {
self.explorer_config.filters.push(filter);
self.explorer_config.filters.insert(filter);
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
}

fn add_node_filter_from_input(mut self, filter: NodeFilterFromInput) -> Result<Self> {
if let Some(input) = self.input_buffer() {
self.explorer_config.filters.push(NodeFilterApplicable::new(
filter.filter,
input,
filter.case_sensitive,
));
self.explorer_config
.filters
.insert(NodeFilterApplicable::new(
filter.filter,
input,
filter.case_sensitive,
));
self.msg_out.push_back(MsgOut::Refresh);
};
Ok(self)
}

fn remove_node_filter(mut self, filter: NodeFilterApplicable) -> Result<Self> {
self.explorer_config.filters = self
.explorer_config
.filters
.into_iter()
.filter(|f| f != &filter)
.collect();
self.explorer_config.filters.remove(&filter);
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
}

fn remove_node_filter_from_input(mut self, filter: NodeFilterFromInput) -> Result<Self> {
if let Some(input) = self.input_buffer() {
self.explorer_config
.filters
.remove(&NodeFilterApplicable::new(
filter.filter,
input,
filter.case_sensitive,
));
self.msg_out.push_back(MsgOut::Refresh);
};
Ok(self)
}

fn toggle_node_filter(self, filter: NodeFilterApplicable) -> Result<Self> {
if self.explorer_config.filters.contains(&filter) {
self.remove_node_filter(filter)
Expand All @@ -1215,15 +1233,15 @@ impl App {
self.explorer_config.filters.clear();

if !self.config.general.show_hidden {
self.explorer_config.filters.push(NodeFilterApplicable::new(
self.add_node_filter(NodeFilterApplicable::new(
NodeFilter::RelativePathDoesNotStartWith,
".".into(),
Default::default(),
));
};
self.msg_out.push_back(MsgOut::Refresh);

Ok(self)
))
} else {
self.msg_out.push_back(MsgOut::Refresh);
Ok(self)
}
}

fn log_info(mut self, message: String) -> Result<Self> {
Expand Down
26 changes: 18 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,12 @@ impl Default for KeyBindings {
ctrl-f:
help: search [/]
messages:
- ResetNodeFilters
- SwitchMode: search
- SetInputBuffer: ""
- Explore
/:
messages:
- ResetNodeFilters
- SwitchMode: search
- SetInputBuffer: ""
- Explore
Expand Down Expand Up @@ -500,7 +498,9 @@ impl Default for Config {
enter:
help: focus
messages:
- ResetNodeFilters
- RemoveNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
- SwitchMode: default
- Explore
Expand All @@ -517,31 +517,39 @@ impl Default for Config {
right:
help: enter
messages:
- ResetNodeFilters
- RemoveNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
- Enter
- SwitchMode: default
- Explore
left:
help: back
messages:
- ResetNodeFilters
- RemoveNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
- Back
- SwitchMode: default
- Explore
esc:
help: cancel
messages:
- ResetNodeFilters
- RemoveNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
- SwitchMode: default
- Explore
backspace:
help: clear
messages:
- RemoveNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
- SetInputBuffer: ""
- ResetNodeFilters
- Explore
ctrl-c:
Expand All @@ -551,8 +559,10 @@ impl Default for Config {
default:
messages:
- RemoveNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
- BufferInputFromKey
- ResetNodeFilters
- AddNodeFilterFromInput:
filter: RelativePathDoesContain
case_sensitive: false
Expand Down

0 comments on commit fa37cd1

Please sign in to comment.