use ic_host_text::escape::terminal_text;
use ic_host_text::table::{Alignment, Options};
pub use ic_host_text::width::fit_line as truncate_text;
use std::{borrow::Cow, fmt::Display};
macro_rules! report_fields {
($report:expr; $($name:tt $(= $value:expr)?),+ $(,)?) => {
[$($crate::presentation::text_value::field($crate::presentation::text_value::report_fields!(@name $name), &$crate::presentation::text_value::report_fields!(@value $report, $name $(, $value)?))),+]
};
(@name $name:literal) => { $name };
(@name $name:ident) => { stringify!($name) };
(@value $report:expr, $name:tt, $value:expr) => { $value };
(@value $report:expr, $name:ident) => { $report.$name };
}
pub(crate) use report_fields;
pub const REPORT_TABLE_OPTIONS: Options = Options {
column_gap: 3,
header_separator: true,
};
pub fn json_scalar_text(value: &serde_json::Value) -> String {
match value {
serde_json::Value::Null => "-".to_string(),
serde_json::Value::String(value) => value.clone(),
_ => value.to_string(),
}
}
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 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()));
}
pub fn push_optional_table_section<const N: usize>(
lines: &mut Vec<String>,
title: Option<&str>,
headers: &[&str; N],
rows: impl Iterator<Item = [String; N]>,
alignments: &[Alignment; N],
) {
if let Some(table) = optional_table(headers, rows, alignments) {
lines.push(String::new());
lines.extend(title.map(str::to_string));
lines.push(table);
}
}
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("-"))
),
]
}
pub fn field(name: &str, value: &dyn Display) -> String {
escaped_line(format!("{name}: {value}"))
}
pub fn escaped_line(line: String) -> String {
match terminal_text(&line) {
Cow::Borrowed(_) => line,
Cow::Owned(escaped) => escaped,
}
}
pub fn table<const N: usize>(
headers: &[impl AsRef<str>; N],
rows: &[[impl AsRef<str>; N]],
alignments: &[Alignment; N],
) -> String {
ic_host_text::table::render(headers, rows, alignments, REPORT_TABLE_OPTIONS)
}
pub fn optional_table<const N: usize>(
headers: &[&str; N],
rows: impl Iterator<Item = [String; N]>,
alignments: &[Alignment; N],
) -> Option<String> {
let rows = rows.collect::<Vec<_>>();
(!rows.is_empty()).then(|| table(headers, &rows, alignments))
}
pub fn refresh_outcome_lines(wrote: bool, replaced: bool, error: Option<&str>) -> [String; 4] {
let [finalized, error] = refresh_finalization_lines(error);
[
field("wrote_cache", &yes_no(wrote)),
field("replaced_existing_cache", &yes_no(replaced)),
finalized,
error,
]
}
pub fn refresh_path_lines(cache: &str, lock: &str, attempt: &str) -> [String; 3] {
[
field("cache_path", &cache),
field("refresh_lock_path", &lock),
field("refresh_attempt_path", &attempt),
]
}