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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
use colored::*;
use regex::Regex;
pub struct SimpleTable {
items: Vec<String>,
line: Vec<Vec<String>>,
length: Vec<usize>,
padding: usize,
}
impl SimpleTable {
/// Create a new Table
pub fn new(items: Vec<impl Into<String>>) -> Self {
Self::new_with_padding(items, 2)
}
/// Create a new Table with padding
pub fn new_with_padding(items: Vec<impl Into<String>>, padding: usize) -> Self {
let items: Vec<String> = items.into_iter().map(|v| v.into()).collect();
let mut length = Vec::with_capacity(items.len());
for item in &items {
length.push(display_width(item));
}
SimpleTable {
items,
padding,
line: Vec::new(),
length,
}
}
/// Push a new row of items to the table
pub fn push_item(&mut self, items: Vec<impl Into<String>>) {
let items: Vec<String> = items.into_iter().map(|v| v.into()).collect();
let mut processed_items = Vec::with_capacity(self.items.len());
for i in 0..self.items.len() {
if i < items.len() {
processed_items.push(items[i].clone());
} else {
processed_items.push(String::new());
}
}
for (i, d) in processed_items.iter().enumerate() {
let d_len = display_width(d);
if d_len > self.length[i] {
self.length[i] = d_len;
}
}
self.line.push(processed_items);
}
/// Insert a new row of items at the specified index
pub fn insert_item(&mut self, index: usize, items: Vec<impl Into<String>>) {
let items: Vec<String> = items.into_iter().map(|v| v.into()).collect();
let mut processed_items = Vec::with_capacity(self.items.len());
for i in 0..self.items.len() {
if i < items.len() {
processed_items.push(items[i].clone());
} else {
processed_items.push(String::new());
}
}
for (i, d) in processed_items.iter().enumerate() {
let d_len = display_width(d);
if d_len > self.length[i] {
self.length[i] = d_len;
}
}
self.line.insert(index, processed_items);
}
/// Get the current maximum column widths
fn get_column_widths(&self) -> &[usize] {
&self.length
}
}
impl std::fmt::Display for SimpleTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let column_widths = self.get_column_widths();
// Build the header row
let header: Vec<String> = self
.items
.iter()
.enumerate()
.map(|(i, item)| {
let target_width = column_widths[i] + self.padding;
let current_width = display_width(item);
let space_count = target_width - current_width;
let space = " ".repeat(space_count);
let result = format!("{}{}", item, space);
result
})
.collect();
writeln!(f, "{}", header.join(""))?;
// Build each data row
for row in &self.line {
let formatted_row: Vec<String> = row
.iter()
.enumerate()
.map(|(i, cell)| {
let target_width = column_widths[i] + self.padding;
let current_width = display_width(cell);
let space_count = target_width - current_width;
let spaces = " ".repeat(space_count);
let result = format!("{}{}", cell, spaces);
result
})
.collect();
writeln!(f, "{}", formatted_row.join(""))?;
}
Ok(())
}
}
pub fn display_width(s: &str) -> usize {
// Filter out ANSI escape sequences before calculating width
let filtered_bytes = strip_ansi_escapes::strip(s);
let filtered_str = match std::str::from_utf8(&filtered_bytes) {
Ok(s) => s,
Err(_) => s, // Fallback to original string if UTF-8 conversion fails
};
let mut width = 0;
for c in filtered_str.chars() {
if c.is_ascii() {
width += 1;
} else {
width += 2;
}
}
width
}
/// Convert byte size to a human-readable string format
///
/// Automatically selects the appropriate unit (B, KB, MB, GB, TB) based on the byte size
/// and formats it as a string with two decimal places
pub fn size_str(total_size: usize) -> String {
if total_size < 1024 {
format!("{} B", total_size)
} else if total_size < 1024 * 1024 {
format!("{:.2} KB", total_size as f64 / 1024.0)
} else if total_size < 1024 * 1024 * 1024 {
format!("{:.2} MB", total_size as f64 / (1024.0 * 1024.0))
} else if total_size < 1024 * 1024 * 1024 * 1024 {
format!("{:.2} GB", total_size as f64 / (1024.0 * 1024.0 * 1024.0))
} else {
format!(
"{:.2} TB",
total_size as f64 / (1024.0 * 1024.0 * 1024.0 * 1024.0)
)
}
}
// Convert the Markdown formatted text into a format supported by the command line
pub fn md(text: impl AsRef<str>) -> String {
let bold_re = Regex::new(r"\*\*(.*?)\*\*").unwrap();
let mut result = bold_re
.replace_all(text.as_ref().trim(), |caps: ®ex::Captures| {
format!("{}", caps[1].bold())
})
.to_string();
let italic_re = Regex::new(r"\*(.*?)\*").unwrap();
result = italic_re
.replace_all(&result, |caps: ®ex::Captures| {
format!("{}", caps[1].italic())
})
.to_string();
result
}
|