@@ -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
184187impl 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