1
0
mirror of https://github.com/bobwen-dev/hunter synced 2025-04-12 00:55:41 +02:00

got file sizes working in listview

This commit is contained in:
rabite 2019-01-21 17:47:58 +01:00
parent 724cc61680
commit 8ad1b657ec
6 changed files with 96 additions and 60 deletions

View File

@ -45,6 +45,23 @@ impl File {
// mtime: None
}
}
pub fn calculate_size(&self) -> (usize, String) {
let mut unit = 0;
let mut size = self.size.unwrap();
while size > 1024 {
size /= 1024;
unit += 1;
}
let unit = match unit {
0 => "",
1 => " KB",
2 => " GB",
3 => " TB",
4 => "wtf are you doing",
_ => ""
}.to_string();
(size, unit)
}
}
pub fn get_files(dir: &str) -> Result<Files, std::io::Error> {

View File

@ -17,12 +17,15 @@ pub struct HBox {
}
impl HBox {
pub fn new(widgets: Vec<Box<Widget>>) -> HBox {
pub fn new(widgets: Vec<Box<Widget>>,
dimensions: (u16, u16),
position: (u16, u16),
main: usize) -> HBox {
HBox {
dimensions: (100, 100),
position: (1, 1),
dimensions: dimensions,
position: position,
children: widgets,
main: 0
main: main
}
}
}
@ -55,6 +58,13 @@ impl Widget for HBox {
fn get_position(&self) -> (u16, u16) {
self.position
}
fn set_dimensions(&mut self, size: (u16, u16)) {
self.dimensions = size;
}
fn set_position(&mut self, position: (u16, u16)) {
self.position = position;
}
fn on_event(&mut self, event: Event) {
self.children[self.main].on_event(event);

View File

@ -1,9 +1,11 @@
use unicode_width::{UnicodeWidthStr};
use termion::event::{Key,Event};
use crate::term;
use crate::files::Files;
use crate::widget::Widget;
use crate::window::{STATUS_BAR_MARGIN, HEADER_MARGIN};
// Maybe also buffer drawlist for efficiency when it doesn't change every draw
pub struct ListView<T> {
pub content: T,
@ -57,6 +59,26 @@ impl<T: 'static> ListView<T> where ListView<T>: Widget {
self.selection += 1;
}
fn render_line(&self, name: &str, size: usize, unit: &str) -> String {
let (xsize, _) = self.get_dimensions();
let sized_string = term::sized_string(name, xsize);
let padding = xsize - sized_string.width() as u16;
format!(
"{}{}{:padding$}{}{}{}{}",
term::normal_color(),
sized_string,
" ",
term::highlight_color(),
term::cursor_left(size.to_string().width() + unit.width()),
size,
unit,
padding = padding as usize
)
}
}
impl Widget for ListView<Files> {
@ -66,15 +88,22 @@ impl Widget for ListView<Files> {
fn get_position(&self) -> (u16, u16) {
self.position
}
fn set_dimensions(&mut self, size: (u16, u16)) {
self.dimensions = size;
}
fn set_position(&mut self, position: (u16, u16)) {
self.position = position;
}
fn refresh(&mut self) {
self.buffer = self.render();
}
fn render(&self) -> Vec<String> {
self.content.iter().map(|file| {
self.render_line(&file.name,
&format!("{:?}", file.size),
false)
let (size, unit) = file.calculate_size();
self.render_line(&file.name, size, &unit)
}).collect()
}

View File

@ -1,3 +1,5 @@
use unicode_width::UnicodeWidthStr;
use std::io::{Stdout, Write};
use termion;
use termion::screen::AlternateScreen;
@ -16,6 +18,10 @@ pub trait ScreenExt: Write {
impl ScreenExt for AlternateScreen<Box<Stdout>> {}
pub fn size() -> (u16, u16) {
termion::terminal_size().unwrap()
}
pub fn xsize() -> usize {
let (xsize, _) = termion::terminal_size().unwrap();
xsize as usize
@ -26,6 +32,17 @@ pub fn ysize() -> usize {
ysize as usize
}
pub fn sized_string(string: &str, xsize: u16) -> String {
let lenstr: String = string.chars().fold("".into(), |acc,ch| {
if acc.width() + 1 >= xsize as usize { acc }
else { acc + &ch.to_string() }
});
lenstr
}
// Do these as constants
pub fn highlight_color() -> String {
format!(
"{}{}",
@ -42,6 +59,7 @@ pub fn normal_color() -> String {
)
}
pub fn cursor_left(n: usize) -> String {
format!("{}", termion::cursor::Left(n as u16))
}
@ -81,4 +99,3 @@ pub fn header_color() -> String {
pub fn status_bg() -> String {
format!("{}", termion::color::Bg(termion::color::LightBlue))
}

View File

@ -1,45 +1,13 @@
use termion::event::{Key, MouseEvent, Event};
use unicode_width::{UnicodeWidthStr};
use crate::term;
pub trait Widget {
fn render(&self) -> Vec<String>;
fn get_dimensions(&self) -> (u16, u16);
fn get_position(&self) -> (u16, u16);
fn render_line(&self, left: &str, right: &str, highlight: bool) -> String {
let (xsize, _) = self.get_dimensions();
let text_color = match highlight {
true => term::highlight_color(),
false => term::normal_color(),
};
let sized_string = self.sized_string(left);
let padding = xsize - sized_string.width() as u16;
format!(
"{}{}{:padding$}{}{}{}",
text_color,
sized_string,
" ",
term::highlight_color(),
term::cursor_left(right.width()),
right,
padding = padding as usize
)
}
// fn add_highlight(&self, line: &str) -> String {
// line.to_string()
// }
fn set_dimensions(&mut self, size: (u16, u16));
fn set_position(&mut self, position: (u16, u16));
fn render_header(&self) -> String;
fn sized_string(&self, string: &str) -> String {
let (xsize, _) = self.get_dimensions();
let lenstr: String = string.chars().fold("".into(), |acc,ch| {
if acc.width() + 1 >= xsize as usize { acc }
else { acc + &ch.to_string() }
});
lenstr
}
fn on_event(&mut self, event: Event) {
match event {
@ -74,8 +42,6 @@ pub trait Widget {
}
}
fn show_status(&mut self, status: &str) {
crate::window::show_status(status);
}

View File

@ -1,6 +1,5 @@
use std::cell::RefCell;
use std::io::{stdin, stdout, Stdout, Write};
use std::process::exit;
use std::rc::*;
use std::sync::{Arc, Mutex};
@ -23,8 +22,6 @@ where T: Widget
pub dimensions: (u16, u16),
}
pub const HEADER_MARGIN: usize = 1;
pub const STATUS_BAR_MARGIN: usize = 2;
impl<T> Window<T>
where