aprender_zram_cli/commands/
status.rs1use crate::output::OutputFormat;
6use clap::Args;
7use serde::Serialize;
8use trueno_zram_core::zram::{format_size, SysfsOps, ZramOps};
9
10#[derive(Debug, Args)]
12pub struct StatusArgs {
13 #[arg(short, long)]
15 pub device: Option<u32>,
16}
17
18#[derive(Debug, Serialize)]
20struct StatusOutput {
21 device: u32,
22 disksize: u64,
23 orig_data_size: u64,
24 compr_data_size: u64,
25 mem_used_total: u64,
26 algorithm: String,
27 ratio: f64,
28}
29
30pub fn status(args: &StatusArgs, format: OutputFormat) -> Result<(), Box<dyn std::error::Error>> {
35 let ops = SysfsOps::new();
36
37 let statuses: Vec<StatusOutput> = if let Some(dev) = args.device {
38 let s = ops.status(dev)?;
39 let ratio = s.compression_ratio();
40 vec![StatusOutput {
41 device: s.device,
42 disksize: s.disksize,
43 orig_data_size: s.orig_data_size,
44 compr_data_size: s.compr_data_size,
45 mem_used_total: s.mem_used_total,
46 algorithm: s.algorithm,
47 ratio,
48 }]
49 } else {
50 ops.list()?
51 .into_iter()
52 .map(|s| {
53 let ratio = s.compression_ratio();
54 StatusOutput {
55 device: s.device,
56 disksize: s.disksize,
57 orig_data_size: s.orig_data_size,
58 compr_data_size: s.compr_data_size,
59 mem_used_total: s.mem_used_total,
60 algorithm: s.algorithm,
61 ratio,
62 }
63 })
64 .collect()
65 };
66
67 match format {
68 OutputFormat::Table => print_table(&statuses),
69 OutputFormat::Json => {
70 println!("{}", serde_json::to_string_pretty(&statuses)?);
71 }
72 OutputFormat::Raw => {
73 for s in &statuses {
74 println!(
75 "{} {} {} {} {} {}",
76 s.device,
77 s.disksize,
78 s.orig_data_size,
79 s.compr_data_size,
80 s.mem_used_total,
81 s.algorithm
82 );
83 }
84 }
85 }
86
87 Ok(())
88}
89
90fn print_table(statuses: &[StatusOutput]) {
91 println!(
92 "{:>6} {:>10} {:>10} {:>10} {:>10} {:>8} {:>8}",
93 "DEVICE", "DISKSIZE", "DATA", "COMPR", "TOTAL", "ALGO", "RATIO"
94 );
95
96 for s in statuses {
97 println!(
98 "zram{:<2} {:>10} {:>10} {:>10} {:>10} {:>8} {:>7.2}x",
99 s.device,
100 format_size(s.disksize),
101 format_size(s.orig_data_size),
102 format_size(s.compr_data_size),
103 format_size(s.mem_used_total),
104 &s.algorithm[..s.algorithm.len().min(8)],
105 s.ratio
106 );
107 }
108}