shorten HOME and show indicator for links

This commit is contained in:
rabite 2019-03-17 06:05:31 +01:00
parent a7abc0d645
commit cc8020ee8c
3 changed files with 55 additions and 24 deletions

View File

@ -8,7 +8,7 @@ use std::time::Duration;
use std::path::PathBuf;
use std::collections::HashMap;
use crate::files::{File, Files};
use crate::files::{File, Files, ShortPaths};
use crate::listview::ListView;
use crate::miller_columns::MillerColumns;
use crate::widget::Widget;
@ -347,12 +347,8 @@ impl FileBrowser {
}
pub fn set_title(&self) -> HResult<()> {
let path = match self.cwd.short_path() {
Ok(path) => path,
Err(_) => self.cwd.path.clone()
};
let path = self.cwd.short_string();
let path = path.to_string_lossy().to_string();
self.screen()?.set_title(&path)?;
Ok(())
}
@ -658,6 +654,9 @@ impl FileBrowser {
let user = file.pretty_user().unwrap_or("NOUSER".into());
let group = file.pretty_group().unwrap_or("NOGROUP".into());
let mtime = file.pretty_mtime().unwrap_or("NOMTIME".into());
let target = if let Some(target) = file.target {
"--> ".to_string() + &target.short_string()
} else { "".to_string() };
let main_widget = self.main_widget()?.widget()?;
let selection = main_widget.lock()?.as_ref().unwrap().get_selection();
@ -671,8 +670,17 @@ impl FileBrowser {
let count_xpos = xsize - file_count.len() as u16;
let count_ypos = ypos + self.get_coordinates()?.ysize();
let status = format!("{} {}:{} {} {} {}", permissions, user, group, mtime,
crate::term::goto_xy(count_xpos, count_ypos), file_count);
let status = format!("{} {}:{} {}{} {}{}{} {}{}",
permissions,
user,
group,
crate::term::header_color(),
mtime,
crate::term::color_yellow(),
target,
crate::term::header_color(),
crate::term::goto_xy(count_xpos, count_ypos),
file_count);
Ok(status)
}
}
@ -693,12 +701,9 @@ impl Widget for FileBrowser {
crate::term::highlight_color() } else {
crate::term::from_lscolor(file.color.as_ref().unwrap()) };
let path = match self.cwd.short_path() {
Ok(path) => path,
Err(_) => file.path
};
let path = self.cwd.short_string();
let mut path = path.to_string_lossy().to_string();
let mut path = path;
if &path == "" { path.clear(); }
if &path == "~/" { path.pop(); }
if &path == "/" { path.pop(); }

View File

@ -350,6 +350,7 @@ pub struct File {
pub name: String,
pub path: PathBuf,
pub kind: Kind,
pub target: Option<PathBuf>,
pub color: Option<lscolors::Color>,
pub meta: Option<std::fs::Metadata>,
pub selected: bool,
@ -368,6 +369,7 @@ impl File {
name: name.to_string(),
kind: if path.is_dir() { Kind::Directory } else { Kind::File },
path: path,
target: None,
meta: None,
color: None,
selected: false,
@ -404,9 +406,13 @@ impl File {
let meta = std::fs::symlink_metadata(&self.path)?;
let color = self.get_color(&meta);
let target = if meta.file_type().is_symlink() {
self.path.read_link().ok()
} else { None };
self.meta = Some(meta);
self.color = color;
self.target = target;
Ok(())
}
@ -603,14 +609,25 @@ impl File {
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())
pub fn short_string(&self) -> String {
self.path.short_string()
}
}
pub trait ShortPaths {
fn short_string(&self) -> String;
}
impl ShortPaths for PathBuf {
fn short_string(&self) -> String {
if let Ok(home) = crate::paths::home_path() {
if let Ok(short) = self.strip_prefix(home) {
let mut path = PathBuf::from("~");
path.push(short);
return path.to_string_lossy().to_string();
}
}
return self.to_string_lossy().to_string();
}
}

View File

@ -335,17 +335,25 @@ impl ListView<Files>
(selection_gap + name, crate::term::color_yellow())
} else { (name.clone(), "".to_string()) };
let (link_indicator, link_indicator_len) = if file.target.is_some() {
(format!("{}{}{}",
term::color_yellow(),
"--> ".to_string(),
term::highlight_color()),
4)
} else { ("".to_string(), 0) };
let xsize = self.get_coordinates().unwrap().xsize();
let sized_string = term::sized_string(&name, xsize);
let size_pos = xsize - (size.to_string().len() as u16
+ unit.to_string().len() as u16);
+ unit.to_string().len() as u16
+ link_indicator_len);
let padding = sized_string.len() - sized_string.width_cjk();
let padding = xsize - padding as u16;
let padding = padding - tag_len;
format!(
"{}{}{}{}{}{}{}",
"{}{}{}{}{}{}{}{}",
termion::cursor::Save,
match &file.color {
Some(color) => format!("{}{}{}{:padding$}{}",
@ -365,6 +373,7 @@ impl ListView<Files>
} ,
termion::cursor::Restore,
termion::cursor::Right(size_pos),
link_indicator,
term::highlight_color(),
size,
unit