Skip to content

Commit

Permalink
feat(config): add bar configurations opt
Browse files Browse the repository at this point in the history
This commit adds a "bar_configurations" option to the static config file
which takes an array of PathBufs.

If this option is defined and the --bar flag is passed to the "komorebic
start" command, komorebic will attempt to launch multiple instances of
komorebi-bar.exe with the --config flag pointing to the PathBufs given.

This configuration option is only consumed by komorebic, not by the
window manager directly, so it could also be used by other status bar
projects to read configuration file locations from.

There is no requirement for the PathBufs to point specifically to
komorebi bar configuration files if the --bar flag is not being used
with "komorebic start".
  • Loading branch information
LGUG2Z committed Oct 6, 2024
1 parent 75d5971 commit 2f0a930
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
5 changes: 5 additions & 0 deletions komorebi/src/static_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ pub struct StaticConfig {
/// How long to wait when compensating for slow applications, in milliseconds (default: 20)
#[serde(skip_serializing_if = "Option::is_none")]
pub slow_application_compensation_time: Option<u64>,
/// Komorebi status bar configuration files for multiple instances on different monitors
#[serde(skip_serializing_if = "Option::is_none")]
// this option is a little special because it is only consumed by komorebic
pub bar_configurations: Option<Vec<PathBuf>>,
}

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
Expand Down Expand Up @@ -556,6 +560,7 @@ impl From<&WindowManager> for StaticConfig {
SLOW_APPLICATION_COMPENSATION_TIME.load(Ordering::SeqCst),
),
slow_application_identifiers: Option::from(SLOW_APPLICATION_IDENTIFIERS.lock().clone()),
bar_configurations: None,
}
}
}
Expand Down
77 changes: 57 additions & 20 deletions komorebic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2025,19 +2025,68 @@ if (!(Get-Process whkd -ErrorAction SilentlyContinue))
}
}

let static_config = arg.config.clone().map_or_else(
|| {
let komorebi_json = HOME_DIR.join("komorebi.json");
if komorebi_json.is_file() {
Option::from(komorebi_json)
} else {
None
}
},
Option::from,
);

if arg.bar {
let script = r"
if let Some(config) = &static_config {
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);

match powershell_script::run(&script) {
Ok(_) => {
println!("{script}");
}
Err(error) => {
println!("Error: {error}");
}
}
}
} else {
let script = r"
if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue))
{
Start-Process komorebi-bar -WindowStyle hidden
}
";
match powershell_script::run(script) {
Ok(_) => {
println!("{script}");
}
Err(error) => {
println!("Error: {error}");
match powershell_script::run(script) {
Ok(_) => {
println!("{script}");
}
Err(error) => {
println!("Error: {error}");
}
}
}
}
}
Expand All @@ -2050,18 +2099,6 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue))
println!("* Join the Discord https://discord.gg/mGkn66PHkx - Chat, ask questions, share your desktops");
println!("* Read the docs https://lgug2z.github.io/komorebi - Quickly search through all komorebic commands");

let static_config = arg.config.clone().map_or_else(
|| {
let komorebi_json = HOME_DIR.join("komorebi.json");
if komorebi_json.is_file() {
Option::from(komorebi_json)
} else {
None
}
},
Option::from,
);

let bar_config = arg.config.map_or_else(
|| {
let bar_json = HOME_DIR.join("komorebi.bar.json");
Expand All @@ -2074,7 +2111,7 @@ if (!(Get-Process komorebi-bar -ErrorAction SilentlyContinue))
Option::from,
);

if let Some(config) = static_config {
if let Some(config) = &static_config {
let path = resolve_home_path(config)?;
let raw = std::fs::read_to_string(path)?;
StaticConfig::aliases(&raw);
Expand Down

1 comment on commit 2f0a930

@CtByte
Copy link
Contributor

@CtByte CtByte commented on 2f0a930 Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this being very popular. Congrats!!! 🎉

Please sign in to comment.