1use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
4
5pub fn bytes(value: u64) -> String {
9 const UNITS: &[&str] = &["B", "K", "M", "G", "T", "P"];
10 let mut v = value as f64;
11 let mut unit = 0;
12 while v >= 1024.0 && unit < UNITS.len() - 1 {
13 v /= 1024.0;
14 unit += 1;
15 }
16 if unit == 0 {
17 format!("{value}B")
18 } else if v < 10.0 {
19 format!("{v:.1}{}", UNITS[unit])
20 } else {
21 format!("{v:.0}{}", UNITS[unit])
22 }
23}
24
25pub fn rate(bytes_per_sec: f64) -> String {
27 format!("{}/s", bytes(bytes_per_sec.max(0.0) as u64))
28}
29
30pub fn duration(secs: u64) -> String {
32 let days = secs / 86_400;
33 let hours = (secs % 86_400) / 3_600;
34 let minutes = (secs % 3_600) / 60;
35 if days > 0 {
36 format!("{days}d{hours:02}h")
37 } else if hours > 0 {
38 format!("{hours}h{minutes:02}m")
39 } else {
40 format!("{minutes:02}:{:02}", secs % 60)
41 }
42}
43
44pub fn age(secs: u64) -> String {
46 match secs {
47 0..10 => "just now".to_owned(),
48 10..120 => format!("{secs}s ago"),
49 120..7_200 => format!("{}m ago", secs / 60),
50 7_200..172_800 => format!("{}h ago", secs / 3_600),
51 _ => format!("{}d ago", secs / 86_400),
52 }
53}
54
55pub fn link_speed(mbps: f64) -> String {
57 if mbps >= 1000.0 {
58 let gbps = mbps / 1000.0;
59 if gbps.fract().abs() < 0.05 {
60 format!("{gbps:.0}G")
61 } else {
62 format!("{gbps:.1}G")
63 }
64 } else {
65 format!("{mbps:.0}M")
66 }
67}
68
69pub fn truncate(text: &str, width: usize) -> String {
74 if text.width() <= width {
75 return text.to_owned();
76 }
77 if width == 0 {
78 return String::new();
79 }
80 let mut out = String::new();
82 let mut used = 0;
83 for ch in text.chars() {
84 let w = ch.width().unwrap_or(0);
85 if used + w > width - 1 {
86 break;
87 }
88 out.push(ch);
89 used += w;
90 }
91 out.push('…');
92 out
93}
94
95pub fn pad(text: &str, width: usize) -> String {
98 let mut out = truncate(text, width);
99 let used = out.width();
100 out.extend(std::iter::repeat_n(' ', width.saturating_sub(used)));
101 out
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107 use unicode_width::UnicodeWidthStr;
108
109 #[test]
110 fn bytes_uses_binary_units_and_short_output() {
111 assert_eq!(bytes(0), "0B");
112 assert_eq!(bytes(1023), "1023B");
113 assert_eq!(bytes(1536), "1.5K");
114 assert_eq!(bytes(20 * 1024 * 1024), "20M");
115 assert_eq!(bytes(6_000_067_395_584), "5.5T");
116 }
117
118 #[test]
119 fn rate_appends_per_second() {
120 assert_eq!(rate(3_560_916.6), "3.4M/s");
121 assert_eq!(rate(-1.0), "0B/s");
122 }
123
124 #[test]
125 fn duration_picks_the_largest_units() {
126 assert_eq!(duration(75), "01:15");
127 assert_eq!(duration(3 * 3600 + 5 * 60), "3h05m");
128 assert_eq!(duration(2 * 86_400 + 4 * 3600), "2d04h");
129 }
130
131 #[test]
132 fn age_picks_a_readable_unit() {
133 assert_eq!(age(3), "just now");
134 assert_eq!(age(45), "45s ago");
135 assert_eq!(age(8 * 60), "8m ago");
136 assert_eq!(age(5 * 3600), "5h ago");
137 assert_eq!(age(3 * 86_400), "3d ago");
138 }
139
140 #[test]
141 fn link_speed_uses_g_and_m() {
142 assert_eq!(link_speed(10_000.0), "10G");
143 assert_eq!(link_speed(2_500.0), "2.5G");
144 assert_eq!(link_speed(1_000.0), "1G");
145 assert_eq!(link_speed(576.4), "576M");
146 }
147
148 #[test]
149 fn wide_characters_count_as_two_columns() {
150 assert_eq!(truncate("イーサネット 3", 14), "イーサネット 3");
152 assert_eq!(truncate("イーサネット 3", 8), "イーサ…");
153 assert_eq!(pad("イーサネット 3", 16), "イーサネット 3 ");
154 assert_eq!(pad("enp9s0", 8), "enp9s0 ");
155 assert_eq!(pad("イーサネット", 5).width(), 5);
156 }
157
158 #[test]
159 fn truncate_marks_the_cut() {
160 assert_eq!(truncate("rigtop", 10), "rigtop");
161 assert_eq!(truncate("rigtop-gui", 6), "rigto…");
162 assert_eq!(truncate("abc", 0), "");
163 }
164}