Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add byteshumanreadable(bytes, num_digits) to expressions #1277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bkueng
Copy link

@bkueng bkueng commented Jan 25, 2025

Description

Can be used to display bytes, e.g. free disk space ${byteshumanreadable(EWW_DISK["/"].free,1)} -> 150.3 GB.

Or also for rates, e.g. ${byteshumanreadable(EWW_NET.wlp3s0.NET_DOWN,0)}/s -> 392 KB/s

The name is a bit long, but I could not think of anything better.

Checklist

Please make sure you can check all the boxes that apply to this PR.

  • All widgets I've added are correctly documented.
  • I added my changes to CHANGELOG.md, if appropriate.
  • The documentation in the docs/content/main directory has been adjusted to reflect my changes.
  • I used cargo fmt to automatically format all code before committing

let mut size = size.as_f64()?;
let digits = if size > 0.0 { digits.as_i32()? } else { 0 };
let mut suffix = "B";
while size >= 1024.0 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is added, it would be good to allow users to choose between binary and decimal prefixes: https://en.wikipedia.org/wiki/Byte#Multiple-byte_units and adjust the code accordingly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add that. Maybe with an optional argument binary/decimal?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

@bkueng bkueng force-pushed the byteshumanreadable branch from f427aaa to a7f9b74 Compare March 2, 2025 13:26
while size >= base {
match suffix {
"B" => {
suffix = "KB";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suffixes should also be adjusted accordingly.

Also, it might be better to not do this using a loop (just thinking in terms of performance). I would use an ordered array of suffixes and just index into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants