Only print one line per item if only one field is set to be printed in verbose mode

This commit is contained in:
NGnius (Graham) 2023-10-21 23:17:30 -04:00
parent d0c7bcda58
commit c7d3b2582e

View file

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