From d989f6e31e1adfccf849edae5a6f38872fcced2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jana=20D=C3=B6nszelmann?= Date: Wed, 25 Feb 2026 01:15:01 +0100 Subject: [PATCH] better json printing --- src/tui/mod.rs | 5 ++--- src/tui/model.rs | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/tui/mod.rs b/src/tui/mod.rs index d6a7068..3811bd0 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -13,6 +13,7 @@ use tui_widget_list::{ListBuilder, ListView}; use crate::tui::{ filter::{FilterKind, WipMatcher}, log_viewer::{InputState, InputTarget, LogViewer}, + model::pretty_print_value, }; use crate::tui::{ filter::{FilterSelection, WipFilter}, @@ -473,9 +474,7 @@ impl Widget for &mut App { let Some((k, v)) = &items.get(cx.index) else { return (Paragraph::new(""), 1); }; - let contents = serde_json::to_string_pretty(&v) - .unwrap_or(String::new()) - .replace("\n", "\n{:width$}"); + let contents = pretty_print_value(&v); let mut res = Paragraph::new(format!("{k:width$} {contents}")) .wrap(Wrap { trim: false }); diff --git a/src/tui/model.rs b/src/tui/model.rs index fe8ec33..c59c484 100644 --- a/src/tui/model.rs +++ b/src/tui/model.rs @@ -9,6 +9,19 @@ use std::{ use jiff::Timestamp; use ratatui::text::Line; use serde::Deserialize; +use serde_json::Value; + +pub fn pretty_print_value(v: &Value) -> String { + match v { + Value::Null => "null".to_string(), + Value::Bool(false) => "false".to_string(), + Value::Bool(true) => "true".to_string(), + Value::Number(number) => number.to_string(), + Value::String(s) => s.clone(), + v @ Value::Array(..) => v.to_string(), + v @ Value::Object(..) => v.to_string(), + } +} #[derive(Deserialize, Debug, Hash)] pub enum Level { @@ -105,13 +118,18 @@ impl LogEntry { raw.fields .message() .map(|i| i.to_string()) - .or_else(|| raw.fields.fields.get("return").map(|i| format!("↩ {i}"))) + .or_else(|| { + raw.fields + .fields + .get("return") + .map(|v| format!("↩ {}", pretty_print_value(v))) + }) .or_else(|| { raw.fields .fields .iter() .next() - .map(|(k, v)| format!("{k} = {v}")) + .map(|(k, v)| format!("{k} = {}", pretty_print_value(v))) }) .unwrap_or_else(|| NO_MESSAGE.to_string()) };