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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
use colored::*;
use just_enough_vcs::lib::data::sheet::SheetMappingMetadata;
use std::{
collections::{BTreeMap, HashMap, VecDeque},
path::PathBuf,
};
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 text = text.as_ref().trim();
let mut result = String::new();
let mut color_stack: VecDeque<String> = VecDeque::new();
let mut i = 0;
let chars: Vec<char> = text.chars().collect();
while i < chars.len() {
// Check for escape character \
if chars[i] == '\\' && i + 1 < chars.len() {
let escaped_char = chars[i + 1];
// Only escape specific characters
if matches!(escaped_char, '*' | '<' | '>' | '`') {
let mut escaped_text = escaped_char.to_string();
// Apply current color stack
for color in color_stack.iter().rev() {
escaped_text = apply_color(&escaped_text, color);
}
result.push_str(&escaped_text);
i += 2;
continue;
}
}
// Check for color tag start [[color]]
if i + 1 < chars.len() && chars[i] == '[' && chars[i + 1] == '[' {
let mut j = i + 2;
while j < chars.len()
&& !(chars[j] == ']' && j + 1 < chars.len() && chars[j + 1] == ']')
{
j += 1;
}
if j + 1 < chars.len() {
let tag_content: String = chars[i + 2..j].iter().collect();
// Check if it's a closing tag [[/]]
if tag_content == "/" {
color_stack.pop_back();
i = j + 2;
continue;
}
// It's a color tag
color_stack.push_back(tag_content.clone());
i = j + 2;
continue;
}
}
// Check for bold **text**
if i + 1 < chars.len() && chars[i] == '*' && chars[i + 1] == '*' {
let mut j = i + 2;
while j + 1 < chars.len() && !(chars[j] == '*' && chars[j + 1] == '*') {
j += 1;
}
if j + 1 < chars.len() {
let bold_text: String = chars[i + 2..j].iter().collect();
let mut formatted_text = bold_text.bold().to_string();
// Apply current color stack
for color in color_stack.iter().rev() {
formatted_text = apply_color(&formatted_text, color);
}
result.push_str(&formatted_text);
i = j + 2;
continue;
}
}
// Check for italic *text*
if chars[i] == '*' {
let mut j = i + 1;
while j < chars.len() && chars[j] != '*' {
j += 1;
}
if j < chars.len() {
let italic_text: String = chars[i + 1..j].iter().collect();
let mut formatted_text = italic_text.italic().to_string();
// Apply current color stack
for color in color_stack.iter().rev() {
formatted_text = apply_color(&formatted_text, color);
}
result.push_str(&formatted_text);
i = j + 1;
continue;
}
}
// Check for angle-bracketed content <text>
if chars[i] == '<' {
let mut j = i + 1;
while j < chars.len() && chars[j] != '>' {
j += 1;
}
if j < chars.len() {
// Include the angle brackets in the output
let angle_text: String = chars[i..=j].iter().collect();
let mut formatted_text = angle_text.cyan().to_string();
// Apply current color stack
for color in color_stack.iter().rev() {
formatted_text = apply_color(&formatted_text, color);
}
result.push_str(&formatted_text);
i = j + 1;
continue;
}
}
// Check for inline code `text`
if chars[i] == '`' {
let mut j = i + 1;
while j < chars.len() && chars[j] != '`' {
j += 1;
}
if j < chars.len() {
// Include the backticks in the output
let code_text: String = chars[i..=j].iter().collect();
let mut formatted_text = code_text.green().to_string();
// Apply current color stack
for color in color_stack.iter().rev() {
formatted_text = apply_color(&formatted_text, color);
}
result.push_str(&formatted_text);
i = j + 1;
continue;
}
}
// Regular character
let mut current_char = chars[i].to_string();
// Apply current color stack
for color in color_stack.iter().rev() {
current_char = apply_color(¤t_char, color);
}
result.push_str(¤t_char);
i += 1;
}
result
}
// Helper function to apply color to text
fn apply_color(text: &str, color_name: &str) -> String {
match color_name {
// Normal colors
"black" => text.black().to_string(),
"red" => text.red().to_string(),
"green" => text.green().to_string(),
"yellow" => text.yellow().to_string(),
"blue" => text.blue().to_string(),
"magenta" => text.magenta().to_string(),
"cyan" => text.cyan().to_string(),
"white" => text.white().to_string(),
"bright_black" => text.bright_black().to_string(),
"bright_red" => text.bright_red().to_string(),
"bright_green" => text.bright_green().to_string(),
"bright_yellow" => text.bright_yellow().to_string(),
"bright_blue" => text.bright_blue().to_string(),
"bright_magenta" => text.bright_magenta().to_string(),
"bright_cyan" => text.bright_cyan().to_string(),
"bright_white" => text.bright_white().to_string(),
// Short aliases for bright colors
"b_black" => text.bright_black().to_string(),
"b_red" => text.bright_red().to_string(),
"b_green" => text.bright_green().to_string(),
"b_yellow" => text.bright_yellow().to_string(),
"b_blue" => text.bright_blue().to_string(),
"b_magenta" => text.bright_magenta().to_string(),
"b_cyan" => text.bright_cyan().to_string(),
"b_white" => text.bright_white().to_string(),
// Gray colors using truecolor
"gray" | "grey" => text.truecolor(128, 128, 128).to_string(),
"bright_gray" | "bright_grey" => text.truecolor(192, 192, 192).to_string(),
"b_gray" | "b_grey" => text.truecolor(192, 192, 192).to_string(),
// Default to white if color not recognized
_ => text.to_string(),
}
}
/// Render a HashMap of PathBuf to SheetMappingMetadata as a tree string.
pub fn render_share_path_tree(paths: &HashMap<PathBuf, SheetMappingMetadata>) -> String {
if paths.is_empty() {
return String::new();
}
// Collect all path components into a tree structure
let mut root = TreeNode::new("".to_string());
for (path, metadata) in paths {
let mut current = &mut root;
let components: Vec<String> = path
.components()
.filter_map(|comp| match comp {
std::path::Component::Normal(s) => s.to_str().map(|s| s.to_string()),
_ => None,
})
.collect();
for (i, comp) in components.iter().enumerate() {
let is_leaf = i == components.len() - 1;
let child = current
.children
.entry(comp.clone())
.or_insert_with(|| TreeNode::new(comp.clone()));
// If this is the leaf node, store the metadata
if is_leaf {
child.metadata = Some((metadata.id.clone(), metadata.version.clone()));
}
current = child;
}
}
// Convert tree to string representation
let mut result = String::new();
let is_root = true;
let prefix = String::new();
let last_stack = vec![true]; // Root is always "last"
add_tree_node_to_string(&root, &mut result, is_root, &prefix, &last_stack);
result
}
/// Internal tree node structure for building the path tree
#[derive(Debug)]
struct TreeNode {
name: String,
children: BTreeMap<String, TreeNode>, // Use BTreeMap for sorted output
metadata: Option<(String, String)>, // Store (id, version) for leaf nodes
}
impl TreeNode {
fn new(name: String) -> Self {
Self {
name,
children: BTreeMap::new(),
metadata: None,
}
}
}
/// Recursively add tree node to string representation
fn add_tree_node_to_string(
node: &TreeNode,
result: &mut String,
is_root: bool,
prefix: &str,
last_stack: &[bool],
) {
if !is_root {
// Add the tree prefix for this node
for &is_last in &last_stack[1..] {
if is_last {
result.push_str(" ");
} else {
result.push_str("│ ");
}
}
// Add the connector for this node
if let Some(&is_last) = last_stack.last() {
if is_last {
result.push_str("└── ");
} else {
result.push_str("├── ");
}
}
// Add node name
result.push_str(&node.name);
// Add metadata for leaf nodes
if let Some((id, version)) = &node.metadata {
// Truncate id to first 11 characters
let truncated_id = if id.len() > 11 { &id[..11] } else { id };
result.push_str(&format!(" [{}|{}]", truncated_id, version));
}
result.push('\n');
}
// Process children
let child_count = node.children.len();
for (i, (_, child)) in node.children.iter().enumerate() {
let is_last_child = i == child_count - 1;
let mut new_last_stack = last_stack.to_vec();
new_last_stack.push(is_last_child);
add_tree_node_to_string(child, result, false, prefix, &new_last_stack);
}
}
|