-
-
Notifications
You must be signed in to change notification settings - Fork 422
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
base: master
Are you sure you want to change the base?
Conversation
crates/simplexpr/src/eval.rs
Outdated
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.
f427aaa
to
a7f9b74
Compare
while size >= base { | ||
match suffix { | ||
"B" => { | ||
suffix = "KB"; |
There was a problem hiding this comment.
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.
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.
docs/content/main
directory has been adjusted to reflect my changes.cargo fmt
to automatically format all code before committing