🆕 Add new script "tree-rs" and update related metadata files
This commit is contained in:
@@ -16,4 +16,5 @@ script-sign-rs
|
||||
script-verify-rs
|
||||
ssh-rs
|
||||
sync-rs
|
||||
tree-rs
|
||||
xattr-rs
|
||||
|
||||
@@ -152,6 +152,14 @@
|
||||
"publish_time": 1737272563310,
|
||||
"update_time": 1775319019530
|
||||
},
|
||||
"tree.rs": {
|
||||
"script_name": "tree.rs",
|
||||
"script_length": 5396,
|
||||
"script_sha256": "9f4f662900f77016545552427d5cb0c45e211e51ccc37f9819d6be4f08fed364",
|
||||
"script_full_url": "https://git.hatter.ink/rust-scripts/scriptbase/raw/branch/main/tree-rs/src/main.rs",
|
||||
"publish_time": 1776447368459,
|
||||
"update_time": 1776447368459
|
||||
},
|
||||
"xattr.rs": {
|
||||
"script_name": "xattr.rs",
|
||||
"script_length": 2029,
|
||||
|
||||
@@ -89,6 +89,11 @@
|
||||
"script_length": 3026,
|
||||
"script_sha256": "ed2cc2f3238ae892a2dc2bcf0c4b74485939f926242c50aa0d0e6affb8eec337"
|
||||
},
|
||||
"tree-rs": {
|
||||
"script_name": "tree-rs",
|
||||
"script_length": 5396,
|
||||
"script_sha256": "9f4f662900f77016545552427d5cb0c45e211e51ccc37f9819d6be4f08fed364"
|
||||
},
|
||||
"xattr-rs": {
|
||||
"script_name": "xattr-rs",
|
||||
"script_length": 2029,
|
||||
|
||||
186
tree-rs/Cargo.lock
generated
Normal file
186
tree-rs/Cargo.lock
generated
Normal file
@@ -0,0 +1,186 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tree-rs"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
7
tree-rs/Cargo.toml
Normal file
7
tree-rs/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "tree-rs"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
208
tree-rs/src/main.rs
Executable file
208
tree-rs/src/main.rs
Executable file
@@ -0,0 +1,208 @@
|
||||
#!/usr/bin/env runrs
|
||||
|
||||
//! ```cargo
|
||||
//! [dependencies]
|
||||
//! clap = { version = "4.5", features = ["derive"] }
|
||||
//! ```
|
||||
|
||||
use clap::{Parser, ValueHint};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "tree")]
|
||||
#[command(version = "0.1.0")]
|
||||
#[command(about = "Display directory tree structure")]
|
||||
struct Args {
|
||||
#[arg(value_hint = ValueHint::DirPath)]
|
||||
path: Option<PathBuf>,
|
||||
|
||||
#[arg(short = 'L', long = "depth", help = "Maximum depth to display")]
|
||||
depth: Option<usize>,
|
||||
|
||||
#[arg(short = 'H', long = "human-readable", help = "Display file sizes")]
|
||||
human_readable: bool,
|
||||
|
||||
#[arg(long = "no-color", help = "Disable color output")]
|
||||
no_color: bool,
|
||||
|
||||
#[arg(long = "hide-dots", help = "Hide directories/files starting with a dot")]
|
||||
hide_dots: bool,
|
||||
|
||||
#[arg(long = "hide", help = "Hide entries matching this name")]
|
||||
hide: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
let path = args.path.unwrap_or_else(|| PathBuf::from("."));
|
||||
|
||||
if !path.exists() {
|
||||
eprintln!("Error: path '{}' does not exist", path.display());
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
print_tree(
|
||||
&path,
|
||||
"",
|
||||
true,
|
||||
true,
|
||||
0,
|
||||
args.depth,
|
||||
args.human_readable,
|
||||
args.no_color,
|
||||
args.hide_dots,
|
||||
args.hide.clone(),
|
||||
);
|
||||
}
|
||||
|
||||
fn print_tree(
|
||||
path: &Path,
|
||||
prefix: &str,
|
||||
is_last: bool,
|
||||
is_root: bool,
|
||||
current_depth: usize,
|
||||
max_depth: Option<usize>,
|
||||
human_readable: bool,
|
||||
no_color: bool,
|
||||
hide_dots: bool,
|
||||
hide: Vec<String>,
|
||||
) {
|
||||
let entry_name = path
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
||||
|
||||
let is_dir = path.is_dir();
|
||||
let size_str = if human_readable && !is_dir {
|
||||
let metadata = fs::metadata(path).ok();
|
||||
metadata
|
||||
.map(|m| format_size(m.len(), no_color))
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
if is_root {
|
||||
println!("{}", entry_name);
|
||||
} else {
|
||||
let connector = if is_last { "└── " } else { "├── " };
|
||||
let colored_name = if is_dir && !no_color {
|
||||
format!("{}{}{}", BLUE, entry_name, RESET)
|
||||
} else {
|
||||
entry_name
|
||||
};
|
||||
println!("{}{}{}{}", prefix, connector, colored_name, size_str);
|
||||
}
|
||||
|
||||
if !path.is_dir() {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(depth) = max_depth {
|
||||
if current_depth >= depth {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let human_readable = human_readable;
|
||||
|
||||
let mut entries: Vec<_> = fs::read_dir(path)
|
||||
.unwrap_or_else(|e| panic!("Failed to read directory {:?}: {}", path, e))
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| {
|
||||
let binding = e.file_name();
|
||||
let name = binding.to_string_lossy();
|
||||
if hide_dots && name.starts_with('.') {
|
||||
return false;
|
||||
}
|
||||
if hide.contains(&name.to_string()) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
})
|
||||
.collect();
|
||||
|
||||
entries.sort_by(|a, b| {
|
||||
let a_is_dir = a.path().is_dir();
|
||||
let b_is_dir = b.path().is_dir();
|
||||
if a_is_dir != b_is_dir {
|
||||
a_is_dir.cmp(&b_is_dir)
|
||||
} else {
|
||||
a.file_name().cmp(&b.file_name())
|
||||
}
|
||||
});
|
||||
|
||||
let total = entries.len();
|
||||
for (i, entry) in entries.iter().enumerate() {
|
||||
let is_last_entry = i == total - 1;
|
||||
|
||||
if is_root {
|
||||
print_tree(
|
||||
&entry.path(),
|
||||
"",
|
||||
is_last_entry,
|
||||
false,
|
||||
1,
|
||||
max_depth,
|
||||
human_readable,
|
||||
no_color,
|
||||
hide_dots,
|
||||
hide.clone(),
|
||||
);
|
||||
} else {
|
||||
let new_prefix = if is_last {
|
||||
format!("{} ", prefix)
|
||||
} else {
|
||||
format!("{}│ ", prefix)
|
||||
};
|
||||
print_tree(
|
||||
&entry.path(),
|
||||
&new_prefix,
|
||||
is_last_entry,
|
||||
false,
|
||||
current_depth + 1,
|
||||
max_depth,
|
||||
human_readable,
|
||||
no_color,
|
||||
hide_dots,
|
||||
hide.clone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const BLUE: &str = "\x1b[34m";
|
||||
const GREEN: &str = "\x1b[32m";
|
||||
const RESET: &str = "\x1b[0m";
|
||||
|
||||
fn format_size(bytes: u64, no_color: bool) -> String {
|
||||
const UNITS: &[&str] = &["B", "K", "M", "G", "T", "P"];
|
||||
let base: f64 = 1024.0;
|
||||
if bytes == 0 {
|
||||
return if no_color {
|
||||
" 0B".to_string()
|
||||
} else {
|
||||
format!("{} 0B{}", GREEN, RESET)
|
||||
};
|
||||
}
|
||||
let exp = ((bytes as f64).ln() / base.ln()).floor() as usize;
|
||||
let exp = exp.min(UNITS.len() - 1);
|
||||
let val = bytes as f64 / base.powi(exp as i32);
|
||||
let size = if val >= 10.0 {
|
||||
format!(" {:.0}{}", val, UNITS[exp])
|
||||
} else if val >= 1.0 {
|
||||
format!(" {:.1}{}", val, UNITS[exp])
|
||||
} else {
|
||||
format!(" {:.0}B", val)
|
||||
};
|
||||
if no_color {
|
||||
size
|
||||
} else {
|
||||
format!("{}{}{}", GREEN, size, RESET)
|
||||
}
|
||||
}
|
||||
|
||||
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260418T013425+08:00.MEQCIBLXDlWGN9mmf8L/gXhw
|
||||
// 92hRbs/GxAezQk646+N4Ho7OAiBAER2l2R1mPi00O51Ns00QIQN+WgAFD+RTyCnc8nbefw==
|
||||
Reference in New Issue
Block a user