Skip to content

Commit

Permalink
feat(payload): allow more space when focused
Browse files Browse the repository at this point in the history
Long json payloads can be quite huge and its easier to watch them when there is more space.
  • Loading branch information
EdJoPaTo committed Apr 9, 2024
1 parent adf9646 commit e96cb00
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Provide better error output on initial MQTT connection errors
- Interactive: When payload is focused it can occupy more space when needed.
- Interactive: Show total amount of messages in the topic overview title
- Log: Provide machine-readable newline-delimited output with `--json`
- Log: Print `--verbose` to stderr instead of stdout
Expand Down
2 changes: 1 addition & 1 deletion src/interactive/details/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl Details {
let history_area = self.payload.draw(
frame,
full_area,
entry,
matches!(focus, ElementInFocus::Payload),
entry,
);
let binary_address = self.payload.binary_state.selected_address();
let json_selector = self.payload.json_state.selected();
Expand Down
33 changes: 19 additions & 14 deletions src/interactive/details/payload_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ impl PayloadView {
&mut self,
frame: &mut Frame,
area: Rect,
entry: &HistoryEntry,
has_focus: bool,
entry: &HistoryEntry,
) -> Rect {
let size = entry.payload_size;
match &entry.payload {
Payload::Binary(data) => self.draw_binary(frame, area, size, data, has_focus),
Payload::Json(json) => self.draw_json(frame, area, size, json, has_focus),
Payload::Binary(data) => self.draw_binary(frame, area, has_focus, size, data),
Payload::Json(json) => self.draw_json(frame, area, has_focus, size, json),
Payload::MessagePack(messagepack) => {
self.draw_messagepack(frame, area, size, messagepack, has_focus)
self.draw_messagepack(frame, area, has_focus, size, messagepack)
}
Payload::String(str) => self.draw_string(frame, area, size, str),
Payload::String(str) => self.draw_string(frame, area, has_focus, size, str),
}
}

Expand All @@ -47,8 +47,12 @@ impl PayloadView {
})
}

fn areas(&mut self, area: Rect, content_height: usize) -> (Rect, Rect) {
let max_payload_height = area.height / 3;
fn areas(&mut self, area: Rect, has_focus: bool, content_height: usize) -> (Rect, Rect) {
let max_payload_height = if has_focus {
area.height.saturating_mul(2) / 3
} else {
area.height / 3
};
#[allow(clippy::cast_possible_truncation)]
let payload_height = min(
max_payload_height as usize,
Expand All @@ -63,9 +67,9 @@ impl PayloadView {
&mut self,
frame: &mut Frame,
area: Rect,
has_focus: bool,
payload_bytes: usize,
data: &[u8],
has_focus: bool,
) -> Rect {
let title = format!("Binary Payload (Bytes: {payload_bytes})");

Expand All @@ -82,7 +86,7 @@ impl PayloadView {
);

let max_lines = widget.get_max_lines_of_data_in_area(area);
let (payload_area, remaining_area) = self.areas(area, max_lines);
let (payload_area, remaining_area) = self.areas(area, has_focus, max_lines);

frame.render_stateful_widget(widget, payload_area, &mut self.binary_state);
remaining_area
Expand All @@ -92,9 +96,9 @@ impl PayloadView {
&mut self,
frame: &mut Frame,
area: Rect,
has_focus: bool,
payload_bytes: usize,
json: &serde_json::Value,
has_focus: bool,
) -> Rect {
let title = format!("JSON Payload (Bytes: {payload_bytes})");
let items = tree_items_from_json(json);
Expand All @@ -104,7 +108,7 @@ impl PayloadView {
.into_iter()
.map(|flattened| flattened.item.height())
.sum::<usize>();
let (payload_area, remaining_area) = self.areas(area, content_height);
let (payload_area, remaining_area) = self.areas(area, has_focus, content_height);

let focus_color = focus_color(has_focus);
let widget = Tree::new(items)
Expand Down Expand Up @@ -132,9 +136,9 @@ impl PayloadView {
&mut self,
frame: &mut Frame,
area: Rect,
has_focus: bool,
payload_bytes: usize,
messagepack: &rmpv::Value,
has_focus: bool,
) -> Rect {
let title = format!("MessagePack Payload (Bytes: {payload_bytes})");
let items = tree_items_from_messagepack(messagepack);
Expand All @@ -144,7 +148,7 @@ impl PayloadView {
.into_iter()
.map(|flattened| flattened.item.height())
.sum::<usize>();
let (payload_area, remaining_area) = self.areas(area, content_height);
let (payload_area, remaining_area) = self.areas(area, has_focus, content_height);

let focus_color = focus_color(has_focus);
let widget = Tree::new(items)
Expand Down Expand Up @@ -172,12 +176,13 @@ impl PayloadView {
&mut self,
frame: &mut Frame,
area: Rect,
has_focus: bool,
payload_bytes: usize,
payload: &str,
) -> Rect {
let title = format!("Payload (Bytes: {payload_bytes})");
let text = Text::from(payload);
let (payload_area, remaining_area) = self.areas(area, text.height());
let (payload_area, remaining_area) = self.areas(area, has_focus, text.height());
let widget = Paragraph::new(text).block(
Block::new()
.border_type(BorderType::Rounded)
Expand Down

0 comments on commit e96cb00

Please sign in to comment.