Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different preview modes: full, count, none #40

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 23 additions & 38 deletions src/lineprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,16 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
available_space -= 1;
}

let mut used_space = 0;
let mut used_space;

if self.preview == Preview::Full {
let quoted_object_keys = self.mode == Mode::Line;
used_space =
self.generate_container_preview(flatjson, row, available_space, quoted_object_keys)?;
used_space = self.generate_container_preview(
flatjson,
row,
available_space,
quoted_object_keys,
)?;

if self.trailing_comma {
used_space += 1;
Expand All @@ -706,8 +710,7 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
)?;
}
}
}
else if self.preview == Preview::None {
} else if self.preview == Preview::None {
self.highlight_str(
" ",
Some(self.value_range.end),
Expand All @@ -717,10 +720,8 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
),
)?;
used_space = 1;
}
else {
used_space =
self.generate_container_count(flatjson, row, available_space)?;
} else {
used_space = self.generate_container_count(flatjson, row, available_space)?;
}

Ok(used_space)
Expand Down Expand Up @@ -826,7 +827,7 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
&mut self,
flatjson: &FlatJson,
row: &Row,
mut available_space: isize
available_space: isize,
) -> Result<isize, fmt::Error> {
debug_assert!(row.is_opening_of_container());

Expand All @@ -836,7 +837,7 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
}

let mut next_sibling = row.first_child();
let mut count:usize = 0;
let mut count: usize = 0;
while let OptionIndex::Index(child) = next_sibling {
next_sibling = flatjson[child].next_sibling;
count += 1;
Expand All @@ -846,14 +847,14 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
let mut count_str = format!(
"{} {} item{} {}",
container_type.open_str(),
count.to_string(),
count,
if count == 1 { "" } else { "s" },
container_type.close_str()
);

if count_str.len() as isize > available_space {
count_str = format!(
"{}…{}",
"{}…{}",
container_type.open_str(),
container_type.close_str()
);
Expand All @@ -862,10 +863,9 @@ impl<'a, 'b, 'c> LinePrinter<'a, 'b, 'c> {
self.highlight_str(
&count_str,
Some(self.value_range.start),
highlighting::PREVIEW_STYLES
highlighting::PREVIEW_STYLES,
)?;
let len = count_str.chars().count() as isize;
available_space -= len;
Ok(len)
}

Expand Down Expand Up @@ -1596,15 +1596,10 @@ mod tests {
..default_line_printer(&mut term)
};

for (available_space, used_space, expected) in vec![
(14, 11, r#"[ 3 items ]"#),
(4, 3, r#"[…]"#),
(2, 0, r#""#),
]
.into_iter()
for (available_space, used_space, expected) in
vec![(14, 11, r#"[ 3 items ]"#), (4, 3, r#"[…]"#), (2, 0, r#""#)].into_iter()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the linter is being overzealous here, and in the rest of the test_ functions in this commit. It's way harder to read this vec![...] on a single line. They were on their own lines with specific spacing to make it easier for humans to read.

{
let used =
line.generate_container_count(&fj_arr, &fj_arr[0], available_space)?;
let used = line.generate_container_count(&fj_arr, &fj_arr[0], available_space)?;
assert_eq!(
expected,
line.terminal.output(),
Expand All @@ -1617,15 +1612,10 @@ mod tests {
line.terminal.clear_output();
}

for (available_space, used_space, expected) in vec![
(14, 11, r#"{ 2 items }"#),
(4, 3, r#"{…}"#),
(2, 0, r#""#),
]
.into_iter()
for (available_space, used_space, expected) in
vec![(14, 11, r#"{ 2 items }"#), (4, 3, r#"{…}"#), (2, 0, r#""#)].into_iter()
{
let used =
line.generate_container_count(&fj_obj, &fj_obj[0], available_space)?;
let used = line.generate_container_count(&fj_obj, &fj_obj[0], available_space)?;
assert_eq!(
expected,
line.terminal.output(),
Expand All @@ -1638,13 +1628,8 @@ mod tests {
line.terminal.clear_output();
}

for (available_space, used_space, expected) in vec![
(14, 10, r#"{ 1 item }"#),
]
.into_iter()
{
let used =
line.generate_container_count(&fj_one, &fj_one[0], available_space)?;
for (available_space, used_space, expected) in vec![(14, 10, r#"{ 1 item }"#)].into_iter() {
let used = line.generate_container_count(&fj_one, &fj_one[0], available_space)?;
assert_eq!(
expected,
line.terminal.output(),
Expand Down
6 changes: 2 additions & 4 deletions src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,7 @@ impl JsonViewer {
if val == Preview::None && self.mode == Mode::Line {
// Emit a warning....?
self.preview = Preview::Count;
}
else {
} else {
self.preview = val;
}
}
Expand All @@ -749,8 +748,7 @@ impl JsonViewer {
Preview::Count => Preview::None,
Preview::None => Preview::Full,
}
}
else {
} else {
self.preview = match self.preview {
Preview::Full => Preview::Count,
Preview::Count => Preview::Full,
Expand Down