Skip to content

Commit 4edaeb3

Browse files
authored
Merge pull request #16 from lonesometraveler/refactor_console_output
Refactor console text handling
2 parents 4591214 + 7cc9d96 commit 4edaeb3

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

src/gui.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::data::{DataContainer, SerialDirection};
1717

1818
const MAX_FPS: f64 = 24.0;
1919

20+
const DEFAULT_FONT_ID: FontId = FontId::new(14.0, FontFamily::Monospace);
21+
2022
#[derive(Clone)]
2123
pub enum Print {
2224
EMPTY,
@@ -133,6 +135,25 @@ impl MyApp {
133135
index: 0,
134136
}
135137
}
138+
139+
fn console_text(&self, packet: &crate::data::Packet) -> Option<String> {
140+
match (self.show_sent_cmds, self.show_timestamps, &packet.direction) {
141+
(true, true, _) => Some(format!(
142+
"[{}] t + {:.3}s: {}",
143+
packet.direction,
144+
packet.time as f32 / 1000.0,
145+
packet.payload
146+
)),
147+
(true, false, _) => Some(format!("[{}]: {}", packet.direction, packet.payload)),
148+
(false, true, SerialDirection::Receive) => Some(format!(
149+
"t + {:.3}s: {}",
150+
packet.time as f32 / 1000.0,
151+
packet.payload
152+
)),
153+
(false, false, SerialDirection::Receive) => Some(packet.payload.clone()),
154+
(_, _, _) => None,
155+
}
156+
}
136157
}
137158

138159
impl eframe::App for MyApp {
@@ -218,46 +239,21 @@ impl eframe::App for MyApp {
218239
.show_rows(ui, row_height, num_rows,
219240
|ui, row_range| {
220241
for row in row_range {
221-
let packet = self.data.raw_traffic[row].clone();
242+
let packet = &self.data.raw_traffic[row];
222243
let color = if self.dark_mode {
223244
egui::Color32::WHITE
224245
} else {
225246
egui::Color32::BLACK
226247
};
227-
ui.horizontal_wrapped(|ui| {
228-
let text;
229-
if self.show_sent_cmds {
230-
if self.show_timestamps {
231-
text = format!("[{}] t + {:.3}s: {}",
232-
packet.direction,
233-
packet.time as f32 / 1000.0,
234-
packet.payload);
235-
ui.label(RichText::new(text).color(color).font(
236-
FontId::new(14.0, FontFamily::Monospace)));
237-
} else {
238-
text = format!("[{}]: {}",
239-
packet.direction,
240-
packet.payload);
241-
ui.label(RichText::new(text).color(color).font(
242-
FontId::new(14.0, FontFamily::Monospace)));
243-
}
244-
} else {
245-
if packet.direction == SerialDirection::Receive {
246-
if self.show_timestamps {
247-
text = format!("t + {:.3}s: {}",
248-
packet.time as f32 / 1000.0,
249-
packet.payload);
250-
ui.label(RichText::new(text).color(color).font(
251-
FontId::new(14.0, FontFamily::Monospace)));
252-
} else {
253-
text = format!("{}",
254-
packet.payload);
255-
ui.label(RichText::new(text).color(color).font(
256-
FontId::new(14.0, FontFamily::Monospace)));
257-
}
258-
}
259-
}
260-
});
248+
ui.horizontal_wrapped(|ui| {
249+
if let Some(text) = self.console_text(packet) {
250+
ui.label(
251+
RichText::new(text)
252+
.color(color)
253+
.font(DEFAULT_FONT_ID),
254+
);
255+
}
256+
});
261257
}
262258
});
263259
let mut text_triggered = false;

0 commit comments

Comments
 (0)