Skip to content

Commit 69215b4

Browse files
committed
Update dependencies and configuration handling
- Added `dirs-next` and `dirs-sys-next` packages to manage directory paths. - Updated `tempfile` dependency to version 3.23.0. - Enhanced configuration file handling in `AppState` to prioritize user-defined paths for `filter.conf`, `theme.conf`, and `keybinds.conf`. - Removed obsolete configuration files: `filter.conf`, `keybinds.conf`, and `theme.conf`.
1 parent 24d597d commit 69215b4

File tree

7 files changed

+121
-9
lines changed

7 files changed

+121
-9
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ path = "src/lib.rs"
1616
[dependencies]
1717
crossterm = "0.29.0"
1818
ratatui = { version = "0.29.0", default-features = false, features = ["crossterm"] }
19+
dirs-next = "2.0"
1920

2021
[dev-dependencies]
21-
tempfile = "3.8"
22+
tempfile = "3.23.0"
2223

2324
[features]
2425
default = []
File renamed without changes.
File renamed without changes.

src/app/mod.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ratatui::widgets::TableState;
1212
use std::time::Instant;
1313

1414
use crate::sys;
15+
use std::path::{PathBuf};
1516

1617
/// Top-level active tab in the UI.
1718
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -448,8 +449,8 @@ impl AppState {
448449
_table_state: TableState::default(),
449450
input_mode: InputMode::Normal,
450451
search_query: String::new(),
451-
theme: Theme::load_or_init("theme.conf"),
452-
keymap: keymap::Keymap::load_or_init("keybinds.conf"),
452+
theme: Theme::load_or_init(&config_file_path("theme.conf")),
453+
keymap: keymap::Keymap::load_or_init(&config_file_path("keybinds.conf")),
453454
modal: None,
454455
users_focus: UsersFocus::UsersList,
455456
sudo_password: None,
@@ -459,13 +460,49 @@ impl AppState {
459460
};
460461

461462
// Load and apply filter configuration from filter.conf (creates default if missing/empty)
462-
let filters_cfg = filterconf::FiltersConfig::load_or_init("filter.conf");
463+
let filters_cfg = filterconf::FiltersConfig::load_or_init(&config_file_path("filter.conf"));
463464
filters_cfg.apply_to(&mut app);
464465

465466
app
466467
}
467468
}
468469

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
476+
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
477+
if !xdg.trim().is_empty() {
478+
let mut p = PathBuf::from(xdg);
479+
p.push("UsrGrpManager");
480+
let _ = std::fs::create_dir_all(&p);
481+
p.push(name);
482+
return p.to_string_lossy().to_string();
483+
}
484+
}
485+
// 2) ~/.config/UsrGrpManager
486+
if let Some(home) = dirs_next::home_dir() {
487+
let mut p = home.clone();
488+
p.push(".config");
489+
p.push("UsrGrpManager");
490+
let _ = std::fs::create_dir_all(&p);
491+
p.push(name);
492+
return p.to_string_lossy().to_string();
493+
}
494+
// 3) Fallback: ~/UsrGrpManager
495+
if let Some(home) = dirs_next::home_dir() {
496+
let mut p = home.clone();
497+
p.push("UsrGrpManager");
498+
let _ = std::fs::create_dir_all(&p);
499+
p.push(name);
500+
return p.to_string_lossy().to_string();
501+
}
502+
// Last resort: current directory
503+
name.to_string()
504+
}
505+
469506
impl Default for AppState {
470507
fn default() -> Self {
471508
Self::new()

src/app/update.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ 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, "filter.conf");
333+
let _ = FiltersConfig::save_from_app(app, &crate::app::config_file_path("filter.conf"));
334334
}
335335
}
336336
KeyCode::Enter => {
@@ -348,7 +348,7 @@ fn handle_modal_key(app: &mut AppState, key: KeyEvent) {
348348
}
349349
close_modal(app);
350350
apply_filters_and_search(app);
351-
let _ = FiltersConfig::save_from_app(app, "filter.conf");
351+
let _ = FiltersConfig::save_from_app(app, &crate::app::config_file_path("filter.conf"));
352352
}
353353
_ => {}
354354
},

0 commit comments

Comments
 (0)