Skip to content

Commit

Permalink
Add cfg save when config is edited
Browse files Browse the repository at this point in the history
Previously the cfg file was only updated after the program was closed. With this change it is easier to share state between to instances (not safely).
  • Loading branch information
weyh committed Apr 1, 2024
1 parent 1dbe0ef commit 00209e2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gcoma"
version = "2.0.0"
version = "2.1.0"
edition = "2021"
readme = "README.md"
license = "Apache-2.0"
Expand Down
13 changes: 8 additions & 5 deletions src/ui/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ fn handle_normal_mode_events(state: &mut ViewState, cfg_path: &str) -> io::Resul
return Ok(true);
}
KeyCode::Char('a') => state.popup_state.show(),
KeyCode::Char('r') => remove_selected(state),
KeyCode::Char('r') => {
remove_selected(state);
state.config.save(cfg_path);
}
KeyCode::Char('R') => {
state.config = load_cfg_from_file(cfg_path).unwrap_or(Config::new());
}
Expand All @@ -124,7 +127,7 @@ fn handle_normal_mode_events(state: &mut ViewState, cfg_path: &str) -> io::Resul
Ok(false)
}

fn handle_edit_mode_events(state: &mut ViewState) -> io::Result<bool> {
fn handle_edit_mode_events(state: &mut ViewState, cfg_path: &str) -> io::Result<bool> {
let popup_state = &mut state.popup_state;

// this is horrible, the state machine needs to be reworked or something :(
Expand All @@ -148,15 +151,15 @@ fn handle_edit_mode_events(state: &mut ViewState) -> io::Result<bool> {
popup_state.increment_state(PopupStateAction::StoreStr(&line));

if popup_state.get_state() == PopupBuilderState::Done {
state.add_temp_session_group_to_cfg();
state.add_temp_session_group_to_cfg(cfg_path);
}
}
},
input => match popup_state.get_state() {
PopupBuilderState::SessionGroupConfirm | PopupBuilderState::Done => {
if input.key == Key::Char('y') {
popup_state.increment_state(PopupStateAction::Store);
state.add_temp_session_group_to_cfg();
state.add_temp_session_group_to_cfg(cfg_path);
}

// Close popup
Expand Down Expand Up @@ -190,7 +193,7 @@ fn handle_events(cfg_path: &str, state: &mut ViewState) -> io::Result<bool> {
if !state.popup_state.is_open() {
return handle_normal_mode_events(state, cfg_path);
} else {
return handle_edit_mode_events(state);
return handle_edit_mode_events(state, cfg_path);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/ui/view_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ impl<'a> ViewState<'a> {
}
}

pub fn add_temp_session_group_to_cfg(&mut self) {
pub fn add_temp_session_group_to_cfg(&mut self, cfg_path: &str) {
if let Some(sg) = self.popup_state.temp_session_group.take() {
self.config.session_groups.push(sg);
self.config.save(cfg_path);
}
}

Expand Down

0 comments on commit 00209e2

Please sign in to comment.