Skip to content

Commit

Permalink
make the separator configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
krav authored and pierrechevalier83 committed Apr 7, 2023
1 parent ad9094a commit 4f41269
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
[other]
fallback_icon = "🤨"
deduplicate_icons = false
separator = ": "
20 changes: 20 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::{BufReader, Read, Write};
use std::path::PathBuf;

const DEFAULT_FALLBACK_ICON: &str = "-";
const DEFAULT_SEPARATOR: &str = ": ";
const DEFAULT_CONFIG: &str = include_str!("../default_config.toml");

#[derive(Debug, Default, Clone)]
Expand All @@ -19,6 +20,7 @@ pub struct Config {
#[serde(default, deny_unknown_fields)]
pub struct Other {
pub fallback_icon: Option<String>,
pub separator: Option<String>,
pub deduplicate_icons: bool,
}

Expand Down Expand Up @@ -46,6 +48,24 @@ impl Config {
.unwrap_or(DEFAULT_FALLBACK_ICON)
}

pub fn separator(&self) -> &str {
let sep = self.other.separator.as_deref();
if let Some(sep) = sep {
let fallback_icon = self.fallback_icon();
if let Some(icon) = self.mappings.values().find(|icon| icon.contains(sep)) {
error!("Can't use separator: \"{sep}\" as it is contained in icon: \"{icon}\".");
DEFAULT_SEPARATOR
} else if fallback_icon.contains(sep) {
error!("Can't use separator: \"{sep}\" as it is contained in fallback icon: \"{fallback_icon}\"");
DEFAULT_SEPARATOR
} else {
sep
}
} else {
DEFAULT_SEPARATOR
}
}

pub fn path() -> Result<PathBuf> {
let mut user_path = dirs::config_dir().context("Could not find the configuration path")?;
let mut system_path = PathBuf::from("/etc/xdg");
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,19 @@ fn run() -> Result<()> {
loop {
// TODO: watch for changes using inotify and read the config only when needed
let config = Config::new()?;
let sep: &str = config.separator();

let workspaces = wm.get_windows_in_each_workspace()?;
for (name, windows) in workspaces {
let new_name = pretty_windows(&config, &windows);
let num = name
.split(':')
.split(sep)
.next()
.context("Unexpected workspace name")?;
if new_name.is_empty() {
wm.rename_workspace(&name, num)?;
} else {
wm.rename_workspace(&name, &format!("{num}: {new_name}"))?;
wm.rename_workspace(&name, &format!("{num}{sep}{new_name}"))?;
}
}

Expand Down

0 comments on commit 4f41269

Please sign in to comment.