All Projects → neosmart → prettysize-rs

neosmart / prettysize-rs

Licence: other
Pretty-print file sizes and more

Programming Languages

rust
11053 projects
Makefile
30231 projects

Projects that are alternatives of or similar to prettysize-rs

kul
A unique textual notation that can be used as both a data format and a markup language and that has powerful extensibility of both lexical syntax and semantics, and a Rust library for parsing it.
Stars: ✭ 12 (-58.62%)
Mutual labels:  crate, rust-library
sf
Simple Bash framework which provides argument parsing, usage output and text formatting variables
Stars: ✭ 16 (-44.83%)
Mutual labels:  formatting
rust-phonenumber
Library for parsing, formatting and validating international phone numbers.
Stars: ✭ 99 (+241.38%)
Mutual labels:  rust-library
har-rs
A HTTP Archive format (HAR) serialization & deserialization library, written in Rust.
Stars: ✭ 25 (-13.79%)
Mutual labels:  rust-library
ghakuf
A Rust library for parsing/building SMF (Standard MIDI File).
Stars: ✭ 30 (+3.45%)
Mutual labels:  rust-library
type-metadata
Rust type metadata reflection library
Stars: ✭ 27 (-6.9%)
Mutual labels:  rust-library
twitter-stream-rs
A Rust library for listening on Twitter Streaming API.
Stars: ✭ 66 (+127.59%)
Mutual labels:  rust-library
httper
An asynchronous HTTP(S) client built on top of hyper.
Stars: ✭ 16 (-44.83%)
Mutual labels:  rust-library
git-global
Keep track of all your git repositories.
Stars: ✭ 23 (-20.69%)
Mutual labels:  crate
rustgraphblas
rust-library to wrap GraphBLAS.h
Stars: ✭ 23 (-20.69%)
Mutual labels:  rust-library
jsonfiddle
JSON Fiddling
Stars: ✭ 14 (-51.72%)
Mutual labels:  formatting
codebreaker-rs
A Rust library to decrypt & encrypt any cheat code for CodeBreaker PS2
Stars: ✭ 18 (-37.93%)
Mutual labels:  rust-library
awesome-python-code-formatters
A curated list of awesome Python code formatters
Stars: ✭ 168 (+479.31%)
Mutual labels:  formatting
colorful
Make your terminal output colorful.
Stars: ✭ 43 (+48.28%)
Mutual labels:  rust-library
rust-lcms2
ICC color profiles in Rust
Stars: ✭ 25 (-13.79%)
Mutual labels:  rust-library
tentacle
A multiplexed p2p network framework that supports custom protocols
Stars: ✭ 41 (+41.38%)
Mutual labels:  rust-library
sublime-postcss-sorting
Sublime Text plugin to sort CSS rules content with specified order.
Stars: ✭ 19 (-34.48%)
Mutual labels:  formatting
rsmorphy
Morphological analyzer / inflection engine for Russian and Ukrainian languages rewritten in Rust
Stars: ✭ 27 (-6.9%)
Mutual labels:  rust-library
finny.rs
Finite State Machines for Rust
Stars: ✭ 48 (+65.52%)
Mutual labels:  rust-library
imgref
A trivial Rust struct for interchange of pixel buffers with width, height & stride
Stars: ✭ 45 (+55.17%)
Mutual labels:  rust-library

PrettySize, rust edition

crates.io docs.rs

A comprehensive file size crate for rust applications, meant to be light and effective. Includes utilities for human-readable formatting of file sizes as well as converting between different base-two and base-ten size units and performing both mathematical and logical operations on strongly-typed file sizes.

See the crate documentation for a more complete summary of what this crate can do and how to use it.

Features

PrettySize provides

  • a Size type that can be used to hold a strongly-typed size (e.g. let size = Size::from_gigabytes(4)) and perform operations on it,
  • definitions for the base-two and base-ten file size units defined as pub const in the size::consts namespace, available both in abbreviated and unabridged forms (i.e. consts::KiB and consts::KIBIBYTE or consts::GB and consts::GIGABYTE),
  • an std::Display impl for Size to automatically display sizes in a human-readable format, automatically choosing the best size unit and numeric precision to give the nicest results (you can also use Size::to_string() instead).
  • a Size.format() method that gives you more control over how sizes are converted to a textual representation, letting you to specify the base of the human-readable units and their style (smart, abbreviated, or full; plus their lowercase variants).
  • mathematical and logical operations on strongly-typed Size values,
  • full support for expressing negative sizes (e.g. the difference between two sizes, or the amount of space reclaimed on a disk)

This crate can also be used in no_std mode (by compiling with default features disabled). This disables string conversion/formatting but keeps all the strongly-typed size conversion and mathematical/logical operations available.

This crate is free of any dependencies.

Usage

Cargo.toml:

[dependencies]
size = "0.4"

and in your code:

use size::consts;
use size::{Base, Size};

fn main() {
  // Create strongly-typed sizes:
  let byte_count = Size::from_kilobytes(42);
  assert_eq!(42_000, byte_count.bytes());

  // Use predefined constants for the various units
  let byte_count = 42 * consts::KiB;
  assert_eq!(43_008, byte_count);

  // `Size` can take any numeric type you throw at it
  let byte_count = Size::from_mib(0.040055);
  assert_eq!(byte_count.bytes(), 42_000);

  // And for those of you that haven't yet drunk the base-two Kool-Aid:
  let file_size = Size::from_kb(42);
  assert_eq!(file_size.bytes(), 42_000);

  println!("{}, I say!", file_size);
  // prints "41 KiB, I say!"

  // Override the default choice of base-2 units
  println!("{}, I meant!", file_size.format().with_base(Base::Base10));
  // prints "42 KB, I meant!"

  // Add and subtract strongly-typed sizes, even with different underlying types
  let sum = Size::from_mb(1.0) + Size::from_kb(200);
  assert_eq!(sum.bytes(), 1_200_000);

  // Multiply and divide strongly-typed sizes by scalar values
  let new_size = Size::from_mib(2) * 2;
  assert_eq!(new_size, Size::from_mib(4));

  // Compare sizes for equality or order
  let size1 = Size::from_gigabytes(2);
  let size2 = Size::from_gibibytes(1.99);
  assert!(size1 < size2);
}

About

This project started off as a port of Mahmoud's PrettySize.NET library from C# to Rust. Like the C# edition of this project. Rust's richer enum types and powerful generics made implementing a custom Size generic over the number type without verbosity additionally possible. Its scope has since grown considerably.

License

PrettySize is written and maintained by Mahmoud Al-Qudsi of NeoSmart Technologies and released to the general public under the terms of the MIT public license.

To-Do

  • Providing a FromStr impl to parse file sizes (coming soon!)

Pull requests are welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].