Skip to content

Commit

Permalink
update name to totally avoid strings
Browse files Browse the repository at this point in the history
  • Loading branch information
0jdxt committed Mar 28, 2021
1 parent 15e7c88 commit 2184e2d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 54 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ wild = "2.0.*"
globset = "0.4.*"
xdg = "2.1.*"
yaml-rust = "0.4.*"
termize = "0.1"
fxhash = "0.2.*"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
fxhash = "0.2.*"
termize = "0.1.1"

[target.'cfg(unix)'.dependencies]
users = "0.11.*"
Expand Down
10 changes: 4 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ use std::path::PathBuf;
pub struct Core {
flags: Flags,
icons: Icons,
//display: Display,
colors: Colors,
sorters: Vec<(SortOrder, sort::SortFn)>,
}

impl Core {
pub fn new(flags: Flags) -> Self {
// termsize allows us to know if the stdout is a tty or not.
// termize allows us to know if the stdout is a tty or not.
let tty_available = termize::dimensions().is_some();

#[cfg(not(target_os = "windows"))]
let console_color_ok = true;

#[cfg(target_os = "windows")]
#[cfg(windows)]
let console_color_ok = ansi_term::enable_ansi_support().is_ok();
#[cfg(unix)]
let console_color_ok = true;

let mut inner_flags = flags.clone();

Expand Down
47 changes: 18 additions & 29 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,13 @@ fn get_output<'a>(
Block::Date => block_vec.push(meta.date.render(colors, &flags)),
Block::Name => {
block_vec.extend(vec![
meta.name.render(colors, icons, &display_option),
meta.name.render(
colors,
icons,
&display_option,
&meta.metadata,
&flags.icons.separator.0,
),
meta.indicator.render(&flags),
]);
if !(flags.no_symlink.0 || flags.dereference.0 || flags.layout == Layout::Grid) {
Expand All @@ -285,37 +291,22 @@ fn get_output<'a>(
}

fn get_visible_width(input: &str) -> usize {
let mut nb_invisible_char = 0;

// If the input has color, do not compute the length contributed by the color to the actual length
for (idx, _) in input.match_indices("\u{1b}[") {
let (_, s) = input.split_at(idx);

let m_pos = s.find('m');
if let Some(len) = m_pos {
nb_invisible_char += len
}
}

UnicodeWidthStr::width(input) - nb_invisible_char
UnicodeWidthStr::width(input)
- input
.match_indices("\u{1b}[")
.map(|(i, _)| input.split_at(i).1.find('m').unwrap_or_default())
.sum::<usize>()
}

fn detect_size_lengths(metas: &[Meta], flags: &Flags) -> usize {
let mut max_value_length: usize = 0;

for meta in metas {
let value_len = meta.size.value_string(flags).len();

if value_len > max_value_length {
max_value_length = value_len;
}

max_value_length = max_value_length.max(meta.size.value_string(flags).len());
if Layout::Tree == flags.layout {
if let Some(subs) = &meta.content {
let sub_length = detect_size_lengths(&subs, flags);
if sub_length > max_value_length {
max_value_length = sub_length;
}
max_value_length = max_value_length.max(detect_size_lengths(&subs, flags));
}
}
}
Expand All @@ -328,7 +319,6 @@ fn get_padding_rules(metas: &[Meta], flags: &Flags) -> HashMap<Block, usize> {

if flags.blocks.0.contains(&Block::Size) {
let size_val = detect_size_lengths(&metas, &flags);

padding_rules.insert(Block::SizeValue, size_val);
}

Expand Down Expand Up @@ -372,7 +362,6 @@ mod tests {
.zip(&[22, 11, 15, 10, 6, 26, 4, 2])
};
}
>>>>>>> 0f3eb3e (general speed up)

#[test]
fn test_display_get_visible_width_without_icons() {
Expand Down Expand Up @@ -510,7 +499,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&Icons::new(icon::Theme::NoIcon),
);

assert_eq!("one.d\n├── .hidden\n└── two\n", output);
Expand Down Expand Up @@ -540,7 +529,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&Icons::new(icon::Theme::NoIcon),
);

let length_before_b = |i| -> usize {
Expand Down Expand Up @@ -579,7 +568,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&Icons::new(icon::Theme::NoIcon),
);

assert_eq!(output.lines().nth(1).unwrap().chars().nth(0).unwrap(), '└');
Expand Down Expand Up @@ -617,7 +606,7 @@ mod tests {
&metas,
&flags,
&Colors::new(color::Theme::NoColor),
&Icons::new(icon::Theme::NoIcon, " ".to_string()),
&Icons::new(icon::Theme::NoIcon),
);

assert!(output.ends_with("└── two\n"));
Expand Down
8 changes: 2 additions & 6 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ impl Meta {

if Display::All == flags.display && flags.layout != Layout::Tree {
let mut current_meta = self.clone();
current_meta.name.set_name(".");
current_meta.name.set_current_dir();

let mut parent_meta =
Self::from_path(&self.path.join(Component::ParentDir), flags.dereference.0)?;
parent_meta.name.set_name("..");
parent_meta.name.set_parent_dir();

content.push(current_meta);
content.push(parent_meta);
Expand Down Expand Up @@ -243,8 +243,4 @@ impl Meta {
content: None,
})
}

pub fn get_symlink(&self) -> &SymLink {
&self.symlink
}
}
19 changes: 14 additions & 5 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ pub enum DisplayOption<'a> {
None,
}

static CUR_DIR: &str = ".";
static PAR_DIR: &str = "..";

#[derive(Clone, Debug, Eq)]
pub struct Name {
// name is an option since we want to use the Cow<str> from path until we actually need a newly allocated String. See set_name and get_name for more
name: Option<String>,
// `name` is an option since we want to use the Cow from `path`.
// The only time we set the `name` to be different is for . and ..
// see set_current_dir, set_parent_dir and get_name.
name: Option<Cow<'static, str>>,
path: PathBuf,
file_type: FileType,
}
Expand All @@ -34,13 +39,17 @@ impl Name {
self.path.extension().map(|e| e.to_string_lossy())
}

pub fn set_name(&mut self, new_name: &str) {
self.name = Some(new_name.to_owned());
pub fn set_current_dir(&mut self) {
self.name = Some(Cow::from(CUR_DIR));
}

pub fn set_parent_dir(&mut self) {
self.name = Some(Cow::from(PAR_DIR));
}

pub fn get_name(&self) -> Cow<'_, str> {
match &self.name {
Some(name) => Cow::from(name),
Some(name) => name.clone(),
None => match self.path.file_name() {
Some(name) => name.to_string_lossy(),
None => self.path.to_string_lossy(),
Expand Down
6 changes: 0 additions & 6 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,6 @@ fn test_tree() {
cmd()
.arg(tmp.path())
.arg("--tree")
.arg("--color")
.arg("never")
.assert()
.stdout(predicate::str::is_match("├── one\n└── one.d\n └── two\n$").unwrap());
}
Expand All @@ -443,8 +441,6 @@ fn test_tree_all_not_show_self() {
.arg(tmp.path())
.arg("--tree")
.arg("--all")
.arg("--color")
.arg("never")
.assert()
.stdout(
predicate::str::is_match("├── one\n└── one.d\n ├── .hidden\n └── two\n$")
Expand Down Expand Up @@ -480,8 +476,6 @@ fn test_tree_d() {
.arg(tmp.path())
.arg("--tree")
.arg("-d")
.arg("--color")
.arg("never")
.assert()
.stdout(predicate::str::is_match("├── one.d\n│ └── one.d\n└── two.d\n$").unwrap());
}
Expand Down

0 comments on commit 2184e2d

Please sign in to comment.