Skip to content

Commit

Permalink
Use TruncatedStrView for printing out values.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Jan 17, 2022
1 parent 00d14ce commit 7f9166f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
64 changes: 45 additions & 19 deletions src/lineprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
return self.fill_in_container_value(buf, available_space, flatjson, row);
}

let mut value_ref: &str;
let mut value_truncated = false;
let value_ref: &str;
let quoted: bool;
let color: Color;

Expand All @@ -418,18 +417,18 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
available_space -= 1;
}

match truncate_right_to_fit(value_ref, available_space, "…") {
NoTruncation(width) => {
used_space += width;
}
Truncated(value_prefix, width) => {
used_space += width;
value_ref = value_prefix;
value_truncated = true;
}
DoesntFit => {
return Ok(0);
}
let truncated_view = TruncatedStrView::init_start(value_ref, available_space);
let space_used_for_value = truncated_view.used_space();
if space_used_for_value.is_none() {
return Ok(0);
}
let space_used_for_value = space_used_for_value.unwrap();
used_space += space_used_for_value;

// If we are just going to show a single ellipsis, we want
// to show a '>' instead.
if truncated_view.is_completely_elided() && !quoted && !self.trailing_comma {
return Ok(0);
}

// Print out the value.
Expand All @@ -438,10 +437,14 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
used_space += 1;
buf.write_char('"')?;
}
write!(buf, "{}", value_ref)?;
if value_truncated {
buf.write_char('…')?;
}
write!(
buf,
"{}",
TruncatedStrSlice {
s: value_ref,
truncated_view: &truncated_view,
}
)?;
if quoted {
used_space += 1;
buf.write_char('"')?;
Expand Down Expand Up @@ -1180,6 +1183,13 @@ mod tests {

// Not enough room, returns 0.
let used_space = line.fill_in_value(&mut buf, 3)?;
assert_eq!("\"\"", buf);
assert_eq!(3, used_space);

buf.clear();

// Not enough room, returns 0.
let used_space = line.fill_in_value(&mut buf, 2)?;
assert_eq!("", buf);
assert_eq!(0, used_space);

Expand Down Expand Up @@ -1219,8 +1229,24 @@ mod tests {

buf.clear();

// Not enough room, returns 0.
let used_space = line.fill_in_value(&mut buf, 2)?;
assert_eq!("…,", buf);
assert_eq!(2, used_space);

buf.clear();

// Not enough room, returns 0.
let used_space = line.fill_in_value(&mut buf, 1)?;
assert_eq!("", buf);
assert_eq!(0, used_space);

// UNQUOTED VALUE, NO TRAILING COMMA
line.trailing_comma = false;

buf.clear();

// Don't print just an ellipsis, print '>' instead.
let used_space = line.fill_in_value(&mut buf, 1)?;
assert_eq!("", buf);
assert_eq!(0, used_space);

Expand Down
20 changes: 20 additions & 0 deletions src/truncatedstrview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ impl TruncatedRange {
end: self.end,
}
}

/// Check whether this is a view of a string that is totally elided,
/// that is, it is represented by a single ellipsis.
pub fn is_completely_elided(&self) -> bool {
self.used_space == 1 && self.start == self.end
}
}

impl TruncatedStrView {
Expand Down Expand Up @@ -169,6 +175,12 @@ impl TruncatedStrView {
}
}

/// Check whether this is a view of a string that is totally elided,
/// that is, it is represented by a single ellipsis.
pub fn is_completely_elided(&self) -> bool {
self.range.map_or(false, |r| r.is_completely_elided())
}

// Creates a RangeAdjuster that represents the current state of
// the TruncatedStrView. This should only be called when the string
// is representable and we have a view.
Expand Down Expand Up @@ -503,6 +515,10 @@ impl<'a> RangeAdjuster<'a> {
self.available_space
));

// This DOESN'T consider the possibility that using a
// replacement character would remove the need for an
// ellipsis (because it's the last character), so
// something like "🦀" is represented as "…", not "�".
let showing_replacement_character =
// We only show a repacement character if we're not
// showing anything at all...
Expand Down Expand Up @@ -612,6 +628,10 @@ mod tests {

assert_init_states("a", 1, "a", "a", Some(1));
assert_init_states("abc", 1, "…", "…", Some(1));
// Note comment in to_view to understand why
// this is a single ellipsis instead of a
// replacement character.
assert_init_states("🦀", 1, "…", "…", Some(1));

assert_init_states("abc", 2, "a…", "…c", Some(2));
assert_init_states("ab", 2, "ab", "ab", Some(2));
Expand Down

0 comments on commit 7f9166f

Please sign in to comment.