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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use chrono::prelude::*;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const BELOW_RC: &str = "/.config/below/belowrc";
pub fn get_unix_timestamp(timestamp: SystemTime) -> u64 {
timestamp
.duration_since(SystemTime::UNIX_EPOCH)
.expect("SystemTime before UNIX EPOCH!")
.as_secs()
}
pub fn get_system_time(timestamp: u64) -> SystemTime {
UNIX_EPOCH + Duration::from_secs(timestamp)
}
pub fn convert_bytes(val: f64) -> String {
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
if val < 1_f64 {
return format!("{:.1} B", val);
}
let delimiter = 1024_f64;
let exponent = std::cmp::min(
(val.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.1}", val / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let unit = units[exponent as usize];
format!("{} {}", pretty_bytes, unit)
}
pub fn get_prefix(collapsed: bool) -> &'static str {
if collapsed { "└+ " } else { "└─ " }
}
pub fn fold_string<F>(val: &str, width: usize, start_idx: usize, stop_filter: F) -> String
where
F: FnMut(char) -> bool,
{
let str_len = val.len();
if start_idx >= str_len || val[start_idx..].len() <= width || width <= 3 {
return val.into();
}
let first_symbo_pos = val[start_idx..].find(stop_filter).unwrap_or(str_len) + 1;
let mid_str_len = (width - 3) >> 1;
let front_len = std::cmp::min(first_symbo_pos, mid_str_len);
let front_string = val[..front_len].to_string();
let back_string = val[str_len - width + front_len + 3..].to_string();
format!("{}...{}", front_string, back_string)
}
pub fn timestamp_to_datetime(timestamp: &i64) -> String {
let naive = NaiveDateTime::from_timestamp(timestamp.clone(), 0);
let datetime: DateTime<Utc> = DateTime::from_utc(naive, Utc);
datetime
.with_timezone(&Local)
.format("%Y-%m-%d %H:%M:%S")
.to_string()
}
pub fn systemtime_to_datetime(system_time: SystemTime) -> String {
timestamp_to_datetime(&(get_unix_timestamp(system_time) as i64))
}
pub fn is_cpu_significant(v: f64) -> Option<cursive::theme::BaseColor> {
if v > 100.0 {
Some(cursive::theme::BaseColor::Red)
} else {
None
}
}
pub fn get_belowrc_filename() -> String {
format!(
"{}{}",
std::env::var("HOME").expect("Fail to obtain HOME env var"),
BELOW_RC
)
}
pub fn get_belowrc_dump_section_key() -> &'static str {
"dump"
}
pub fn get_belowrc_cmd_section_key() -> &'static str {
"cmd"
}
pub fn get_belowrc_view_section_key() -> &'static str {
"view"
}