From 7011023babde960b8ce7a4fbbdddaab696c44649 Mon Sep 17 00:00:00 2001 From: rabite Date: Sun, 17 Mar 2019 01:50:38 +0100 Subject: shorten home path for display --- src/file_browser.rs | 20 +++++++++++++++++--- src/files.rs | 11 +++++++++++ src/paths.rs | 5 +++++ src/widget.rs | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/file_browser.rs b/src/file_browser.rs index f39772a..75027db 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -347,8 +347,13 @@ impl FileBrowser { } pub fn set_title(&self) -> HResult<()> { - let cwd = &self.cwd.path.to_string_lossy(); - self.screen()?.set_title(cwd)?; + let path = match self.cwd.short_path() { + Ok(path) => path, + Err(_) => self.cwd.path.clone() + }; + + let path = path.to_string_lossy().to_string(); + self.screen()?.set_title(&path)?; Ok(()) } @@ -688,7 +693,16 @@ impl Widget for FileBrowser { crate::term::highlight_color() } else { crate::term::from_lscolor(file.color.as_ref().unwrap()) }; - let path = file.path.parent()?.to_string_lossy().to_string(); + let path = match self.cwd.short_path() { + Ok(path) => path, + Err(_) => file.path + }; + + let mut path = path.to_string_lossy().to_string(); + if &path == "" { path.clear(); } + if &path == "~/" { path.pop(); } + if &path == "/" { path.pop(); } + let pretty_path = format!("{}/{}{}", path, &color, name ); let sized_path = crate::term::sized_string(&pretty_path, xsize); diff --git a/src/files.rs b/src/files.rs index 5cc7dd8..49741df 100644 --- a/src/files.rs +++ b/src/files.rs @@ -602,4 +602,15 @@ impl File { = chrono::Local.timestamp(self.meta().unwrap().mtime(), 0); Some(time.format("%F %R").to_string()) } + + pub fn short_path(&self) -> HResult { + if let Ok(home) = crate::paths::home_path() { + if let Ok(short) = self.path.strip_prefix(home) { + let mut path = PathBuf::from("~"); + path.push(short); + return Ok(path); + } + } + return Ok(self.path.clone()) + } } diff --git a/src/paths.rs b/src/paths.rs index be40658..b3f3e1d 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -4,6 +4,11 @@ use std::path::PathBuf; use crate::fail::HResult; +pub fn home_path() -> HResult { + let home = dirs_2::home_dir()?; + Ok(home) +} + pub fn hunter_path() -> HResult { let mut config_dir = dirs_2::config_dir()?; config_dir.push("hunter/"); diff --git a/src/widget.rs b/src/widget.rs index 8c9942b..09a5d0a 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -257,7 +257,6 @@ pub trait Widget { self.refresh().log(); self.draw().log(); self.after_draw().log(); - self.get_core_mut()?.screen.flush().ok(); } Ok(()) } @@ -299,6 +298,7 @@ pub trait Widget { &self.get_header_drawlist().unwrap_or("".to_string()) + &self.get_footer_drawlist().unwrap_or("".to_string()); self.write_to_screen(&output).log(); + self.screen()?.flush().ok(); Ok(()) } -- cgit v1.2.3