shorten home path for display

This commit is contained in:
rabite 2019-03-17 01:50:38 +01:00
parent 8b1c4db9cf
commit 7011023bab
4 changed files with 34 additions and 4 deletions

View File

@ -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);

View File

@ -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<PathBuf> {
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())
}
}

View File

@ -4,6 +4,11 @@ use std::path::PathBuf;
use crate::fail::HResult;
pub fn home_path() -> HResult<PathBuf> {
let home = dirs_2::home_dir()?;
Ok(home)
}
pub fn hunter_path() -> HResult<PathBuf> {
let mut config_dir = dirs_2::config_dir()?;
config_dir.push("hunter/");

View File

@ -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(())
}