From acc9491aedce4b719c4e248baa5a3723a3d3a12e Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 4 Jan 2024 20:33:58 +0000 Subject: [PATCH] Examine User: compact way of displaying fields. This is much simpler than my previous idea of trying to modify the HTML DOM to insert the heading at the start of the first paragraph! --- src/coloured_string.rs | 1 + src/text.rs | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/coloured_string.rs b/src/coloured_string.rs index 185a16a..574d055 100644 --- a/src/coloured_string.rs +++ b/src/coloured_string.rs @@ -47,6 +47,7 @@ impl ColouredString { pub fn width(&self) -> usize { UnicodeWidthStr::width(&self.text as &str) } pub fn text(&self) -> &str { &self.text } + pub fn colours(&self) -> &str { &self.colour } pub fn recolour(&self, colour: char) -> Self { Self::uniform(&self.text, colour) } diff --git a/src/text.rs b/src/text.rs index e268752..cbcfa10 100644 --- a/src/text.rs +++ b/src/text.rs @@ -2309,8 +2309,24 @@ impl TextFragment for ExamineUserDisplay { if !self.info_fields.is_empty() { push_fragment(&mut lines, self.info_header.render(width)); for (key, value) in &self.info_fields { - push_fragment(&mut lines, key.render(width)); - push_fragment(&mut lines, value.render_indented(width, 4)); + // Display a single line 'key: value' if it fits. + // Otherwise, let the value go to the next line, + // indented further. + let rkey = key.render(width); + let rval = value.render_indented(width, 4); + if rkey.len() == 1 && rval.len() == 1 && + rkey[0].width() + rval[0].width() + 2 <= width + { + let rval = &rval[0]; + assert!(rval.text().starts_with(" ")); + // Trim 3 spaces off, leaving one after the colon + let rval = ColouredString::general(&rval.text()[3..], + &rval.colours()[3..]); + lines.push(rkey[0].to_owned() + &rval); + } else { + push_fragment(&mut lines, rkey); + push_fragment(&mut lines, rval); + } } push_fragment(&mut lines, self.blank.render(width)); } -- 2.30.2