Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
1. update version to 0.2.1
2. add `display_width` to fix the alignment err of non-ascii
3. modify some output infomation.
  • Loading branch information
seamile committed Jan 8, 2023
1 parent 2ada8b6 commit ff41f9c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fcnt"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
readme = "README.md"
homepage = "https://github.com/seamile/fcnt"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ Name Files Dirs Size

## TODO

- [ ] fix the display bug of Chinese, Japanese, Korean and emoji.
- [ ] add test cases.
- [ ] add documentation in the source code.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {

#[derive(Parser)]
#[command(name = "fcnt")]
#[command(version = "0.2.0")]
#[command(version = "0.2.1")]
#[command(about = "Count the total number of files in given directories.")]
struct CmdLineArgs {
/// the directories (default: ./)
Expand Down Expand Up @@ -74,7 +74,7 @@ impl CmdLineArgs {
if dir.is_dir() {
directories.push(dir);
} else {
let msg = format!("{:?} is not directory.", dir);
let msg = format!("{:?} is not a directory.", dir);
println!("{}", output::warn(&msg));
}
}
Expand Down
171 changes: 67 additions & 104 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ pub enum Effect {
Hidden = 8,
}

#[allow(unused)]
impl Effect {
pub fn bold(arg: &dyn Display, color: Color) -> String {
color_me(arg, color, Effect::Bold)
}
pub fn dark(arg: &dyn Display, color: Color) -> String {
color_me(arg, color, Effect::Dark)
}
pub fn inverse(arg: &dyn Display, color: Color) -> String {
color_me(arg, color, Effect::Inverse)
}
pub fn underline(arg: &dyn Display, color: Color) -> String {
color_me(arg, color, Effect::Underline)
}
pub fn blink(arg: &dyn Display, color: Color) -> String {
color_me(arg, color, Effect::Blink)
}
pub fn hidden(arg: &dyn Display, color: Color) -> String {
color_me(arg, color, Effect::Hidden)
}
}

#[allow(unused)]
pub enum Color {
Black = 30,
Expand All @@ -54,58 +32,6 @@ pub enum Color {
Default = 99,
}

#[allow(unused)]
impl Color {
pub fn black(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Black, effect);
}
pub fn red(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Red, effect);
}
pub fn green(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Green, effect);
}
pub fn yellow(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Yellow, effect);
}
pub fn blue(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Blue, effect);
}
pub fn magenta(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Magenta, effect);
}
pub fn cyan(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Cyan, effect);
}
pub fn white(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::White, effect);
}
pub fn grey(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::Grey, effect);
}
pub fn bright_red(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightRed, effect);
}
pub fn bright_green(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightGreen, effect);
}
pub fn bright_yellow(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightYellow, effect);
}
pub fn bright_blue(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightBlue, effect);
}
pub fn bright_magenta(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightMagenta, effect);
}
pub fn bright_cyan(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightCyan, effect);
}
pub fn bright_white(arg: &dyn Display, effect: Effect) -> String {
return color_me(arg, Color::BrightWhite, effect);
}
}

