summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyohei Uto <im@kyoheiu.dev>2024-01-20 06:42:04 +0900
committerKyohei Uto <im@kyoheiu.dev>2024-01-20 06:42:04 +0900
commit7b1d4811e6304f8b0a7e3af3e805fed13c0bb701 (patch)
treede20b8e5f346b5342bd7fcb477a224c9163fe668
parent5123d53d14b5d4762d8b4154579d31de9932f955 (diff)
Add config_path field to State
-rw-r--r--src/config.rs19
-rw-r--r--src/state.rs10
2 files changed, 21 insertions, 8 deletions
diff --git a/src/config.rs b/src/config.rs
index de8e6d7..df57efe 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -54,6 +54,11 @@ const CONFIG_EXAMPLE: &str = r###"
# symlink_fg: LightYellow
# dirty_fg: Red
"###;
+#[derive(Debug, Clone)]
+pub struct ConfigWithPath {
+ pub config_path: Option<PathBuf>,
+ pub config: Config,
+}
#[derive(Deserialize, Debug, Clone)]
pub struct Config {
@@ -115,13 +120,16 @@ impl Default for Config {
}
}
-fn read_config(p: &Path) -> Result<Config, FxError> {
+fn read_config(p: &Path) -> Result<ConfigWithPath, FxError> {
let s = read_to_string(p)?;
let deserialized: Config = serde_yaml::from_str(&s)?;
- Ok(deserialized)
+ Ok(ConfigWithPath {
+ config_path: Some(p.to_path_buf()),
+ config: deserialized,
+ })
}
-pub fn read_config_or_default() -> Result<Config, FxError> {
+pub fn read_config_or_default() -> Result<ConfigWithPath, FxError> {
//First, declare default config file path.
let (config_file_path1, config_file_path2) = {
let mut config_path = {
@@ -172,6 +180,9 @@ pub fn read_config_or_default() -> Result<Config, FxError> {
if let Some(config_file) = config_file {
read_config(&config_file)
} else {
- Ok(Config::default())
+ Ok(ConfigWithPath {
+ config_path: None,
+ config: Config::default(),
+ })
}
}
diff --git a/src/state.rs b/src/state.rs
index 3401eca..7c14273 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -46,6 +46,7 @@ pub struct State {
pub list: Vec<ItemInfo>,
pub current_dir: PathBuf,
pub trash_dir: PathBuf,
+ pub config_path: Option<PathBuf>,
pub lwd_file: Option<PathBuf>,
pub match_vim_exit_behavior: bool,
pub has_zoxide: bool,
@@ -234,12 +235,12 @@ impl State {
pub fn new(session_path: &std::path::Path) -> Result<Self, FxError> {
//Read config file.
//Use default configuration if the file does not exist or cannot be read.
- let config = read_config_or_default();
- let config = match config {
- Ok(c) => c,
+ let config_with_path = read_config_or_default();
+ let (config_path, config) = match config_with_path {
+ Ok(c) => (c.config_path, c.config),
Err(e) => {
eprintln!("Cannot read the config file properly.\nError: {}\nfelix launches with default configuration.", e);
- Config::default()
+ (None, Config::default())
}
};
@@ -281,6 +282,7 @@ impl State {
},
current_dir: PathBuf::new(),
trash_dir: PathBuf::new(),
+ config_path,
lwd_file: None,
match_vim_exit_behavior: config.match_vim_exit_behavior.unwrap_or_default(),
has_zoxide,