Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hide specified files and dirs #2089

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions yazi-config/src/manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Manager {
pub scrolloff: u8,
pub mouse_events: MouseEvents,
pub title_format: String,
pub ignore_rule: String,
}

impl FromStr for Manager {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/tab/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem;

use yazi_config::{LAYOUT, MANAGER};
use yazi_dds::Pubsub;
use yazi_fs::{Cha, File, Files, FilesOp, FolderStage, Step};
use yazi_fs::{Cha, File, Files, FilesOp, FolderStage, Step, Filter};
use yazi_proxy::ManagerProxy;
use yazi_shared::{Id, url::{Url, Urn}};

Expand All @@ -24,7 +24,7 @@ impl Default for Folder {
Self {
url: Default::default(),
cha: Default::default(),
files: Files::new(MANAGER.show_hidden),
files: Files::new(MANAGER.show_hidden, Filter::new(MANAGER.ignore_rule.as_str(), yazi_fs::FilterCase::Smart).ok()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better place to initialize ignore_filter?

stage: Default::default(),
offset: Default::default(),
cursor: Default::default(),
Expand Down
30 changes: 29 additions & 1 deletion yazi-fs/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Files {

sorter: FilesSorter,
filter: Option<Filter>,
ignore_filter: Option<Filter>,
show_hidden: bool,
}

Expand All @@ -28,7 +29,7 @@ impl Deref for Files {
}

impl Files {
pub fn new(show_hidden: bool) -> Self { Self { show_hidden, ..Default::default() } }
pub fn new(show_hidden: bool, ignore_filter: Option<Filter>) -> Self { Self { show_hidden, ignore_filter, ..Default::default() } }

pub async fn from_dir(dir: &Url) -> std::io::Result<UnboundedReceiver<File>> {
let mut it = fs::read_dir(dir).await?;
Expand Down Expand Up @@ -321,6 +322,13 @@ impl Files {
}

fn split_files(&self, files: impl IntoIterator<Item = File>) -> (Vec<File>, Vec<File>) {
let mut files: Vec<File> = files.into_iter().collect();
if let Some(ignore_filter) = &self.ignore_filter {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if condition will be called lots of times, I think it just should be called when hover a dir? Should it be put in set_ignore_filter or other place?

files = files
.into_iter()
.filter(|f| !ignore_filter.matches(f.name()))
.collect();
}
if let Some(filter) = &self.filter {
files
.into_iter()
Expand Down Expand Up @@ -381,6 +389,26 @@ impl Files {
true
}

// --- Ignore Filter
#[inline]
pub fn ignore_filter(&self) -> Option<&Filter> { self.ignore_filter.as_ref() }

pub fn set_ignore_filter(&mut self, filter: Option<Filter>) -> bool {
if self.ignore_filter == filter {
return false;
}

self.ignore_filter = filter;
if self.ignore_filter.is_none() {
return true;
}

let it = mem::take(&mut self.items).into_iter().chain(mem::take(&mut self.hidden));
(self.hidden, self.items) = self.split_files(it);
self.sorter.sort(&mut self.items, &self.sizes);
true
}

// --- Show hidden
pub fn set_show_hidden(&mut self, state: bool) {
if self.show_hidden == state {
Expand Down
Loading