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

improved string handling

This commit is contained in:
rabite 2019-02-01 01:37:16 +01:00
parent b7bbcff284
commit 1e6719fe33
7 changed files with 24 additions and 33 deletions

View File

@ -102,9 +102,6 @@ impl FileBrowser {
}
impl Widget for FileBrowser {
fn render(&self) -> Vec<String> {
vec![]
}
fn get_size(&self) -> &Size {
&self.columns.get_size()
}

View File

@ -89,15 +89,18 @@ where
let xsize = self.get_size().xsize();
let sized_string = term::sized_string(&name, xsize);
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!(
"{}{}{}{}{}",
styled_string,
match &file.color {
Some(color) => format!("{}{:padding$}",
term::from_lscolor(color),
&sized_string,
padding = xsize as usize),
_ => format!("{}{:padding$}",
term::normal_color(),
&sized_string,
padding = xsize as usize),
} ,
term::highlight_color(),
term::cursor_left(size.to_string().width() + unit.width()),
size,
@ -204,6 +207,14 @@ where
None => self.show_status(""),
}
}
fn render(&self) -> Vec<String> {
self.content
.files
.par_iter()
.map(|file| self.render_line(&file))
.collect()
}
}
impl Widget for ListView<Files> {
@ -232,13 +243,7 @@ impl Widget for ListView<Files> {
fn refresh(&mut self) {
self.buffer = self.render();
}
fn render(&self) -> Vec<String> {
self.content
.files
.par_iter()
.map(|file| self.render_line(&file))
.collect()
}
fn get_drawlist(&self) -> String {
let mut output = term::reset();

View File

@ -108,9 +108,6 @@ impl<T> Widget for MillerColumns<T>
where
T: Widget,
{
fn render(&self) -> Vec<String> {
vec![]
}
fn get_size(&self) -> &Size {
&self.coordinates.size
}

View File

@ -25,9 +25,6 @@ impl Previewer {
}
impl Widget for Previewer {
fn render(&self) -> Vec<String> {
vec![]
}
fn get_size(&self) -> &Size {
&self.coordinates.size
}

View File

@ -1,5 +1,3 @@
use unicode_width::UnicodeWidthStr;
use std::io::{Stdout, Write};
use termion;
use termion::screen::AlternateScreen;
@ -29,14 +27,14 @@ pub fn ysize() -> 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 {
string.chars().fold("".to_string(), |acc, ch| {
let width: usize = unicode_width::UnicodeWidthStr::width_cjk(acc.as_str());
if width + 1 >= xsize as usize {
acc
} else {
acc + &ch.to_string()
}
});
lenstr
})
}
// Do these as constants

View File

@ -28,9 +28,6 @@ impl TextView {
}
impl Widget for TextView {
fn render(&self) -> Vec<String> {
vec![]
}
fn get_size(&self) -> &Size {
&self.coordinates.size
}

View File

@ -3,7 +3,7 @@ use termion::event::{Event, Key, MouseEvent};
use crate::coordinates::{Coordinates, Position, Size};
pub trait Widget {
fn render(&self) -> Vec<String>;
//fn render(&self) -> Vec<String>;
fn get_size(&self) -> &Size;
fn get_position(&self) -> &Position;
fn set_size(&mut self, size: Size);