sql_cli/ui/rendering/
ui_layout_utils.rs1const TABLE_BORDER_WIDTH: u16 = 4; const INPUT_AREA_HEIGHT: u16 = 3; const STATUS_BAR_HEIGHT: u16 = 3; const TOTAL_UI_CHROME: u16 = INPUT_AREA_HEIGHT + STATUS_BAR_HEIGHT; const TABLE_CHROME_ROWS: u16 = 3; #[must_use]
16pub fn calculate_available_data_rows(terminal_height: u16) -> u16 {
17 terminal_height
18 .saturating_sub(TOTAL_UI_CHROME) .saturating_sub(TABLE_CHROME_ROWS) }
21
22#[must_use]
25pub fn calculate_table_data_rows(table_area_height: u16) -> u16 {
26 table_area_height.saturating_sub(TABLE_CHROME_ROWS)
27}
28
29#[must_use]
32pub fn extract_timing_from_debug_string(s: &str) -> Option<f64> {
33 if let Some(total_pos) = s.find("total=") {
34 let after_total = &s[total_pos + 6..];
35 let time_str =
36 if let Some(end_pos) = after_total.find(',').or_else(|| after_total.find(')')) {
37 &after_total[..end_pos]
38 } else {
39 after_total
40 };
41
42 if let Some(us_pos) = time_str.find("µs") {
43 time_str[..us_pos].parse::<f64>().ok().map(|us| us / 1000.0)
44 } else if let Some(ms_pos) = time_str.find("ms") {
45 time_str[..ms_pos].parse::<f64>().ok()
46 } else if let Some(s_pos) = time_str.find('s') {
47 time_str[..s_pos].parse::<f64>().ok().map(|s| s * 1000.0)
48 } else {
49 None
50 }
51 } else {
52 None
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_calculate_available_data_rows() {
62 assert_eq!(calculate_available_data_rows(50), 41);
64
65 assert_eq!(calculate_available_data_rows(5), 0); }
68
69 #[test]
70 fn test_calculate_table_data_rows() {
71 assert_eq!(calculate_table_data_rows(20), 17);
73
74 assert_eq!(calculate_table_data_rows(2), 0);
76 }
77
78 #[test]
79 fn test_extract_timing_from_debug_string() {
80 assert_eq!(
82 extract_timing_from_debug_string("query executed total=1500µs"),
83 Some(1.5)
84 );
85
86 assert_eq!(
88 extract_timing_from_debug_string("query executed total=2.5ms"),
89 Some(2.5)
90 );
91
92 assert_eq!(
94 extract_timing_from_debug_string("query executed total=1.2s"),
95 Some(1200.0)
96 );
97
98 assert_eq!(
100 extract_timing_from_debug_string("query executed total=800µs, other=123"),
101 Some(0.8)
102 );
103
104 assert_eq!(
106 extract_timing_from_debug_string("query executed (total=300µs)"),
107 Some(0.3)
108 );
109
110 assert_eq!(extract_timing_from_debug_string("no timing info"), None);
112 assert_eq!(extract_timing_from_debug_string("total=invalid"), None);
113 }
114}