Skip to content

Commit b86a980

Browse files
committed
Implement upstream swap readout
This is currently limited to Linux.
1 parent a4b50a4 commit b86a980

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ readme = "README.md"
1111
build = "build.rs"
1212

1313
[dependencies]
14-
libmacchina = { version = "8.0.0", features = ["version"] }
14+
libmacchina = { git = "https://github.com/Macchina-CLI/libmacchina", features = ["version"] }
1515
bytesize = "1.3.0"
1616
shellexpand = "3.1.0"
1717
clap = { version = "4.4.6", features = ["derive"] }

src/data/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum ReadoutKey {
3333
Processor,
3434
ProcessorLoad,
3535
Memory,
36+
Swap,
3637
Battery,
3738
GPU,
3839
DiskSpace,
@@ -58,6 +59,7 @@ impl Display for ReadoutKey {
5859
Self::Processor => write!(f, "Processor"),
5960
Self::ProcessorLoad => write!(f, "ProcessorLoad"),
6061
Self::Memory => write!(f, "Memory"),
62+
Self::Swap => write!(f, "Swap"),
6163
Self::Battery => write!(f, "Battery"),
6264
Self::GPU => write!(f, "GPU"),
6365
Self::DiskSpace => write!(f, "DiskSpace"),
@@ -188,6 +190,7 @@ pub fn get_all_readouts<'a>(
188190
handle_readout_processor_load(&mut readout_values, &general_readout, theme)
189191
}
190192
ReadoutKey::Memory => handle_readout_memory(&mut readout_values, theme, opt),
193+
ReadoutKey::Swap => handle_readout_swap(&mut readout_values, theme, opt),
191194
ReadoutKey::Battery => handle_readout_battery(&mut readout_values, theme),
192195
ReadoutKey::DesktopEnvironment => {
193196
handle_readout_desktop_environment(&mut readout_values, &general_readout)
@@ -426,6 +429,30 @@ fn handle_readout_memory(readout_values: &mut Vec<Readout>, theme: &Theme, opt:
426429
}
427430
}
428431

432+
fn handle_readout_swap(readout_values: &mut Vec<Readout>, theme: &Theme, opt: &Opt) {
433+
use crate::format::memory as format_mem;
434+
use libmacchina::traits::MemoryReadout as _;
435+
436+
let memory_readout = MemoryReadout::new();
437+
let total = memory_readout.swap_total();
438+
let used = memory_readout.swap_used();
439+
440+
match (total, used) {
441+
(Ok(total), Ok(used)) => {
442+
if theme.get_bar().is_visible() {
443+
let bar = create_bar(theme, crate::bars::usage(used, total));
444+
readout_values.push(Readout::new(ReadoutKey::Swap, bar))
445+
} else {
446+
readout_values.push(Readout::new(
447+
ReadoutKey::Swap,
448+
format_mem(total, used, opt.memory_percentage),
449+
))
450+
}
451+
}
452+
(Err(e), _) | (_, Err(e)) => readout_values.push(Readout::new_err(ReadoutKey::Swap, e)),
453+
}
454+
}
455+
429456
fn handle_readout_battery(readout_values: &mut Vec<Readout>, theme: &Theme) {
430457
use crate::format::battery as format_bat;
431458
use libmacchina::traits::BatteryReadout as _;

src/theme/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ impl Theme {
216216
ReadoutKey::Backlight => self.keys.get_backlight(),
217217
ReadoutKey::Uptime => self.keys.get_uptime(),
218218
ReadoutKey::Memory => self.keys.get_memory(),
219+
ReadoutKey::Swap => self.keys.get_swap(),
219220
ReadoutKey::GPU => self.keys.get_gpu(),
220221
ReadoutKey::DiskSpace => self.keys.get_disk_space(),
221222
}

src/theme/components.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ pub struct Keys {
303303
pub packages: Option<String>,
304304
pub uptime: Option<String>,
305305
pub memory: Option<String>,
306+
pub swap: Option<String>,
306307
pub machine: Option<String>,
307308
pub local_ip: Option<String>,
308309
pub backlight: Option<String>,
@@ -328,6 +329,7 @@ impl Default for Keys {
328329
packages: Some(String::from("Packages")),
329330
uptime: Some(String::from("Uptime")),
330331
memory: Some(String::from("Memory")),
332+
swap: Some(String::from("Swap")),
331333
machine: Some(String::from("Machine")),
332334
local_ip: Some(String::from("Local IP")),
333335
backlight: Some(String::from("Brightness")),
@@ -437,6 +439,14 @@ impl Keys {
437439
"Memory"
438440
}
439441

442+
pub fn get_swap(&self) -> &str {
443+
if let Some(m) = &self.swap {
444+
return m;
445+
}
446+
447+
"Swap"
448+
}
449+
440450
pub fn get_machine(&self) -> &str {
441451
if let Some(m) = &self.machine {
442452
return m;

0 commit comments

Comments
 (0)