mirror of
https://github.com/lune-org/lune.git
synced 2025-04-10 21:40:54 +01:00
Fix indentation and newline in new pretty format algo
This commit is contained in:
parent
0dc1625cff
commit
98aebe582a
2 changed files with 11 additions and 5 deletions
|
@ -34,13 +34,14 @@ pub(crate) fn lua_value_as_plain_string_key(value: &LuaValue) -> Option<String>
|
||||||
|
|
||||||
This does not recursively format tables.
|
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 {
|
match value {
|
||||||
LuaValue::Nil => COLOR_YELLOW.apply_to("nil").to_string(),
|
LuaValue::Nil => COLOR_YELLOW.apply_to("nil").to_string(),
|
||||||
LuaValue::Boolean(true) => COLOR_YELLOW.apply_to("true").to_string(),
|
LuaValue::Boolean(true) => COLOR_YELLOW.apply_to("true").to_string(),
|
||||||
LuaValue::Boolean(false) => COLOR_YELLOW.apply_to("false").to_string(),
|
LuaValue::Boolean(false) => COLOR_YELLOW.apply_to("false").to_string(),
|
||||||
LuaValue::Number(n) => COLOR_CYAN.apply_to(n).to_string(),
|
LuaValue::Number(n) => COLOR_CYAN.apply_to(n).to_string(),
|
||||||
LuaValue::Integer(i) => COLOR_CYAN.apply_to(i).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
|
LuaValue::String(s) => COLOR_GREEN
|
||||||
.apply_to({
|
.apply_to({
|
||||||
let mut s = s.to_string_lossy().to_string();
|
let mut s = s.to_string_lossy().to_string();
|
||||||
|
|
|
@ -9,6 +9,8 @@ use super::{
|
||||||
style::STYLE_DIM,
|
style::STYLE_DIM,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const INDENT: &str = " ";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Representation of a pointer in memory to a Lua value.
|
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 (key, value) = res.expect("conversion to LuaValue should never fail");
|
||||||
let formatted = if let Some(plain_key) = lua_value_as_plain_string_key(&key) {
|
let formatted = if let Some(plain_key) = lua_value_as_plain_string_key(&key) {
|
||||||
format!(
|
format!(
|
||||||
"{plain_key} {} {}{}",
|
"{}{plain_key} {} {}{}",
|
||||||
|
INDENT.repeat(1 + depth),
|
||||||
STYLE_DIM.apply_to("="),
|
STYLE_DIM.apply_to("="),
|
||||||
format_value_recursive(&value, config, visited, depth + 1)?,
|
format_value_recursive(&value, config, visited, depth + 1)?,
|
||||||
STYLE_DIM.apply_to(","),
|
STYLE_DIM.apply_to(","),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
format!(
|
format!(
|
||||||
"{}{}{} {} {}{}",
|
"{}{}{}{} {} {}{}",
|
||||||
|
INDENT.repeat(1 + depth),
|
||||||
STYLE_DIM.apply_to("["),
|
STYLE_DIM.apply_to("["),
|
||||||
format_value_recursive(&key, config, visited, depth + 1)?,
|
format_value_recursive(&key, config, visited, depth + 1)?,
|
||||||
STYLE_DIM.apply_to("]"),
|
STYLE_DIM.apply_to("]"),
|
||||||
|
@ -74,10 +78,11 @@ pub(crate) fn format_value_recursive(
|
||||||
}
|
}
|
||||||
|
|
||||||
visited.remove(&LuaValueId::from(t));
|
visited.remove(&LuaValueId::from(t));
|
||||||
writeln!(buffer, "{}", STYLE_DIM.apply_to("}"))?;
|
write!(buffer, "\n{}", STYLE_DIM.apply_to("}"))?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
write!(buffer, "{}", format_value_styled(value))?;
|
let prefer_plain = depth == 0;
|
||||||
|
write!(buffer, "{}", format_value_styled(value, prefer_plain))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
|
|
Loading…
Add table
Reference in a new issue