diff options
-rw-r--r-- | src/file_browser.rs | 41 | ||||
-rw-r--r-- | src/files.rs | 26 | ||||
-rw-r--r-- | src/listview.rs | 2 |
3 files changed, 53 insertions, 16 deletions
diff --git a/src/file_browser.rs b/src/file_browser.rs index 0211f01..1f2493b 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -38,32 +38,34 @@ impl FileBrowser { } pub fn enter_dir(&mut self) { - let fileview = self.columns.get_main_widget(); + let file = self.selected_file(); - let path = fileview.selected_file().path(); - match Files::new_from_path(&path) { + match file.read_dir() { Ok(files) => { - std::env::set_current_dir(path).unwrap(); + std::env::set_current_dir(&file.path).unwrap(); let view = ListView::new(files); self.columns.push_widget(view); self.update_preview(); - } - Err(_) => { - //self.show_status(&format!("Can't open this path: {}", err)); + }, + Err(ref err) if err.description() == "placeholder".to_string() => + self.show_status("No! Can't open this!"), + _ => { let status = std::process::Command::new("xdg-open") - .args(dbg!(path.file_name())) + .args(dbg!(file.path.file_name())) .status(); + match status { - Ok(status) => { - self.show_status(&format!("\"{}\" exited with {}", "xdg-open", status)) - } - Err(err) => { - self.show_status(&format!("Can't run this \"{}\": {}", "xdg-open", err)) - } + Ok(status) => + self.show_status(&format!("\"{}\" exited with {}", + "xdg-open", status)), + Err(err) => + self.show_status(&format!("Can't run this \"{}\": {}", + "xdg-open", err)) + } } - }; + } } pub fn go_back(&mut self) { @@ -87,6 +89,7 @@ impl FileBrowser { } pub fn update_preview(&mut self) { + if self.columns.get_main_widget().content.len() == 0 { return } let file = self.columns.get_main_widget().selected_file().clone(); let preview = &mut self.columns.preview; preview.set_file(&file); @@ -103,6 +106,14 @@ impl FileBrowser { self.columns.get_main_widget().content.directory.clone() } + pub fn selected_file(&self) -> &File { + self.main_column().selected_file() + } + + pub fn main_column(&self) -> &ListView<Files> { + self.columns.get_main_widget() + } + pub fn quit_with_dir(&self) { let cwd = self.cwd().path; diff --git a/src/files.rs b/src/files.rs index e03c3a0..93120d1 100644 --- a/src/files.rs +++ b/src/files.rs @@ -77,6 +77,11 @@ impl Files { }; files.sort(); + + if files.files.len() == 0 { + files.files = vec![File::new_placeholder(&path)?]; + } + Ok(files) } @@ -133,6 +138,7 @@ pub enum Kind { File, Link, Pipe, + Placeholder } impl std::fmt::Display for SortBy { @@ -203,6 +209,13 @@ impl File { Ok(File::new(&name, pathbuf, kind, size as usize, mtime, color)) } + pub fn new_placeholder(path: &Path) -> Result<File, Box<Error>> { + let mut file = File::new_from_path(path)?; + file.name = "<empty>".to_string(); + file.kind = Kind::Placeholder; + Ok(file) + } + pub fn calculate_size(&self) -> (usize, String) { let mut unit = 0; let mut size = self.size.unwrap(); @@ -236,6 +249,19 @@ impl File { self.kind == Kind::Directory } + pub fn read_dir(&self) -> Result<Files, Box<Error>> { + match self.kind { + Kind::Placeholder => { + let e: Box<Error> + = From::from("placeholder".to_string()); + Err(e) + }, + _ => Files::new_from_path(&self.path) + } + + } + + pub fn path(&self) -> PathBuf { self.path.clone() } diff --git a/src/listview.rs b/src/listview.rs index a8d029d..ad1c148 100644 --- a/src/listview.rs +++ b/src/listview.rs @@ -74,7 +74,7 @@ where let ysize = self.coordinates.ysize() as usize; let mut offset = 0; - while position + 1 > ysize + offset { + while position >= ysize - 2 + offset { offset += 1 } |