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

minimize allocations in render_fn

This commit is contained in:
rabite 2020-01-27 02:12:58 +01:00
parent 89aef8ba76
commit 874e7e306f
3 changed files with 87 additions and 54 deletions

View File

@ -671,7 +671,7 @@ impl FileBrowser {
// Don't even call previewer on empty files to save CPU cycles // Don't even call previewer on empty files to save CPU cycles
match (file.is_dir(), file.calculate_size()) { match (file.is_dir(), file.calculate_size()) {
(false, Ok((size, unit))) => if size == 0 && unit.as_str() == "" { (false, Ok((size, unit))) => if size == 0 && unit == "" {
self.preview_widget_mut()?.set_stale().log(); self.preview_widget_mut()?.set_stale().log();
return Ok(()); return Ok(());
}, },

View File

@ -988,9 +988,9 @@ impl File {
} }
} }
pub fn calculate_size(&self) -> HResult<(u64, String)> { pub fn calculate_size(&self) -> HResult<(u32, &str)> {
if let Some(ref dirsize) = self.dirsize { if let Some(ref dirsize) = self.dirsize {
return Ok((dirsize.value.clone()? as u64, "".to_string())) return Ok((*dirsize.value.as_ref().unwrap_or(&0) as u32, ""))
} }
@ -1008,9 +1008,9 @@ impl File {
4 => " TB", 4 => " TB",
5 => " wtf are you doing", 5 => " wtf are you doing",
_ => "", _ => "",
} };
.to_string();
Ok((size, unit)) Ok((size as u32, unit))
} }
pub fn get_mime(&self) -> Option<mime_guess::Mime> { pub fn get_mime(&self) -> Option<mime_guess::Mime> {

View File

@ -762,73 +762,106 @@ impl ListView<Files>
#[allow(trivial_bounds)] #[allow(trivial_bounds)]
fn render_line_fn(&self) -> impl Fn(&File) -> String { fn render_line_fn(&self) -> impl Fn(&File) -> String {
use std::fmt::Write;
let xsize = self.get_coordinates().unwrap().xsize(); let xsize = self.get_coordinates().unwrap().xsize();
let icons = self.core.config().icons; let icons = self.core.config().icons;
move |file| -> String { move |file| -> String {
let icon = if icons { let mut line = String::with_capacity(500);
file.icon()
} else { "" };
let name = String::from(icon) + &file.name; let icon = match icons {
let (size, unit) = file.calculate_size().unwrap_or((0, "".to_string())); true => file.icon(),
false => ""
};
let name = &file.name;
let size = file.calculate_size();
let (size, unit) = match size {
Ok((size, unit)) => (size, unit),
Err(_) => (0 as u32, "")
};
let tag = match file.is_tagged() { let tag = match file.is_tagged() {
Ok(true) => term::color_red() + "*", Ok(true) => Some(term::color_red() + "*"),
_ => "".to_string() _ => None
}; };
let tag_len = if tag != "" { 1 } else { 0 }; let tag = tag.as_ref()
.map(|t| t.as_str())
.unwrap_or("");
let selection_gap = " ".to_string(); let tag_len = match tag {
let (name, selection_color) = if file.is_selected() { "*" => 1,
(selection_gap + &name, crate::term::color_yellow()) "" => 0,
} else { (name.clone(), "".to_string()) }; _ => unreachable!()
};
let (link_indicator, link_indicator_len) = if file.target.is_some() { let selection_color = crate::term::color_yellow();
(format!("{}{}{}", let (selection_gap, selection_color) = match file.is_selected() {
true => (" ", selection_color.as_str()),
false => ("", "")
};
let (link_indicator, link_indicator_len) = match file.target {
Some(_) => (Some(format!("{}{}{}",
term::color_yellow(), term::color_yellow(),
"--> ".to_string(), "--> ",
term::highlight_color()), term::highlight_color())), Some(4)),
4) None => (None, None)
} else { ("".to_string(), 0) }; };
let link_indicator = link_indicator.as_ref()
.map(|l| l.as_str())
.unwrap_or("");
let link_indicator_len = link_indicator_len.unwrap_or(0);
let sized_string = term::sized_string(&name, xsize); let sized_string = term::sized_string(&name, xsize);
let size_pos = xsize - (size.to_string().len() as u16
+ unit.to_string().len() as u16 let size = size.to_string();
+ link_indicator_len); let size_pos = xsize - (size.len() as u16 +
unit.len() as u16 +
link_indicator_len as u16);
let padding = sized_string.len() - sized_string.width_cjk(); let padding = sized_string.len() - sized_string.width_cjk();
let padding = xsize - padding as u16; let padding = xsize - padding as u16;
let padding = padding - tag_len; let padding = padding - tag_len;
format!( write!(&mut line, "{}", termion::cursor::Save).unwrap();
"{}{}{}{}{}{}{}{}",
termion::cursor::Save,
match &file.color { match &file.color {
Some(color) => format!("{}{}{}{:padding$}{}", Some(color) => write!(&mut line,
"{}{}{}{}{}{:padding$}{}",
tag, tag,
term::from_lscolor(color), term::from_lscolor(color),
selection_color, selection_color,
selection_gap,
icon,
&sized_string, &sized_string,
term::normal_color(), term::normal_color(),
padding = padding as usize), padding = padding as usize),
_ => format!("{}{}{}{:padding$}{}", None => write!(&mut line,
"{}{}{}{}{}{:padding$}{}",
tag, tag,
term::normal_color(), term::normal_color(),
selection_color, selection_color,
selection_gap,
icon,
&sized_string, &sized_string,
term::normal_color(), term::normal_color(),
padding = padding as usize), padding = padding as usize),
} , }.unwrap();
write!(&mut line,
"{}{}{}{}{}{}",
termion::cursor::Restore, termion::cursor::Restore,
termion::cursor::Right(size_pos), termion::cursor::Right(size_pos),
link_indicator, link_indicator,
term::highlight_color(), term::highlight_color(),
size, size,
unit unit).unwrap();
)
line
} }
} }