~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
23 lines · 780 B Raw History
use std::io::Read;
use tally::count;

fn main() {
    let mut args: Vec<String> = std::env::args().skip(1).collect();
    let json = args.iter().position(|a| a == "--json").map(|i| args.remove(i)).is_some();
    let path = args.first();
    let text = match path {
        Some(path) => std::fs::read_to_string(path).unwrap(),
        None => {
            let mut text = String::new();
            std::io::stdin().read_to_string(&mut text).unwrap();
            text
        }
    };
    let c = count(&text);
    let name = path.map(String::as_str).unwrap_or("-");
    if json {
        println!(r#"{{"lines":{},"words":{},"bytes":{},"name":"{name}"}}"#, c.lines, c.words, c.bytes);
    } else {
        println!("{:>8} {:>8} {:>8} {name}", c.lines, c.words, c.bytes);
    }
}