diff --git a/espanso/src/cli/launcher/mod.rs b/espanso/src/cli/launcher/mod.rs index ad354ca..b7989ff 100644 --- a/espanso/src/cli/launcher/mod.rs +++ b/espanso/src/cli/launcher/mod.rs @@ -17,8 +17,10 @@ * along with espanso. If not, see . */ +use log::{error}; use self::util::MigrationError; use crate::preferences::Preferences; +use crate::exit_code::{LAUNCHER_CONFIG_DIR_POPULATION_FAILURE, LAUNCHER_SUCCESS}; use super::{CliModule, CliModuleArgs}; @@ -161,7 +163,14 @@ fn launcher_main(args: CliModuleArgs) -> i32 { true }; - // TODO: initialize config directory if not present + if !espanso_config::is_legacy_config(&paths.config) { + if let Err(err) = crate::config::populate_default_config(&paths.config) { + error!("Error populating the config directory: {:?}", err); + + // TODO: show an error message with GUI + return LAUNCHER_CONFIG_DIR_POPULATION_FAILURE; + } + } if should_launch_daemon { // We hide the dock icon on macOS to avoid having it around when the daemon is running @@ -173,7 +182,7 @@ fn launcher_main(args: CliModuleArgs) -> i32 { daemon::launch_daemon(&paths_overrides).expect("failed to launch daemon"); } - 0 + LAUNCHER_SUCCESS } #[cfg(not(feature = "modulo"))] diff --git a/espanso/src/config.rs b/espanso/src/config.rs new file mode 100644 index 0000000..4c36588 --- /dev/null +++ b/espanso/src/config.rs @@ -0,0 +1,58 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use anyhow::Result; +use log::info; +use std::path::Path; + +const DEFAULT_CONFIG_FILE_CONTENT: &str = include_str!("./res/config/default.yml"); +const DEFAULT_MATCH_FILE_CONTENT: &str = include_str!("./res/config/base.yml"); + +pub fn populate_default_config(config_dir: &Path) -> Result<()> { + if !config_dir.is_dir() { + info!("generating base configuration directory in: {:?}", config_dir); + std::fs::create_dir_all(config_dir)?; + } + + let sub_config_dir = config_dir.join("config"); + let sub_match_dir = config_dir.join("match"); + + if !sub_config_dir.is_dir() { + info!("generating config directory in: {:?}", sub_config_dir); + std::fs::create_dir_all(&sub_config_dir)?; + } + if !sub_match_dir.is_dir() { + info!("generating match directory in: {:?}", sub_match_dir); + std::fs::create_dir_all(&sub_match_dir)?; + } + + let default_file = sub_config_dir.join("default.yml"); + let match_file = sub_match_dir.join("base.yml"); + + if !default_file.is_file() { + info!("populating default.yml file with initial content: {:?}", default_file); + std::fs::write(default_file, DEFAULT_CONFIG_FILE_CONTENT)?; + } + if !match_file.is_file() { + info!("populating base.yml file with initial content: {:?}", match_file); + std::fs::write(match_file, DEFAULT_MATCH_FILE_CONTENT)?; + } + + Ok(()) +} \ No newline at end of file diff --git a/espanso/src/exit_code.rs b/espanso/src/exit_code.rs index d97b81e..58ccae7 100644 --- a/espanso/src/exit_code.rs +++ b/espanso/src/exit_code.rs @@ -40,6 +40,9 @@ pub const MIGRATE_UNEXPECTED_FAILURE: i32 = 101; pub const ADD_TO_PATH_SUCCESS: i32 = 0; pub const ADD_TO_PATH_FAILURE: i32 = 1; +pub const LAUNCHER_SUCCESS: i32 = 0; +pub const LAUNCHER_CONFIG_DIR_POPULATION_FAILURE: i32 = 1; + use std::sync::Mutex; lazy_static! { diff --git a/espanso/src/main.rs b/espanso/src/main.rs index 28fe541..e708277 100644 --- a/espanso/src/main.rs +++ b/espanso/src/main.rs @@ -36,6 +36,7 @@ use simplelog::{ use crate::cli::{LogMode, PathsOverrides}; mod cli; +mod config; mod engine; mod exit_code; mod gui; diff --git a/espanso/src/res/config/base.yml b/espanso/src/res/config/base.yml new file mode 100644 index 0000000..c082c9a --- /dev/null +++ b/espanso/src/res/config/base.yml @@ -0,0 +1,37 @@ +# espanso match file + +# For a complete introduction, visit the official docs at: https://espanso.org/docs/ + +# You can use this file to define the base matches (aka snippets) +# that will be available in every application when using espanso. + +# Matches are substitution rules: when you type the "trigger" string +# it gets replaced by the "replace" string. +matches: + # Simple text replacement + - trigger: ":espanso" + replace: "Hi there!" + + # NOTE: espanso uses YAML to define matches, so pay attention to the indentation! + + # But matches can also be dynamic: + + # Print the current date + - trigger: ":date" + replace: "{{mydate}}" + vars: + - name: mydate + type: date + params: + format: "%m/%d/%Y" + + # Print the output of a shell command + - trigger: ":shell" + replace: "{{output}}" + vars: + - name: output + type: shell + params: + cmd: "echo Hello from your shell" + + # And much more! For more information, visit the docs: https://espanso.org/docs/ \ No newline at end of file diff --git a/espanso/src/res/config/default.yml b/espanso/src/res/config/default.yml new file mode 100644 index 0000000..4f52b02 --- /dev/null +++ b/espanso/src/res/config/default.yml @@ -0,0 +1,40 @@ +# espanso configuration file + +# For a complete introduction, visit the official docs at: https://espanso.org/docs/ + +# You can use this file to define the global configuration options for espanso. +# These are the parameters that will be used by default on every application, +# but you can also override them on a per-application basis. + +# To make customization easier, this file contains some of the commonly used +# parameters. Feel free to uncomment and tune them to fit your needs! + +# --- Toggle key + +# Customize the key used to disable and enable espanso (when double tapped) +# Available options: CTRL, SHIFT, ALT, CMD, OFF +# You can also specify the key variant, such as LEFT_CTRL, RIGHT_SHIFT, etc... +# toggle_key: ALT +# You can also disable the toggle key completely with +# toggle_key: OFF + +# --- Injection Backend + +# Espanso supports multiple ways of injecting text into applications. Each of +# them has its quirks, therefore you may want to change it if you are having problems. +# By default, espanso uses the "Auto" backend which should work well in most cases, +# but you may want to try the "Clipboard" or "Inject" backend in case of issues. +# backend: Clipboard + +# --- Auto-restart + +# Enable/disable the config auto-reload after a file change is detected. +# auto_restart: false + +# --- Clipboard threshold + +# Because injecting long texts char-by-char is a slow operation, espanso automatically +# uses the clipboard if the text is longer than 'clipboard_threshold' characters. +# clipboard_threshold: 100 + +# For a list of all the available options, visit the official docs at: https://espanso.org/docs/ \ No newline at end of file