1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00

filtering by name

This commit is contained in:
rabite 2019-03-20 23:38:58 +01:00
parent fc2d6d268c
commit 095cd1074e
4 changed files with 59 additions and 8 deletions

View File

@ -397,6 +397,16 @@ impl FileBrowser {
pub fn update_preview(&mut self) -> HResult<()> { pub fn update_preview(&mut self) -> HResult<()> {
if !self.main_widget()?.ready() { return Ok(()) } if !self.main_widget()?.ready() { return Ok(()) }
if self.main_widget()?
.widget()?
.lock()?
.as_ref()
.unwrap()
.content
.len() == 0 {
self.preview_widget_mut()?.set_stale();
return Ok(());
}
let file = self.selected_file()?.clone(); let file = self.selected_file()?.clone();
let selection = self.get_selection(&file).ok().cloned(); let selection = self.get_selection(&file).ok().cloned();
let cached_files = self.get_cached_files(&file).ok(); let cached_files = self.get_cached_files(&file).ok();

View File

@ -61,6 +61,7 @@ pub struct Files {
pub dirs_first: bool, pub dirs_first: bool,
pub reverse: bool, pub reverse: bool,
pub show_hidden: bool, pub show_hidden: bool,
pub filter: Option<String>,
pub dirty: DirtyBit pub dirty: DirtyBit
} }
@ -104,6 +105,7 @@ impl Files {
dirs_first: true, dirs_first: true,
reverse: false, reverse: false,
show_hidden: true, show_hidden: true,
filter: None,
dirty: DirtyBit::new() dirty: DirtyBit::new()
}; };
@ -148,6 +150,7 @@ impl Files {
dirs_first: true, dirs_first: true,
reverse: false, reverse: false,
show_hidden: true, show_hidden: true,
filter: None,
dirty: DirtyBit::new() dirty: DirtyBit::new()
}; };
@ -295,8 +298,25 @@ impl Files {
} }
} }
pub fn set_filter(&mut self, filter: Option<String>) {
self.filter = filter;
self.set_dirty();
}
pub fn get_filter(&self) -> Option<String> {
self.filter.clone()
}
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.files.len() match &self.filter {
None => self.files.len(),
Some(filter) => {
self.files
.iter()
.filter(|f| f.name.contains(filter))
.count()
}
}
} }
pub fn get_selected(&self) -> Vec<&File> { pub fn get_selected(&self) -> Vec<&File> {

View File

@ -51,6 +51,7 @@ impl Listable for ListView<Files> {
self.refresh()?; self.refresh()?;
}, },
Key::Ctrl('s') => { self.find_file().ok(); } Key::Ctrl('s') => { self.find_file().ok(); }
Key::Char('F') => { self.filter().log(); }
Key::Left => self.goto_grand_parent()?, Key::Left => self.goto_grand_parent()?,
Key::Right => self.goto_selected()?, Key::Right => self.goto_selected()?,
Key::Char(' ') => self.multi_select_file(), Key::Char(' ') => self.multi_select_file(),
@ -331,6 +332,17 @@ impl ListView<Files>
Ok(()) Ok(())
} }
fn filter(&mut self) -> HResult<()> {
let filter = self.minibuffer("filter").ok();
self.content.set_filter(filter);
if self.get_selection() > self.len() {
self.set_selection(self.len());
}
Ok(())
}
fn render_line(&self, file: &File) -> String { fn render_line(&self, file: &File) -> String {
let name = &file.name; let name = &file.name;
let (size, unit) = file.calculate_size().unwrap_or((0, "".to_string())); let (size, unit) = file.calculate_size().unwrap_or((0, "".to_string()));
@ -391,11 +403,19 @@ impl ListView<Files>
} }
fn render(&self) -> Vec<String> { fn render(&self) -> Vec<String> {
self.content match self.content.get_filter() {
.files Some(filter) => self.content
.iter() .files
.map(|file| self.render_line(&file)) .iter()
.collect() .filter(|f| f.name.contains(&filter))
.map(|file| self.render_line(&file))
.collect(),
None => self.content
.files
.iter()
.map(|file| self.render_line(&file))
.collect()
}
} }
} }

View File

@ -303,8 +303,8 @@ impl Previewer {
pub fn set_file(&mut self, pub fn set_file(&mut self,
file: &File, file: &File,
selection: Option<File>, selection: Option<File>,
cached_files: Option<Files>) { cached_files: Option<Files>) -> HResult<()> {
if Some(file) == self.file.as_ref() { return } if Some(file) == self.file.as_ref() && !self.widget.is_stale()? { return Ok(()) }
self.file = Some(file.clone()); self.file = Some(file.clone());
self.selection = selection.clone(); self.selection = selection.clone();
self.cached_files = cached_files.clone(); self.cached_files = cached_files.clone();
@ -345,6 +345,7 @@ impl Previewer {
return Ok(blank) return Ok(blank)
} }
})))); }))));
Ok(())
} }
pub fn reload(&mut self) { pub fn reload(&mut self) {