From 496f2d63c8c46d89b957ebb6f9148c4ebb45b089 Mon Sep 17 00:00:00 2001 From: alireza_sariri Date: Mon, 29 Apr 2024 10:04:03 +0330 Subject: [PATCH 1/4] in tree diagram we set color for branches based on their size --- Cargo.toml | 1 + src/diagram/tree.rs | 26 ++++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a5db067..20516db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ path = "src/main.rs" clap={version="4.5.4",features=["cargo"]} rs-abbreviation-number = "0.2.1" cli-table = "0.4" +colored = "2.1.0" diff --git a/src/diagram/tree.rs b/src/diagram/tree.rs index 7a029eb..397cb84 100644 --- a/src/diagram/tree.rs +++ b/src/diagram/tree.rs @@ -1,5 +1,5 @@ use rs_abbreviation_number::NumericAbbreviate; - +use colored::Colorize; use crate::args::CommandArgs; use crate::crawler::Node; use crate::util::*; @@ -31,11 +31,7 @@ fn crawl_tree(tree: &Arc, args: &CommandArgs, result: &mut String) { } fn add_branch(node: &Node) -> String { - let mut branch_char = MIDDLE_CHAR; - if node.is_last_child() { - branch_char = END_CHAR; - } - + let branch_char = get_branch_char(node); let mut size = node.get_size().get().abbreviate_number(); size.push_str("iB"); format!( @@ -51,3 +47,21 @@ fn add_branch(node: &Node) -> String { ) ) } + +fn get_branch_char(node:&Node)->String{ + const GREEN_SIZE_BYTES:u64=1_024_000_000; + const YELLOW_SIZE_BYTES:u64=GREEN_SIZE_BYTES*10; + const YELLOW_STAR:u64=GREEN_SIZE_BYTES+1; + + let mut branch_char = MIDDLE_CHAR; + if node.is_last_child() { + branch_char = END_CHAR; + } + match node.get_size().get() { + 0..=GREEN_SIZE_BYTES=>branch_char.green().to_string(), + YELLOW_STAR..=YELLOW_SIZE_BYTES=>branch_char.yellow().to_string(), + _=>branch_char.red().to_string(), + } +} + + From 552a1935c5fdba2b13bc284738d87c98b5b1f436 Mon Sep 17 00:00:00 2001 From: alireza_sariri Date: Wed, 1 May 2024 09:33:33 +0330 Subject: [PATCH 2/4] imple color feature as shared lib for table and tree diagram --- src/diagram.rs | 10 +++++++--- src/diagram/shared.rs | 11 +++++++++++ src/diagram/table.rs | 11 ++++++++--- src/diagram/tree.rs | 21 +++++++++------------ src/util/mod.rs | 2 +- 5 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 src/diagram/shared.rs diff --git a/src/diagram.rs b/src/diagram.rs index 759a764..51051ad 100644 --- a/src/diagram.rs +++ b/src/diagram.rs @@ -1,9 +1,13 @@ -use cli_table::print_stdout; +mod table; +mod tree; +mod shared; + +use cli_table::print_stdout; use crate::crawler::Node; use std::sync::Arc; -mod table; -mod tree; + + use crate::args::{CommandArgs, DiagramType}; pub fn show_diagram(root: &Arc, arguments: &CommandArgs) { match arguments.diagram { diff --git a/src/diagram/shared.rs b/src/diagram/shared.rs new file mode 100644 index 0000000..270d48c --- /dev/null +++ b/src/diagram/shared.rs @@ -0,0 +1,11 @@ + +pub(crate) fn get_color_from_size(size:u64)->&'static str{ + const GREEN_SIZE_BYTES:u64=1_024_000_000; + const YELLOW_SIZE_BYTES:u64=GREEN_SIZE_BYTES*10; + const YELLOW_START:u64=GREEN_SIZE_BYTES+1; + match size { + 0..=GREEN_SIZE_BYTES=>"Green", + YELLOW_START..=YELLOW_SIZE_BYTES=>"Yellow", + _=>"Red", + } +} \ No newline at end of file diff --git a/src/diagram/table.rs b/src/diagram/table.rs index 39f6e8f..2c6886f 100644 --- a/src/diagram/table.rs +++ b/src/diagram/table.rs @@ -3,10 +3,14 @@ use crate::{ args::{CommandArgs, SortType}, util::thousends_seperator, }; +use cli_table::Color; use cli_table::{format::Justify, Cell, CellStruct, Style, Table, TableStruct}; use rs_abbreviation_number::NumericAbbreviate; +use std::str::FromStr; use std::{ops::Deref, sync::Arc}; +use super::shared::get_color_from_size; + pub fn create_table_diagram(tree: &Arc, args: &CommandArgs) -> TableStruct { let mut nodes: Vec> = Vec::new(); crawl_tree(tree, args, &mut nodes); @@ -23,14 +27,15 @@ fn export_to_table(nodes: Vec>, args: &CommandArgs) -> Vec> = Vec::new(); let sorted_data = sort_data(&nodes, args.sort.clone()); for (name, size) in sorted_data { - let size = format!( + let formated_size = format!( "{}iB ({} bytes)", size.abbreviate_number(), thousends_seperator(size) ); + let color=Color::from_str(get_color_from_size(size)).unwrap_or(Color::White); result.push(vec![ - name.cell().justify(Justify::Center), - size.cell().justify(Justify::Center), + name.cell().foreground_color(Some(color)).justify(Justify::Center), + formated_size.cell().foreground_color(Some(color)).justify(Justify::Center), ]); } result diff --git a/src/diagram/tree.rs b/src/diagram/tree.rs index 397cb84..fd180cd 100644 --- a/src/diagram/tree.rs +++ b/src/diagram/tree.rs @@ -1,9 +1,10 @@ use rs_abbreviation_number::NumericAbbreviate; -use colored::Colorize; +use colored::{Color, Colorize}; use crate::args::CommandArgs; use crate::crawler::Node; use crate::util::*; -use std::{ops::Deref, sync::Arc}; +use std::{ops::Deref, str::FromStr, sync::Arc}; +use crate::diagram::shared::*; pub const MIDDLE_CHAR: &'static str = "├──"; pub const END_CHAR: &'static str = "└──"; @@ -49,19 +50,15 @@ fn add_branch(node: &Node) -> String { } fn get_branch_char(node:&Node)->String{ - const GREEN_SIZE_BYTES:u64=1_024_000_000; - const YELLOW_SIZE_BYTES:u64=GREEN_SIZE_BYTES*10; - const YELLOW_STAR:u64=GREEN_SIZE_BYTES+1; - let mut branch_char = MIDDLE_CHAR; if node.is_last_child() { branch_char = END_CHAR; } - match node.get_size().get() { - 0..=GREEN_SIZE_BYTES=>branch_char.green().to_string(), - YELLOW_STAR..=YELLOW_SIZE_BYTES=>branch_char.yellow().to_string(), - _=>branch_char.red().to_string(), - } + let color_str=get_color_from_size(node.get_size().get()); + let colored= Color::from_str(color_str).unwrap_or(Color::White); + branch_char.color(colored).to_string() } - +mod tests{ + +} \ No newline at end of file diff --git a/src/util/mod.rs b/src/util/mod.rs index a54e12f..bac7812 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,6 +1,6 @@ -pub fn thousends_seperator(i: u64) -> String { +pub(crate) fn thousends_seperator(i: u64) -> String { let mut s = String::new(); let i_str = i.to_string(); let a = i_str.chars().rev().enumerate(); From 0de18f564aa7e63db8277873f3384093fa964cc3 Mon Sep 17 00:00:00 2001 From: alireza_sariri Date: Wed, 1 May 2024 15:47:18 +0330 Subject: [PATCH 3/4] impl tree tests --- src/diagram/tree.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/diagram/tree.rs b/src/diagram/tree.rs index fd180cd..895f52b 100644 --- a/src/diagram/tree.rs +++ b/src/diagram/tree.rs @@ -59,6 +59,44 @@ fn get_branch_char(node:&Node)->String{ branch_char.color(colored).to_string() } + + mod tests{ -} \ No newline at end of file + use std::path::PathBuf; + + use super::*; + #[test] + fn get_branch_char_heavy_size_test(){ + let node=Node::new(PathBuf::from(""), "root".to_string(), 1_000_000_000_000, 0); + assert_eq!("\u{1b}[31m├──\u{1b}[0m",get_branch_char(&node)); + } + #[test] + fn get_branch_char_medium_size_test(){ + let node=Node::new(PathBuf::from(""), "root".to_string(), 8_024_000_000, 0); + assert_eq!("\u{1b}[33m├──\u{1b}[0m",get_branch_char(&node)); + } + + + #[test] + fn get_branch_char_small_size_test(){ + let node=Node::new(PathBuf::from(""), "root".to_string(), 5, 0); + assert_eq!("\u{1b}[32m├──\u{1b}[0m",get_branch_char(&node)); + } + + #[test] + fn add_branch_root_test(){ + let node=Node::new(PathBuf::from(""), "root".to_string(), 5000, 0); + let input=add_branch(&node); + let expect="\n\u{1b}[32m├──\u{1b}[0mroot 5KiB(5,000 bytes)".to_string(); + assert_eq!(expect,input); + } + + #[test] + fn add_branch_child_test(){ + let child=Node::new(PathBuf::from(""), "child".to_string(), 8_024_000_000, 1); + let input=add_branch(&child); + let expect="\n\t\u{1b}[33m├──\u{1b}[0mchild 8.02GiB(8,024,000,000 bytes)".to_string(); + assert_eq!(expect,input); + } +} From e5f2d03c754074d90d4b9dafb2b3e0334658bad9 Mon Sep 17 00:00:00 2001 From: alireza_sariri Date: Wed, 1 May 2024 20:28:31 +0330 Subject: [PATCH 4/4] new version : set color based on folder size --- Cargo.toml | 2 +- src/diagram/tree.rs | 55 +++++++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 20516db..3b775ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "DiskAnalyzer" -version = "0.2.2" +version = "0.2.3" edition = "2021" repository="https://github.com/alirezasariri78/DiskAnalyzer" description="make diagram base on system folder size" diff --git a/src/diagram/tree.rs b/src/diagram/tree.rs index 895f52b..f89e05a 100644 --- a/src/diagram/tree.rs +++ b/src/diagram/tree.rs @@ -1,10 +1,10 @@ -use rs_abbreviation_number::NumericAbbreviate; -use colored::{Color, Colorize}; use crate::args::CommandArgs; use crate::crawler::Node; +use crate::diagram::shared::*; use crate::util::*; +use colored::{Color, Colorize}; +use rs_abbreviation_number::NumericAbbreviate; use std::{ops::Deref, str::FromStr, sync::Arc}; -use crate::diagram::shared::*; pub const MIDDLE_CHAR: &'static str = "├──"; pub const END_CHAR: &'static str = "└──"; @@ -49,54 +49,51 @@ fn add_branch(node: &Node) -> String { ) } -fn get_branch_char(node:&Node)->String{ +fn get_branch_char(node: &Node) -> String { let mut branch_char = MIDDLE_CHAR; if node.is_last_child() { branch_char = END_CHAR; } - let color_str=get_color_from_size(node.get_size().get()); - let colored= Color::from_str(color_str).unwrap_or(Color::White); + let color_str = get_color_from_size(node.get_size().get()); + let colored = Color::from_str(color_str).unwrap_or(Color::White); branch_char.color(colored).to_string() } +mod tests { - -mod tests{ - use std::path::PathBuf; use super::*; #[test] - fn get_branch_char_heavy_size_test(){ - let node=Node::new(PathBuf::from(""), "root".to_string(), 1_000_000_000_000, 0); - assert_eq!("\u{1b}[31m├──\u{1b}[0m",get_branch_char(&node)); + fn get_branch_char_heavy_size_test() { + let node = Node::new(PathBuf::from(""), "root".to_string(), 1_000_000_000_000, 0); + assert_eq!("\u{1b}[31m├──\u{1b}[0m", get_branch_char(&node)); } #[test] - fn get_branch_char_medium_size_test(){ - let node=Node::new(PathBuf::from(""), "root".to_string(), 8_024_000_000, 0); - assert_eq!("\u{1b}[33m├──\u{1b}[0m",get_branch_char(&node)); + fn get_branch_char_medium_size_test() { + let node = Node::new(PathBuf::from(""), "root".to_string(), 8_024_000_000, 0); + assert_eq!("\u{1b}[33m├──\u{1b}[0m", get_branch_char(&node)); } - #[test] - fn get_branch_char_small_size_test(){ - let node=Node::new(PathBuf::from(""), "root".to_string(), 5, 0); - assert_eq!("\u{1b}[32m├──\u{1b}[0m",get_branch_char(&node)); + fn get_branch_char_small_size_test() { + let node = Node::new(PathBuf::from(""), "root".to_string(), 5, 0); + assert_eq!("\u{1b}[32m├──\u{1b}[0m", get_branch_char(&node)); } #[test] - fn add_branch_root_test(){ - let node=Node::new(PathBuf::from(""), "root".to_string(), 5000, 0); - let input=add_branch(&node); - let expect="\n\u{1b}[32m├──\u{1b}[0mroot 5KiB(5,000 bytes)".to_string(); - assert_eq!(expect,input); + fn add_branch_root_test() { + let node = Node::new(PathBuf::from(""), "root".to_string(), 5000, 0); + let input = add_branch(&node); + let expect = "\n\u{1b}[32m├──\u{1b}[0mroot 5KiB(5,000 bytes)".to_string(); + assert_eq!(expect, input); } #[test] - fn add_branch_child_test(){ - let child=Node::new(PathBuf::from(""), "child".to_string(), 8_024_000_000, 1); - let input=add_branch(&child); - let expect="\n\t\u{1b}[33m├──\u{1b}[0mchild 8.02GiB(8,024,000,000 bytes)".to_string(); - assert_eq!(expect,input); + fn add_branch_child_test() { + let child = Node::new(PathBuf::from(""), "child".to_string(), 8_024_000_000, 1); + let input = add_branch(&child); + let expect = "\n\t\u{1b}[33m├──\u{1b}[0mchild 8.02GiB(8,024,000,000 bytes)".to_string(); + assert_eq!(expect, input); } }