Crates.io | dirsearch |
lib.rs | dirsearch |
version | 0.1.0 |
source | src |
created_at | 2022-12-16 20:47:54.022827 |
updated_at | 2022-12-16 20:47:54.022827 |
description | đž Rust CLI Template using clap v4 đž |
homepage | https://github.com/ekusiadadus/rust-cli-template |
repository | https://github.com/ekusiadadus/rust-cli-template |
max_upload_size | |
id | 739213 |
size | 21,874 |
$ dirsearch --number [num]
ăăă«ăĄăŻă@ekusiadadusă§ăă CLI ăăŒă«äœăŁăŠăăŸăăïŒ CLI ăăŒă«ă Rust ă§äœăăšăă«ăæŻćç°ćąăæŽăăăźăéąćă ăŁăăźă§ăăăłăăŹăäœăăŸăăă ä»ćăŻăăźăăłăăŹăäœżăŁăŠăç°ĄæçăȘ CLI ăăŒă«ă Rust ă§çéă§äœăŁăŠăżăŸăă
ăăłăăŹăŒăăŻăăĄăă§ăă https://github.com/ekusiadadus/rust-cli-template
ä»ćäœăăłăăłăă©ă€ăłăăŒă«ăŻăăĄăă§ăă https://github.com/ekusiadadus/dirsearch
ä»ćăŻăăăăăăăŁăŹăŻăăȘé äžă«ććšăăăă©ă«ăăăăĄă€ă«ăźæ°ăšăăźć€§ăăăèĄšç€șăă CLI ăăŒă«ăçéă§äœăŁăŠăżăŸăă
ăŸăăŻăăăłăăŹăăŻăăŒăłăăŸăă
git clone https://github.com/ekusiadadus/rust-cli-template.git
cargo run
ă§ćźèĄăăŠăżăăăłăăŹăäœżăă«ăŻăăăłăăŹăźăăŁăŹăŻăăȘă«ç§»ćăăŠăcargo run
ăćźèĄăăŸăă
cd rust-cli-template
cargo run
ăăŸăăăă°ăăăăȘæăă§ărust-cli-template
ăšăăććăź CLI ăăŒă«ăćźèĄăăăŸăă
æąă«ăăźæź”éă§ăcargo run
㧠CLI ăăŒă«ăćźèĄă§ăăç°ćąăæŽăŁăŠăăŸăă
mold + cargo watch ăŻäœżăăȘăăŠăăăă§ăăă仄äžăźçčă§äŸżć©ă§ăă
ăăăèŸșăŻăćèèšäșăèČŒăŁăŠăăăźă§ăăăăăŁăăäœżăŁăŠăżăŠăă ăăă
https://keens.github.io/blog/2021/12/20/moldwotsukautorustnobirudogahayakunaru/
https://qiita.com/kyamamoto9120/items/2081bc44c6c987b9ec29
ä»ćăźć Žćăcargo watch -s 'mold -run cargo run'
ă§ăăăăȘăăŒăă§ăăç°ćąă«ăăŠăăŸăă
Makefile ăèŒăăŠăăăźă§ămake watch
ă§ćăăŸăă
äżćăăăšèȘćçă«ăă«ăăăăŠăćźèĄăăăŸăă
walkDir ăäœżăŁăŠăăăŁăŹăŻăăȘé äžăźăăĄă€ă«ăăă©ă«ăăźæ°ăšć€§ăăăèĄšç€șăăăăă«ăăŸăă
walkdirăă€ăăăŸăă
cargo add walkdir
walkDir ăäœżăă«ăŻăuse walkdir::WalkDir;
ăèżœć ăăŸăă
use walkdir::WalkDir;
fn main() {
for entry in WalkDir::new(".") {
let entry = entry.unwrap();
println!("{}", entry.path().display());
}
}
cargo run
ă§ćźèĄăăăšăăăŁăŹăŻăăȘé
äžăźăăĄă€ă«ăăă©ă«ăăèĄšç€șăăăŸăă
ăăŁăŹăŻăăȘé äžăźăăĄă€ă«ăšăăă©ă«ăăè”°æ»ăăŠăăăĄă€ă«ăźæ°ăšć€§ăăăèĄšç€șăăăăă«ăăŸăă walkdir ăäœżăăšéćžžă«ç°Ąćă«ăăĄă€ă«ăšăă©ă«ăăè”°æ»ă§ăăŸăă
use walkdir::WalkDir;
const DIR: &str = "./";
fn main() {
let mut size: u64 = 0;
let mut count: u64 = 0;
for entry in WalkDir::new(DIR).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() {
size += path.metadata().unwrap().len();
count += 1;
}
println!("{}", entry.path().display());
}
println!("{} files, {} bytes", count, size);
}
ćźéă«cargo run
ă§è”°ăăăŠăżăăšăăăȘæăă
çŸćšăźăăŁăŹăŻăăȘé
äžă«ăŻă626 ćăźăăĄă€ă«ăććšăăŠăç·ćă§304742935 bytes
ăźć€§ăăă«ăȘăăăšăăăăăŸăă
ăăĄă€ă«ă”ă€ășă Rust ă§ăăæăă«èĄšç€șăăă«ăŻăfile_sizeăäœżăăŸăă
use file_size::fit_4;
assert_eq!(&fit_4(999), "999");
assert_eq!(&fit_4(12345), "12K");
assert_eq!(&fit_4(999_999), "1.0M");
assert_eq!(&fit_4(7_155_456_789_012), "7.2T");
ăăăȘæăă§ăăăæăă«ăăĄă€ă«ă”ă€ășăèĄšç€șăăŠăăăăŻăŹăŒăă§ăă
println!("{} files, {} bytes", count, fit_4(size));
äœżăŁăŠăżăăšăăăȘæăă
ăăăăă
ăăŁăŹăŻăăȘé
äžăźăăĄă€ă«ă§äžäœ N 件ăæăŁăŠăăăăă«ăăŸăă
ăăš main
ă性ăăăȘăŁăŠăăăźă§ăéąæ°ă«ćăćșăăŸăă
fn get_dir_size(dir: &str) -> Result<(), Box<dyn Error>> {
let mut size: u64 = 0;
let mut count: u64 = 0;
let mut tops: Vec<Entry> = Vec::with_capacity(NUM + 1);
let mut min_tops: u64 = 0;
for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if path.is_file() {
let t = path.metadata().unwrap().len();
if t > min_tops {
tops.push(Entry {
path: path.to_str().unwrap().to_string(),
size: t,
});
tops.sort_by(|a, b| b.size.cmp(&a.size));
tops.truncate(NUM);
min_tops = tops.last().unwrap().size;
}
size += path.metadata().unwrap().len();
count += 1;
}
}
println!("{} files, {} bytes", count, fit_4(size));
println!("{} largest files:", NUM);
println!("{} | {}", "Size", "Path");
for t in tops {
println!("{} | {}", fit_4(t.size), t.path);
}
Ok(())
}
ćźèĄăăăšăăăȘæăă
clap v4 ăäœżăŁăŠăăłăăłăă©ă€ăłăăŒă«ă«ăăŸăă v4 ăŻăv3 ăšăŻăăȘăéăăźă§ăclap v4 ăźăăă„ăĄăłăăèŠăȘăăéČăăŸăăăă
use clap::Parser;
#[derive(Parser)]
#[command(author, version, about, long_about = None)] // Read from `Cargo.toml`
struct Cli {
#[arg(long)]
number: usize,
}
fn main() {
let cli = Cli::parse();
let num = cli.number;
let dir = DIR;
if num == 0 {
println!("Number of files to show must be greater than 0");
return;
}
get_dir_size(dir, num).unwrap();
}
ćźéă«ćźèĄăăăšăăăȘæăă«ăȘăăŸăă
--number
argument ăćżăăăšæăăăŸăă
äŸăă°ăäžäœ 100 件ăèĄšç€șăăă«ăŻă--number 100
ăšăăŸăă
ăăă©ă«ă㧠--help
ăäœżăăăăă«ăȘăŁăŠăăŸăă
Cargo.toml
ă«æžăăæ
ć ±ăă--help
ă§èĄšç€șăăăŸăă
[package]
name = "rust-cli-template"
version = "0.1.0"
edition = "2021"
license = "MIT"
description = "đž Rust CLI Template using clap v4 đž"
readme = "README.md"
homepage = "https://github.com/ekusiadadus/rust-cli-template"
repository = "https://github.com/ekusiadadus/rust-cli-template"
keywords = ["cli", "Japan", "Rust"]
categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.0.29", features = ["derive"] }
file-size = "1.0.3"
walkdir = "2.3.2"