use ic_host_text::{escape::terminal_text, table::Options};
pub const REPORT_TABLE_OPTIONS: Options = Options {
column_gap: 3,
header_separator: true,
};
pub fn text_or_dash(value: &str) -> String {
if value.is_empty() {
"-".to_string()
} else {
terminal_text(value).into_owned()
}
}
pub fn optional_text(value: Option<&String>) -> String {
value.map_or_else(
|| "-".to_string(),
|value| terminal_text(value).into_owned(),
)
}
pub fn compact_text(value: &str, chars: usize) -> String {
terminal_text(value).chars().take(chars).collect()
}
pub fn truncate_text(value: &str, limit: usize) -> String {
let value = terminal_text(value).into_owned();
if value.chars().count() <= limit {
return value;
}
let mut truncated = value.chars().take(limit).collect::<String>();
truncated.push_str("...");
truncated
}
pub fn optional_u64_text(value: Option<u64>) -> String {
value.map_or_else(|| "-".to_string(), |value| value.to_string())
}
pub fn optional_bool_text(value: Option<bool>) -> &'static str {
value.map_or("-", yes_no)
}
pub const fn yes_no(value: bool) -> &'static str {
if value { "yes" } else { "no" }
}
pub fn push_titled_table_section(lines: &mut Vec<String>, title: &str, table: Option<String>) {
lines.push(String::new());
lines.push(title.to_string());
lines.push(table.unwrap_or_else(|| "-".to_string()));
}
#[cfg(any(feature = "nns-host", feature = "sns-host"))]
pub fn refresh_finalization_lines(error: Option<&str>) -> [String; 2] {
[
format!("attempt_finalized: {}", yes_no(error.is_none())),
format!(
"attempt_finalization_error: {}",
terminal_text(error.unwrap_or("-"))
),
]
}