Add more information to EWW_RAM, fix documentation of magic variables

Better EWW_RAM / Docs update
This commit is contained in:
ElKowar 2021-08-27 18:35:38 +02:00 committed by GitHub
commit 70285e0ebf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 26 deletions

View file

@ -22,14 +22,14 @@ macro_rules! builtin_vars {
pub fn get_inbuilt_vars() -> HashMap<VarName, ScriptVarDefinition> {
builtin_vars! {Duration::new(2, 0),
// @desc EWW_TEMPS - Heat of the components in Celcius\nExample: `{(CPU_TEMPS.core_1 + CPU_TEMPS.core_2) / 2}`
"EWW_TEMPS" => || Ok(DynVal::from(cores())),
// @desc EWW_TEMPS - Heat of the components in Celcius
"EWW_TEMPS" => || Ok(DynVal::from(get_temperatures())),
// @desc EWW_RAM - The current RAM + Swap usage
"EWW_RAM" => || Ok(DynVal::from(format!("{:.2}", ram()))),
// @desc EWW_RAM - Information on ram and swap usage in kB.
"EWW_RAM" => || Ok(DynVal::from(get_ram())),
// @desc EWW_DISK - Information on on all mounted partitions (Might report inaccurately on some filesystems, like btrfs)\nExample: `{EWW_DISK["/"]}`
"EWW_DISK" => || Ok(DynVal::from(disk())),
"EWW_DISK" => || Ok(DynVal::from(get_disks())),
// @desc EWW_BATTERY - Battery capacity in procent of the main battery
"EWW_BATTERY" => || Ok(DynVal::from(
@ -42,8 +42,8 @@ pub fn get_inbuilt_vars() -> HashMap<VarName, ScriptVarDefinition> {
}
)),
// @desc EWW_CPU_USAGE - Average CPU usage (all cores) since the last update (No MacOS support)
"EWW_CPU_USAGE" => || Ok(DynVal::from(get_avg_cpu_usage())),
// @desc EWW_CPU - Information on the CPU cores: frequency and usage (No MacOS support)
"EWW_CPU" => || Ok(DynVal::from(get_cpus())),
// @desc EWW_NET - Bytes up/down on all interfaces
"EWW_NET" => || Ok(DynVal::from(net())),

View file

@ -7,7 +7,7 @@ use sysinfo::{ComponentExt, DiskExt, NetworkExt, NetworksExt, ProcessorExt, Syst
static SYSTEM: Lazy<Mutex<System>> = Lazy::new(|| Mutex::new(System::new()));
pub fn disk() -> String {
pub fn get_disks() -> String {
let mut c = SYSTEM.lock().unwrap();
c.refresh_disks_list();
@ -15,24 +15,44 @@ pub fn disk() -> String {
"{{ {} }}",
c.get_disks()
.iter()
.map(|c| format!(
r#""{}": {{"name": {:?}, "total": {}, "free": {}}}"#,
c.get_mount_point().display(),
c.get_name(),
c.get_total_space(),
c.get_available_space(),
))
.map(|c| {
let total_space = c.get_total_space();
let available_space = c.get_available_space();
let used_space = total_space - available_space;
format!(
r#""{}": {{"name": {:?}, "total": {}, "free": {}, "used": {}, "used_perc": {}}}"#,
c.get_mount_point().display(),
c.get_name(),
total_space,
available_space,
used_space,
(used_space as f32 / total_space as f32) * 100f32,
)
})
.join(",")
)
}
pub fn ram() -> f32 {
pub fn get_ram() -> String {
let mut c = SYSTEM.lock().unwrap();
c.refresh_memory();
(c.get_used_memory() as f32 + c.get_used_swap() as f32) / 1_000_000f32
let total_memory = c.get_total_memory();
let available_memory = c.get_available_memory();
let used_memory = total_memory as f32 - available_memory as f32;
format!(
r#"{{"total_mem": {}, "free_mem": {}, "total_swap": {}, "free_swap": {}, "available_mem": {}, "used_mem": {}, "used_mem_perc": {}}}"#,
total_memory,
c.get_free_memory(),
c.get_total_swap(),
c.get_free_swap(),
available_memory,
used_memory,
(used_memory / total_memory as f32) * 100f32,
)
}
pub fn cores() -> String {
pub fn get_temperatures() -> String {
let mut c = SYSTEM.lock().unwrap();
c.refresh_components_list();
c.refresh_components();
@ -45,7 +65,7 @@ pub fn cores() -> String {
)
}
pub fn get_avg_cpu_usage() -> String {
pub fn get_cpus() -> String {
let mut c = SYSTEM.lock().unwrap();
c.refresh_cpu();
let processors = c.get_processors();
@ -53,7 +73,12 @@ pub fn get_avg_cpu_usage() -> String {
r#"{{ "cores": [{}], "avg": {} }}"#,
processors
.iter()
.map(|a| format!(r#"{{"core": "{}", "freq": {}, "usage": {}}}"#, a.get_name(), a.get_frequency(), a.get_cpu_usage()))
.map(|a| format!(
r#"{{"core": "{}", "freq": {}, "usage": {:.0}}}"#,
a.get_name(),
a.get_frequency(),
a.get_cpu_usage()
))
.join(","),
processors.iter().map(|a| a.get_cpu_usage()).avg()
)
@ -133,11 +158,12 @@ pub fn get_battery_capacity() -> Result<String> {
#[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "linux"))]
pub fn get_battery_capacity() -> Result<u8> {
anyhow!("eww doesn't support your OS for getting the battery capacity")
anyhow!("Eww doesn't support your OS for getting the battery capacity")
}
pub fn net() -> String {
let mut c = SYSTEM.lock().unwrap();
c.refresh_networks_list();
let interfaces = format!(
"{{ {} }}",
&c.get_networks()
@ -145,6 +171,5 @@ pub fn net() -> String {
.map(|a| format!(r#""{}": {{ "NET_UP": {}, "NET_DOWN": {} }}"#, a.0, a.1.get_transmitted(), a.1.get_received()))
.join(","),
);
c.refresh_networks_list();
interfaces
}

View file

@ -184,15 +184,25 @@ impl ToDiagnostic for ValidationError {
"Hint: Define it as a global variable"
}
};
diag.with_notes(vec![format!(
"Hint: If you meant to use the literal value \"{}\", surround the value in quotes",
name
)])
let mut extra_notes =
vec![format!("Hint: If you meant to use the literal value \"{}\", surround the value in quotes", name)];
if let Some(deprecation_note) = variable_deprecation_note(name.to_string()) {
extra_notes.push(deprecation_note)
};
diag.with_notes(extra_notes)
}
}
}
}
fn variable_deprecation_note(var_name: String) -> Option<String> {
(var_name == "EWW_CPU_USAGE")
.then(|| "Note: EWW_CPU_USAGE has recently been removed, and has now been renamed to EWW_CPU".to_string())
}
fn lalrpop_error_to_diagnostic<T: std::fmt::Display, E: Spanned + ToDiagnostic>(
error: &lalrpop_util::ParseError<usize, T, E>,
file_id: usize,