mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-04-25 01:58:51 +02:00
refactor(wm): standardize config env var handling
This commit ensures that whenever komorebi.json is read and deserialized into StaticConfig via StaticConfig::read, all known paths where $Env:KOMOREBI_CONFIG_HOME and $Env:USERPROFILE are accepted will be run through the resolve_home_path helper fn.
This commit is contained in:
@@ -83,14 +83,6 @@ impl From<&KomorebiConfig> for Komorebi {
|
|||||||
if let Some(configuration_switcher) = &value.configuration_switcher {
|
if let Some(configuration_switcher) = &value.configuration_switcher {
|
||||||
let mut configuration_switcher = configuration_switcher.clone();
|
let mut configuration_switcher = configuration_switcher.clone();
|
||||||
for (_, location) in configuration_switcher.configurations.iter_mut() {
|
for (_, location) in configuration_switcher.configurations.iter_mut() {
|
||||||
if let Ok(expanded) = std::env::var("KOMOREBI_CONFIG_HOME") {
|
|
||||||
*location = location.replace("$Env:KOMOREBI_CONFIG_HOME", &expanded);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(expanded) = std::env::var("USERPROFILE") {
|
|
||||||
*location = location.replace("$Env:USERPROFILE", &expanded);
|
|
||||||
}
|
|
||||||
|
|
||||||
*location = dunce::simplified(&PathBuf::from(location.clone()))
|
*location = dunce::simplified(&PathBuf::from(location.clone()))
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|||||||
@@ -934,7 +934,34 @@ impl StaticConfig {
|
|||||||
|
|
||||||
pub fn read(path: &PathBuf) -> Result<Self> {
|
pub fn read(path: &PathBuf) -> Result<Self> {
|
||||||
let content = std::fs::read_to_string(path)?;
|
let content = std::fs::read_to_string(path)?;
|
||||||
let value: Self = serde_json::from_str(&content)?;
|
let mut value: Self = serde_json::from_str(&content)?;
|
||||||
|
|
||||||
|
if let Some(path) = &mut value.app_specific_configuration_path {
|
||||||
|
*path = resolve_home_path(&*path)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(monitors) = &mut value.monitors {
|
||||||
|
for m in monitors {
|
||||||
|
for w in &mut m.workspaces {
|
||||||
|
if let Some(path) = &mut w.custom_layout {
|
||||||
|
*path = resolve_home_path(&*path)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(map) = &mut w.custom_layout_rules {
|
||||||
|
for path in map.values_mut() {
|
||||||
|
*path = resolve_home_path(&*path)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(bar_configurations) = &mut value.bar_configurations {
|
||||||
|
for path in bar_configurations {
|
||||||
|
*path = resolve_home_path(&*path)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -944,8 +971,7 @@ impl StaticConfig {
|
|||||||
incoming: Receiver<WindowManagerEvent>,
|
incoming: Receiver<WindowManagerEvent>,
|
||||||
unix_listener: Option<UnixListener>,
|
unix_listener: Option<UnixListener>,
|
||||||
) -> Result<WindowManager> {
|
) -> Result<WindowManager> {
|
||||||
let content = std::fs::read_to_string(path)?;
|
let mut value = Self::read(path)?;
|
||||||
let mut value: Self = serde_json::from_str(&content)?;
|
|
||||||
value.apply_globals()?;
|
value.apply_globals()?;
|
||||||
|
|
||||||
let listener = match unix_listener {
|
let listener = match unix_listener {
|
||||||
@@ -1024,8 +1050,7 @@ impl StaticConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn postload(path: &PathBuf, wm: &Arc<Mutex<WindowManager>>) -> Result<()> {
|
pub fn postload(path: &PathBuf, wm: &Arc<Mutex<WindowManager>>) -> Result<()> {
|
||||||
let content = std::fs::read_to_string(path)?;
|
let value = Self::read(path)?;
|
||||||
let value: Self = serde_json::from_str(&content)?;
|
|
||||||
let mut wm = wm.lock();
|
let mut wm = wm.lock();
|
||||||
|
|
||||||
if let Some(monitors) = value.monitors {
|
if let Some(monitors) = value.monitors {
|
||||||
@@ -1092,8 +1117,7 @@ impl StaticConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn reload(path: &PathBuf, wm: &mut WindowManager) -> Result<()> {
|
pub fn reload(path: &PathBuf, wm: &mut WindowManager) -> Result<()> {
|
||||||
let content = std::fs::read_to_string(path)?;
|
let mut value = Self::read(path)?;
|
||||||
let mut value: Self = serde_json::from_str(&content)?;
|
|
||||||
|
|
||||||
value.apply_globals()?;
|
value.apply_globals()?;
|
||||||
|
|
||||||
|
|||||||
@@ -1517,26 +1517,15 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
println!("Found komorebi.json; this file can be passed to the start command with the --config flag\n");
|
println!("Found komorebi.json; this file can be passed to the start command with the --config flag\n");
|
||||||
|
|
||||||
if let Ok(config) = &parsed_config {
|
if let Ok(config) = StaticConfig::read(&static_config) {
|
||||||
if let Some(asc_path) = config.get("app_specific_configuration_path") {
|
match config.app_specific_configuration_path {
|
||||||
let mut normalized_asc_path = asc_path
|
None => {
|
||||||
.to_string()
|
println!("Application specific configuration file path has not been set. Try running 'komorebic fetch-asc'\n");
|
||||||
.replace(
|
|
||||||
"$Env:USERPROFILE",
|
|
||||||
&dirs::home_dir().unwrap().to_string_lossy(),
|
|
||||||
)
|
|
||||||
.replace('"', "")
|
|
||||||
.replace('\\', "/");
|
|
||||||
|
|
||||||
if let Ok(komorebi_config_home) = std::env::var("KOMOREBI_CONFIG_HOME") {
|
|
||||||
normalized_asc_path = normalized_asc_path
|
|
||||||
.replace("$Env:KOMOREBI_CONFIG_HOME", &komorebi_config_home)
|
|
||||||
.replace('"', "")
|
|
||||||
.replace('\\', "/");
|
|
||||||
}
|
}
|
||||||
|
Some(path) => {
|
||||||
if !Path::exists(Path::new(&normalized_asc_path)) {
|
if !Path::exists(Path::new(&path)) {
|
||||||
println!("Application specific configuration file path '{normalized_asc_path}' does not exist. Try running 'komorebic fetch-asc'\n");
|
println!("Application specific configuration file path '{}' does not exist. Try running 'komorebic fetch-asc'\n", path.display());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2042,26 +2031,8 @@ if (!(Get-Process whkd -ErrorAction SilentlyContinue))
|
|||||||
let mut config = StaticConfig::read(config)?;
|
let mut config = StaticConfig::read(config)?;
|
||||||
if let Some(display_bar_configurations) = &mut config.bar_configurations {
|
if let Some(display_bar_configurations) = &mut config.bar_configurations {
|
||||||
for config_file_path in &mut *display_bar_configurations {
|
for config_file_path in &mut *display_bar_configurations {
|
||||||
let mut normalized = config_file_path
|
|
||||||
.to_string_lossy()
|
|
||||||
.to_string()
|
|
||||||
.replace(
|
|
||||||
"$Env:USERPROFILE",
|
|
||||||
&dirs::home_dir().unwrap().to_string_lossy(),
|
|
||||||
)
|
|
||||||
.replace('"', "")
|
|
||||||
.replace('\\', "/");
|
|
||||||
|
|
||||||
if let Ok(komorebi_config_home) = std::env::var("KOMOREBI_CONFIG_HOME")
|
|
||||||
{
|
|
||||||
normalized = normalized
|
|
||||||
.replace("$Env:KOMOREBI_CONFIG_HOME", &komorebi_config_home)
|
|
||||||
.replace('"', "")
|
|
||||||
.replace('\\', "/");
|
|
||||||
}
|
|
||||||
|
|
||||||
let script = r"Start-Process 'komorebi-bar' '--config CONFIGFILE' -WindowStyle hidden"
|
let script = r"Start-Process 'komorebi-bar' '--config CONFIGFILE' -WindowStyle hidden"
|
||||||
.replace("CONFIGFILE", &normalized);
|
.replace("CONFIGFILE", &config_file_path.to_string_lossy());
|
||||||
|
|
||||||
match powershell_script::run(&script) {
|
match powershell_script::run(&script) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user