better json printing

This commit is contained in:
Jana Dönszelmann 2026-02-25 01:15:01 +01:00
parent 3037d0711e
commit d989f6e31e
No known key found for this signature in database
2 changed files with 22 additions and 5 deletions

View file

@ -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 });

View file

@ -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())
};