Skip to content

Commit 15fff4b

Browse files
committed
Very basic loading/saving of last port settings
1 parent 5f54a94 commit 15fff4b

6 files changed

Lines changed: 268 additions & 30 deletions

File tree

Cargo.lock

Lines changed: 57 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ color-eyre = "0.6.3"
2020
derivative = "2.2.0"
2121
enum-rotate = "0.1.1"
2222
espflash = { version = "3.3.0", optional = true }
23+
fs-err = "3.1.0"
2324
human-panic = "2.0.2"
2425
int-enum = { git = "https://github.com/nullstalgia/int-enum-rs", branch = "feat/colors-and-discriminators" }
2526
itertools = "0.14.0"
@@ -31,12 +32,15 @@ ratatui = { version = "0.29.0", features = ["unstable-rendered-line-info"] }
3132
ratatui-macros = "0.6.0"
3233
rolling-file = "0.2.0"
3334
serde = { version = "1.0", features = ["derive"] }
35+
serde-inline-default = "0.2.3"
3436
serialport = { version = "4.7.0", features = ["serde"] }
37+
snailquote = "0.3.1"
3538
strip-ansi-escapes = "0.2.1"
3639
struct-table = { path = "struct-table" }
3740
strum = { version = "0.27", features = ["derive"] }
3841
takeable = "0.2.2"
3942
textwrap = "0.16.2"
43+
toml = "0.8.21"
4044
tracing = { version = "0.1.41", features = ["log"] }
4145
tracing-appender = "0.2.3"
4246
tracing-subscriber = { version = "0.3.19", features = ["chrono", "env-filter"] }

src/app.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::{
3636
serial::{
3737
PortSettings, PrintablePortInfo, Reconnections, SerialEvent, SerialHandle, MOCK_PORT_NAME,
3838
},
39+
settings::Settings,
3940
traits::LastIndex,
4041
tui::{
4142
buffer::Buffer,
@@ -179,12 +180,51 @@ pub struct App {
179180
// buffer_rendered_lines: usize,
180181
repeating_line_flip: bool,
181182
failed_send_at: Option<Instant>,
183+
184+
settings: Settings,
182185
}
183186

184187
impl App {
185188
pub fn new(tx: Sender<Event>, rx: Receiver<Event>) -> Self {
189+
let exe_path = std::env::current_exe().unwrap();
190+
let config_path = exe_path.with_extension("toml");
191+
192+
let settings = match Settings::load(&config_path, false) {
193+
Ok(settings) => settings,
194+
// Err(RedefaulterError::TomlDe(e)) => {
195+
// error!("Settings load failed: {e}");
196+
// // TODO move human_span formatting into thiserror fmt attr?
197+
// let err_str = e.to_string();
198+
// // Only grabbing the top line since it has the human-readable line and column information
199+
// // (the error's span method is in *bytes*, not lines and columns)
200+
// let human_span = err_str.lines().next().unwrap_or("").to_owned();
201+
// let reason = e.message().to_owned();
202+
// let new_err = RedefaulterError::SettingsLoad { human_span, reason };
203+
204+
// settings_load_failed_popup(new_err, lock_file);
205+
// }
206+
Err(e) => {
207+
error!("Settings load failed: {e}");
208+
panic!("Settings load failed: {e}");
209+
}
210+
};
211+
212+
let mut user_input = UserInput::default();
213+
214+
let saved_baud_rate = settings.behavior.last_port_settings.baud_rate;
215+
let selected_baud_index = COMMON_BAUD
216+
.iter()
217+
.position(|b| *b == saved_baud_rate)
218+
.unwrap_or_else(|| {
219+
user_input.input_box = Input::new(saved_baud_rate.to_string());
220+
COMMON_BAUD.last_index()
221+
});
222+
223+
debug!("{settings:#?}");
224+
186225
let (event_carousel, carousel_thread) = CarouselHandle::new();
187-
let (serial_handle, serial_thread) = SerialHandle::new(tx.clone());
226+
let (serial_handle, serial_thread) =
227+
SerialHandle::new(tx.clone(), settings.behavior.last_port_settings.clone());
188228

189229
let tick_tx = tx.clone();
190230
event_carousel.add_repeating(
@@ -214,7 +254,7 @@ impl App {
214254
tx,
215255
rx,
216256
table_state: TableState::new().with_selected(Some(0)),
217-
single_line_state: SingleLineSelectorState::new().with_selected(COMMON_BAUD_DEFAULT),
257+
single_line_state: SingleLineSelectorState::new().with_selected(selected_baud_index),
218258
ports: Vec::new(),
219259

220260
carousel: event_carousel,
@@ -223,8 +263,8 @@ impl App {
223263
serial: serial_handle,
224264
serial_thread: Takeable::new(serial_thread),
225265
serial_healthy: false,
226-
scratch_port_settings: PortSettings::default(),
227-
user_input: UserInput::default(),
266+
scratch_port_settings: settings.behavior.last_port_settings.clone(),
267+
user_input,
228268

229269
buffer: Buffer::new(&line_ending),
230270
// buffer_scroll: 0,
@@ -235,6 +275,7 @@ impl App {
235275
repeating_line_flip: false,
236276
failed_send_at: None,
237277
// failed_send_at: Instant::now(),
278+
settings,
238279
}
239280
}
240281
fn is_running(&self) -> bool {
@@ -691,8 +732,9 @@ impl App {
691732
_ = self.popup.take();
692733
self.table_state.select(None);
693734

735+
self.settings.behavior.last_port_settings = self.scratch_port_settings.clone();
736+
self.settings.save().unwrap();
694737
self.buffer.line_ending = self.scratch_port_settings.line_ending.clone();
695-
696738
self.serial
697739
.update_settings(self.scratch_port_settings.clone());
698740
return;
@@ -714,6 +756,9 @@ impl App {
714756

715757
self.scratch_port_settings.baud_rate = baud_rate;
716758

759+
self.settings.behavior.last_port_settings = self.scratch_port_settings.clone();
760+
self.settings.save().unwrap();
761+
717762
self.serial
718763
.connect(&info, self.scratch_port_settings.clone());
719764

0 commit comments

Comments
 (0)