mirror of
https://github.com/bobwen-dev/hunter
synced 2025-04-12 00:55:41 +02:00
listview fixes
This commit is contained in:
parent
3a2b02cba6
commit
ca521059e4
12
src/files.rs
12
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,
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
30
src/term.rs
30
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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user