feat(package/prettify-log): colored output

This commit is contained in:
2025-01-31 23:45:28 +00:00
parent d613d51d41
commit 4d900a5f23
3 changed files with 24 additions and 2 deletions

View File

@@ -62,6 +62,17 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored_json"
version = "5.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e35980a1b846f8e3e359fd18099172a0857140ba9230affc4f71348081e039b6"
dependencies = [
"serde",
"serde_json",
"yansi",
]
[[package]] [[package]]
name = "difflib" name = "difflib"
version = "0.4.0" version = "0.4.0"
@@ -191,6 +202,7 @@ name = "prettify-log"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"assert_cmd", "assert_cmd",
"colored_json",
"float-cmp", "float-cmp",
"predicates", "predicates",
"serde", "serde",
@@ -432,3 +444,9 @@ checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
dependencies = [ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "yansi"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
colored_json = "5.0.0"
serde = "1.0.217" serde = "1.0.217"
serde_json = "1.0.138" serde_json = "1.0.138"

View File

@@ -1,7 +1,9 @@
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use serde_json::Value; use serde_json::Value;
use colored_json::ToColoredJson;
/// Finds the substring from the first '{' to its matching '}', handling nested braces. /// Finds the first '{' and tries to match nested braces until the
/// corresponding '}'. Returns (start, end) byte offsets if found.
fn find_json_block(line: &str) -> Option<(usize, usize)> { fn find_json_block(line: &str) -> Option<(usize, usize)> {
let start = line.find('{')?; let start = line.find('{')?;
let mut brace_count = 0; let mut brace_count = 0;
@@ -27,7 +29,8 @@ fn main() -> io::Result<()> {
if let Some((start, end)) = find_json_block(&line) { if let Some((start, end)) = find_json_block(&line) {
let candidate = &line[start..end]; let candidate = &line[start..end];
if let Ok(json) = serde_json::from_str::<Value>(candidate) { if let Ok(json) = serde_json::from_str::<Value>(candidate) {
let pretty = serde_json::to_string_pretty(&json).unwrap(); // Prettify and reconstruct the line
let pretty = serde_json::to_string_pretty(&json).unwrap().to_colored_json_auto().unwrap();
let prefix = &line[..start]; let prefix = &line[..start];
let suffix = &line[end..]; let suffix = &line[end..];
println!("{}{}{}", prefix, pretty, suffix); println!("{}{}{}", prefix, pretty, suffix);