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

Add roundsize function #926

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add trigonometric functions (`sin`, `cos`, `tan`, `cot`) and degree/radian conversions (`degtorad`, `radtodeg`) (By: end-4)
- Add `substring` function to simplexpr
- Add `--duration` flag to `eww open`
- Add `roundsize` function to simplexpr

## [0.4.0] (04.09.2022)

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/simplexpr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ chrono-tz = "0.8.2"
strum = { version = "0.24", features = ["derive"] }

eww_shared_util = { version = "0.1.0", path = "../eww_shared_util" }
number_prefix = "0.4.0"


[build-dependencies]
Expand Down
29 changes: 29 additions & 0 deletions crates/simplexpr/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use cached::proc_macro::cached;
use chrono::{Local, LocalResult, TimeZone};
use itertools::Itertools;
use number_prefix::NumberPrefix;

use crate::{
ast::{AccessType, BinOp, SimplExpr, UnaryOp},
dynval::{ConversionError, DynVal},
};
use eww_shared_util::{Span, Spanned, VarName};
use serde_json::json;
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
Expand Down Expand Up @@ -320,6 +322,33 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
}
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"roundsize" => match args.as_slice() {
[size] => {
let size = size.as_f64()?;
Ok(match NumberPrefix::binary(size) {
NumberPrefix::Standalone(bytes) => DynVal::from(json!({"size": bytes, "units": "B"}).to_string()),
NumberPrefix::Prefixed(prefix, n) => {
DynVal::from(json!({"size": n, "units": format!("{}B", prefix)}).to_string())
}
})
}
[size, format_type] => {
let format_type = format_type.as_string()?;
let func = match format_type.as_str() {
"decimal" => NumberPrefix::decimal,
"binary" => NumberPrefix::binary,
_ => return Err(EvalError::NoVariablesAllowed(format_type.parse().unwrap())),
};
let size = size.as_f64()?;
Ok(match func(size) {
NumberPrefix::Standalone(bytes) => DynVal::from(json!({"size": bytes, "units": "B"}).to_string()),
NumberPrefix::Prefixed(prefix, n) => {
DynVal::from(json!({"size": n, "units": format!("{}B", prefix)}).to_string())
}
})
}
_ => Err(EvalError::WrongArgCount(name.to_string())),
},
"sin" => match args.as_slice() {
[num] => {
let num = num.as_f64()?;
Expand Down
2 changes: 2 additions & 0 deletions docs/src/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ Supported currently are the following features:
Same as other `formattime`, but does not accept timezone. Instead, it uses system's local timezone.
Check [chrono's documentation](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) for more
information about format string.
- `roundsize(bytes)`: Round a number of bytes to human-readable on binary base (KiB, MiB, GiB). Returned value format `{ size, units }`
- `roundsize(bytes, format_type)`: Round a number of bytes to human-readable with specific format type it's can be `binary (KiB, MiB, GiB...)` or `decimal (KB, MB, GB...)`. Returned value format `{ size, units }`