aboutsummaryrefslogtreecommitdiff
path: root/mingling_cli/src/config.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-09 14:39:54 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-09 14:39:54 +0800
commit55a208fdbbc6468b732192bf63ec69877ee20b34 (patch)
tree85b1e519a456133e83c3373c4aaf13a29e5b647b /mingling_cli/src/config.rs
parentb2001ed62d6a4abd7193daeb5d818395b46433b6 (diff)
feat(config): add cfg --pair flag and refactor config path
Add `--pair` flag to output config values as `"key" = "value"` pairs, and provide escaped formatting for display. Move config path resolution to a helper function and expose the underlying hash map for rendering and completion.
Diffstat (limited to 'mingling_cli/src/config.rs')
-rw-r--r--mingling_cli/src/config.rs24
1 files changed, 18 insertions, 6 deletions
diff --git a/mingling_cli/src/config.rs b/mingling_cli/src/config.rs
index 834b98c..32a0994 100644
--- a/mingling_cli/src/config.rs
+++ b/mingling_cli/src/config.rs
@@ -1,14 +1,20 @@
-use std::collections::HashMap;
-
use mingling::{LazyInit, Program, macros::program_setup};
+use std::collections::HashMap;
use crate::ThisProgram;
+pub mod cmd_cfg;
+
#[derive(Debug, Default, Clone)]
pub struct ResMlingConfig {
kvp: HashMap<String, String>,
}
+/// Get the path to the config file.
+fn config_path() -> Option<std::path::PathBuf> {
+ dirs::data_dir().map(|data_dir| data_dir.join("mingling").join("mling-cfg.json"))
+}
+
impl ResMlingConfig {
/// Edit a KVP entry. Empty string removes the key.
pub fn edit(&mut self, key: &str, value: &str) {
@@ -43,8 +49,10 @@ impl ResMlingConfig {
/// Read the config from disk, defaulting to empty if not present.
pub fn read() -> Self {
- let path = std::path::Path::new("");
- Self::read_from_path(path)
+ match config_path() {
+ Some(path) => Self::read_from_path(&path),
+ None => Self::default(),
+ }
}
/// Read the config from a file at the given path.
@@ -81,11 +89,15 @@ impl ResMlingConfig {
/// Write the config to the default data directory path.
pub fn write(&self) {
- if let Some(data_dir) = dirs::data_dir() {
- let path = data_dir.join("mingling").join("mling-cfg.json");
+ if let Some(path) = config_path() {
self.write_to_path(&path);
}
}
+
+ /// Get the underlying hash map as a reference.
+ pub fn get_hash_map(&self) -> &HashMap<String, String> {
+ &self.kvp
+ }
}
impl From<HashMap<String, String>> for ResMlingConfig {