mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Refactor tests
Make use of the 'is' more to test the entire output. Add test for no_color mode. Replace several calls to format_string with calls that build a string directly. I feel format_string is becoming unweildly and this simplfies the tests
This commit is contained in:
+13
-28
@@ -9,6 +9,7 @@ static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
||||
pub struct DisplayData {
|
||||
pub short_paths: bool,
|
||||
pub is_reversed: bool,
|
||||
pub colors_on: bool,
|
||||
}
|
||||
|
||||
impl DisplayData {
|
||||
@@ -76,7 +77,7 @@ pub fn draw_it(
|
||||
permissions: bool,
|
||||
use_full_path: bool,
|
||||
is_reversed: bool,
|
||||
colors_on: bool,
|
||||
no_colors: bool,
|
||||
root_node: Node,
|
||||
) {
|
||||
if !permissions {
|
||||
@@ -85,23 +86,16 @@ pub fn draw_it(
|
||||
let display_data = DisplayData {
|
||||
short_paths: !use_full_path,
|
||||
is_reversed,
|
||||
colors_on: !no_colors,
|
||||
};
|
||||
|
||||
for c in display_data.get_children_from_node(root_node) {
|
||||
let first_tree_chars = display_data.get_first_chars();
|
||||
display_node(c, true, colors_on, first_tree_chars, &display_data)
|
||||
display_node(c, true, first_tree_chars, &display_data)
|
||||
}
|
||||
}
|
||||
|
||||
fn display_node(
|
||||
node: Node,
|
||||
is_biggest: bool,
|
||||
colors_on: bool,
|
||||
indent: &str,
|
||||
display_data: &DisplayData,
|
||||
) {
|
||||
let short = display_data.short_paths;
|
||||
|
||||
fn display_node(node: Node, is_biggest: bool, indent: &str, display_data: &DisplayData) {
|
||||
let mut num_siblings = node.children.len() as u64;
|
||||
let max_sibling = num_siblings;
|
||||
let new_indent = clean_indentation_string(indent);
|
||||
@@ -109,7 +103,7 @@ fn display_node(
|
||||
let size = node.size;
|
||||
|
||||
if !display_data.is_reversed {
|
||||
print_this_node(&*name, size, is_biggest, short, colors_on, indent);
|
||||
print_this_node(&*name, size, is_biggest, display_data, indent);
|
||||
}
|
||||
|
||||
for c in display_data.get_children_from_node(node) {
|
||||
@@ -117,11 +111,11 @@ fn display_node(
|
||||
let chars = display_data.get_tree_chars(num_siblings, max_sibling, !c.children.is_empty());
|
||||
let is_biggest = display_data.is_biggest(num_siblings, max_sibling);
|
||||
let full_indent = new_indent.clone() + chars;
|
||||
display_node(c, is_biggest, colors_on, &*full_indent, display_data)
|
||||
display_node(c, is_biggest, &*full_indent, display_data)
|
||||
}
|
||||
|
||||
if display_data.is_reversed {
|
||||
print_this_node(&*name, size, is_biggest, short, colors_on, indent);
|
||||
print_this_node(&*name, size, is_biggest, display_data, indent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,34 +140,25 @@ fn print_this_node(
|
||||
name: &str,
|
||||
size: u64,
|
||||
is_biggest: bool,
|
||||
short_paths: bool,
|
||||
colors_on: bool,
|
||||
display_data: &DisplayData,
|
||||
indentation: &str,
|
||||
) {
|
||||
let pretty_size = format!("{:>5}", human_readable_number(size),);
|
||||
println!(
|
||||
"{}",
|
||||
format_string(
|
||||
name,
|
||||
is_biggest,
|
||||
short_paths,
|
||||
colors_on,
|
||||
&*pretty_size,
|
||||
indentation
|
||||
)
|
||||
format_string(name, is_biggest, display_data, &*pretty_size, indentation)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn format_string(
|
||||
dir_name: &str,
|
||||
is_biggest: bool,
|
||||
short_paths: bool,
|
||||
colors_on: bool,
|
||||
display_data: &DisplayData,
|
||||
size: &str,
|
||||
indentation: &str,
|
||||
) -> String {
|
||||
let printable_name = {
|
||||
if short_paths {
|
||||
if display_data.short_paths {
|
||||
dir_name.split('/').last().unwrap_or(dir_name)
|
||||
} else {
|
||||
dir_name
|
||||
@@ -181,7 +166,7 @@ pub fn format_string(
|
||||
};
|
||||
format!(
|
||||
"{} {} {}",
|
||||
if is_biggest && colors_on {
|
||||
if is_biggest && display_data.colors_on {
|
||||
Fixed(196).paint(size)
|
||||
} else {
|
||||
Style::new().paint(size)
|
||||
|
||||
Reference in New Issue
Block a user