diff --git a/crates/lune-utils/src/fmt/value/basic.rs b/crates/lune-utils/src/fmt/value/basic.rs index 5c392ce..cc4f9fb 100644 --- a/crates/lune-utils/src/fmt/value/basic.rs +++ b/crates/lune-utils/src/fmt/value/basic.rs @@ -34,13 +34,14 @@ pub(crate) fn lua_value_as_plain_string_key(value: &LuaValue) -> Option This does not recursively format tables. */ -pub(crate) fn format_value_styled(value: &LuaValue) -> String { +pub(crate) fn format_value_styled(value: &LuaValue, prefer_plain: bool) -> String { match value { LuaValue::Nil => COLOR_YELLOW.apply_to("nil").to_string(), LuaValue::Boolean(true) => COLOR_YELLOW.apply_to("true").to_string(), LuaValue::Boolean(false) => COLOR_YELLOW.apply_to("false").to_string(), LuaValue::Number(n) => COLOR_CYAN.apply_to(n).to_string(), LuaValue::Integer(i) => COLOR_CYAN.apply_to(i).to_string(), + LuaValue::String(s) if prefer_plain => s.to_string_lossy().to_string(), LuaValue::String(s) => COLOR_GREEN .apply_to({ let mut s = s.to_string_lossy().to_string(); diff --git a/crates/lune-utils/src/fmt/value/recursive.rs b/crates/lune-utils/src/fmt/value/recursive.rs index d6880ea..7dfbef7 100644 --- a/crates/lune-utils/src/fmt/value/recursive.rs +++ b/crates/lune-utils/src/fmt/value/recursive.rs @@ -9,6 +9,8 @@ use super::{ style::STYLE_DIM, }; +const INDENT: &str = " "; + /** Representation of a pointer in memory to a Lua value. */ @@ -54,14 +56,16 @@ pub(crate) fn format_value_recursive( let (key, value) = res.expect("conversion to LuaValue should never fail"); let formatted = if let Some(plain_key) = lua_value_as_plain_string_key(&key) { format!( - "{plain_key} {} {}{}", + "{}{plain_key} {} {}{}", + INDENT.repeat(1 + depth), STYLE_DIM.apply_to("="), format_value_recursive(&value, config, visited, depth + 1)?, STYLE_DIM.apply_to(","), ) } else { format!( - "{}{}{} {} {}{}", + "{}{}{}{} {} {}{}", + INDENT.repeat(1 + depth), STYLE_DIM.apply_to("["), format_value_recursive(&key, config, visited, depth + 1)?, STYLE_DIM.apply_to("]"), @@ -74,10 +78,11 @@ pub(crate) fn format_value_recursive( } visited.remove(&LuaValueId::from(t)); - writeln!(buffer, "{}", STYLE_DIM.apply_to("}"))?; + write!(buffer, "\n{}", STYLE_DIM.apply_to("}"))?; } } else { - write!(buffer, "{}", format_value_styled(value))?; + let prefer_plain = depth == 0; + write!(buffer, "{}", format_value_styled(value, prefer_plain))?; } Ok(buffer)