#[allow(unused)]
pub fn color_me(arg: &dyn Display, color: Color, effect: Effect) -> String {
return format!("\x1b[{};{}m{}\x1b[0m", effect as u8, color as u8, arg);
Expand Down Expand Up @@ -135,18 +61,25 @@ fn spaces(width: usize) -> String {
return String::from_utf8(vec![32_u8; width]).unwrap();
}

pub fn display_width(s: &String) -> usize {
return s
.chars()
.map(|c| if c as u32 > 0x2e80 { 2_usize } else { 1_usize })
.sum::<usize>();
}

#[allow(unused)]
pub fn align_center(s: &dyn ToString, width: usize) -> String {
let mut string = s.to_string();
let len = string.len();
if len < width {
let n_fill = width - string.len();
let d_width = display_width(&string);
if d_width < width {
let n_fill = width - d_width;
if n_fill % 2 == 0 {
let fill = spaces(n_fill / 2);
string.insert_str(len, &fill);
string.insert_str(string.len(), &fill);
string.insert_str(0, &fill);
} else {
string.insert_str(len, &spaces(n_fill / 2 + 1));
string.insert_str(string.len(), &spaces(n_fill / 2 + 1));
string.insert_str(0, &spaces(n_fill / 2));
}
}
Expand All @@ -156,25 +89,38 @@ pub fn align_center(s: &dyn ToString, width: usize) -> String {
#[allow(unused)]
pub fn align_left(s: &dyn ToString, width: usize) -> String {
let mut string = s.to_string();
let len = string.len();
if len < width {
let n_fill = width - string.len();
string.insert_str(len, &spaces(n_fill));
let d_width = display_width(&string);
if d_width < width {
let n_fill = width - d_width;
string.insert_str(string.len(), &spaces(n_fill));
}
return string;
}

#[allow(unused)]
pub fn align_right(s: &dyn ToString, width: usize) -> String {
let mut string = s.to_string();
let len = string.len();
if len < width {
let n_fill = width - string.len();
let d_width = display_width(&string);
if d_width < width {
let n_fill = width - d_width;
string.insert_str(0, &spaces(n_fill));
}
return string;
}

#[test]
fn test_color() {
let s = "Hello World";
println!(
"yellow + underline: {}\n",
color_me(&s, Color::Yellow, Effect::Underline)
);
println!("title: {}", title(&s));
println!("info : {}", info(&s));
println!("warn : {}", warn(&s));
println!("err : {}", err(&s));
}

#[test]
fn test_align() {
let s = "HelloWorld";
Expand All @@ -183,25 +129,42 @@ fn test_align() {
assert_eq!(align_left(&s, 15), String::from("HelloWorld "));
assert_eq!(align_right(&s, 15), String::from(" HelloWorld"));

let t = vec![
align_left(&"Name", 8),
align_right(&"Files", 5),
align_right(&"Dirs", 5),
align_right(&"Size", 9),
]
.join(" ");
println!("{}", title(&t));
let t = title(
&vec![
align_left(&"Name", 8),
align_right(&"Files", 5),
align_right(&"Dirs", 5),
align_right(&"Size", 9),
]
.join(" "),
);

println!("{}", t);
assert_eq!(
t,
"\x1b[4;92mName Files Dirs Size\x1b[0m".to_string()
);
}

#[test]
fn test_color() {
let s = "Hello World";
println!(
"yellow + underline: {}\n",
Color::yellow(&s, Effect::Underline)
);
println!("title: {}", title(&s));
println!("info: {}", info(&s));
println!("warn: {}", warn(&s));
println!("err: {}", err(&s));
fn test_non_ascii() {
let s1 = "Hello World!";
let s2 = "你好 Rust";
let s3 = "Séamile: 🌊😀";
let s4 = "EVA,人の作り出した物";

let aligned_s1 = align_right(&s1, 25);
let aligned_s2 = align_center(&s2, 25);
let aligned_s3 = align_left(&s3, 25);
let aligned_s4 = align_left(&s4, 25);

println!("s1 => |{}|", aligned_s1);
println!("s2 => |{}|", aligned_s2);
println!("s3 => |{}|", aligned_s3);
println!("s3 => |{}|", aligned_s4);

assert_eq!(aligned_s1.len(), 25);
assert_eq!(aligned_s2.len(), 27);
assert_eq!(aligned_s3.len(), 30);
assert_eq!(aligned_s4.len(), 34);
}
14 changes: 7 additions & 7 deletions src/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ impl Counter {
}
}

pub fn len(&self) -> (usize, usize, usize, usize) {
pub fn lengths(&self) -> (usize, usize, usize, usize) {
return (
self.name().len(),
op::display_width(&self.name().to_string()),
self.n_files.to_string().len(),
self.n_dirs.to_string().len(),
self.readable_size().len(),
Expand Down Expand Up @@ -124,7 +124,7 @@ impl Counter {
let mut lines: Vec<String> = vec![];
let lens = counters
.iter()
.map(|c| c.len())
.map(|c| c.lengths())
.map(|w| (w.0.max(4), w.1.max(5), w.2.max(4), w.3.max(4)))
.reduce(|m, n| (n.0.max(m.0), n.1.max(m.1), n.2.max(m.2), n.3.max(m.3)))
.unwrap();
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn parallel_walk(

// send dirlist to path channel
for path in dirlist {
path_tx.send(path.clone()).expect("path send err");
path_tx.send(path.clone()).expect("send path err");
}

// create walk threads which amount is n_thread
Expand All @@ -234,13 +234,13 @@ pub fn parallel_walk(

// traverse all files in the directory
let (sub_dirs, sub_cnt) = walk(&dirpath, with_hidden, count_sz)
.expect(format!("{} Error", &dirpath.to_string_lossy()).as_str());
.expect(&format!("walk err: {}", &dirpath.to_str().unwrap()));

// send the sub_dirs and the sub_counter back
for path in sub_dirs {
_path_tx.send(path.clone()).expect("path send err");
_path_tx.send(path.clone()).expect("send path err");
}
_cnt_tx.send(sub_cnt).expect("counter send err");
_cnt_tx.send(sub_cnt).expect("send counter err");

// switch stat to IDLE
{
Expand Down

0 comments on commit ff41f9c

Please sign in to comment.