summaryrefslogtreecommitdiffstats
path: root/src/modules/memory_usage.rs
blob: b8fe4fbf9a1c4e7d264aea17214ffa6bba3949f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use ansi_term::Color;

use super::{Context, Module};
use byte_unit::{Byte, ByteUnit};
use sysinfo::{RefreshKind, SystemExt};

/// Creates a module with system memory usage information
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    const DEFAULT_THRESHOLD: i64 = 75;
    const DEFAULT_SHOW_PERCENTAGE: bool = false;
    const RAM_CHAR: &str = "🐏 ";

    let mut module = context.new_module("memory_usage");

    let module_style = module
        .config_value_style("style")
        .unwrap_or_else(|| Color::White.bold().dimmed());

    let system = sysinfo::System::new_with_specifics(RefreshKind::new().with_system());

    let used_memory_kib = system.get_used_memory();
    let total_memory_kib = system.get_total_memory();
    let used_swap_kib = system.get_used_swap();
    let total_swap_kib = system.get_total_swap();

    let percent_mem_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.;
    let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.;

    let threshold = module
        .config_value_i64("threshold")
        .unwrap_or(DEFAULT_THRESHOLD);

    if percent_mem_used.round() < threshold as f64 {
        return None;
    }

    let show_percentage = module
        .config_value_bool("show_percentage")
        .unwrap_or(DEFAULT_SHOW_PERCENTAGE);

    let (display_mem, display_swap) = if show_percentage {
        (
            format!("{:.0}%", percent_mem_used),
            format!("{:.0}%", percent_swap_used),
        )
    } else {
        fn format_kib(n_kib: u64) -> String {
            let byte = Byte::from_unit(n_kib as f64, ByteUnit::KiB)
                .unwrap_or_else(|_| Byte::from_bytes(0));
            let mut display_bytes = byte.get_appropriate_unit(true).format(0);
            display_bytes.retain(|c| c != ' ');
            display_bytes
        }
        (
            format!(
                "{}/{}",
                format_kib(used_memory_kib),
                format_kib(total_memory_kib)
            ),
            format!(
                "{}/{}",
                format_kib(used_swap_kib),
                format_kib(total_swap_kib)
            ),
        )
    };

    let show_swap = module
        .config_value_bool("show_swap")
        .unwrap_or(total_swap_kib != 0);

    module.new_segment("symbol", RAM_CHAR);

    module.set_style(module_style);
    if show_swap {
        module.new_segment(
            "memory_usage",
            &format!("{} | {}", display_mem, display_swap),
        );
    } else {
        module.new_segment("memory_usage", &display_mem);
    }

    module.get_prefix().set_value("");

    Some(module)
}