Skip to content

Commit 98f5d16

Browse files
committed
Refactor configuration file handling for improved path resolution
- Enhanced `load_or_init` methods in `FiltersConfig` and `Keymap` to prioritize existing configuration files and handle path resolution more effectively. - Updated `AppState` to utilize new path resolution functions for loading configuration files, ensuring user-defined paths are respected. - Modified `handle_modal_key` to save configuration changes using the resolved paths, improving the application's responsiveness to user settings.
1 parent 69215b4 commit 98f5d16

4 files changed

Lines changed: 61 additions & 33 deletions

File tree

src/app/filterconf.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ impl FiltersConfig {
4343

4444
pub fn load_or_init(path: &str) -> Self {
4545
let p = std::path::Path::new(path);
46-
if !p.exists() || std::fs::read_to_string(p).map(|s| s.trim().is_empty()).unwrap_or(true) {
47-
let cfg = Self::default_all_false();
48-
let _ = cfg.write_file(path);
49-
return cfg;
46+
if p.exists() {
47+
return Self::from_file(path).unwrap_or_else(Self::default_all_false);
5048
}
51-
Self::from_file(path).unwrap_or_else(Self::default_all_false)
49+
// try read path resolution in case caller passed a write path but an existing file is elsewhere
50+
if let Some(existing) = crate::app::config_file_read_path("filter.conf") {
51+
return Self::from_file(&existing).unwrap_or_else(Self::default_all_false);
52+
}
53+
let cfg = Self::default_all_false();
54+
let _ = cfg.write_file(path);
55+
cfg
5256
}
5357

5458
pub fn from_file(path: &str) -> Option<Self> {

src/app/keymap.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ impl Keymap {
6161

6262
pub fn load_or_init(path: &str) -> Self {
6363
let p = std::path::Path::new(path);
64-
if !p.exists() || std::fs::read_to_string(p).map(|s| s.trim().is_empty()).unwrap_or(true) {
65-
let km = Self::default();
66-
let _ = km.write_file(path);
67-
return km;
64+
if p.exists() {
65+
return Self::from_file(path).unwrap_or_else(Self::default);
6866
}
69-
match Self::from_file(path) {
70-
Some(km) => km,
71-
None => Self::default(),
67+
if let Some(existing) = crate::app::config_file_read_path("keybinds.conf") {
68+
return Self::from_file(&existing).unwrap_or_else(Self::default);
7269
}
70+
let km = Self::default();
71+
let _ = km.write_file(path);
72+
km
7373
}
7474

7575
pub fn from_file(path: &str) -> Option<Self> {

src/app/mod.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,12 @@ impl AppState {
449449
_table_state: TableState::default(),
450450
input_mode: InputMode::Normal,
451451
search_query: String::new(),
452-
theme: Theme::load_or_init(&config_file_path("theme.conf")),
453-
keymap: keymap::Keymap::load_or_init(&config_file_path("keybinds.conf")),
452+
theme: Theme::load_or_init(
453+
&config_file_read_path("theme.conf").unwrap_or_else(|| config_file_write_path("theme.conf")),
454+
),
455+
keymap: keymap::Keymap::load_or_init(
456+
&config_file_read_path("keybinds.conf").unwrap_or_else(|| config_file_write_path("keybinds.conf")),
457+
),
454458
modal: None,
455459
users_focus: UsersFocus::UsersList,
456460
sudo_password: None,
@@ -460,46 +464,62 @@ impl AppState {
460464
};
461465

462466
// Load and apply filter configuration from filter.conf (creates default if missing/empty)
463-
let filters_cfg = filterconf::FiltersConfig::load_or_init(&config_file_path("filter.conf"));
467+
let filters_cfg = filterconf::FiltersConfig::load_or_init(
468+
&config_file_read_path("filter.conf").unwrap_or_else(|| config_file_write_path("filter.conf")),
469+
);
464470
filters_cfg.apply_to(&mut app);
465471

472+
// Apply the loaded filters to seed the initial views
473+
crate::search::apply_filters_and_search(&mut app);
474+
466475
app
467476
}
468477
}
469478

470-
/// Resolve a configuration file path according to priority:
471-
/// 1) $XDG_CONFIG_HOME/UsrGrpManager/<name>
472-
/// 2) ~/.config/UsrGrpManager/<name>
473-
/// 3) ~/UsrGrpManager/<name>
474-
pub fn config_file_path(name: &str) -> String {
475-
// 1) XDG_CONFIG_HOME
479+
/// Candidate roots in priority order for config files.
480+
fn config_roots() -> Vec<PathBuf> {
481+
let mut roots: Vec<PathBuf> = Vec::new();
476482
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
477483
if !xdg.trim().is_empty() {
478484
let mut p = PathBuf::from(xdg);
479485
p.push("UsrGrpManager");
480-
let _ = std::fs::create_dir_all(&p);
481-
p.push(name);
482-
return p.to_string_lossy().to_string();
486+
roots.push(p);
483487
}
484488
}
485-
// 2) ~/.config/UsrGrpManager
486489
if let Some(home) = dirs_next::home_dir() {
487490
let mut p = home.clone();
488491
p.push(".config");
489492
p.push("UsrGrpManager");
490-
let _ = std::fs::create_dir_all(&p);
491-
p.push(name);
492-
return p.to_string_lossy().to_string();
493+
roots.push(p);
493494
}
494-
// 3) Fallback: ~/UsrGrpManager
495495
if let Some(home) = dirs_next::home_dir() {
496496
let mut p = home.clone();
497497
p.push("UsrGrpManager");
498-
let _ = std::fs::create_dir_all(&p);
498+
roots.push(p);
499+
}
500+
roots
501+
}
502+
503+
/// Resolve existing config file path (read) according to priority order.
504+
pub fn config_file_read_path(name: &str) -> Option<String> {
505+
for root in config_roots() {
506+
let mut p = root.clone();
507+
p.push(name);
508+
if p.exists() {
509+
return Some(p.to_string_lossy().to_string());
510+
}
511+
}
512+
None
513+
}
514+
515+
/// Resolve a path for writing a config file; ensures the directory exists.
516+
pub fn config_file_write_path(name: &str) -> String {
517+
for root in config_roots() {
518+
let _ = std::fs::create_dir_all(&root);
519+
let mut p = root.clone();
499520
p.push(name);
500521
return p.to_string_lossy().to_string();
501522
}
502-
// Last resort: current directory
503523
name.to_string()
504524
}
505525

src/app/update.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ fn handle_modal_key(app: &mut AppState, key: KeyEvent) {
330330
7 => app.users_filter_chips.expired = !app.users_filter_chips.expired,
331331
_ => {}
332332
}
333-
let _ = FiltersConfig::save_from_app(app, &crate::app::config_file_path("filter.conf"));
333+
let path = crate::app::config_file_read_path("filter.conf")
334+
.unwrap_or_else(|| crate::app::config_file_write_path("filter.conf"));
335+
let _ = FiltersConfig::save_from_app(app, &path);
334336
}
335337
}
336338
KeyCode::Enter => {
@@ -348,7 +350,9 @@ fn handle_modal_key(app: &mut AppState, key: KeyEvent) {
348350
}
349351
close_modal(app);
350352
apply_filters_and_search(app);
351-
let _ = FiltersConfig::save_from_app(app, &crate::app::config_file_path("filter.conf"));
353+
let path = crate::app::config_file_read_path("filter.conf")
354+
.unwrap_or_else(|| crate::app::config_file_write_path("filter.conf"));
355+
let _ = FiltersConfig::save_from_app(app, &path);
352356
}
353357
_ => {}
354358
},

0 commit comments

Comments
 (0)