Skip to content

Commit

Permalink
Use Row.key_range for printing out keys and remove Row.key.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Nov 7, 2021
1 parent a226811 commit 5d10274
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
6 changes: 1 addition & 5 deletions src/flatjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ pub struct Row {
pub index: Index,
pub range: Range<usize>,
pub key_range: Option<Range<usize>>,
pub key: Option<String>,
pub value: Value,
}

Expand Down Expand Up @@ -412,7 +411,6 @@ fn flatten_json(serde_value: SerdeValue, flat_json: &mut Vec<Row>, parents: &mut
index: 0,
range: 0..0, // Not populated in when using Serde
key_range: None, // Not populated in when using Serde
key: None,
value: Value::Null,
};

Expand Down Expand Up @@ -481,7 +479,6 @@ fn flatten_json(serde_value: SerdeValue, flat_json: &mut Vec<Row>, parents: &mut
index: 0,
range: 0..0, // Not populated in when using Serde
key_range: None, // Not populated in when using Serde
key: None,
value: Value::CloseContainer {
container_type: ContainerType::Array,
collapsed: false,
Expand Down Expand Up @@ -534,7 +531,7 @@ fn flatten_json(serde_value: SerdeValue, flat_json: &mut Vec<Row>, parents: &mut

child.index = i;
child.prev_sibling = prev_sibling;
child.key = Some(k);
child.key_range = Some(0..0);

if let OptionIndex::Index(prev_sibling_index) = prev_sibling {
flat_json[prev_sibling_index].next_sibling =
Expand All @@ -554,7 +551,6 @@ fn flatten_json(serde_value: SerdeValue, flat_json: &mut Vec<Row>, parents: &mut
index: 0,
range: 0..0, // Not populated in when using Serde
key_range: None, // Not populated in when using Serde
key: None,
value: Value::CloseContainer {
container_type: ContainerType::Object,
collapsed: false,
Expand Down
7 changes: 2 additions & 5 deletions src/jsonparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,14 @@ impl<'a> JsonParser<'a> {
return self.unexpected_token();
}

let (key, key_range) = {
let key_range = {
let key_range_start = self.pretty_printed.len();
let key_span_len = self.tokenizer.span().len();
let key_range = key_range_start..key_range_start + key_span_len;

self.pretty_printed.push_str(self.tokenizer.slice());
let actual_key = self.tokenizer.slice()[1..key_span_len - 1].to_string();
self.advance_and_consume_whitespace();
(actual_key, key_range)
key_range
};

if self.peek_token()? != JsonToken::Colon {
Expand All @@ -305,7 +304,6 @@ impl<'a> JsonParser<'a> {
self.pretty_printed.push_str(": ");

let child = self.parse_elem()?;
self.rows[child].key = Some(key);
self.rows[child].key_range = Some(key_range);
self.consume_whitespace();

Expand Down Expand Up @@ -444,7 +442,6 @@ impl<'a> JsonParser<'a> {
prev_sibling: OptionIndex::Nil,
next_sibling: OptionIndex::Nil,
index: 0,
key: None,
key_range: None,
});

Expand Down
17 changes: 10 additions & 7 deletions src/lineprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,10 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {

let container_type = row.value.container_type().unwrap();
available_space -= 2;
let mut num_printed = 2;
let mut num_printed = 0;

buf.write_char(container_type.open_char())?;
num_printed += 1;

let mut next_sibling = row.first_child();
let mut is_first_child = true;
Expand Down Expand Up @@ -646,6 +647,7 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
}

buf.write_char(container_type.close_char())?;
num_printed += 1;

Ok(num_printed)
}
Expand All @@ -666,7 +668,9 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
let mut quoted_object_key = quoted_object_keys;
let mut space_available_for_key = available_space - 1;

if let Some(key) = &row.key {
if let Some(key_range) = &row.key_range {
let key = &flatjson.1[key_range.start + 1..key_range.end - 1];

if quoted_object_keys || !JS_IDENTIFIER.is_match(key) {
required_characters += 2;
space_available_for_key -= 2;
Expand All @@ -687,12 +691,11 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
let mut used_space = 0;

// Let's print out the object key
if let Some(key) = &row.key {
let mut key_ref = key.as_str();
if let Some(key_range) = &row.key_range {
let mut key_ref = &flatjson.1[key_range.start + 1..key_range.end - 1];
let mut key_truncated = false;

// Remove 2 for ": "
match truncate_right_to_fit(key, space_available_for_key, "…") {
match truncate_right_to_fit(key_ref, space_available_for_key, "…") {
NoTruncation(width) => {
available_space -= width;
used_space += width;
Expand Down Expand Up @@ -743,7 +746,7 @@ impl<'a, TUI: TUIControl> LinePrinter<'a, TUI> {
// Make sure to print out ellipsis for the value if we printed out an
// object key, but couldn't print out the value. Space was already
// allocated for this at the start of the function.
if row.key.is_some() && space_used_for_value == 0 {
if row.key_range.is_some() && space_used_for_value == 0 {
buf.write_char('…')?;
used_space += 1;
}
Expand Down
7 changes: 5 additions & 2 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ impl ScreenWriter {
let number_value: String;

// Set up key label.
if let Some(key) = &row.key {
if let Some(key_range) = &row.key_range {
let key = &viewer.flatjson.1[key_range.start + 1..key_range.end - 1];
label = Some(lp::LineLabel::Key { key });
}

Expand Down Expand Up @@ -420,7 +421,9 @@ impl ScreenWriter {
ScreenWriter::build_path_to_focused_node(viewer, buf, parent_index);
}

if let Some(key) = &row.key {
if let Some(key_range) = &row.key_range {
let key = &viewer.flatjson.1[key_range.start + 1..key_range.end - 1];

if JS_IDENTIFIER.is_match(key) {
write!(buf, ".{}", key).unwrap();
} else {
Expand Down

0 comments on commit 5d10274

Please sign in to comment.