From c7d3b2582ea0e07c57f7f825fbdd378eea44d9b0 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sat, 21 Oct 2023 23:17:30 -0400 Subject: [PATCH] Only print one line per item if only one field is set to be printed in verbose mode --- src/debug_state.rs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/debug_state.rs b/src/debug_state.rs index 3fc9446..02416ad 100644 --- a/src/debug_state.rs +++ b/src/debug_state.rs @@ -35,7 +35,10 @@ pub enum VerboseField { } impl VerboseField { - fn print_field(&self, item: &Item, terminal: &mut console::Term, args: &crate::cli::CliArgs) -> bool { + fn print_field(&self, item: &Item, terminal: &mut console::Term, args: &crate::cli::CliArgs, needs_prompt: bool) -> bool { + if needs_prompt { + write!(terminal, " {}", args.prompt).expect(TERMINAL_WRITE_ERROR); + } match self { Self::ShowFieldValue(f) => { if let Some(field) = item.field(f) { @@ -44,14 +47,13 @@ impl VerboseField { if field_str.len() >= max_len { write!( terminal, - " {}{}: `{}[...]`", - args.prompt, + "{}: `{}[...]`", f, &field_str[..max_len - 5] ) .expect(TERMINAL_WRITE_ERROR); } else { - write!(terminal, " {}{}: `{}`", args.prompt, f, field_str).expect(TERMINAL_WRITE_ERROR); + write!(terminal, "{}: `{}`", f, field_str).expect(TERMINAL_WRITE_ERROR); } true } else { @@ -61,7 +63,7 @@ impl VerboseField { Self::ShowFieldType(f) => { if let Some(field) = item.field(f) { let field_str = field.type_str(); - write!(terminal, " {}{}: {}", args.prompt, f, field_str).expect(TERMINAL_WRITE_ERROR); + write!(terminal, "{}: {}", f, field_str).expect(TERMINAL_WRITE_ERROR); true } else { false @@ -74,14 +76,13 @@ impl VerboseField { if field_str.len() >= max_len { write!( terminal, - " {}{}: {}[...]]", - args.prompt, + "{}: {}[...]]", f, &field_str[..max_len - 5] ) .expect(TERMINAL_WRITE_ERROR); } else { - write!(terminal, " {}{}: {}", args.prompt, f, field_str).expect(TERMINAL_WRITE_ERROR); + write!(terminal, "{}: {}", f, field_str).expect(TERMINAL_WRITE_ERROR); } true } else { @@ -96,17 +97,24 @@ impl Verbosity { pub fn print_item(&self, item: &Item, terminal: &mut console::Term, args: &crate::cli::CliArgs) { Self::item_prompt(terminal, args); if self.debug { - writeln!(terminal, "--\\/-- \\/ --\\/--").expect(TERMINAL_WRITE_ERROR); if self.field_handlers.is_empty() { let mut fields: Vec<_> = item.iter().collect(); fields.sort(); + let fields_gt_1 = fields.len() > 1; + if fields_gt_1 { writeln!(terminal, "--\\/-- \\/ --\\/--").expect(TERMINAL_WRITE_ERROR); } for field in fields { - VerboseField::ShowFieldValue(field.to_owned()).print_field(item, terminal, args); + VerboseField::ShowFieldValue(field.to_owned()).print_field(item, terminal, args, fields_gt_1); + writeln!(terminal, "").expect(TERMINAL_WRITE_ERROR); + } + } else if self.field_handlers.len() == 1 { + let has_printed = self.field_handlers.get(0).unwrap().print_field(item, terminal, args, false); + if has_printed { writeln!(terminal, "").expect(TERMINAL_WRITE_ERROR); } } else { + writeln!(terminal, "--\\/-- \\/ --\\/--").expect(TERMINAL_WRITE_ERROR); for vf in &self.field_handlers { - let has_printed = vf.print_field(item, terminal, args); + let has_printed = vf.print_field(item, terminal, args, true); if has_printed { writeln!(terminal, "").expect(TERMINAL_WRITE_ERROR); }