From eec07d9631ededaa5f2fc8d0d4a9ccc5adfbdd58 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 3 Jan 2024 17:04:08 -0700 Subject: [PATCH] Add home button, canonicalize paths --- src/main.rs | 37 +++++++++++++++++++------------------ src/tab.rs | 14 ++++++++++++-- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c27d17..2c6a60f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,6 +77,16 @@ fn main() -> Result<(), Box> { Ok(()) } +fn home_dir() -> PathBuf { + match dirs::home_dir() { + Some(home) => home, + None => { + log::warn!("failed to locate home directory"); + PathBuf::from("/") + } + } +} + #[derive(Clone, Debug)] pub struct Flags { config_handler: Option, @@ -148,16 +158,6 @@ pub struct App { } impl App { - fn open_home(&mut self) -> Command { - match dirs::home_dir() { - Some(home) => self.open_tab(home), - None => { - log::warn!("failed to locate home directory"); - self.open_tab("/") - } - } - } - fn open_tab>(&mut self, path: P) -> Command { let tab = Tab::new(path); self.tab_model @@ -263,7 +263,7 @@ impl Application for App { } if app.tab_model.iter().next().is_none() { - let _ = app.open_home(); + let _ = app.open_tab(home_dir()); } let command = app.update_title(); @@ -355,14 +355,11 @@ impl Application for App { } Message::TabNew => { let active = self.tab_model.active(); - let path_opt = match self.tab_model.data::(active) { - Some(tab) => Some(tab.path.clone()), - None => None, + let path = match self.tab_model.data::(active) { + Some(tab) => tab.path.clone(), + None => home_dir(), }; - match path_opt { - Some(path) => return self.open_tab(path), - None => return self.open_home(), - } + return self.open_tab(path); } Message::ToggleContextPage(context_page) => { if self.context_page == context_page { @@ -397,6 +394,10 @@ impl Application for App { .on_press(Message::TabNew) .padding(space_xxs) .style(style::Button::Icon), + widget::button(widget::icon::from_name("go-home-symbolic").size(16).icon()) + .on_press(Message::TabMessage(active, tab::Message::Home)) + .padding(space_xxs) + .style(style::Button::Icon), widget::button(widget::icon::from_name("go-up-symbolic").size(16).icon()) .on_press(Message::TabMessage(active, tab::Message::Parent)) .padding(space_xxs) diff --git a/src/tab.rs b/src/tab.rs index 1cf9580..47ab7f6 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -49,6 +49,7 @@ pub fn open_command(path: &PathBuf) -> process::Command { #[derive(Clone, Copy, Debug)] pub enum Message { Click(usize), + Home, Parent, } @@ -69,9 +70,15 @@ pub struct Tab { impl Tab { pub fn new>(path: P) -> Self { - //TODO: store absolute path + let path = path.into(); let mut tab = Self { - path: path.into(), + path: match fs::canonicalize(&path) { + Ok(absolute) => absolute, + Err(err) => { + log::warn!("failed to canonicalize {:?}: {}", path, err); + path + } + }, context_menu: None, items: Vec::new(), }; @@ -167,6 +174,9 @@ impl Tab { } } } + Message::Home => { + cd = Some(crate::home_dir()); + } Message::Parent => { if let Some(parent) = self.path.parent() { cd = Some(parent.to_owned());