diff options
author | rabite <rabite@posteo.de> | 2019-01-29 15:47:45 +0100 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-01-29 15:47:45 +0100 |
commit | ca521059e46ec5b9ff04bfad60284b9eb7ebcb1b (patch) | |
tree | 73b5e08b863d0e85a3ba1ea12750a3c9705ff831 /src | |
parent | 3a2b02cba6bd2c74a96923354dc205ffa1758a94 (diff) |
listview fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/files.rs | 12 | ||||
-rw-r--r-- | src/listview.rs | 42 | ||||
-rw-r--r-- | src/term.rs | 30 |
3 files changed, 55 insertions, 29 deletions
diff --git a/src/files.rs b/src/files.rs index 1d652e1..1473fd5 100644 --- a/src/files.rs +++ b/src/files.rs @@ -49,12 +49,12 @@ impl Files { let size = meta.len() / 1024; let mtime = meta.modified()?; - let style + let color = match COLORS.style_for_path_with_metadata(file.path(), Some(&meta)) { - Some(style) => Some(style.clone()), + Some(style) => { style.clone().foreground }, None => None }; - let file = File::new(&name, path, kind, size as usize, mtime, style); + let file = File::new(&name, path, kind, size as usize, mtime, color); files.push(file) } @@ -151,7 +151,7 @@ pub struct File { pub size: Option<usize>, pub kind: Kind, pub mtime: SystemTime, - pub style: Option<Style>, + pub color: Option<lscolors::Color>, // owner: Option<String>, // group: Option<String>, // flags: Option<String>, @@ -164,14 +164,14 @@ impl File { kind: Kind, size: usize, mtime: SystemTime, - style: Option<Style>) -> File { + color: Option<lscolors::Color>) -> File { File { name: name.to_string(), path: path, size: Some(size), kind: kind, mtime: mtime, - style: style + color: color // owner: None, // group: None, // flags: None, diff --git a/src/listview.rs b/src/listview.rs index c6d200a..76bb240 100644 --- a/src/listview.rs +++ b/src/listview.rs @@ -79,29 +79,32 @@ impl<T: 'static> ListView<T> where ListView<T>: Widget { fn render_line(&self, file: &File) -> String { let name = &file.name; let (size, unit) = file.calculate_size(); - + let xsize = self.get_size().xsize(); let sized_string = term::sized_string(&name, xsize); - let padding = xsize - sized_string.width() as u16; - let styled_string = match &file.style { - Some(style) => style.to_ansi_term_style().paint(sized_string).to_string(), - _ => format!("{}{}", term::normal_color(), sized_string), + let padding = xsize as usize - sized_string.width(); + let padded_string = format!("{:padding$}", + sized_string, + padding = xsize as usize); + let styled_string = match &file.color { + Some(color) => format!("{}{}", + term::from_lscolor(color), + &padded_string), + _ => format!("{}{}", + term::normal_color(), + padded_string) }; - + format!( - "{}{:padding$}{}{}{}{}", + "{}{}{}{}{}", styled_string, - " ", term::highlight_color(), term::cursor_left(size.to_string().width() + unit.width()), size, - unit, - padding = padding as usize) - + unit) } - } impl ListView<Files> where @@ -140,11 +143,11 @@ impl ListView<Files> where pub fn goto_path(&mut self, path: &Path) { match crate::files::Files::new_from_path(path){ - Ok(files) => { + Ok(files) => { self.content = files; self.selection = 0; self.offset = 0; - self.refresh(); + self.refresh(); }, Err(err) => { self.show_status(&format!("Can't open this path: {}", err)); @@ -228,10 +231,8 @@ impl Widget for ListView<Files> { fn get_drawlist(&self) -> String { let mut output = term::reset(); - let (xsize, ysize) = self.coordinates.size().size(); + let (xsize, ysize) = self.get_size().size(); let (xpos, ypos) = self.coordinates.position().position(); - output += &term::reset(); - for (i, item) in self.buffer .iter() @@ -253,8 +254,11 @@ impl Widget for ListView<Files> { if ysize as usize > self.buffer.len() { let start_y = self.buffer.len() + 1 + ypos as usize; - for i in start_y..ysize as usize { - output += &format!("{}{:xsize$}{}", term::gotoy(i), " ", xsize = xsize as usize); + for i in start_y..(ysize+2) as usize { + output += &format!("{}{:xsize$}", + term::goto_xy(xpos,i as u16), + " ", + xsize = xsize as usize); } } diff --git a/src/term.rs b/src/term.rs index 022f16b..a8ae0e0 100644 --- a/src/term.rs +++ b/src/term.rs @@ -45,20 +45,42 @@ pub fn sized_string(string: &str, xsize: u16) -> String { pub fn highlight_color() -> String { format!( - "{}{}", + "{}", termion::color::Fg(termion::color::LightGreen), - termion::color::Bg(termion::color::Black) + //termion::color::Bg(termion::color::Black) ) } pub fn normal_color() -> String { format!( - "{}{}", + "{}", termion::color::Fg(termion::color::LightBlue), - termion::color::Bg(termion::color::Black) + //termion::color::Bg(termion::color::Black) ) } +pub fn from_lscolor(color: &lscolors::Color) -> String { + match color { + lscolors::Color::Black => { + format!("{}", termion::color::Fg(termion::color::Black)) }, + lscolors::Color::Red => { + format!("{}", termion::color::Fg(termion::color::Red)) } + lscolors::Color::Green => { + format!("{}",termion::color::Fg(termion::color::Green)) } + lscolors::Color::Yellow => { + format!("{}",termion::color::Fg(termion::color::Yellow)) } + lscolors::Color::Blue => { + format!("{}",termion::color::Fg(termion::color::Blue)) } + lscolors::Color::Magenta => { + format!("{}", termion::color::Fg(termion::color::Magenta)) } + lscolors::Color::Cyan => { + format!("{}",termion::color::Fg(termion::color::Cyan)) } + lscolors::Color::White => { + format!("{}",termion::color::Fg(termion::color::White)) } , + _ => { format!("{}", normal_color()) } + } +} + pub fn cursor_left(n: usize) -> String { format!("{}", termion::cursor::Left(n as u16)) |