use std::io::Read; use tally::count; fn main() { let mut args: Vec = 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); } }