pub(crate) const SCOPE_SQL_CHARS: usize = 80;
pub(crate) fn scope_line(connection: Option<&str>, executed_sql: &str) -> Option<String> {
let query = single_line(executed_sql);
let short = truncate_chars(&query, SCOPE_SQL_CHARS);
match connection.filter(|c| !c.is_empty()) {
Some(profile) if short.is_empty() => Some(format!("from {profile}")),
Some(profile) => Some(format!("from {profile} · {short}")),
None if short.is_empty() => None,
None => Some(short),
}
}
pub(crate) fn with_scope_line(
table_text: String,
connection: Option<&str>,
executed_sql: &str,
) -> String {
match scope_line(connection, executed_sql) {
Some(line) => format!("{table_text}\n{line}"),
None => table_text,
}
}
fn single_line(sql: &str) -> String {
sql.split_whitespace().collect::<Vec<_>>().join(" ")
}
fn truncate_chars(text: &str, limit: usize) -> String {
let chars: Vec<char> = text.chars().collect();
if chars.len() <= limit {
return text.to_string();
}
let mut short: String = chars[..limit.saturating_sub(1)].iter().collect();
short.push('…');
short
}