Recursivity fix
This commit is contained in:
@@ -6,8 +6,6 @@ use std::{
|
|||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
|
|
||||||
// use clap::builder::styling::Metadata;
|
|
||||||
|
|
||||||
use crate::utils::get_icon_file_type;
|
use crate::utils::get_icon_file_type;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||||
@@ -32,8 +30,7 @@ pub struct Element {
|
|||||||
impl Element {
|
impl Element {
|
||||||
pub fn new(path_str: &str) -> Element {
|
pub fn new(path_str: &str) -> Element {
|
||||||
let path_built = Path::new(path_str);
|
let path_built = Path::new(path_str);
|
||||||
// println!("{:?}", path_built);
|
let metadata: fs::Metadata;
|
||||||
let metadata: fs::Metadata; // = fs::metadata(path_str).unwrap();
|
|
||||||
if let Result::Ok(m) = fs::metadata(path_str) {
|
if let Result::Ok(m) = fs::metadata(path_str) {
|
||||||
metadata = m;
|
metadata = m;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ pub struct Args {
|
|||||||
#[arg(short, long, default_value_t = false)]
|
#[arg(short, long, default_value_t = false)]
|
||||||
list: bool,
|
list: bool,
|
||||||
|
|
||||||
|
/// Print contents of directories recursively,
|
||||||
|
/// specify maximum recursive depth
|
||||||
#[arg(short, long, default_value_t = 0)]
|
#[arg(short, long, default_value_t = 0)]
|
||||||
recursive: usize,
|
recursive: usize,
|
||||||
|
|
||||||
@@ -29,13 +31,6 @@ fn main() {
|
|||||||
|
|
||||||
let elements = get_elements_from_path(args.path, args.all);
|
let elements = get_elements_from_path(args.path, args.all);
|
||||||
|
|
||||||
// let paths = fs::read_dir(args.path).unwrap();
|
|
||||||
|
|
||||||
// let elements: Vec<Element> = paths
|
|
||||||
// .map(|e| Element::new(e.unwrap().path().to_str().unwrap()))
|
|
||||||
// .filter(|element| args.all || !element.get_name().starts_with('.'))
|
|
||||||
// .collect();
|
|
||||||
|
|
||||||
if args.list {
|
if args.list {
|
||||||
list(elements, args.recursive);
|
list(elements, args.recursive);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
134
src/out/list.rs
134
src/out/list.rs
@@ -3,8 +3,8 @@ use crate::utils::{
|
|||||||
get_elements_from_path, get_size_string, get_string_length, pad_string, system_time_to_string,
|
get_elements_from_path, get_size_string, get_string_length, pad_string, system_time_to_string,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn list(mut elements: Vec<Element>, recursive_limit: usize) {
|
pub fn list(elements: Vec<Element>, recursive_limit: usize) {
|
||||||
elements.sort_unstable_by_key(|a: &Element| a.get_name());
|
// elements.sort_unstable_by_key(|a: &Element| a.get_name());
|
||||||
let width = term_size::dimensions().unwrap().0;
|
let width = term_size::dimensions().unwrap().0;
|
||||||
// ╭──────────────╼ File name ╾──────────────┬─╼ Size ╾─┬──╼ Creation ╾──╮
|
// ╭──────────────╼ File name ╾──────────────┬─╼ Size ╾─┬──╼ Creation ╾──╮
|
||||||
// │ some_example_file │ 420.69 G │ 01-01-70 00:00 │
|
// │ some_example_file │ 420.69 G │ 01-01-70 00:00 │
|
||||||
@@ -15,7 +15,7 @@ pub fn list(mut elements: Vec<Element>, recursive_limit: usize) {
|
|||||||
let name_length = name_max_len
|
let name_length = name_max_len
|
||||||
.max(14 + (elements.len() as f32).log10() as usize)
|
.max(14 + (elements.len() as f32).log10() as usize)
|
||||||
.max(13)
|
.max(13)
|
||||||
.min(width - 30);
|
.min(width - 31);
|
||||||
|
|
||||||
print_header(name_length);
|
print_header(name_length);
|
||||||
print_elements(&elements, name_length, recursive_limit, 0, &Vec::new());
|
print_elements(&elements, name_length, recursive_limit, 0, &Vec::new());
|
||||||
@@ -43,79 +43,50 @@ fn print_elements(
|
|||||||
) {
|
) {
|
||||||
let mut new_is_last_element = is_last_element.to_owned();
|
let mut new_is_last_element = is_last_element.to_owned();
|
||||||
|
|
||||||
// println!("{:?}", is_last_element);
|
|
||||||
|
|
||||||
for (i, e) in elements.iter().enumerate() {
|
for (i, e) in elements.iter().enumerate() {
|
||||||
let str_len = get_string_length(e.get_name().as_str());
|
|
||||||
print!("│ ");
|
print!("│ ");
|
||||||
if get_string_length(e.to_string().as_str()) > name_length {
|
|
||||||
let mut e_string = String::new();
|
let mut e_string = String::new();
|
||||||
if current_depth > 0 {
|
if current_depth > 0 {
|
||||||
for &is_last in &new_is_last_element[1..] {
|
add_recursive_lines(&mut e_string, &new_is_last_element, i == elements.len() - 1);
|
||||||
if is_last {
|
|
||||||
e_string.push_str(" ");
|
|
||||||
} else {
|
|
||||||
e_string.push_str("│ ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if i == elements.len() - 1 {
|
|
||||||
e_string.push_str("╰─");
|
|
||||||
} else {
|
|
||||||
e_string.push_str("├─");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
e_string.push_str(e.to_string().as_str());
|
e_string.push_str(e.to_string().as_str());
|
||||||
print!(
|
print!(
|
||||||
"{}",
|
"{}",
|
||||||
pad_string(
|
pad_string(
|
||||||
e_string.as_str()[..=name_length].to_string(),
|
get_slice_of_string(e_string.as_str(), name_length - 1, 0, current_depth),
|
||||||
name_length - 2,
|
name_length,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
print_size_and_creation_date(e);
|
print_size_and_creation_date(e);
|
||||||
let mut e_name = String::new();
|
|
||||||
|
let num_splits =
|
||||||
|
get_string_length(e.get_name().as_str()) / (name_length - (3 + 2 * current_depth));
|
||||||
|
// println!("{}", num_splits + 1);
|
||||||
|
for j in 1..num_splits + 1 {
|
||||||
|
let mut e_name = String::from("│ ");
|
||||||
if current_depth > 0 {
|
if current_depth > 0 {
|
||||||
for _ in 0..current_depth {
|
add_recursive_lines_for_name_resize(
|
||||||
e_name.push_str(" ");
|
&mut e_name,
|
||||||
}
|
&new_is_last_element,
|
||||||
}
|
i == elements.len() - 1,
|
||||||
e_name.push_str(e.get_name().as_str());
|
|
||||||
for i in 1..(str_len / (name_length - 5) + 1) {
|
|
||||||
print!(
|
|
||||||
"│ {}",
|
|
||||||
pad_string(
|
|
||||||
e.get_name().as_str()
|
|
||||||
[((name_length - 5) * i)..((name_length - 5) * (i + 1)).min(str_len)]
|
|
||||||
.to_string(),
|
|
||||||
name_length - 3,
|
|
||||||
true
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
e_name.push_str(" ");
|
||||||
|
e_name.push_str(
|
||||||
|
get_slice_of_string(
|
||||||
|
e.get_name().as_str(),
|
||||||
|
name_length - (3 + 2 * current_depth),
|
||||||
|
j,
|
||||||
|
current_depth,
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
);
|
);
|
||||||
|
print!("{}", pad_string(e_name, name_length + 2, true));
|
||||||
println!("│ │ │");
|
println!("│ │ │");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
let mut e_string = String::new();
|
|
||||||
if current_depth > 0 {
|
|
||||||
for &is_last in &new_is_last_element[1..] {
|
|
||||||
if is_last {
|
|
||||||
e_string.push_str(" ");
|
|
||||||
} else {
|
|
||||||
e_string.push_str("│ ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if i == elements.len() - 1 {
|
|
||||||
e_string.push_str("╰─");
|
|
||||||
} else {
|
|
||||||
e_string.push_str("├─");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e_string.push_str(e.to_string().as_str());
|
|
||||||
print!("{}", pad_string(e_string, name_length, true));
|
|
||||||
print_size_and_creation_date(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.get_file_type() == TypeOfFile::Dir && current_depth < recursive_limit {
|
if e.get_file_type() == TypeOfFile::Dir && current_depth < recursive_limit {
|
||||||
let dir_path = e.get_path_string();
|
let dir_path = e.get_path_string();
|
||||||
@@ -128,11 +99,56 @@ fn print_elements(
|
|||||||
&new_is_last_element,
|
&new_is_last_element,
|
||||||
);
|
);
|
||||||
new_is_last_element.pop();
|
new_is_last_element.pop();
|
||||||
// println!("{:?}", is_last_element);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_recursive_lines(e_string: &mut String, is_last_element: &[bool], is_last: bool) {
|
||||||
|
for &is_last in &is_last_element[1..] {
|
||||||
|
if is_last {
|
||||||
|
e_string.push_str(" ");
|
||||||
|
} else {
|
||||||
|
e_string.push_str("│ ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_last {
|
||||||
|
e_string.push_str("╰─");
|
||||||
|
} else {
|
||||||
|
// println!("yahooo");
|
||||||
|
e_string.push_str("├─");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_recursive_lines_for_name_resize(
|
||||||
|
e_string: &mut String,
|
||||||
|
is_last_element: &[bool],
|
||||||
|
is_last: bool,
|
||||||
|
) {
|
||||||
|
for &is_last in &is_last_element[1..] {
|
||||||
|
if is_last {
|
||||||
|
e_string.push_str(" ");
|
||||||
|
} else {
|
||||||
|
e_string.push_str("│ ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_last {
|
||||||
|
e_string.push_str(" ");
|
||||||
|
} else {
|
||||||
|
e_string.push_str("│ ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn get_slice_of_string(e: &str, name_length: usize, i: usize, _current_depth: usize) -> String {
|
||||||
|
// println!("--{}--", e);
|
||||||
|
e.chars().collect::<Vec<char>>()[((name_length) * i).min(get_string_length(e))
|
||||||
|
..((name_length) * (i + 1)).min(get_string_length(e))]
|
||||||
|
.iter()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
fn print_size_and_creation_date(e: &Element) {
|
fn print_size_and_creation_date(e: &Element) {
|
||||||
print!("│");
|
print!("│");
|
||||||
if e.get_file_type() == TypeOfFile::Dir {
|
if e.get_file_type() == TypeOfFile::Dir {
|
||||||
|
|||||||
28
src/utils.rs
28
src/utils.rs
@@ -16,15 +16,17 @@ pub fn get_elements_from_path(path: String, all: bool) -> Vec<Element> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn pad_string(s: String, pad: usize, after: bool) -> String {
|
pub fn pad_string(s: String, pad: usize, after: bool) -> String {
|
||||||
|
// println!("{}", pad);
|
||||||
|
|
||||||
let mut s2 = String::new();
|
let mut s2 = String::new();
|
||||||
// println!("{}, {}", s.len(), pad);
|
let s_length = get_string_length(&s);
|
||||||
if after {
|
if after {
|
||||||
s2.push_str(s.as_str());
|
s2.push_str(s.as_str());
|
||||||
for _ in 0..(pad - get_string_length(&s)) {
|
for _ in 0..(pad.saturating_sub(s_length)) {
|
||||||
s2.push(' ');
|
s2.push(' ');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _ in 0..(pad - get_string_length(&s)) {
|
for _ in 0..(pad.saturating_sub(s_length)) {
|
||||||
s2.push(' ');
|
s2.push(' ');
|
||||||
}
|
}
|
||||||
s2.push_str(s.as_str());
|
s2.push_str(s.as_str());
|
||||||
@@ -36,7 +38,6 @@ pub fn pad_string(s: String, pad: usize, after: bool) -> String {
|
|||||||
// character when using .len()
|
// character when using .len()
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_string_length(s: &str) -> usize {
|
pub fn get_string_length(s: &str) -> usize {
|
||||||
s.chars().collect::<Vec<char>>().len();
|
|
||||||
s.chars().count()
|
s.chars().count()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +47,6 @@ pub fn get_size_string(bytes: u64) -> String {
|
|||||||
}
|
}
|
||||||
let bytes_f32 = bytes as f32;
|
let bytes_f32 = bytes as f32;
|
||||||
let exp = bytes_f32.log(1024.0).floor();
|
let exp = bytes_f32.log(1024.0).floor();
|
||||||
// print!("\n{}", exp);
|
|
||||||
let divided_num = bytes_f32 / 1024.0_f32.powf(exp);
|
let divided_num = bytes_f32 / 1024.0_f32.powf(exp);
|
||||||
let unit = ['B', 'K', 'M', 'G', 'T', 'P', 'Y', 'E'][exp as usize];
|
let unit = ['B', 'K', 'M', 'G', 'T', 'P', 'Y', 'E'][exp as usize];
|
||||||
format!("{:.2} {} ", divided_num, unit)
|
format!("{:.2} {} ", divided_num, unit)
|
||||||
@@ -60,21 +60,21 @@ pub fn system_time_to_string(system_time: SystemTime) -> String {
|
|||||||
pub fn get_icon_file_type<'a>(filename: String) -> &'a str {
|
pub fn get_icon_file_type<'a>(filename: String) -> &'a str {
|
||||||
let extension = filename.split('.').collect::<Vec<&str>>()[1..].join(".");
|
let extension = filename.split('.').collect::<Vec<&str>>()[1..].join(".");
|
||||||
match extension.to_lowercase().as_str() {
|
match extension.to_lowercase().as_str() {
|
||||||
"zip" | "rar" | "7zip" | "tar" | "tar.gz" | "tgz" => " ",
|
|
||||||
"rs" => " ",
|
|
||||||
"jpg" | "jpeg" | "png" | "bmp" | "gif" | "webp" | "svg" => " ",
|
"jpg" | "jpeg" | "png" | "bmp" | "gif" | "webp" | "svg" => " ",
|
||||||
|
"zip" | "rar" | "7zip" | "tar" | "tar.gz" | "tgz" => " ",
|
||||||
"flv" | "avi" | "mp4" | "webm" | "mov" => " ",
|
"flv" | "avi" | "mp4" | "webm" | "mov" => " ",
|
||||||
"exe" | "ini" | "bat" => " ",
|
"exe" | "ini" | "bat" => " ",
|
||||||
"py" => " ",
|
|
||||||
"c" => " ",
|
|
||||||
"cpp" => " ",
|
|
||||||
"json" => " ",
|
|
||||||
"pdf" => " ",
|
|
||||||
"java" | "jar" => " ",
|
"java" | "jar" => " ",
|
||||||
"js" => " ",
|
"json" => " ",
|
||||||
"html" => " ",
|
"html" => " ",
|
||||||
"css" => " ",
|
|
||||||
"csv" => " ",
|
"csv" => " ",
|
||||||
|
"cpp" => " ",
|
||||||
|
"pdf" => " ",
|
||||||
|
"css" => " ",
|
||||||
|
"rs" => " ",
|
||||||
|
"py" => " ",
|
||||||
|
"js" => " ",
|
||||||
|
"c" => " ",
|
||||||
_ => " ",
|
_ => " ",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user