{"id":13587100,"url":"https://github.com/LeopoldArkham/humansize","last_synced_at":"2025-04-07T19:30:41.112Z","repository":{"id":46313015,"uuid":"74229244","full_name":"LeopoldArkham/humansize","owner":"LeopoldArkham","description":"Humansize - A flexible crate for humanizing file sizes","archived":false,"fork":false,"pushed_at":"2023-08-13T05:28:28.000Z","size":102,"stargazers_count":93,"open_issues_count":11,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-14T12:10:29.635Z","etag":null,"topics":["byes","file","humanize","humansize","rust","sizes"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LeopoldArkham.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-11-19T19:07:32.000Z","updated_at":"2024-03-30T16:14:04.000Z","dependencies_parsed_at":"2024-02-13T21:53:44.667Z","dependency_job_id":null,"html_url":"https://github.com/LeopoldArkham/humansize","commit_stats":{"total_commits":90,"total_committers":8,"mean_commits":11.25,"dds":0.3222222222222222,"last_synced_commit":"caa4cc5260cf7bece6e53aa60a94f8c8115464dd"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeopoldArkham%2Fhumansize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeopoldArkham%2Fhumansize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeopoldArkham%2Fhumansize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeopoldArkham%2Fhumansize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LeopoldArkham","download_url":"https://codeload.github.com/LeopoldArkham/humansize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226215,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["byes","file","humanize","humansize","rust","sizes"],"created_at":"2024-08-01T15:06:01.256Z","updated_at":"2025-04-07T19:30:41.098Z","avatar_url":"https://github.com/LeopoldArkham.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# **Humansize** [![Actively Maintained](https://img.shields.io/badge/Maintenance%20Level-Actively%20Maintained-green.svg)](https://gist.github.com/cheerfulstoic/d107229326a01ff0f333a1d3476e068d)\r\n\r\n[Documentation](https://docs.rs/humansize/latest/humansize/)\r\n\r\n## Features\r\n\r\nHumansize is a humanization library for information size that is:\r\n\r\n- Simple \u0026 convenient to use\r\n- Customizable\r\n- Supports byte or bit sizes\r\n- `no-std`\r\n- Optionally non-allocating\r\n- Optionally accepts signed values\r\n\r\n## How to use it...\r\n\r\nAdd humansize as a dependency to your project's `cargo.toml`:\r\n\r\n```toml\r\n[dependencies]\r\n...\r\nhumansize = \"2.1.3\"\r\n```\r\n\r\n### ... to easily format a size:\r\n\r\n1. Import the `format_size` function as well as your preferred set of defaults:\r\n   - `DECIMAL` (SI)\r\n   - `BINARY` (IEC)\r\n   - `WINDOWS` (IEC values but SI units)\r\n2. Call `format_size` with an unsigned integer\r\n\r\n```rust\r\nuse humansize::{format_size, DECIMAL};\r\n\r\nlet size = 1_000_000u64;\r\nlet res: String = format_size(size, DECIMAL);\r\n\r\nassert_eq!(\u0026res, \"1 MB\");\r\n\r\n```\r\n\r\n### ... to format many sizes:\r\n\r\nTo improve reusability, you can use `make_format`, which returns a formatter function akin to `format_size` but with the options argument curried so it doesn't need to be specified again:\r\n\r\n```rust\r\nuse humansize::{make_format, DECIMAL};\r\n\r\nlet formatter = make_format(DECIMAL);\r\n\r\nassert_eq!(formatter(1_000_000u64), \"1 MB\");\r\nassert_eq!(formatter(1_000_000_000u64), \"1 GB\");\r\n//...\r\n\r\n```\r\n\r\n### ... to avoid allocation:\r\n\r\nSpecify the `no_alloc` feature flag in your project's `cargo.toml`:\r\n\r\n```toml\r\n[dependencies]\r\n...\r\nhumansize = { version = \"2.0.0\", features = [\"no_alloc\"] }\r\n```\r\n\r\nThis excludes all allocating code from compilation. You may now use the library's internal `SizeFormatter` struct, which implements `core::fmt::display` so that you can `write!` it to a custom buffer of your choice:\r\n\r\n```rust\r\nuse humansize::{SizeFormatter, DECIMAL};\r\n\r\nlet formatter = SizeFormatter::new(1_000_000usize, DECIMAL);\r\nassert_eq!(format!(\"{}\", formatter), \"1 MB\");\r\n```\r\n\r\n### ... with the `impl` style API:\r\n\r\nFor stylistic reasons, you may prefer to use the impl-style API of earlier versions of the crate.\r\nTo do so, specify the `impl-style` feature flag in your project's `cargo.toml`:\r\n\r\n```toml\r\n[dependencies]\r\n...\r\nhumansize = { version = \"2.0.0\", features = [\"impl_style\"] }\r\n```\r\n\r\nEnabling this feature makes two methods available:\r\n\r\n- `format_size` on unsigned integers types\r\n- `format_size_i` on signed integer types.\r\n\r\nTo use it, bring the FormatSize trait into scope and call its method on an integer type:\r\n\r\n```ignore\r\nuse humansize::{FormatSize, FormatSizeI, DECIMAL};\r\n\r\nassert_eq!(1_000_000u64.format_size(DECIMAL), \"1 MB\");\r\nassert_eq!((-1_000_000).format_size_i(DECIMAL), \"-1 MB\");\r\n```\r\n\r\n### ... to further customize the output:\r\n\r\nHumansize exports three default option sets:\r\n\r\n- `Decimal`: kilo = 1000, unit format is `XB`.\r\n- `Binary`: kilo = 1024, unit format is `XiB`.\r\n- `WINDOWS` (Windows): kilo = 1024, unit format is `XB`.\r\n\r\nThe formatting can be further customized by providing providing your own option set. See the documentation of the `FormatSizeOptions` struct to see all the addressable parameters, and [this example](examples/custom_options.rs) for its usage.\r\n\r\n### ... to accept negative values:\r\n\r\nThe solutions presented above only accept unsigned integer types as input (`usize`, `8`, `u16`, `u32` and `u64`). If however accepting negative values is correct for your application, a signed alternative exists for each of them that will accept signed integer types, and format them accordingly if negative:\r\n\r\n- `format_size` : `format_size_i`\r\n- `make_format` : `make_format_i`\r\n- `FormatSize` trait : `FormatSizeI` trait\r\n- `SizeFormatter` : `ISizeFormatter`\r\n\r\n```rust\r\nuse humansize::{format_size_i, make_format_i, ISizeFormatter, DECIMAL};\r\n\r\nassert_eq!(\u0026format_size_i(-1_000_000, DECIMAL), \"-1 MB\");\r\n\r\nlet signed_formatter = make_format_i(DECIMAL);\r\nassert_eq!(\u0026signed_formatter(-1_000_000), \"-1 MB\");\r\n\r\n// With the `impl-style` feature enabled:\r\n// use humansize::FormatSizeI;\r\n// assert_eq(-1_000_000.format_size(DECIMAL), \"-1 MB\");\r\n\r\nlet signed_size_formatter = ISizeFormatter::new(-1_000_000, DECIMAL);\r\nassert_eq!(format!(\"{}\", signed_size_formatter), \"-1 MB\");\r\n\r\n```\r\n\r\n## License\r\n\r\nThis project is licensed under either of\r\n\r\n- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or\r\n  http://www.apache.org/licenses/LICENSE-2.0)\r\n- MIT license ([LICENSE-MIT](LICENSE-MIT) or\r\n  http://opensource.org/licenses/MIT)\r\n\r\n### Contribution\r\n\r\nUnless you explicitly state otherwise, any contribution intentionally submitted\r\nfor inclusion in humansize by you, as defined in the Apache-2.0 license, shall be\r\ndual licensed as above, without any additional terms or conditions.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLeopoldArkham%2Fhumansize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLeopoldArkham%2Fhumansize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLeopoldArkham%2Fhumansize/lists"}