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
use serde_json::Value;
/// Manages column-related operations for table display
pub struct ColumnManager;
impl ColumnManager {
/// Calculate optimal column widths based on data content
#[must_use]
pub fn calculate_optimal_widths(data: &[Value]) -> Vec<u16> {
if data.is_empty() {
return Vec::new();
}
let first_row = match data.first() {
Some(row) => row,
None => return Vec::new(),
};
let obj = match first_row.as_object() {
Some(obj) => obj,
None => return Vec::new(),
};
let headers: Vec<&str> = obj.keys().map(std::string::String::as_str).collect();
let mut widths = Vec::new();
// For large datasets, sample rows instead of checking all
const MAX_ROWS_TO_CHECK: usize = 100;
let total_rows = data.len();
// Determine which rows to sample
let rows_to_check: Vec<usize> = if total_rows <= MAX_ROWS_TO_CHECK {
// Check all rows for small datasets
(0..total_rows).collect()
} else {
// Sample evenly distributed rows for large datasets
let step = total_rows / MAX_ROWS_TO_CHECK;
(0..MAX_ROWS_TO_CHECK)
.map(|i| (i * step).min(total_rows - 1))
.collect()
};
for header in &headers {
// Start with header width
let mut max_width = header.len();
// Check only sampled rows for this column
for &row_idx in &rows_to_check {
if let Some(row) = data.get(row_idx) {
if let Some(obj) = row.as_object() {
if let Some(value) = obj.get(*header) {
let display_len = match value {
Value::String(s) => s.len(),
Value::Number(n) => n.to_string().len(),
Value::Bool(b) => b.to_string().len(),
Value::Null => 4, // "null".len()
_ => value.to_string().len(),
};
max_width = max_width.max(display_len);
}
}
}
}
// Add some padding and set reasonable limits
let optimal_width = (max_width + 2).max(4).min(50); // 4-50 char range with 2 char padding
widths.push(optimal_width as u16);
}
widths
}
/// Calculate column widths for filtered data (string arrays)
#[must_use]
pub fn calculate_widths_for_filtered(headers: &[String], data: &[Vec<String>]) -> Vec<u16> {
let mut widths = Vec::new();
// For large datasets, sample rows instead of checking all
const MAX_ROWS_TO_CHECK: usize = 100;
let total_rows = data.len();
// Determine which rows to sample
let rows_to_check: Vec<usize> = if total_rows <= MAX_ROWS_TO_CHECK {
(0..total_rows).collect()
} else {
let step = total_rows / MAX_ROWS_TO_CHECK;
(0..MAX_ROWS_TO_CHECK)
.map(|i| (i * step).min(total_rows - 1))
.collect()
};
for (col_idx, header) in headers.iter().enumerate() {
// Start with header width
let mut max_width = header.len();
// Check only sampled rows for this column
for &row_idx in &rows_to_check {
if let Some(row) = data.get(row_idx) {
if let Some(value) = row.get(col_idx) {
max_width = max_width.max(value.len());
}
}
}
// Add some padding and set reasonable limits
let optimal_width = (max_width + 2).max(4).min(50);
widths.push(optimal_width as u16);
}
widths
}
/// Get display width for a single value
#[must_use]
pub fn get_value_display_width(value: &Value) -> usize {
match value {
Value::String(s) => s.len(),
Value::Number(n) => n.to_string().len(),
Value::Bool(b) => b.to_string().len(),
Value::Null => 4, // "null"
_ => value.to_string().len(),
}
}
}