Skip to content

Commit 921d262

Browse files
committed
clean up cfg
1 parent 470da01 commit 921d262

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

src/main.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn chrono_like_now() -> impl std::fmt::Display {
127127
let z = s.div_euclid(SECS_PER_DAY);
128128
let secs_of_day = s.rem_euclid(SECS_PER_DAY);
129129
let a = z + 719468;
130-
let era = (if a >= 0 { a } else { a - 146096 });
130+
let era = if a >= 0 { a } else { a - 146096 };
131131
let doe = a - era * 146097;
132132
let yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365;
133133
let y = (yoe as i32) + era as i32 * 400;
@@ -336,11 +336,15 @@ fn main() -> Result<()> {
336336
&mut terminal,
337337
ui_rx,
338338
serial_rx,
339-
port.clone(),
340-
running.clone(),
341-
line_ending,
342-
tx_log_writer.clone(),
343-
args.log_ts,
339+
SerialConfig {
340+
port: port.clone(),
341+
},
342+
UiConfig {
343+
running: running.clone(),
344+
line_ending,
345+
tx_log: tx_log_writer.clone(),
346+
log_ts: args.log_ts,
347+
},
344348
);
345349

346350
// Cleanup terminal
@@ -512,19 +516,27 @@ impl AppState {
512516
}
513517
}
514518

515-
fn run_ui<B: Backend>(
516-
terminal: &mut Terminal<B>,
517-
ui_rx: Receiver<UiMessage>,
518-
serial_rx: Receiver<SerialData>,
519+
struct SerialConfig {
519520
port: Arc<Mutex<Box<dyn SerialPort + Send>>>,
521+
}
522+
523+
struct UiConfig {
520524
running: Arc<AtomicBool>,
521525
line_ending: LineEnding,
522526
tx_log: Option<Arc<Mutex<BufWriter<std::fs::File>>>>,
523527
log_ts: bool,
528+
}
529+
530+
fn run_ui<B: Backend>(
531+
terminal: &mut Terminal<B>,
532+
ui_rx: Receiver<UiMessage>,
533+
serial_rx: Receiver<SerialData>,
534+
serial_config: SerialConfig,
535+
ui_config: UiConfig,
524536
) -> Result<()> {
525537
let mut app_state = AppState::new();
526538

527-
while running.load(Ordering::SeqCst) && !app_state.should_quit {
539+
while ui_config.running.load(Ordering::SeqCst) && !app_state.should_quit {
528540
// Check for UI messages (like quit from Ctrl-C)
529541
if let Ok(msg) = ui_rx.try_recv() {
530542
match msg {
@@ -566,22 +578,22 @@ fn run_ui<B: Backend>(
566578
KeyCode::Enter => {
567579
// Send the complete line to serial port
568580
if !app_state.input_line.is_empty() {
569-
write_bytes(&port, app_state.input_line.as_bytes())?;
570-
if let Some(w) = &tx_log
581+
write_bytes(&serial_config.port, app_state.input_line.as_bytes())?;
582+
if let Some(w) = &ui_config.tx_log
571583
&& let Ok(mut lw) = w.lock() {
572-
if log_ts { let _ = write!(lw, "[{}] ", now_rfc3339()); }
584+
if ui_config.log_ts { let _ = write!(lw, "[{}] ", now_rfc3339()); }
573585
let _ = lw.write_all(app_state.input_line.as_bytes());
574586
let _ = lw.flush();
575587
}
576588
}
577589

578590
// Send line ending
579-
let end = line_ending.bytes();
591+
let end = ui_config.line_ending.bytes();
580592
if !end.is_empty() {
581-
write_bytes(&port, end)?;
582-
if let Some(w) = &tx_log
593+
write_bytes(&serial_config.port, end)?;
594+
if let Some(w) = &ui_config.tx_log
583595
&& let Ok(mut lw) = w.lock() {
584-
if log_ts && app_state.input_line.is_empty() { let _ = write!(lw, "[{}] ", now_rfc3339()); }
596+
if ui_config.log_ts && app_state.input_line.is_empty() { let _ = write!(lw, "[{}] ", now_rfc3339()); }
585597
let _ = lw.write_all(end);
586598
let _ = lw.flush();
587599
}
@@ -634,7 +646,7 @@ fn run_ui<B: Backend>(
634646
terminal.draw(|f| draw_ui(f, &mut app_state))?;
635647
}
636648

637-
running.store(false, Ordering::SeqCst);
649+
ui_config.running.store(false, Ordering::SeqCst);
638650
Ok(())
639651
}
640652

0 commit comments

Comments
 (0)