Add value abbreviations (#407)

This commit is contained in:
JT 2021-12-03 10:07:44 +13:00 committed by GitHub
parent 687fefd791
commit 19766556f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View file

@ -164,7 +164,7 @@ impl Command for Table {
style: TextStyle::default_field(),
},
StyledString {
contents: v.into_string(", ", &config),
contents: v.into_abbreviated_string(&config),
style: TextStyle::default(),
},
])
@ -225,7 +225,7 @@ fn convert_to_table(
if headers.is_empty() {
// if header row is empty, this is probably a list so format it that way
row.push(("list".to_string(), item.into_string(", ", config)))
row.push(("list".to_string(), item.into_abbreviated_string(config)))
} else {
for header in headers.iter().skip(1) {
let result = match item {
@ -241,7 +241,7 @@ fn convert_to_table(
match result {
Ok(value) => row.push((
(&value.get_type()).to_string(),
value.into_string(", ", config),
value.into_abbreviated_string(config),
)),
Err(_) => row.push(("empty".to_string(), String::new())),
}

View file

@ -328,6 +328,34 @@ impl Value {
}
}
/// Convert Value into string. Note that Streams will be consumed.
pub fn into_abbreviated_string(self, config: &Config) -> String {
match self {
Value::Bool { val, .. } => val.to_string(),
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format_filesize(val, config),
Value::Duration { val, .. } => format_duration(val),
Value::Date { val, .. } => HumanTime::from(val).to_string(),
Value::Range { val, .. } => {
format!(
"{}..{}",
val.from.into_string(", ", config),
val.to.into_string(", ", config)
)
}
Value::String { val, .. } => val,
Value::List { vals: val, .. } => format!("[list {} items]", val.len()),
Value::Record { cols, .. } => format!("{{record {} fields}}", cols.len()),
Value::Block { val, .. } => format!("<Block {}>", val),
Value::Nothing { .. } => String::new(),
Value::Error { error } => format!("{:?}", error),
Value::Binary { val, .. } => format!("{:?}", val),
Value::CellPath { val, .. } => val.into_string(),
Value::CustomValue { val, .. } => val.value_string(),
}
}
/// Convert Value into a debug string
pub fn debug_value(self) -> String {
format!("{:#?}", self)