Fix: Remove first space char

Before we assumed 5 chars for printing the size of a node. This change
allows us to check if we need 5 chars or if we can manage with 4 which
may save us a character.

(1023B = 5 chars, 1.2K = 4 chars)

Note: Mac test runners create very long filenames in tmp directories. To
fix this we must either
1) Tell the terminal it has a very wide width
2) Duplicate the code to reduce the size of the filename (add .. at the
   end).

This commit changes from (2) to (1)
This commit is contained in:
andy.boot
2022-08-18 15:19:26 +01:00
parent a7fbcb8156
commit b62f35291d
4 changed files with 63 additions and 75 deletions
+10 -2
View File
@@ -130,7 +130,7 @@ pub fn draw_it(
let max_size = biggest.size; let max_size = biggest.size;
max_size.separate_with_commas().chars().count() max_size.separate_with_commas().chars().count()
} else { } else {
5 // Under normal usage we need 5 chars to display the size of a directory find_biggest_size_str(&root_node, iso)
}; };
assert!( assert!(
@@ -182,6 +182,14 @@ pub fn draw_it(
} }
} }
fn find_biggest_size_str(node: &DisplayNode, iso: bool) -> usize {
let mut mx = human_readable_number(node.size, iso).chars().count();
for n in node.children.iter() {
mx = max(mx, find_biggest_size_str(n, iso));
}
mx
}
fn find_longest_dir_name( fn find_longest_dir_name(
node: &DisplayNode, node: &DisplayNode,
indent: usize, indent: usize,
@@ -349,7 +357,7 @@ fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayD
human_readable_number(node.size, display_data.iso) human_readable_number(node.size, display_data.iso)
}; };
let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count(); let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count();
let output = output + " ".repeat(spaces_to_add).as_str(); let output = " ".repeat(spaces_to_add) + output.as_str();
if is_biggest && display_data.colors_on { if is_biggest && display_data.colors_on {
format!("{}", Red.paint(output)) format!("{}", Red.paint(output))
+32 -32
View File
@@ -82,19 +82,19 @@ fn main_output() -> Vec<String> {
// Some linux currently thought to be Manjaro, Arch // Some linux currently thought to be Manjaro, Arch
// Although probably depends on how drive is formatted // Although probably depends on how drive is formatted
let mac_and_some_linux = r#" let mac_and_some_linux = r#"
0B ┌── a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% 0B ┌── a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0%
4.0K ├── hello_file│████████████████████████████████████████████████ │ 100% 4.0K ├── hello_file│████████████████████████████████████████████████ │ 100%
4.0K ┌─┴ many │████████████████████████████████████████████████ │ 100% 4.0K ┌─┴ many │████████████████████████████████████████████████ │ 100%
4.0K ┌─┴ test_dir │████████████████████████████████████████████████ │ 100% 4.0K ┌─┴ test_dir │████████████████████████████████████████████████ │ 100%
"# "#
.trim() .trim()
.to_string(); .to_string();
let ubuntu = r#" let ubuntu = r#"
0B ┌── a_file │ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% 0B ┌── a_file │ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0%
4.0K ├── hello_file│ ░░░░░░░░░░░░░░░░█████████████████ │ 33% 4.0K ├── hello_file│ ░░░░░░░░░░░░░░░░█████████████████ │ 33%
8.0K ┌─┴ many │ █████████████████████████████████ │ 67% 8.0K ┌─┴ many │ █████████████████████████████████ │ 67%
12K ┌─┴ test_dir │████████████████████████████████████████████████ │ 100% 12K ┌─┴ test_dir │████████████████████████████████████████████████ │ 100%
"# "#
.trim() .trim()
.to_string(); .to_string();
@@ -111,18 +111,18 @@ pub fn test_main_long_paths() {
fn main_output_long_paths() -> Vec<String> { fn main_output_long_paths() -> Vec<String> {
let mac_and_some_linux = r#" let mac_and_some_linux = r#"
0B ┌── /tmp/test_dir/many/a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% 0B ┌── /tmp/test_dir/many/a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0%
4.0K ├── /tmp/test_dir/many/hello_file│█████████████████████████████ │ 100% 4.0K ├── /tmp/test_dir/many/hello_file│█████████████████████████████ │ 100%
4.0K ┌─┴ /tmp/test_dir/many │█████████████████████████████ │ 100% 4.0K ┌─┴ /tmp/test_dir/many │█████████████████████████████ │ 100%
4.0K ┌─┴ /tmp/test_dir │█████████████████████████████ │ 100% 4.0K ┌─┴ /tmp/test_dir │█████████████████████████████ │ 100%
"# "#
.trim() .trim()
.to_string(); .to_string();
let ubuntu = r#" let ubuntu = r#"
0B ┌── /tmp/test_dir/many/a_file │ ░░░░░░░░░░░░░░░░░░░█ │ 0% 0B ┌── /tmp/test_dir/many/a_file │ ░░░░░░░░░░░░░░░░░░░█ │ 0%
4.0K ├── /tmp/test_dir/many/hello_file│ ░░░░░░░░░░██████████ │ 33% 4.0K ├── /tmp/test_dir/many/hello_file│ ░░░░░░░░░░██████████ │ 33%
8.0K ┌─┴ /tmp/test_dir/many │ ████████████████████ │ 67% 8.0K ┌─┴ /tmp/test_dir/many │ ████████████████████ │ 67%
12K ┌─┴ /tmp/test_dir │█████████████████████████████ │ 100% 12K ┌─┴ /tmp/test_dir │█████████████████████████████ │ 100%
"# "#
.trim() .trim()
.to_string(); .to_string();
@@ -139,24 +139,24 @@ pub fn test_substring_of_names_and_long_names() {
fn no_substring_of_names_output() -> Vec<String> { fn no_substring_of_names_output() -> Vec<String> {
let ubuntu = " let ubuntu = "
0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goe.. 0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goes..
4.0K ├── dir_name_clash 4.0K ├── dir_name_clash
4.0K │ ┌── hello 4.0K │ ┌── hello
8.0K ├─┴ dir 8.0K ├─┴ dir
4.0K │ ┌── hello 4.0K │ ┌── hello
8.0K ├─┴ dir_substring 8.0K ├─┴ dir_substring
24K ┌─┴ test_dir2 24K ┌─┴ test_dir2
" "
.trim() .trim()
.into(); .into();
let mac_and_some_linux = " let mac_and_some_linux = "
0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goe.. 0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goes..
4.0K │ ┌── hello 4.0K │ ┌── hello
4.0K ├─┴ dir 4.0K ├─┴ dir
4.0K ├── dir_name_clash 4.0K ├── dir_name_clash
4.0K │ ┌── hello 4.0K │ ┌── hello
4.0K ├─┴ dir_substring 4.0K ├─┴ dir_substring
12K ┌─┴ test_dir2 12K ┌─┴ test_dir2
" "
.trim() .trim()
@@ -176,15 +176,15 @@ fn unicode_dir() -> Vec<String> {
let ubuntu = " let ubuntu = "
0B ┌── ラウトは難しいです!.japan│ █ │ 0% 0B ┌── ラウトは難しいです!.japan│ █ │ 0%
0B ├── 👩.unicode │ █ │ 0% 0B ├── 👩.unicode │ █ │ 0%
4.0K ┌─┴ test_dir_unicode │██████████████████████████████████ │ 100% 4.0K ┌─┴ test_dir_unicode │██████████████████████████████████ │ 100%
" "
.trim() .trim()
.into(); .into();
let mac_and_some_linux = " let mac_and_some_linux = "
0B ┌── ラウトは難しいです!.japan│ █ │ 0% 0B ┌── ラウトは難しいです!.japan│ █ │ 0%
0B ├── 👩.unicode │ █ │ 0% 0B ├── 👩.unicode │ █ │ 0%
0B ┌─┴ test_dir_unicode │ █ │ 0% 0B ┌─┴ test_dir_unicode │ █ │ 0%
" "
.trim() .trim()
.into(); .into();
+2 -2
View File
@@ -73,8 +73,8 @@ pub fn test_ignore_dir() {
#[test] #[test]
pub fn test_with_bad_param() { pub fn test_with_bad_param() {
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let stderr = cmd.arg("-").unwrap().stderr; let result = cmd.arg("bad_place").unwrap();
let stderr = str::from_utf8(&stderr).unwrap(); let stderr = str::from_utf8(&result.stderr).unwrap();
assert!(stderr.contains("Did not have permissions for all directories")); assert!(stderr.contains("Did not have permissions for all directories"));
} }
+12 -32
View File
@@ -1,41 +1,15 @@
use assert_cmd::Command; use assert_cmd::Command;
use std::cmp::max;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::PathBuf; use std::path::PathBuf;
use std::str; use std::str;
use terminal_size::{terminal_size, Height, Width};
use unicode_width::UnicodeWidthStr;
use tempfile::Builder; use tempfile::Builder;
use tempfile::TempDir; use tempfile::TempDir;
// File sizes differ on both platform and on the format of the disk. // File sizes differ on both platform and on the format of the disk.
// Windows: `ln` is not usually an available command; creation of symbolic links requires special enhanced permissions // Windows: `ln` is not usually an available command; creation of symbolic links requires special enhanced permissions
fn get_width_of_terminal() -> u16 {
if let Some((Width(w), Height(_h))) = terminal_size() {
max(w, 80)
} else {
80
}
}
// Mac test runners create tmp files with very long names, hence it may be shortened in the output
fn get_file_name(name: String) -> String {
let terminal_plus_buffer = (get_width_of_terminal() - 12) as usize;
if UnicodeWidthStr::width(&*name) > terminal_plus_buffer {
let trimmed_name = name
.chars()
.take(terminal_plus_buffer - 2)
.collect::<String>();
trimmed_name + ".."
} else {
name
}
}
fn build_temp_file(dir: &TempDir) -> PathBuf { fn build_temp_file(dir: &TempDir) -> PathBuf {
let file_path = dir.path().join("notes.txt"); let file_path = dir.path().join("notes.txt");
let mut file = File::create(&file_path).unwrap(); let mut file = File::create(&file_path).unwrap();
@@ -60,12 +34,16 @@ pub fn test_soft_sym_link() {
.output(); .output();
assert!(c.is_ok()); assert!(c.is_ok());
let c = format!(" ├── {}", get_file_name(link_name_s.into())); let c = format!(" ├── {}", link_name_s);
let b = format!(" ┌── {}", get_file_name(file_path_s.into())); let b = format!(" ┌── {}", file_path_s);
let a = format!("─┴ {}", dir_s); let a = format!("─┴ {}", dir_s);
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd.arg("-p").arg("-c").arg("-s").arg(dir_s).unwrap().stdout; // Mac test runners create long filenames in tmp directories
let output = cmd
.args(["-p", "-c", "-s", "-w 999", dir_s])
.unwrap()
.stdout;
let output = str::from_utf8(&output).unwrap(); let output = str::from_utf8(&output).unwrap();
@@ -90,11 +68,12 @@ pub fn test_hard_sym_link() {
.output(); .output();
assert!(c.is_ok()); assert!(c.is_ok());
let file_output = format!(" ┌── {}", get_file_name(file_path_s.into())); let file_output = format!(" ┌── {}", file_path_s);
let dirs_output = format!("─┴ {}", dir_s); let dirs_output = format!("─┴ {}", dir_s);
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd.arg("-p").arg("-c").arg(dir_s).unwrap().stdout; // Mac test runners create long filenames in tmp directories
let output = cmd.args(["-p", "-c", "-w 999", dir_s]).unwrap().stdout;
// The link should not appear in the output because multiple inodes are now ordered // The link should not appear in the output because multiple inodes are now ordered
// then filtered. // then filtered.
@@ -120,7 +99,7 @@ pub fn test_recursive_sym_link() {
assert!(c.is_ok()); assert!(c.is_ok());
let a = format!("─┬ {}", dir_s); let a = format!("─┬ {}", dir_s);
let b = format!(" └── {}", get_file_name(link_name_s.into())); let b = format!(" └── {}", link_name_s);
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
let output = cmd let output = cmd
@@ -128,6 +107,7 @@ pub fn test_recursive_sym_link() {
.arg("-c") .arg("-c")
.arg("-r") .arg("-r")
.arg("-s") .arg("-s")
.arg("-w 999")
.arg(dir_s) .arg(dir_s)
.unwrap() .unwrap()
.stdout; .stdout;