pretty-bytes-0.2.2/.gitignore01006440000765000002400000000007126105630530014373 0ustar0000000000000000target pretty-bytes-0.2.2/Cargo.toml.orig01006440000765000002400000000425131643230500015272 0ustar0000000000000000[package] name = "pretty-bytes" version = "0.2.2" authors = ["Kohei Hasegawa"] description = "Convert bytes to a human readable string" repository = "https://github.com/banyan/rust-pretty-bytes" license = "MIT" readme = "README.md" [dependencies] getopts = "0.2" atty = "0.2" pretty-bytes-0.2.2/Cargo.toml0000644000000015000010521 0ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "pretty-bytes" version = "0.2.2" authors = ["Kohei Hasegawa"] description = "Convert bytes to a human readable string" readme = "README.md" license = "MIT" repository = "https://github.com/banyan/rust-pretty-bytes" [dependencies.getopts] version = "0.2" [dependencies.atty] version = "0.2" pretty-bytes-0.2.2/Cargo.toml.orig0000644000000004250011465 0ustar00[package] name = "pretty-bytes" version = "0.2.2" authors = ["Kohei Hasegawa"] description = "Convert bytes to a human readable string" repository = "https://github.com/banyan/rust-pretty-bytes" license = "MIT" readme = "README.md" [dependencies] getopts = "0.2" atty = "0.2" pretty-bytes-0.2.2/circle.yml01006440000765000002400000000534131620615050014372 0ustar0000000000000000machine: pre: - curl -sS https://static.rust-lang.org/rustup.sh > rustup.sh - chmod +x ./rustup.sh - ./rustup.sh --yes - sudo apt-get install libXxf86vm-dev libosmesa6-dev checkout: post: - git config --global --unset url.ssh://git@github.com:.insteadof test: override: - cargo build --verbose -j 2 - cargo test pretty-bytes-0.2.2/README.md01006440000765000002400000001112126140045570013663 0ustar0000000000000000# rust-pretty-bytes [![Circle CI](https://img.shields.io/circleci/project/banyan/rust-pretty-bytes.svg)](https://circleci.com/gh/banyan/rust-pretty-bytes) >Convert bytes to a human readable string: 1337 → 1.34 kB Useful for displaying file sizes for humans, Ported from [sindresorhus/pretty-bytes](https://github.com/sindresorhus/pretty-bytes) ## Usage ### CLI ```shell $ pretty-bytes 1337 1.34 kB $ echo 1337 | pretty-bytes 1.34 kB ``` ### API ```rust extern crate pretty_bytes; use pretty_bytes::converter::convert; println!("{}", convert(1337_f64)); ``` ### License MIT pretty-bytes-0.2.2/src/cli.rs01006440000765000002400000002535131620615050014315 0ustar0000000000000000use ::converter; use std::io; use std::env; use getopts::Options; use atty::{self, Stream}; fn print_usage(program: &str, opts: Options) { let brief = format!( r#"Usage: $ {} $ echo | {}"#, program, program); print!("{}", opts.usage(&brief)); } fn print_version() { println!("{}", env!("CARGO_PKG_VERSION")); } pub fn run(args: env::Args) -> () { let num: f64 = if atty::is(Stream::Stdin) { let args: Vec = args.collect(); let ref program = args[0]; let mut opts = Options::new(); opts.optflag("h", "help", "print this help menu"); opts.optflag("v", "version", "print version"); let matches = match opts.parse(&args[1..]) { Ok(m) => { m } Err(f) => { panic!(f.to_string()) } }; if matches.opt_present("v") { return print_version(); } else if matches.opt_present("h") || args.len() != 2 { return print_usage(&program, opts); } match args[1].parse::() { Ok(n) => n, Err(f) => { panic!(f.to_string()) } } } else { let mut input = String::new(); io::stdin().read_line(&mut input).ok().expect("Unable to read from console!"); input.trim().parse::().unwrap() }; println!("{}", converter::convert(num)); } pretty-bytes-0.2.2/src/converter.rs01006440000765000002400000001134126135360730015556 0ustar0000000000000000use std::cmp; pub fn convert(num: f64) -> String { let negative = if num.is_sign_positive() { "" } else { "-" }; let num = num.abs(); let units = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; if num < 1_f64 { return format!("{}{} {}", negative, num, "B"); } let delimiter = 1000_f64; let exponent = cmp::min((num.ln() / delimiter.ln()).floor() as i32, (units.len() - 1) as i32); let pretty_bytes = format!("{:.2}", num / delimiter.powi(exponent)).parse::().unwrap() * 1_f64; let unit = units[exponent as usize]; format!("{}{} {}", negative, pretty_bytes, unit) } pretty-bytes-0.2.2/src/lib.rs01006440000765000002400000000112131620615050014301 0ustar0000000000000000extern crate getopts; extern crate atty; pub mod cli; pub mod converter; pretty-bytes-0.2.2/tests/converter.rs01006440000765000002400000001420126135364060016127 0ustar0000000000000000extern crate pretty_bytes; use pretty_bytes::converter::convert; #[test] fn it_converts_bytes_to_human_readable_strings() { assert_eq!(convert(0_f64), "0 B"); assert_eq!(convert(0.4_f64), "0.4 B"); assert_eq!(convert(0.7_f64), "0.7 B"); assert_eq!(convert(10_f64), "10 B"); assert_eq!(convert(10.1_f64), "10.1 B"); assert_eq!(convert(999_f64), "999 B"); assert_eq!(convert(1001_f64), "1 kB"); assert_eq!(convert(1e16), "10 PB"); assert_eq!(convert(1e30), "1000000 YB"); } #[test] fn it_supports_negative_numbers() { assert_eq!(convert(-0.4_f64), "-0.4 B"); assert_eq!(convert(-0.7_f64), "-0.7 B"); assert_eq!(convert(-10.1_f64), "-10.1 B"); assert_eq!(convert(-999_f64), "-999 B"); assert_eq!(convert(-1001_f64), "-1 kB"); }