1 unstable release
0.1.0 | Dec 16, 2022 |
---|
#2494 in Command line utilities
13KB
55 lines
dirsearch
Usage
$ dirsearch --number [num]
Article
ăăă«ăĄăŻă@ekusiadadusă§ăă CLI ăăŒă«äœăŁăŠăăŸăăïŒ CLI ăăŒă«ă Rust ă§äœăăšăă«ăæŻćç°ćąăæŽăăăźăéąćă ăŁăăźă§ăăăłăăŹăäœăăŸăăă ä»ćăŻăăźăăłăăŹăäœżăŁăŠăç°ĄæçăȘ CLI ăăŒă«ă Rust ă§çéă§äœăŁăŠăżăŸăă
ăăłăăŹăŒăăŻăăĄăă§ăă https://github.com/ekusiadadus/rust-cli-template
ä»ćäœăăłăăłăă©ă€ăłăăŒă«ăŻăăĄăă§ăă https://github.com/ekusiadadus/dirsearch
ăăłăăŹăäœżăŁăŠ CLI ăăŒă«ăäœă
ä»ćăŻăăăăăăăŁăŹăŻăăȘé äžă«ććšăăăă©ă«ăăăăĄă€ă«ăźæ°ăšăăźć€§ăăăèĄšç€șăă 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 ăäœżă
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 ăă€ăłăčăăŒă«ăă
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
ăźć€§ăăă«ăȘăăăšăăăăăŸăă
(äœè« 2) ăăĄă€ă«ă”ă€ășă Rust ă§ăăæăă«èĄšç€șăăă«ăŻ...
ăăĄă€ă«ă”ă€ășă 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 件ăæăŁăŠăă
ăăŁăŹăŻăăȘé
äžăźăăĄă€ă«ă§äžäœ 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(())
}
ćźèĄăăăšăăăȘæăă
ăăŁăŹăŻăăȘé äžăźăăĄă€ă«ă§äžäœ N 件ăæăŁăŠăă (䞊ććŠç)
Clap ăäœżăŁăŠăăłăăłăă©ă€ăłăăŒă«ă«ăă
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"
Dependencies
~1.3â8.5MB
~71K SLoC