Skip to content

Commit

Permalink
Implement upstream swap readout
Browse files Browse the repository at this point in the history
This is currently limited to Linux.
  • Loading branch information
grtcdr committed Oct 29, 2024
1 parent a4b50a4 commit b86a980
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
build = "build.rs"

[dependencies]
libmacchina = { version = "8.0.0", features = ["version"] }
libmacchina = { git = "https://github.com/Macchina-CLI/libmacchina", features = ["version"] }
bytesize = "1.3.0"
shellexpand = "3.1.0"
clap = { version = "4.4.6", features = ["derive"] }
Expand Down
27 changes: 27 additions & 0 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum ReadoutKey {
Processor,
ProcessorLoad,
Memory,
Swap,
Battery,
GPU,
DiskSpace,
Expand All @@ -58,6 +59,7 @@ impl Display for ReadoutKey {
Self::Processor => write!(f, "Processor"),
Self::ProcessorLoad => write!(f, "ProcessorLoad"),
Self::Memory => write!(f, "Memory"),
Self::Swap => write!(f, "Swap"),
Self::Battery => write!(f, "Battery"),
Self::GPU => write!(f, "GPU"),
Self::DiskSpace => write!(f, "DiskSpace"),
Expand Down Expand Up @@ -188,6 +190,7 @@ pub fn get_all_readouts<'a>(
handle_readout_processor_load(&mut readout_values, &general_readout, theme)
}
ReadoutKey::Memory => handle_readout_memory(&mut readout_values, theme, opt),
ReadoutKey::Swap => handle_readout_swap(&mut readout_values, theme, opt),
ReadoutKey::Battery => handle_readout_battery(&mut readout_values, theme),
ReadoutKey::DesktopEnvironment => {
handle_readout_desktop_environment(&mut readout_values, &general_readout)
Expand Down Expand Up @@ -426,6 +429,30 @@ fn handle_readout_memory(readout_values: &mut Vec<Readout>, theme: &Theme, opt:
}
}

fn handle_readout_swap(readout_values: &mut Vec<Readout>, theme: &Theme, opt: &Opt) {
use crate::format::memory as format_mem;
use libmacchina::traits::MemoryReadout as _;

let memory_readout = MemoryReadout::new();
let total = memory_readout.swap_total();
let used = memory_readout.swap_used();

match (total, used) {
(Ok(total), Ok(used)) => {
if theme.get_bar().is_visible() {
let bar = create_bar(theme, crate::bars::usage(used, total));
readout_values.push(Readout::new(ReadoutKey::Swap, bar))
} else {
readout_values.push(Readout::new(
ReadoutKey::Swap,
format_mem(total, used, opt.memory_percentage),
))
}
}
(Err(e), _) | (_, Err(e)) => readout_values.push(Readout::new_err(ReadoutKey::Swap, e)),
}
}

fn handle_readout_battery(readout_values: &mut Vec<Readout>, theme: &Theme) {
use crate::format::battery as format_bat;
use libmacchina::traits::BatteryReadout as _;
Expand Down
1 change: 1 addition & 0 deletions src/theme/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ impl Theme {
ReadoutKey::Backlight => self.keys.get_backlight(),
ReadoutKey::Uptime => self.keys.get_uptime(),
ReadoutKey::Memory => self.keys.get_memory(),
ReadoutKey::Swap => self.keys.get_swap(),
ReadoutKey::GPU => self.keys.get_gpu(),
ReadoutKey::DiskSpace => self.keys.get_disk_space(),
}
Expand Down
10 changes: 10 additions & 0 deletions src/theme/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub struct Keys {
pub packages: Option<String>,
pub uptime: Option<String>,
pub memory: Option<String>,
pub swap: Option<String>,
pub machine: Option<String>,
pub local_ip: Option<String>,
pub backlight: Option<String>,
Expand All @@ -328,6 +329,7 @@ impl Default for Keys {
packages: Some(String::from("Packages")),
uptime: Some(String::from("Uptime")),
memory: Some(String::from("Memory")),
swap: Some(String::from("Swap")),
machine: Some(String::from("Machine")),
local_ip: Some(String::from("Local IP")),
backlight: Some(String::from("Brightness")),
Expand Down Expand Up @@ -437,6 +439,14 @@ impl Keys {
"Memory"
}

pub fn get_swap(&self) -> &str {
if let Some(m) = &self.swap {
return m;
}

"Swap"
}

pub fn get_machine(&self) -> &str {
if let Some(m) = &self.machine {
return m;
Expand Down

0 comments on commit b86a980

Please sign in to comment.