1pub fn bytes_to_human_format(bytes: i64) -> String {
2 const KB: f64 = 1024.0;
3 const MB: f64 = KB * 1024.0;
4 const GB: f64 = MB * 1024.0;
5 const TB: f64 = GB * 1024.0;
6
7 if bytes == 0 {
8 return "0 B".to_string();
9 }
10
11 let (value, unit) = if bytes < (KB - 25f64) as i64 {
12 (bytes as f64, "B")
13 } else if bytes < (MB - 25f64) as i64 {
14 (bytes as f64 / KB, "KB")
15 } else if bytes < (GB - 25f64) as i64 {
16 (bytes as f64 / MB, "MB")
17 } else if bytes < (TB - 25f64) as i64 {
18 (bytes as f64 / GB, "GB")
19 } else {
20 (bytes as f64 / TB, "TB")
21 };
22
23 format!("{value:.1} {unit}")
24}
25
26pub fn seconds_to_human_format(seconds: i64) -> String {
27 const MINUTE: i64 = 60;
28 const HOUR: i64 = MINUTE * 60;
29 const DAY: i64 = HOUR * 24;
30
31 if seconds == 0 {
32 return "0s".to_string();
33 }
34
35 let mut curr_string = String::new();
36
37 let mut rest = seconds;
38 if seconds > DAY {
39 let days = rest / DAY;
40 rest %= DAY;
41
42 curr_string = format!("{curr_string}{days}d");
43 }
44
45 if seconds > HOUR {
46 let hours = rest / HOUR;
47 rest %= HOUR;
48 curr_string = format!("{curr_string}{hours}h");
49 if seconds > DAY {
51 return curr_string;
52 }
53 }
54
55 if seconds > MINUTE {
56 let minutes = rest / MINUTE;
57 rest %= MINUTE;
58 curr_string = format!("{curr_string}{minutes}m");
59 }
60
61 curr_string = format!("{curr_string}{rest}s");
62 curr_string
63}
64
65pub fn truncated_str(str: &str, max: usize) -> String {
66 if str.chars().count() < max {
67 str.to_string()
68 } else {
69 let truncated: String = str.chars().take(max).collect();
70 format!("\"{truncated}...\"")
71 }
72}