From dac76565722d413cf29f7516783f5b65474801c4 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 7 Mar 2024 22:20:59 -0500 Subject: [PATCH] Improve value display --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/value.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eef6e70..5c413b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -204,6 +204,7 @@ dependencies = [ "clap", "colored", "env_logger", + "stanza", ] [[package]] @@ -404,6 +405,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "stanza" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d230b987a5b524a015ded47fed54c5177598a71c90508acee1c5d0b4c955f74" + [[package]] name = "strsim" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index b29a433..91c9de5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ chumsky = { version = "1.0.0-alpha.6", features = ["pratt", "label"] } clap = { version = "4.5.2", features = ["derive"] } colored = "2.1.0" env_logger = "0.11.3" +stanza = "0.5.1" diff --git a/src/value.rs b/src/value.rs index 0070887..b65be9c 100644 --- a/src/value.rs +++ b/src/value.rs @@ -6,6 +6,12 @@ use std::{ sync::{Arc, OnceLock}, }; +use stanza::{ + renderer::{console::Console, Renderer}, + style::{HAlign, MinWidth, Styles}, + table::Table, +}; + use crate::{ abstract_tree::{Identifier, Type}, error::ValidationError, @@ -128,22 +134,35 @@ impl Display for Value { fn fmt(&self, f: &mut Formatter) -> fmt::Result { use ValueInner::*; + fn create_table() -> Table { + Table::with_styles(Styles::default().with(HAlign::Centred).with(MinWidth(3))) + } + match self.inner().as_ref() { Boolean(boolean) => write!(f, "{boolean}"), Float(float) => write!(f, "{float}"), Integer(integer) => write!(f, "{integer}"), - List(_) => todo!(), - Map(map) => { - writeln!(f, "{{")?; + List(list) => { + let mut table = create_table(); - for (identifier, value) in map { - writeln!(f, " {identifier} = {value}")?; + for value in list { + table = table.with_row([value.to_string()]); } - write!(f, "}}") + write!(f, "{}", Console::default().render(&table)) + } + Map(map) => { + let mut table = create_table(); + + for (identifier, value) in map { + table = table.with_row([identifier.as_str(), &value.to_string()]); + } + + write!(f, "{}", Console::default().render(&table)) } Range(_) => todo!(), String(string) => write!(f, "{string}"), + Enum(_, _) => todo!(), } }