Skip to content

Commit bceebd4

Browse files
committed
Start of logging RX'd/TX'd data to disk!
1 parent 71c6a8d commit bceebd4

15 files changed

Lines changed: 1656 additions & 983 deletions

File tree

src/app.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use tui_input::{Input, StateChanged, backend::crossterm::EventHandler};
3838
use unicode_width::UnicodeWidthStr;
3939

4040
use crate::{
41+
buffer::Buffer,
4142
event_carousel::{self, CarouselHandle},
4243
history::{History, UserInput},
4344
keybinds::{Keybinds, methods::*},
@@ -53,7 +54,6 @@ use crate::{
5354
settings::{Behavior, PortSettings, Rendering, Settings},
5455
traits::{LastIndex, LineHelpers, ToggleBool},
5556
tui::{
56-
buffer::Buffer,
5757
centered_rect_size,
5858
prompts::{DisconnectPrompt, PromptTable, centered_rect},
5959
single_line_selector::{SingleLineSelector, SingleLineSelectorState, StateBottomed},
@@ -316,8 +316,12 @@ impl App {
316316
// );
317317

318318
let line_ending = settings.last_port_settings.rx_line_ending.as_bytes();
319-
let buffer = Buffer::new(line_ending, settings.rendering.clone());
320-
debug!("{buffer:#?}");
319+
let buffer = Buffer::new(
320+
line_ending,
321+
settings.rendering.clone(),
322+
settings.logging.clone(),
323+
);
324+
// debug!("{buffer:#?}");
321325
Self {
322326
state: RunningState::Running,
323327
menu: Menu::PortSelection(PortSelectionElement::Ports),
@@ -437,19 +441,34 @@ impl App {
437441
}
438442

439443
Event::Serial(SerialEvent::Connected(reconnect)) => {
440-
info!("Connected!");
441-
self.buffer.scroll_by(0);
442-
let text = match reconnect {
443-
Some(ReconnectType::PerfectMatch) => "Reconnected to same device!",
444-
Some(ReconnectType::UsbStrict) => "Reconnected to same device?",
445-
Some(ReconnectType::UsbLoose) => "Connected to similar USB device.",
446-
Some(ReconnectType::LastDitch) => "Connected to COM port by name.",
447-
None => "",
448-
// None => "Connected to port!",
449-
};
450-
if !text.is_empty() {
444+
if let Some(reconnect_type) = &reconnect {
445+
info!("Reconnected!");
446+
let text = match reconnect_type {
447+
ReconnectType::PerfectMatch => "Reconnected to same device!",
448+
ReconnectType::UsbStrict => "Reconnected to same device?",
449+
ReconnectType::UsbLoose => "Connected to similar USB device.",
450+
ReconnectType::LastDitch => "Connected to COM port by name.",
451+
};
452+
451453
self.notifs.notify_str(text, Color::Green);
454+
} else {
455+
// If starting session with device.
456+
info!("Connected!");
457+
458+
// self.notifs.notify_str("Connected to port!", Color::Green);
452459
}
460+
461+
if let Some(current_port) = &self.serial.port_status.load().current_port {
462+
self.buffer
463+
.log_handle
464+
.log_port_connected(current_port.to_owned(), reconnect.clone())
465+
.unwrap();
466+
} else {
467+
error!("Was told about a port connection but no current port exists!");
468+
panic!("Was told about a port connection but no current port exists!");
469+
}
470+
471+
self.buffer.scroll_by(0);
453472
}
454473
Event::Serial(SerialEvent::Disconnected(reason)) => {
455474
#[cfg(feature = "espflash")]
@@ -458,6 +477,7 @@ impl App {
458477
// if let Some(reason) = reason {
459478
// self.notify(format!("Disconnected from port! {reason}"), Color::Red);
460479
// }
480+
self.buffer.log_handle.log_port_disconnected(false).unwrap();
461481
if reason.is_some() {
462482
let reconnect_text = match &self.settings.last_port_settings.reconnections {
463483
Reconnections::Disabled => "Not attempting to reconnect.",
@@ -1749,7 +1769,7 @@ impl App {
17491769
self.ports.clear();
17501770
self.serial.request_port_scan().unwrap();
17511771

1752-
self.buffer.clear();
1772+
self.buffer.intentional_disconnect();
17531773
// Clear the input box, but keep the user history!
17541774
self.user_input.clear();
17551775

Lines changed: 12 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ use crate::{
1515
traits::{ByteSuffixCheck, FirstChars, LineHelpers},
1616
};
1717

18-
#[derive(Debug)]
18+
#[derive(Debug, Clone)]
1919
pub struct BufLine {
20-
pub timestamp: DateTime<Local>,
20+
pub(super) timestamp: DateTime<Local>,
2121
timestamp_str: CompactString,
2222

2323
index_info: CompactString,
2424

25-
value: Line<'static>,
25+
pub(super) value: Line<'static>,
2626

2727
/// How many vertical lines are needed in the terminal to fully show this line.
2828
// Truncated from usize, since even the ratatui sizes are capped there.
2929
rendered_line_height: u16,
3030

31-
pub(super) raw_buffer_index: usize,
32-
pub(super) line_type: LineType,
31+
pub raw_buffer_index: usize,
32+
pub line_type: LineType,
3333
}
3434

3535
// impl PartialEq for BufLine {
@@ -100,7 +100,7 @@ impl BufLine {
100100
// determine_color(&mut line, &[]);
101101
// }
102102

103-
let index_info = index_info(raw_value, raw_buffer_index, line_type);
103+
let index_info = make_index_info(raw_value, raw_buffer_index, line_type);
104104

105105
let mut bufline = Self {
106106
timestamp_str: now.format(time_format).to_compact_string(),
@@ -121,41 +121,13 @@ impl BufLine {
121121
area_width: u16,
122122
rendering: &Rendering,
123123
) {
124-
self.index_info = index_info(full_line_slice, self.raw_buffer_index, self.line_type);
124+
self.index_info = make_index_info(full_line_slice, self.raw_buffer_index, self.line_type);
125125

126126
self.value = line;
127127
self.value.remove_unsavory_chars();
128128
self.update_line_height(area_width, rendering);
129129
}
130130

131-
// pub fn new(
132-
// raw_value: &[u8],
133-
// raw_buffer_index: usize,
134-
// area_width: u16,
135-
// with_timestamp: bool,
136-
// ) -> Self {
137-
// let time_format = "[%H:%M:%S%.3f] ";
138-
139-
// let value = determine_color(raw_value);
140-
141-
// let mut line = Self {
142-
// value,
143-
// // raw_value: raw_value.to_owned(),
144-
// // raw_buffer_index,
145-
// // style: None,
146-
// rendered_line_height: 0,
147-
// timestamp: Local::now().format(time_format).to_string(),
148-
// };
149-
// line.update_line_height(area_width, with_timestamp);
150-
// line
151-
// }
152-
153-
// pub fn new_user_line(raw)
154-
155-
// fn completed(&self, line_ending: &str) -> bool {
156-
// self.value.ends_with(line_ending)
157-
// }
158-
159131
pub fn update_line_height(&mut self, area_width: u16, rendering: &Rendering) -> usize {
160132
let para = Paragraph::new(self.as_line(rendering)).wrap(Wrap { trim: false });
161133
// TODO make the sub 1 for margin/scrollbar more sane/clear
@@ -196,43 +168,13 @@ impl BufLine {
196168
pub fn index_in_buffer(&self) -> usize {
197169
self.raw_buffer_index
198170
}
199-
200-
// pub fn timestamp(&self) -> (DateTime<Local>, &str) {
201-
// (self.timestamp, &self.timestamp_str)
202-
// }
203-
204-
// pub fn is_bytes(&self) -> bool {}
205-
206-
// pub fn is_macro(&self) -> bool {}
207-
208-
// pub fn bytes(&self) -> &[u8] {
209-
// self.raw_value.as_slice()
210-
// }
211171
}
212172

213-
// fn determine_color(line: &mut Line, rules: &[u8]) {
214-
// assert_eq!(line.spans.len(), 1);
215-
// if let Some(slice) = line.spans[0].content.first_chars(5) {
216-
// let mut style = Style::new();
217-
// style = match slice {
218-
// // "USER>" => style.dark_gray(),
219-
// "Got m" => style.blue(),
220-
// "ID:0x" => style.green(),
221-
// "Chan." => style.dark_gray(),
222-
// "Mode:" => style.yellow(),
223-
// "Power" => style.red(),
224-
// // "keepa" => style.red(),
225-
// _ => style,
226-
// };
227-
228-
// if style != Style::new() {
229-
// line.style = style;
230-
// line.style_all_spans(style);
231-
// }
232-
// }
233-
// }
234-
235-
fn index_info(full_line_slice: &[u8], start_index: usize, line_type: LineType) -> CompactString {
173+
fn make_index_info(
174+
full_line_slice: &[u8],
175+
start_index: usize,
176+
line_type: LineType,
177+
) -> CompactString {
236178
if let LineType::User { .. } = line_type {
237179
format_compact!(
238180
"({start:06}->{end:06}, {len:3}) ",
@@ -249,41 +191,3 @@ fn index_info(full_line_slice: &[u8], start_index: usize, line_type: LineType) -
249191
)
250192
}
251193
}
252-
253-
// fn determine_color(bytes: &[u8]) -> Line<'static> {
254-
// // let ratatui::text::Text {
255-
// // alignment: _alignment,
256-
// // style,
257-
// // mut lines,
258-
// // } = bytes.into_line(Style::new(), None).unwrap();
259-
// // debug!("{:?}", style);
260-
// // assert_eq!(lines.len(), 1);
261-
262-
// // lines.pop().unwrap()
263-
264-
// bytes.into_line(None, Style::new()).unwrap()
265-
266-
// // // Not sure if this is actually worth keeping, we'll see once I add proper custom rules.
267-
// // if self.style.is_some() {
268-
// // return;
269-
// // }
270-
// // What do I pass into here?
271-
// // The rules? Should it instead be an outside decider that supplies the color?
272-
273-
// // if let Some(slice) = self.value.first_chars(5) {
274-
// // let mut style = Style::new();
275-
// // style = match slice {
276-
// // "USER>" => style.dark_gray(),
277-
// // "Got m" => style.blue(),
278-
// // "ID:0x" => style.green(),
279-
// // "Chan." => style.dark_gray(),
280-
// // "Mode:" => style.yellow(),
281-
// // "Power" => style.red(),
282-
// // _ => style,
283-
// // };
284-
285-
// // if style != Style::new() {
286-
// // self.style = Some(style);
287-
// // }
288-
// // }
289-
// }

0 commit comments

Comments
 (0)