const 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]
pub fn calculate_available_data_rows(terminal_height: u16) -> u16 {
terminal_height
.saturating_sub(TOTAL_UI_CHROME) .saturating_sub(TABLE_CHROME_ROWS) }
#[must_use]
pub fn calculate_table_data_rows(table_area_height: u16) -> u16 {
table_area_height.saturating_sub(TABLE_CHROME_ROWS)
}
#[must_use]
pub fn extract_timing_from_debug_string(s: &str) -> Option<f64> {
if let Some(total_pos) = s.find("total=") {
let after_total = &s[total_pos + 6..];
let time_str =
if let Some(end_pos) = after_total.find(',').or_else(|| after_total.find(')')) {
&after_total[..end_pos]
} else {
after_total
};
if let Some(us_pos) = time_str.find("µs") {
time_str[..us_pos].parse::<f64>().ok().map(|us| us / 1000.0)
} else if let Some(ms_pos) = time_str.find("ms") {
time_str[..ms_pos].parse::<f64>().ok()
} else if let Some(s_pos) = time_str.find('s') {
time_str[..s_pos].parse::<f64>().ok().map(|s| s * 1000.0)
} else {
None
}
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_calculate_available_data_rows() {
assert_eq!(calculate_available_data_rows(50), 41);
assert_eq!(calculate_available_data_rows(5), 0); }
#[test]
fn test_calculate_table_data_rows() {
assert_eq!(calculate_table_data_rows(20), 17);
assert_eq!(calculate_table_data_rows(2), 0);
}
#[test]
fn test_extract_timing_from_debug_string() {
assert_eq!(
extract_timing_from_debug_string("query executed total=1500µs"),
Some(1.5)
);
assert_eq!(
extract_timing_from_debug_string("query executed total=2.5ms"),
Some(2.5)
);
assert_eq!(
extract_timing_from_debug_string("query executed total=1.2s"),
Some(1200.0)
);
assert_eq!(
extract_timing_from_debug_string("query executed total=800µs, other=123"),
Some(0.8)
);
assert_eq!(
extract_timing_from_debug_string("query executed (total=300µs)"),
Some(0.3)
);
assert_eq!(extract_timing_from_debug_string("no timing info"), None);
assert_eq!(extract_timing_from_debug_string("total=invalid"), None);
}
}