Skip to content

Commit

Permalink
refactor(wm): standardize config env var handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LGUG2Z committed Oct 13, 2024
1 parent 71d65cf commit 3489163
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
8 changes: 0 additions & 8 deletions komorebi-bar/src/komorebi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ impl From<&KomorebiConfig> for Komorebi {
if let Some(configuration_switcher) = &value.configuration_switcher {
let mut configuration_switcher = configuration_switcher.clone();
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()))
.to_string_lossy()
.to_string();
Expand Down
38 changes: 31 additions & 7 deletions komorebi/src/static_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,34 @@ impl StaticConfig {

pub fn read(path: &PathBuf) -> Result<Self> {
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)
}

Expand All @@ -944,8 +971,7 @@ impl StaticConfig {
incoming: Receiver<WindowManagerEvent>,
unix_listener: Option<UnixListener>,
) -> Result<WindowManager> {
let content = std::fs::read_to_string(path)?;
let mut value: Self = serde_json::from_str(&content)?;
let mut value = Self::read(path)?;
value.apply_globals()?;

let listener = match unix_listener {
Expand Down Expand Up @@ -1024,8 +1050,7 @@ impl StaticConfig {
}

pub fn postload(path: &PathBuf, wm: &Arc<Mutex<WindowManager>>) -> Result<()> {
let content = std::fs::read_to_string(path)?;
let value: Self = serde_json::from_str(&content)?;
let value = Self::read(path)?;
let mut wm = wm.lock();

if let Some(monitors) = value.monitors {
Expand Down Expand Up @@ -1092,8 +1117,7 @@ impl StaticConfig {
}

pub fn reload(path: &PathBuf, wm: &mut WindowManager) -> Result<()> {
let content = std::fs::read_to_string(path)?;
let mut value: Self = serde_json::from_str(&content)?;
let mut value = Self::read(path)?;

value.apply_globals()?;

Expand Down
47 changes: 9 additions & 38 deletions komorebic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

if let Ok(config) = &parsed_config {
if let Some(asc_path) = config.get("app_specific_configuration_path") {
let mut normalized_asc_path = asc_path
.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_asc_path = normalized_asc_path
.replace("$Env:KOMOREBI_CONFIG_HOME", &komorebi_config_home)
.replace('"', "")
.replace('\\', "/");
if let Ok(config) = StaticConfig::read(&static_config) {
match config.app_specific_configuration_path {
None => {
println!("Application specific configuration file path has not been set. Try running 'komorebic fetch-asc'\n");
}

if !Path::exists(Path::new(&normalized_asc_path)) {
println!("Application specific configuration file path '{normalized_asc_path}' does not exist. Try running 'komorebic fetch-asc'\n");
Some(path) => {
if !Path::exists(Path::new(&path)) {
println!("Application specific configuration file path '{}' does not exist. Try running 'komorebic fetch-asc'\n", path.display());
}
}
}
}
Expand Down Expand Up @@ -2042,26 +2031,8 @@ if (!(Get-Process whkd -ErrorAction SilentlyContinue))
let mut config = StaticConfig::read(config)?;
if let Some(display_bar_configurations) = &mut config.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"
.replace("CONFIGFILE", &normalized);
.replace("CONFIGFILE", &config_file_path.to_string_lossy());

match powershell_script::run(&script) {
Ok(_) => {
Expand Down

0 comments on commit 3489163

Please sign in to comment.