Skip to content

Commit

Permalink
feat: add config_dir-based file option (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed May 10, 2023
1 parent 1cc8b3d commit 067e565
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 27 deletions.
133 changes: 119 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ radix_trie = "0.2"
rustc-hash = "1.1.0"
miette = { version = "5.7.0", features = ["fancy"] }
thiserror = "1.0.38"
dirs = "5.0.1"

# kanata-keyberon = "0.16.0"
# Uncomment below and comment out above for testing local keyberon changes.
Expand Down
62 changes: 49 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ pub struct ValidatedArgs {
symlink_path: Option<String>,
}

fn default_cfg() -> Vec<PathBuf> {
let mut cfgs = Vec::new();

let default = PathBuf::from("kanata.kbd");
if default.is_file() {
cfgs.push(default);
}

if let Some(config_dir) = dirs::config_dir() {
let fallback = config_dir.join("kanata").join("kanata.kbd");
if fallback.is_file() {
cfgs.push(fallback);
}
}

cfgs
}

#[derive(Parser, Debug)]
#[command(author, version, verbatim_doc_comment)]
/// kanata: an advanced software key remapper
Expand All @@ -39,10 +57,27 @@ pub struct ValidatedArgs {
///
/// https://github.com/jtroo/kanata
struct Args {
/// Configuration file(s) to use with kanata. If not specified, defaults to
/// kanata.kbd in the current working directory.
#[arg(short, long, default_value = "kanata.kbd", verbatim_doc_comment)]
cfg: Vec<String>,
// Display different platform specific paths based on the target OS
#[cfg_attr(
target_os = "windows",
doc = r"Configuration file(s) to use with kanata. If not specified, defaults to
kanata.kbd in the current working directory and
'C:\Users\user\AppData\Roaming\kanata\kanata.kbd'"
)]
#[cfg_attr(
target_os = "macos",
doc = "Configuration file(s) to use with kanata. If not specified, defaults to
kanata.kbd in the current working directory and
'$HOME/Library/Application Support/kanata/kanata.kbd.'"
)]
#[cfg_attr(
not(any(target_os = "macos", target_os = "windows")),
doc = "Configuration file(s) to use with kanata. If not specified, defaults to
kanata.kbd in the current working directory and
'$XDG_CONFIG_HOME/kanata/kanata.kbd'"
)]
#[arg(short, long, verbatim_doc_comment)]
cfg: Option<Vec<PathBuf>>,

/// Port to run the optional TCP server on. If blank, no TCP port will be
/// listened on.
Expand All @@ -68,10 +103,7 @@ struct Args {
fn cli_init() -> Result<ValidatedArgs> {
let args = Args::parse();

let mut cfg_paths = args.cfg.iter().map(PathBuf::from).collect::<Vec<_>>();
if cfg_paths.is_empty() {
cfg_paths.push(PathBuf::from("kanata.kbd"));
}
let cfg_paths = args.cfg.unwrap_or_else(default_cfg);

let log_lvl = match (args.debug, args.trace) {
(_, true) => LevelFilter::Trace,
Expand All @@ -96,11 +128,15 @@ fn cli_init() -> Result<ValidatedArgs> {
#[cfg(all(feature = "interception_driver", target_os = "windows"))]
log::info!("using the Interception driver for keyboard IO");

if !cfg_paths[0].exists() {
bail!(
"Could not find the config file ({})\nFor more info, pass the `-h` or `--help` flags.",
cfg_paths[0].to_str().unwrap_or("?")
)
if let Some(config_file) = cfg_paths.first() {
if !config_file.exists() {
bail!(
"Could not find the config file ({})\nFor more info, pass the `-h` or `--help` flags.",
cfg_paths[0].to_str().unwrap_or("?")
)
}
} else {
bail!("No config files provided\nFor more info, pass the `-h` or `--help` flags.");
}

Ok(ValidatedArgs {
Expand Down

0 comments on commit 067e565

Please sign in to comment.