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:
parent
b7bbcff284
commit
1e6719fe33
@ -102,9 +102,6 @@ impl FileBrowser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for FileBrowser {
|
impl Widget for FileBrowser {
|
||||||
fn render(&self) -> Vec<String> {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
fn get_size(&self) -> &Size {
|
fn get_size(&self) -> &Size {
|
||||||
&self.columns.get_size()
|
&self.columns.get_size()
|
||||||
}
|
}
|
||||||
|
@ -89,15 +89,18 @@ where
|
|||||||
let xsize = self.get_size().xsize();
|
let xsize = self.get_size().xsize();
|
||||||
let sized_string = term::sized_string(&name, 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!(
|
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::highlight_color(),
|
||||||
term::cursor_left(size.to_string().width() + unit.width()),
|
term::cursor_left(size.to_string().width() + unit.width()),
|
||||||
size,
|
size,
|
||||||
@ -204,6 +207,14 @@ where
|
|||||||
None => self.show_status(""),
|
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> {
|
impl Widget for ListView<Files> {
|
||||||
@ -232,13 +243,7 @@ impl Widget for ListView<Files> {
|
|||||||
fn refresh(&mut self) {
|
fn refresh(&mut self) {
|
||||||
self.buffer = self.render();
|
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 {
|
fn get_drawlist(&self) -> String {
|
||||||
let mut output = term::reset();
|
let mut output = term::reset();
|
||||||
|
@ -108,9 +108,6 @@ impl<T> Widget for MillerColumns<T>
|
|||||||
where
|
where
|
||||||
T: Widget,
|
T: Widget,
|
||||||
{
|
{
|
||||||
fn render(&self) -> Vec<String> {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
fn get_size(&self) -> &Size {
|
fn get_size(&self) -> &Size {
|
||||||
&self.coordinates.size
|
&self.coordinates.size
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,6 @@ impl Previewer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for Previewer {
|
impl Widget for Previewer {
|
||||||
fn render(&self) -> Vec<String> {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
fn get_size(&self) -> &Size {
|
fn get_size(&self) -> &Size {
|
||||||
&self.coordinates.size
|
&self.coordinates.size
|
||||||
}
|
}
|
||||||
|
10
src/term.rs
10
src/term.rs
@ -1,5 +1,3 @@
|
|||||||
use unicode_width::UnicodeWidthStr;
|
|
||||||
|
|
||||||
use std::io::{Stdout, Write};
|
use std::io::{Stdout, Write};
|
||||||
use termion;
|
use termion;
|
||||||
use termion::screen::AlternateScreen;
|
use termion::screen::AlternateScreen;
|
||||||
@ -29,14 +27,14 @@ pub fn ysize() -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn sized_string(string: &str, xsize: u16) -> String {
|
pub fn sized_string(string: &str, xsize: u16) -> String {
|
||||||
let lenstr: String = string.chars().fold("".into(), |acc, ch| {
|
string.chars().fold("".to_string(), |acc, ch| {
|
||||||
if acc.width() + 1 >= xsize as usize {
|
let width: usize = unicode_width::UnicodeWidthStr::width_cjk(acc.as_str());
|
||||||
|
if width + 1 >= xsize as usize {
|
||||||
acc
|
acc
|
||||||
} else {
|
} else {
|
||||||
acc + &ch.to_string()
|
acc + &ch.to_string()
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
lenstr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do these as constants
|
// Do these as constants
|
||||||
|
@ -28,9 +28,6 @@ impl TextView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for TextView {
|
impl Widget for TextView {
|
||||||
fn render(&self) -> Vec<String> {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
fn get_size(&self) -> &Size {
|
fn get_size(&self) -> &Size {
|
||||||
&self.coordinates.size
|
&self.coordinates.size
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ use termion::event::{Event, Key, MouseEvent};
|
|||||||
use crate::coordinates::{Coordinates, Position, Size};
|
use crate::coordinates::{Coordinates, Position, Size};
|
||||||
|
|
||||||
pub trait Widget {
|
pub trait Widget {
|
||||||
fn render(&self) -> Vec<String>;
|
//fn render(&self) -> Vec<String>;
|
||||||
fn get_size(&self) -> &Size;
|
fn get_size(&self) -> &Size;
|
||||||
fn get_position(&self) -> &Position;
|
fn get_position(&self) -> &Position;
|
||||||
fn set_size(&mut self, size: Size);
|
fn set_size(&mut self, size: Size);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user