Adopt rustfmt formatting for the project. Fix #137
This commit is contained in:
parent
949f1e4acf
commit
5b537b4a57
3
build.rs
3
build.rs
|
@ -47,8 +47,7 @@ fn print_config() {
|
|||
println!("cargo:rustc-link-lib=framework=IOKit");
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
fn main() {
|
||||
let dst = get_config();
|
||||
|
||||
println!("cargo:rustc-link-search=native={}", dst.display());
|
||||
|
|
|
@ -17,11 +17,11 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::os::raw::{c_void, c_char};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
|
||||
#[allow(improper_ctypes)]
|
||||
#[link(name = "linuxbridge", kind = "static")]
|
||||
extern {
|
||||
extern "C" {
|
||||
pub fn check_x11() -> i32;
|
||||
pub fn initialize(s: *const c_void) -> i32;
|
||||
pub fn eventloop();
|
||||
|
@ -34,8 +34,9 @@ extern {
|
|||
pub fn is_current_window_special() -> i32;
|
||||
|
||||
// Keyboard
|
||||
pub fn register_keypress_callback(cb: extern fn(_self: *mut c_void, *const u8,
|
||||
i32, i32, i32));
|
||||
pub fn register_keypress_callback(
|
||||
cb: extern "C" fn(_self: *mut c_void, *const u8, i32, i32, i32),
|
||||
);
|
||||
|
||||
pub fn send_string(string: *const c_char);
|
||||
pub fn delete_string(count: i32);
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::os::raw::{c_void, c_char};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MacMenuItem {
|
||||
|
@ -28,7 +28,7 @@ pub struct MacMenuItem {
|
|||
|
||||
#[allow(improper_ctypes)]
|
||||
#[link(name = "macbridge", kind = "static")]
|
||||
extern {
|
||||
extern "C" {
|
||||
pub fn initialize(s: *const c_void, icon_path: *const c_char, show_icon: i32);
|
||||
pub fn eventloop();
|
||||
pub fn headless_eventloop();
|
||||
|
@ -48,13 +48,14 @@ extern {
|
|||
pub fn set_clipboard_image(path: *const c_char) -> i32;
|
||||
|
||||
// UI
|
||||
pub fn register_icon_click_callback(cb: extern fn(_self: *mut c_void));
|
||||
pub fn register_icon_click_callback(cb: extern "C" fn(_self: *mut c_void));
|
||||
pub fn show_context_menu(items: *const MacMenuItem, count: i32) -> i32;
|
||||
pub fn register_context_menu_click_callback(cb: extern fn(_self: *mut c_void, id: i32));
|
||||
pub fn register_context_menu_click_callback(cb: extern "C" fn(_self: *mut c_void, id: i32));
|
||||
|
||||
// Keyboard
|
||||
pub fn register_keypress_callback(cb: extern fn(_self: *mut c_void, *const u8,
|
||||
i32, i32, i32));
|
||||
pub fn register_keypress_callback(
|
||||
cb: extern "C" fn(_self: *mut c_void, *const u8, i32, i32, i32),
|
||||
);
|
||||
|
||||
pub fn send_string(string: *const c_char);
|
||||
pub fn send_vkey(vk: i32);
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::os::raw::{c_void};
|
||||
use std::os::raw::c_void;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct WindowsMenuItem {
|
||||
|
@ -28,9 +28,14 @@ pub struct WindowsMenuItem {
|
|||
|
||||
#[allow(improper_ctypes)]
|
||||
#[link(name = "winbridge", kind = "static")]
|
||||
extern {
|
||||
extern "C" {
|
||||
pub fn start_daemon_process() -> i32;
|
||||
pub fn initialize(s: *const c_void, ico_path: *const u16, bmp_path: *const u16, show_icon: i32) -> i32;
|
||||
pub fn initialize(
|
||||
s: *const c_void,
|
||||
ico_path: *const u16,
|
||||
bmp_path: *const u16,
|
||||
show_icon: i32,
|
||||
) -> i32;
|
||||
|
||||
// SYSTEM
|
||||
pub fn get_active_window_name(buffer: *mut u16, size: i32) -> i32;
|
||||
|
@ -40,8 +45,8 @@ extern {
|
|||
pub fn show_notification(message: *const u16) -> i32;
|
||||
pub fn close_notification();
|
||||
pub fn show_context_menu(items: *const WindowsMenuItem, count: i32) -> i32;
|
||||
pub fn register_icon_click_callback(cb: extern fn(_self: *mut c_void));
|
||||
pub fn register_context_menu_click_callback(cb: extern fn(_self: *mut c_void, id: i32));
|
||||
pub fn register_icon_click_callback(cb: extern "C" fn(_self: *mut c_void));
|
||||
pub fn register_context_menu_click_callback(cb: extern "C" fn(_self: *mut c_void, id: i32));
|
||||
pub fn cleanup_ui();
|
||||
|
||||
// CLIPBOARD
|
||||
|
@ -50,8 +55,9 @@ extern {
|
|||
pub fn set_clipboard_image(path: *const u16) -> i32;
|
||||
|
||||
// KEYBOARD
|
||||
pub fn register_keypress_callback(cb: extern fn(_self: *mut c_void, *const u16,
|
||||
i32, i32, i32, i32, i32));
|
||||
pub fn register_keypress_callback(
|
||||
cb: extern "C" fn(_self: *mut c_void, *const u16, i32, i32, i32, i32, i32),
|
||||
);
|
||||
|
||||
pub fn eventloop();
|
||||
pub fn send_string(string: *const u16);
|
||||
|
|
12
src/check.rs
12
src/check.rs
|
@ -27,20 +27,18 @@ pub fn check_preconditions() -> bool {
|
|||
let mut result = true;
|
||||
|
||||
// Make sure notify-send is installed
|
||||
let status = Command::new("notify-send")
|
||||
.arg("-v")
|
||||
.output();
|
||||
let status = Command::new("notify-send").arg("-v").output();
|
||||
if status.is_err() {
|
||||
println!("Error: 'notify-send' command is needed for espanso to work correctly, please install it.");
|
||||
result = false;
|
||||
}
|
||||
|
||||
// Make sure xclip is installed
|
||||
let status = Command::new("xclip")
|
||||
.arg("-version")
|
||||
.output();
|
||||
let status = Command::new("xclip").arg("-version").output();
|
||||
if status.is_err() {
|
||||
println!("Error: 'xclip' command is needed for espanso to work correctly, please install it.");
|
||||
println!(
|
||||
"Error: 'xclip' command is needed for espanso to work correctly, please install it."
|
||||
);
|
||||
result = false;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,18 +17,16 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::process::{Command, Stdio};
|
||||
use std::io::{Write};
|
||||
use log::{error};
|
||||
use log::error;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
pub struct LinuxClipboardManager {}
|
||||
|
||||
impl super::ClipboardManager for LinuxClipboardManager {
|
||||
fn get_clipboard(&self) -> Option<String> {
|
||||
let res = Command::new("xclip")
|
||||
.args(&["-o", "-sel", "clip"])
|
||||
.output();
|
||||
let res = Command::new("xclip").args(&["-o", "-sel", "clip"]).output();
|
||||
|
||||
if let Ok(output) = res {
|
||||
if output.status.success() {
|
||||
|
@ -71,14 +69,14 @@ impl super::ClipboardManager for LinuxClipboardManager {
|
|||
Some(ext) => {
|
||||
let ext = ext.to_string_lossy().to_lowercase();
|
||||
match ext.as_ref() {
|
||||
"png" => {"image/png"},
|
||||
"jpg" | "jpeg" => {"image/jpeg"},
|
||||
"gif" => {"image/gif"},
|
||||
"svg" => {"image/svg"},
|
||||
_ => {"image/png"},
|
||||
"png" => "image/png",
|
||||
"jpg" | "jpeg" => "image/jpeg",
|
||||
"gif" => "image/gif",
|
||||
"svg" => "image/svg",
|
||||
_ => "image/png",
|
||||
}
|
||||
},
|
||||
None => {"image/png"},
|
||||
}
|
||||
None => "image/png",
|
||||
};
|
||||
|
||||
let image_path = image_path.to_string_lossy().into_owned();
|
||||
|
|
|
@ -17,15 +17,13 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::os::raw::c_char;
|
||||
use crate::bridge::macos::*;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::path::Path;
|
||||
use log::{error, warn};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::c_char;
|
||||
use std::path::Path;
|
||||
|
||||
pub struct MacClipboardManager {
|
||||
|
||||
}
|
||||
pub struct MacClipboardManager {}
|
||||
|
||||
impl super::ClipboardManager for MacClipboardManager {
|
||||
fn get_clipboard(&self) -> Option<String> {
|
||||
|
|
|
@ -17,13 +17,11 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use widestring::U16CString;
|
||||
use crate::bridge::windows::{set_clipboard, get_clipboard, set_clipboard_image};
|
||||
use crate::bridge::windows::{get_clipboard, set_clipboard, set_clipboard_image};
|
||||
use std::path::Path;
|
||||
use widestring::U16CString;
|
||||
|
||||
pub struct WindowsClipboardManager {
|
||||
|
||||
}
|
||||
pub struct WindowsClipboardManager {}
|
||||
|
||||
impl WindowsClipboardManager {
|
||||
pub fn new() -> WindowsClipboardManager {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -17,13 +17,13 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use regex::Regex;
|
||||
use super::{ConfigSet, Configs};
|
||||
use crate::matcher::Match;
|
||||
use crate::system::SystemManager;
|
||||
use log::{debug, warn};
|
||||
use regex::Regex;
|
||||
use std::cell::RefCell;
|
||||
use std::time::SystemTime;
|
||||
use log::{debug, warn};
|
||||
use super::{Configs, ConfigSet};
|
||||
use crate::matcher::Match;
|
||||
|
||||
pub struct RuntimeConfigManager<'a, S: SystemManager> {
|
||||
set: ConfigSet,
|
||||
|
@ -37,7 +37,7 @@ pub struct RuntimeConfigManager<'a, S: SystemManager> {
|
|||
|
||||
// Cache
|
||||
last_config_update: RefCell<SystemTime>,
|
||||
last_config: RefCell<Option<&'a Configs>>
|
||||
last_config: RefCell<Option<&'a Configs>>,
|
||||
}
|
||||
|
||||
impl<'a, S: SystemManager> RuntimeConfigManager<'a, S> {
|
||||
|
@ -101,7 +101,7 @@ impl <'a, S: SystemManager> RuntimeConfigManager<'a, S> {
|
|||
exec_regexps,
|
||||
system_manager,
|
||||
last_config_update,
|
||||
last_config
|
||||
last_config,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,10 +118,12 @@ impl <'a, S: SystemManager> RuntimeConfigManager<'a, S> {
|
|||
for (i, regex) in self.title_regexps.iter().enumerate() {
|
||||
if let Some(regex) = regex {
|
||||
if regex.is_match(&title) {
|
||||
debug!("Matched 'filter_title' for '{}' config, using custom settings.",
|
||||
self.set.specific[i].name);
|
||||
debug!(
|
||||
"Matched 'filter_title' for '{}' config, using custom settings.",
|
||||
self.set.specific[i].name
|
||||
);
|
||||
|
||||
return &self.set.specific[i]
|
||||
return &self.set.specific[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -135,10 +137,12 @@ impl <'a, S: SystemManager> RuntimeConfigManager<'a, S> {
|
|||
for (i, regex) in self.exec_regexps.iter().enumerate() {
|
||||
if let Some(regex) = regex {
|
||||
if regex.is_match(&executable) {
|
||||
debug!("Matched 'filter_exec' for '{}' config, using custom settings.",
|
||||
self.set.specific[i].name);
|
||||
debug!(
|
||||
"Matched 'filter_exec' for '{}' config, using custom settings.",
|
||||
self.set.specific[i].name
|
||||
);
|
||||
|
||||
return &self.set.specific[i]
|
||||
return &self.set.specific[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -152,10 +156,12 @@ impl <'a, S: SystemManager> RuntimeConfigManager<'a, S> {
|
|||
for (i, regex) in self.class_regexps.iter().enumerate() {
|
||||
if let Some(regex) = regex {
|
||||
if regex.is_match(&class) {
|
||||
debug!("Matched 'filter_class' for '{}' config, using custom settings.",
|
||||
self.set.specific[i].name);
|
||||
debug!(
|
||||
"Matched 'filter_class' for '{}' config, using custom settings.",
|
||||
self.set.specific[i].name
|
||||
);
|
||||
|
||||
return &self.set.specific[i]
|
||||
return &self.set.specific[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -204,8 +210,8 @@ impl <'a, S: SystemManager> super::ConfigManager<'a> for RuntimeConfigManager<'a
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::config::ConfigManager;
|
||||
use crate::config::tests::{create_temp_espanso_directories, create_user_config_file};
|
||||
use crate::config::ConfigManager;
|
||||
|
||||
struct DummySystemManager {
|
||||
title: RefCell<String>,
|
||||
|
@ -228,7 +234,7 @@ mod tests {
|
|||
DummySystemManager {
|
||||
title: RefCell::new(title.to_owned()),
|
||||
class: RefCell::new(class.to_owned()),
|
||||
exec: RefCell::new(exec.to_owned())
|
||||
exec: RefCell::new(exec.to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -247,21 +253,33 @@ mod tests {
|
|||
fn test_runtime_constructor_regex_load_correctly() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: myname1
|
||||
filter_exec: "Title"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific2.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific2.yml",
|
||||
r###"
|
||||
name: myname2
|
||||
filter_title: "Yeah"
|
||||
filter_class: "Car"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific3.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific3.yml",
|
||||
r###"
|
||||
name: myname3
|
||||
filter_title: "Nice"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
@ -270,12 +288,24 @@ mod tests {
|
|||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
let sp1index = config_manager.set.specific
|
||||
.iter().position(|x| x.name == "myname1").unwrap();
|
||||
let sp2index = config_manager.set.specific
|
||||
.iter().position(|x| x.name == "myname2").unwrap();
|
||||
let sp3index = config_manager.set.specific
|
||||
.iter().position(|x| x.name == "myname3").unwrap();
|
||||
let sp1index = config_manager
|
||||
.set
|
||||
.specific
|
||||
.iter()
|
||||
.position(|x| x.name == "myname1")
|
||||
.unwrap();
|
||||
let sp2index = config_manager
|
||||
.set
|
||||
.specific
|
||||
.iter()
|
||||
.position(|x| x.name == "myname2")
|
||||
.unwrap();
|
||||
let sp3index = config_manager
|
||||
.set
|
||||
.specific
|
||||
.iter()
|
||||
.position(|x| x.name == "myname3")
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config_manager.exec_regexps.len(), 3);
|
||||
assert_eq!(config_manager.title_regexps.len(), 3);
|
||||
|
@ -298,21 +328,33 @@ mod tests {
|
|||
fn test_runtime_constructor_malformed_regexes_are_ignored() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: myname1
|
||||
filter_exec: "[`-_]"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific2.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific2.yml",
|
||||
r###"
|
||||
name: myname2
|
||||
filter_title: "[`-_]"
|
||||
filter_class: "Car"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific3.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific3.yml",
|
||||
r###"
|
||||
name: myname3
|
||||
filter_title: "Nice"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
@ -321,12 +363,24 @@ mod tests {
|
|||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
let sp1index = config_manager.set.specific
|
||||
.iter().position(|x| x.name == "myname1").unwrap();
|
||||
let sp2index = config_manager.set.specific
|
||||
.iter().position(|x| x.name == "myname2").unwrap();
|
||||
let sp3index = config_manager.set.specific
|
||||
.iter().position(|x| x.name == "myname3").unwrap();
|
||||
let sp1index = config_manager
|
||||
.set
|
||||
.specific
|
||||
.iter()
|
||||
.position(|x| x.name == "myname1")
|
||||
.unwrap();
|
||||
let sp2index = config_manager
|
||||
.set
|
||||
.specific
|
||||
.iter()
|
||||
.position(|x| x.name == "myname2")
|
||||
.unwrap();
|
||||
let sp3index = config_manager
|
||||
.set
|
||||
.specific
|
||||
.iter()
|
||||
.position(|x| x.name == "myname3")
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(config_manager.exec_regexps.len(), 3);
|
||||
assert_eq!(config_manager.title_regexps.len(), 3);
|
||||
|
@ -349,15 +403,20 @@ mod tests {
|
|||
fn test_runtime_calculate_active_config_specific_title_match() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: chrome
|
||||
filter_title: "Chrome"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
||||
let dummy_system_manager = DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
let dummy_system_manager =
|
||||
DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
|
@ -368,15 +427,20 @@ mod tests {
|
|||
fn test_runtime_calculate_active_config_specific_class_match() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: chrome
|
||||
filter_class: "Chrome"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
||||
let dummy_system_manager = DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
let dummy_system_manager =
|
||||
DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
|
@ -387,15 +451,20 @@ mod tests {
|
|||
fn test_runtime_calculate_active_config_specific_exec_match() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: chrome
|
||||
filter_exec: "chrome.exe"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
||||
let dummy_system_manager = DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
let dummy_system_manager =
|
||||
DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
|
@ -406,16 +475,21 @@ mod tests {
|
|||
fn test_runtime_calculate_active_config_specific_multi_filter_match() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: chrome
|
||||
filter_class: Browser
|
||||
filter_exec: "firefox.exe"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
||||
let dummy_system_manager = DummySystemManager::new_custom("Google Chrome", "Browser", "C:\\Path\\chrome.exe");
|
||||
let dummy_system_manager =
|
||||
DummySystemManager::new_custom("Google Chrome", "Browser", "C:\\Path\\chrome.exe");
|
||||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
|
@ -426,15 +500,20 @@ mod tests {
|
|||
fn test_runtime_calculate_active_config_no_match() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: firefox
|
||||
filter_title: "Firefox"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
||||
let dummy_system_manager = DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
let dummy_system_manager =
|
||||
DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
|
@ -445,22 +524,29 @@ mod tests {
|
|||
fn test_runtime_active_config_cache() {
|
||||
let (data_dir, package_dir) = create_temp_espanso_directories();
|
||||
|
||||
create_user_config_file(&data_dir.path(), "specific.yml", r###"
|
||||
create_user_config_file(
|
||||
&data_dir.path(),
|
||||
"specific.yml",
|
||||
r###"
|
||||
name: firefox
|
||||
filter_title: "Firefox"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let config_set = ConfigSet::load(data_dir.path(), package_dir.path());
|
||||
assert!(config_set.is_ok());
|
||||
|
||||
let dummy_system_manager = DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
let dummy_system_manager =
|
||||
DummySystemManager::new_custom("Google Chrome", "Chrome", "C:\\Path\\chrome.exe");
|
||||
|
||||
let config_manager = RuntimeConfigManager::new(config_set.unwrap(), dummy_system_manager);
|
||||
|
||||
assert_eq!(config_manager.active_config().name, "default");
|
||||
assert_eq!(config_manager.calculate_active_config().name, "default");
|
||||
|
||||
config_manager.system_manager.change("Firefox", "Browser", "C\\Path\\firefox.exe");
|
||||
config_manager
|
||||
.system_manager
|
||||
.change("Firefox", "Browser", "C\\Path\\firefox.exe");
|
||||
|
||||
// Active config should have changed, but not cached one
|
||||
assert_eq!(config_manager.calculate_active_config().name, "firefox");
|
||||
|
|
|
@ -17,18 +17,18 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::os::raw::{c_void, c_char};
|
||||
use crate::event::*;
|
||||
use crate::event::KeyModifier::*;
|
||||
use crate::bridge::linux::*;
|
||||
use std::process::exit;
|
||||
use crate::config::Configs;
|
||||
use crate::event::KeyModifier::*;
|
||||
use crate::event::*;
|
||||
use log::{debug, error};
|
||||
use std::ffi::CStr;
|
||||
use std::os::raw::{c_char, c_void};
|
||||
use std::process::exit;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering::Acquire;
|
||||
use crate::config::Configs;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct LinuxContext {
|
||||
|
@ -37,11 +37,13 @@ pub struct LinuxContext {
|
|||
}
|
||||
|
||||
impl LinuxContext {
|
||||
pub fn new(_: Configs, send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<LinuxContext> {
|
||||
pub fn new(
|
||||
_: Configs,
|
||||
send_channel: Sender<Event>,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Box<LinuxContext> {
|
||||
// Check if the X11 context is available
|
||||
let x11_available = unsafe {
|
||||
check_x11()
|
||||
};
|
||||
let x11_available = unsafe { check_x11() };
|
||||
|
||||
if x11_available < 0 {
|
||||
error!("Error, can't connect to X11 context");
|
||||
|
@ -50,7 +52,7 @@ impl LinuxContext {
|
|||
|
||||
let context = Box::new(LinuxContext {
|
||||
send_channel,
|
||||
is_injecting
|
||||
is_injecting,
|
||||
});
|
||||
|
||||
unsafe {
|
||||
|
@ -79,14 +81,21 @@ impl super::Context for LinuxContext {
|
|||
|
||||
impl Drop for LinuxContext {
|
||||
fn drop(&mut self) {
|
||||
unsafe { cleanup(); }
|
||||
unsafe {
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Native bridge code
|
||||
|
||||
extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, _len: i32,
|
||||
event_type: i32, key_code: i32) {
|
||||
extern "C" fn keypress_callback(
|
||||
_self: *mut c_void,
|
||||
raw_buffer: *const u8,
|
||||
_len: i32,
|
||||
event_type: i32,
|
||||
key_code: i32,
|
||||
) {
|
||||
unsafe {
|
||||
let _self = _self as *mut LinuxContext;
|
||||
|
||||
|
@ -98,7 +107,8 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, _len: i32
|
|||
return;
|
||||
}
|
||||
|
||||
if event_type == 0 { // Char event
|
||||
if event_type == 0 {
|
||||
// Char event
|
||||
// Convert the received buffer to a string
|
||||
let c_str = CStr::from_ptr(raw_buffer as *const c_char);
|
||||
let char_str = c_str.to_str();
|
||||
|
@ -108,12 +118,13 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, _len: i32
|
|||
Ok(char_str) => {
|
||||
let event = Event::Key(KeyEvent::Char(char_str.to_owned()));
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Unable to receive char: {}", e);
|
||||
},
|
||||
}
|
||||
}else if event_type == 1 { // Modifier event
|
||||
}
|
||||
} else if event_type == 1 {
|
||||
// Modifier event
|
||||
|
||||
let modifier: Option<KeyModifier> = match key_code {
|
||||
133 => Some(LEFT_META),
|
||||
|
@ -132,11 +143,13 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, _len: i32
|
|||
if let Some(modifier) = modifier {
|
||||
let event = Event::Key(KeyEvent::Modifier(modifier));
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}else{ // Not one of the default modifiers, send an "other" event
|
||||
} else {
|
||||
// Not one of the default modifiers, send an "other" event
|
||||
let event = Event::Key(KeyEvent::Other);
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}
|
||||
}else{ // Other type of event
|
||||
} else {
|
||||
// Other type of event
|
||||
let event = Event::Key(KeyEvent::Other);
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}
|
||||
|
|
|
@ -17,21 +17,21 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::os::raw::{c_void, c_char};
|
||||
use crate::bridge::macos::*;
|
||||
use crate::event::{Event, KeyEvent, KeyModifier, ActionType, SystemEvent};
|
||||
use crate::config::Configs;
|
||||
use crate::event::KeyModifier::*;
|
||||
use std::ffi::{CString, CStr};
|
||||
use std::{fs, thread};
|
||||
use log::{info, error, debug};
|
||||
use crate::event::{ActionType, Event, KeyEvent, KeyModifier, SystemEvent};
|
||||
use crate::system::macos::MacSystemManager;
|
||||
use log::{debug, error, info};
|
||||
use std::cell::RefCell;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_void};
|
||||
use std::process::exit;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering::Acquire;
|
||||
use crate::config::Configs;
|
||||
use std::cell::RefCell;
|
||||
use crate::system::macos::MacSystemManager;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, thread};
|
||||
|
||||
const STATUS_ICON_BINARY: &[u8] = include_bytes!("../res/mac/icon.png");
|
||||
|
||||
|
@ -43,14 +43,20 @@ pub struct MacContext {
|
|||
}
|
||||
|
||||
impl MacContext {
|
||||
pub fn new(config: Configs, send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<MacContext> {
|
||||
pub fn new(
|
||||
config: Configs,
|
||||
send_channel: Sender<Event>,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Box<MacContext> {
|
||||
// Check accessibility
|
||||
unsafe {
|
||||
let res = prompt_accessibility();
|
||||
|
||||
if res == 0 {
|
||||
error!("Accessibility must be enabled to make espanso work on MacOS.");
|
||||
error!("Please allow espanso in the Security & Privacy panel, then restart espanso.");
|
||||
error!(
|
||||
"Please allow espanso in the Security & Privacy panel, then restart espanso."
|
||||
);
|
||||
error!("For more information: https://espanso.org/install/mac/");
|
||||
exit(1);
|
||||
}
|
||||
|
@ -71,7 +77,10 @@ impl MacContext {
|
|||
info!("Status icon already initialized, skipping.");
|
||||
} else {
|
||||
fs::write(&status_icon_target, STATUS_ICON_BINARY).unwrap_or_else(|e| {
|
||||
error!("Error copying the Status Icon to the espanso data directory: {}", e);
|
||||
error!(
|
||||
"Error copying the Status Icon to the espanso data directory: {}",
|
||||
e
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -82,12 +91,9 @@ impl MacContext {
|
|||
register_icon_click_callback(icon_click_callback);
|
||||
register_context_menu_click_callback(context_menu_click_callback);
|
||||
|
||||
let status_icon_path = CString::new(status_icon_target.to_str().unwrap_or_default()).unwrap_or_default();
|
||||
let show_icon = if config.show_icon {
|
||||
1
|
||||
}else{
|
||||
0
|
||||
};
|
||||
let status_icon_path =
|
||||
CString::new(status_icon_target.to_str().unwrap_or_default()).unwrap_or_default();
|
||||
let show_icon = if config.show_icon { 1 } else { 0 };
|
||||
|
||||
initialize(context_ptr, status_icon_path.as_ptr(), show_icon);
|
||||
}
|
||||
|
@ -155,8 +161,13 @@ impl super::Context for MacContext {
|
|||
|
||||
// Native bridge code
|
||||
|
||||
extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, len: i32,
|
||||
event_type: i32, key_code: i32) {
|
||||
extern "C" fn keypress_callback(
|
||||
_self: *mut c_void,
|
||||
raw_buffer: *const u8,
|
||||
len: i32,
|
||||
event_type: i32,
|
||||
key_code: i32,
|
||||
) {
|
||||
unsafe {
|
||||
let _self = _self as *mut MacContext;
|
||||
|
||||
|
@ -168,7 +179,8 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, len: i32,
|
|||
return;
|
||||
}
|
||||
|
||||
if event_type == 0 { // Char event
|
||||
if event_type == 0 {
|
||||
// Char event
|
||||
// Convert the received buffer to a string
|
||||
let c_str = CStr::from_ptr(raw_buffer as (*const c_char));
|
||||
let char_str = c_str.to_str();
|
||||
|
@ -178,12 +190,13 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, len: i32,
|
|||
Ok(char_str) => {
|
||||
let event = Event::Key(KeyEvent::Char(char_str.to_owned()));
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Unable to receive char: {}", e);
|
||||
},
|
||||
}
|
||||
}else if event_type == 1 { // Modifier event
|
||||
}
|
||||
} else if event_type == 1 {
|
||||
// Modifier event
|
||||
let modifier: Option<KeyModifier> = match key_code {
|
||||
0x37 => Some(LEFT_META),
|
||||
0x36 => Some(RIGHT_META),
|
||||
|
@ -201,18 +214,20 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u8, len: i32,
|
|||
if let Some(modifier) = modifier {
|
||||
let event = Event::Key(KeyEvent::Modifier(modifier));
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}else{ // Not one of the default modifiers, send an "other" event
|
||||
} else {
|
||||
// Not one of the default modifiers, send an "other" event
|
||||
let event = Event::Key(KeyEvent::Other);
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}
|
||||
}else{ // Other type of event
|
||||
} else {
|
||||
// Other type of event
|
||||
let event = Event::Key(KeyEvent::Other);
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern fn icon_click_callback(_self: *mut c_void) {
|
||||
extern "C" fn icon_click_callback(_self: *mut c_void) {
|
||||
unsafe {
|
||||
let _self = _self as *mut MacContext;
|
||||
|
||||
|
@ -221,7 +236,7 @@ extern fn icon_click_callback(_self: *mut c_void) {
|
|||
}
|
||||
}
|
||||
|
||||
extern fn context_menu_click_callback(_self: *mut c_void, id: i32) {
|
||||
extern "C" fn context_menu_click_callback(_self: *mut c_void, id: i32) {
|
||||
unsafe {
|
||||
let _self = _self as *mut MacContext;
|
||||
|
||||
|
|
|
@ -26,13 +26,13 @@ mod linux;
|
|||
#[cfg(target_os = "macos")]
|
||||
pub(crate) mod macos;
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use crate::event::Event;
|
||||
use std::path::PathBuf;
|
||||
use std::fs::create_dir_all;
|
||||
use std::sync::{Once, Arc};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use crate::config::Configs;
|
||||
use crate::event::Event;
|
||||
use std::fs::create_dir_all;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::{Arc, Once};
|
||||
|
||||
pub trait Context {
|
||||
fn eventloop(&self);
|
||||
|
@ -40,19 +40,31 @@ pub trait Context {
|
|||
|
||||
// MAC IMPLEMENTATION
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn new(config: Configs, send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<dyn Context> {
|
||||
pub fn new(
|
||||
config: Configs,
|
||||
send_channel: Sender<Event>,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Box<dyn Context> {
|
||||
macos::MacContext::new(config, send_channel, is_injecting)
|
||||
}
|
||||
|
||||
// LINUX IMPLEMENTATION
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn new(config: Configs, send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<dyn Context> {
|
||||
pub fn new(
|
||||
config: Configs,
|
||||
send_channel: Sender<Event>,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Box<dyn Context> {
|
||||
linux::LinuxContext::new(config, send_channel, is_injecting)
|
||||
}
|
||||
|
||||
// WINDOWS IMPLEMENTATION
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn new(config: Configs, send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<dyn Context> {
|
||||
pub fn new(
|
||||
config: Configs,
|
||||
send_channel: Sender<Event>,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Box<dyn Context> {
|
||||
windows::WindowsContext::new(config, send_channel, is_injecting)
|
||||
}
|
||||
|
||||
|
@ -75,7 +87,10 @@ pub fn get_config_dir() -> PathBuf {
|
|||
if let Some(parent) = exe_dir {
|
||||
let config_dir = parent.join(".espanso");
|
||||
if config_dir.exists() {
|
||||
println!("PORTABLE MODE, using config folder: '{}'", config_dir.to_string_lossy());
|
||||
println!(
|
||||
"PORTABLE MODE, using config folder: '{}'",
|
||||
config_dir.to_string_lossy()
|
||||
);
|
||||
return config_dir;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,18 +17,18 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::sync::mpsc::Sender;
|
||||
use crate::bridge::windows::*;
|
||||
use crate::event::{Event, KeyEvent, KeyModifier, ActionType};
|
||||
use crate::event::KeyModifier::*;
|
||||
use std::ffi::c_void;
|
||||
use std::{fs};
|
||||
use widestring::{U16CString, U16CStr};
|
||||
use log::{info, error, debug};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering::Acquire;
|
||||
use crate::config::Configs;
|
||||
use crate::event::KeyModifier::*;
|
||||
use crate::event::{ActionType, Event, KeyEvent, KeyModifier};
|
||||
use log::{debug, error, info};
|
||||
use std::ffi::c_void;
|
||||
use std::fs;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::Acquire;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::Arc;
|
||||
use widestring::{U16CStr, U16CString};
|
||||
|
||||
const BMP_BINARY: &[u8] = include_bytes!("../res/win/espanso.bmp");
|
||||
const ICO_BINARY: &[u8] = include_bytes!("../res/win/espanso.ico");
|
||||
|
@ -39,31 +39,42 @@ pub struct WindowsContext {
|
|||
}
|
||||
|
||||
impl WindowsContext {
|
||||
pub fn new(config: Configs, send_channel: Sender<Event>, is_injecting: Arc<AtomicBool>) -> Box<WindowsContext> {
|
||||
pub fn new(
|
||||
config: Configs,
|
||||
send_channel: Sender<Event>,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Box<WindowsContext> {
|
||||
// Initialize image resources
|
||||
|
||||
let espanso_dir = super::get_data_dir();
|
||||
|
||||
info!("Initializing Espanso resources in {}", espanso_dir.as_path().display());
|
||||
info!(
|
||||
"Initializing Espanso resources in {}",
|
||||
espanso_dir.as_path().display()
|
||||
);
|
||||
|
||||
let espanso_bmp_image = espanso_dir.join("espansoicon.bmp");
|
||||
if espanso_bmp_image.exists() {
|
||||
info!("BMP already initialized, skipping.");
|
||||
} else {
|
||||
fs::write(&espanso_bmp_image, BMP_BINARY)
|
||||
.expect("Unable to write windows bmp file");
|
||||
fs::write(&espanso_bmp_image, BMP_BINARY).expect("Unable to write windows bmp file");
|
||||
|
||||
info!("Extracted bmp icon to: {}", espanso_bmp_image.to_str().unwrap_or("error"));
|
||||
info!(
|
||||
"Extracted bmp icon to: {}",
|
||||
espanso_bmp_image.to_str().unwrap_or("error")
|
||||
);
|
||||
}
|
||||
|
||||
let espanso_ico_image = espanso_dir.join("espanso.ico");
|
||||
if espanso_ico_image.exists() {
|
||||
info!("ICO already initialized, skipping.");
|
||||
} else {
|
||||
fs::write(&espanso_ico_image, ICO_BINARY)
|
||||
.expect("Unable to write windows ico file");
|
||||
fs::write(&espanso_ico_image, ICO_BINARY).expect("Unable to write windows ico file");
|
||||
|
||||
info!("Extracted 'ico' icon to: {}", espanso_ico_image.to_str().unwrap_or("error"));
|
||||
info!(
|
||||
"Extracted 'ico' icon to: {}",
|
||||
espanso_ico_image.to_str().unwrap_or("error")
|
||||
);
|
||||
}
|
||||
|
||||
let bmp_icon = espanso_bmp_image.to_str().unwrap_or_default();
|
||||
|
@ -87,14 +98,15 @@ impl WindowsContext {
|
|||
let ico_file_c = U16CString::from_str(ico_icon).unwrap();
|
||||
let bmp_file_c = U16CString::from_str(bmp_icon).unwrap();
|
||||
|
||||
let show_icon = if config.show_icon {
|
||||
1
|
||||
}else{
|
||||
0
|
||||
};
|
||||
let show_icon = if config.show_icon { 1 } else { 0 };
|
||||
|
||||
// Initialize the windows
|
||||
let res = initialize(context_ptr, ico_file_c.as_ptr(), bmp_file_c.as_ptr(), show_icon);
|
||||
let res = initialize(
|
||||
context_ptr,
|
||||
ico_file_c.as_ptr(),
|
||||
bmp_file_c.as_ptr(),
|
||||
show_icon,
|
||||
);
|
||||
if res != 1 {
|
||||
panic!("Can't initialize Windows context")
|
||||
}
|
||||
|
@ -114,8 +126,15 @@ impl super::Context for WindowsContext {
|
|||
|
||||
// Native bridge code
|
||||
|
||||
extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32,
|
||||
event_type: i32, key_code: i32, variant: i32, is_key_down: i32) {
|
||||
extern "C" fn keypress_callback(
|
||||
_self: *mut c_void,
|
||||
raw_buffer: *const u16,
|
||||
len: i32,
|
||||
event_type: i32,
|
||||
key_code: i32,
|
||||
variant: i32,
|
||||
is_key_down: i32,
|
||||
) {
|
||||
unsafe {
|
||||
let _self = _self as *mut WindowsContext;
|
||||
|
||||
|
@ -127,8 +146,10 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32
|
|||
return;
|
||||
}
|
||||
|
||||
if event_type == 0 { // Char event
|
||||
if is_key_down != 0 { // KEY DOWN EVENT
|
||||
if event_type == 0 {
|
||||
// Char event
|
||||
if is_key_down != 0 {
|
||||
// KEY DOWN EVENT
|
||||
// Convert the received buffer to a string
|
||||
let buffer = std::slice::from_raw_parts(raw_buffer, len as usize);
|
||||
let c_string = U16CStr::from_slice_with_nul(buffer);
|
||||
|
@ -141,17 +162,19 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32
|
|||
Ok(string) => {
|
||||
let event = Event::Key(KeyEvent::Char(string));
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Unable to receive char: {}", e);
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!("unable to decode widechar");
|
||||
}
|
||||
}
|
||||
}else if event_type == 1 { // Modifier event
|
||||
if is_key_down == 1 { // Keyup event
|
||||
} else if event_type == 1 {
|
||||
// Modifier event
|
||||
if is_key_down == 1 {
|
||||
// Keyup event
|
||||
let modifier: Option<KeyModifier> = match (key_code, variant) {
|
||||
(0x5B, _) => Some(LEFT_META),
|
||||
(0x5C, _) => Some(RIGHT_META),
|
||||
|
@ -169,7 +192,8 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32
|
|||
if let Some(modifier) = modifier {
|
||||
let event = Event::Key(KeyEvent::Modifier(modifier));
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}else{ // Not one of the default modifiers, send an "other" event
|
||||
} else {
|
||||
// Not one of the default modifiers, send an "other" event
|
||||
let event = Event::Key(KeyEvent::Other);
|
||||
(*_self).send_channel.send(event).unwrap();
|
||||
}
|
||||
|
@ -182,7 +206,7 @@ extern fn keypress_callback(_self: *mut c_void, raw_buffer: *const u16, len: i32
|
|||
}
|
||||
}
|
||||
|
||||
extern fn icon_click_callback(_self: *mut c_void) {
|
||||
extern "C" fn icon_click_callback(_self: *mut c_void) {
|
||||
unsafe {
|
||||
let _self = _self as *mut WindowsContext;
|
||||
|
||||
|
@ -191,8 +215,7 @@ extern fn icon_click_callback(_self: *mut c_void) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
extern fn context_menu_click_callback(_self: *mut c_void, id: i32) {
|
||||
extern "C" fn context_menu_click_callback(_self: *mut c_void, id: i32) {
|
||||
unsafe {
|
||||
let _self = _self as *mut WindowsContext;
|
||||
|
||||
|
|
17
src/edit.rs
17
src/edit.rs
|
@ -20,11 +20,17 @@
|
|||
use std::path::Path;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn default_editor() -> String{ "/bin/nano".to_owned() }
|
||||
fn default_editor() -> String {
|
||||
"/bin/nano".to_owned()
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
fn default_editor() -> String{ "/usr/bin/nano".to_owned() }
|
||||
fn default_editor() -> String {
|
||||
"/usr/bin/nano".to_owned()
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
fn default_editor() -> String{ "C:\\Windows\\System32\\notepad.exe".to_owned() }
|
||||
fn default_editor() -> String {
|
||||
"C:\\Windows\\System32\\notepad.exe".to_owned()
|
||||
}
|
||||
|
||||
pub fn open_editor(file_path: &Path) -> bool {
|
||||
use std::process::Command;
|
||||
|
@ -44,9 +50,7 @@ pub fn open_editor(file_path: &Path) -> bool {
|
|||
|
||||
// Start the editor and wait for its termination
|
||||
let status = if cfg!(target_os = "windows") {
|
||||
Command::new(&editor)
|
||||
.arg(file_path)
|
||||
.spawn()
|
||||
Command::new(&editor).arg(file_path).spawn()
|
||||
} else {
|
||||
// On Unix, spawn the editor using the shell so that it can
|
||||
// accept parameters. See issue #245
|
||||
|
@ -56,7 +60,6 @@ pub fn open_editor(file_path: &Path) -> bool {
|
|||
.spawn()
|
||||
};
|
||||
|
||||
|
||||
if let Ok(mut child) = status {
|
||||
// Wait for the user to edit the configuration
|
||||
let result = child.wait();
|
||||
|
|
163
src/engine.rs
163
src/engine.rs
|
@ -17,25 +17,31 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::matcher::{Match, MatchReceiver};
|
||||
use crate::keyboard::KeyboardManager;
|
||||
use crate::config::ConfigManager;
|
||||
use crate::config::BackendType;
|
||||
use crate::clipboard::ClipboardManager;
|
||||
use log::{info, warn, debug, error};
|
||||
use crate::ui::{UIManager, MenuItem, MenuItemType};
|
||||
use crate::event::{ActionEventReceiver, ActionType, SystemEventReceiver, SystemEvent};
|
||||
use crate::render::{Renderer, RenderResult};
|
||||
use crate::config::BackendType;
|
||||
use crate::config::ConfigManager;
|
||||
use crate::event::{ActionEventReceiver, ActionType, SystemEvent, SystemEventReceiver};
|
||||
use crate::keyboard::KeyboardManager;
|
||||
use crate::matcher::{Match, MatchReceiver};
|
||||
use crate::protocol::{send_command_or_warn, IPCCommand, Service};
|
||||
use crate::render::{RenderResult, Renderer};
|
||||
use crate::ui::{MenuItem, MenuItemType, UIManager};
|
||||
use log::{debug, error, info, warn};
|
||||
use regex::Regex;
|
||||
use std::cell::RefCell;
|
||||
use std::process::exit;
|
||||
use regex::{Regex};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::Release;
|
||||
use crate::protocol::{Service, IPCCommand, send_command_or_warn};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Engine<'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>,
|
||||
U: UIManager, R: Renderer> {
|
||||
pub struct Engine<
|
||||
'a,
|
||||
S: KeyboardManager,
|
||||
C: ClipboardManager,
|
||||
M: ConfigManager<'a>,
|
||||
U: UIManager,
|
||||
R: Renderer,
|
||||
> {
|
||||
keyboard_manager: &'a S,
|
||||
clipboard_manager: &'a C,
|
||||
config_manager: &'a M,
|
||||
|
@ -46,14 +52,27 @@ pub struct Engine<'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<
|
|||
enabled: RefCell<bool>,
|
||||
}
|
||||
|
||||
impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIManager, R: Renderer>
|
||||
Engine<'a, S, C, M, U, R> {
|
||||
pub fn new(keyboard_manager: &'a S, clipboard_manager: &'a C,
|
||||
config_manager: &'a M, ui_manager: &'a U,
|
||||
renderer: &'a R, is_injecting: Arc<AtomicBool>) -> Engine<'a, S, C, M, U, R> {
|
||||
impl<
|
||||
'a,
|
||||
S: KeyboardManager,
|
||||
C: ClipboardManager,
|
||||
M: ConfigManager<'a>,
|
||||
U: UIManager,
|
||||
R: Renderer,
|
||||
> Engine<'a, S, C, M, U, R>
|
||||
{
|
||||
pub fn new(
|
||||
keyboard_manager: &'a S,
|
||||
clipboard_manager: &'a C,
|
||||
config_manager: &'a M,
|
||||
ui_manager: &'a U,
|
||||
renderer: &'a R,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
) -> Engine<'a, S, C, M, U, R> {
|
||||
let enabled = RefCell::new(true);
|
||||
|
||||
Engine{keyboard_manager,
|
||||
Engine {
|
||||
keyboard_manager,
|
||||
clipboard_manager,
|
||||
config_manager,
|
||||
ui_manager,
|
||||
|
@ -67,11 +86,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
let mut menu = Vec::new();
|
||||
|
||||
let enabled = self.enabled.borrow();
|
||||
let toggle_text = if *enabled {
|
||||
"Disable"
|
||||
}else{
|
||||
"Enable"
|
||||
}.to_owned();
|
||||
let toggle_text = if *enabled { "Disable" } else { "Enable" }.to_owned();
|
||||
menu.push(MenuItem {
|
||||
item_type: MenuItemType::Button,
|
||||
item_name: toggle_text,
|
||||
|
@ -110,8 +125,8 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
// clipboard content in order to restore it later.
|
||||
if self.config_manager.default_config().preserve_clipboard {
|
||||
match self.clipboard_manager.get_clipboard() {
|
||||
Some(clipboard) => {Some(clipboard)},
|
||||
None => {None},
|
||||
Some(clipboard) => Some(clipboard),
|
||||
None => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
|
@ -123,9 +138,15 @@ lazy_static! {
|
|||
static ref VAR_REGEX: Regex = Regex::new("\\{\\{\\s*(?P<name>\\w+)\\s*\\}\\}").unwrap();
|
||||
}
|
||||
|
||||
impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIManager, R: Renderer>
|
||||
MatchReceiver for Engine<'a, S, C, M, U, R>{
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: KeyboardManager,
|
||||
C: ClipboardManager,
|
||||
M: ConfigManager<'a>,
|
||||
U: UIManager,
|
||||
R: Renderer,
|
||||
> MatchReceiver for Engine<'a, S, C, M, U, R>
|
||||
{
|
||||
fn on_match(&self, m: &Match, trailing_separator: Option<char>, trigger_offset: usize) {
|
||||
let config = self.config_manager.active_config();
|
||||
|
||||
|
@ -146,13 +167,16 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
|
||||
let mut previous_clipboard_content: Option<String> = None;
|
||||
|
||||
let rendered = self.renderer.render_match(m, trigger_offset, config, vec![]);
|
||||
let rendered = self
|
||||
.renderer
|
||||
.render_match(m, trigger_offset, config, vec![]);
|
||||
|
||||
match rendered {
|
||||
RenderResult::Text(mut target_string) => {
|
||||
// If a trailing separator was counted in the match, add it back to the target string
|
||||
if let Some(trailing_separator) = trailing_separator {
|
||||
if trailing_separator == '\r' { // If the trailing separator is a carriage return,
|
||||
if trailing_separator == '\r' {
|
||||
// If the trailing separator is a carriage return,
|
||||
target_string.push('\n'); // convert it to new line
|
||||
} else {
|
||||
target_string.push(trailing_separator);
|
||||
|
@ -187,7 +211,9 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
if cfg!(target_os = "linux") {
|
||||
let all_ascii = target_string.chars().all(|c| c.is_ascii());
|
||||
if all_ascii {
|
||||
debug!("All elements of the replacement are ascii, using Inject backend");
|
||||
debug!(
|
||||
"All elements of the replacement are ascii, using Inject backend"
|
||||
);
|
||||
&BackendType::Inject
|
||||
} else {
|
||||
debug!("There are non-ascii characters, using Clipboard backend");
|
||||
|
@ -212,15 +238,16 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
|
||||
self.keyboard_manager.send_string(&config, split);
|
||||
}
|
||||
},
|
||||
}
|
||||
BackendType::Clipboard => {
|
||||
// If the preserve_clipboard option is enabled, save the current
|
||||
// clipboard content to restore it later.
|
||||
previous_clipboard_content = self.return_content_if_preserve_clipboard_is_enabled();
|
||||
previous_clipboard_content =
|
||||
self.return_content_if_preserve_clipboard_is_enabled();
|
||||
|
||||
self.clipboard_manager.set_clipboard(&target_string);
|
||||
self.keyboard_manager.trigger_paste(&config);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
error!("Unsupported backend type evaluation.");
|
||||
return;
|
||||
|
@ -231,7 +258,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
// Simulate left arrow key presses to bring the cursor into the desired position
|
||||
self.keyboard_manager.move_cursor_left(&config, moves);
|
||||
}
|
||||
},
|
||||
}
|
||||
RenderResult::Image(image_path) => {
|
||||
// If the preserve_clipboard option is enabled, save the current
|
||||
// clipboard content to restore it later.
|
||||
|
@ -239,19 +266,22 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
|
||||
self.clipboard_manager.set_clipboard_image(&image_path);
|
||||
self.keyboard_manager.trigger_paste(&config);
|
||||
},
|
||||
}
|
||||
RenderResult::Error => {
|
||||
error!("Could not render match: {}", m.triggers[trigger_offset]);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Restore previous clipboard content
|
||||
if let Some(previous_clipboard_content) = previous_clipboard_content {
|
||||
// Sometimes an expansion gets overwritten before pasting by the previous content
|
||||
// A delay is needed to mitigate the problem
|
||||
std::thread::sleep(std::time::Duration::from_millis(config.restore_clipboard_delay as u64));
|
||||
std::thread::sleep(std::time::Duration::from_millis(
|
||||
config.restore_clipboard_delay as u64,
|
||||
));
|
||||
|
||||
self.clipboard_manager.set_clipboard(&previous_clipboard_content);
|
||||
self.clipboard_manager
|
||||
.set_clipboard(&previous_clipboard_content);
|
||||
}
|
||||
|
||||
// Re-allow espanso to interpret actions
|
||||
|
@ -316,8 +346,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
} else {
|
||||
info!("Passive mode activated");
|
||||
|
||||
let rendered = self.renderer.render_passive(&clipboard,
|
||||
&config);
|
||||
let rendered = self.renderer.render_passive(&clipboard, &config);
|
||||
|
||||
match rendered {
|
||||
RenderResult::Text(payload) => {
|
||||
|
@ -326,10 +355,8 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
|
||||
std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding
|
||||
self.keyboard_manager.trigger_paste(&config);
|
||||
},
|
||||
_ => {
|
||||
warn!("Cannot expand passive match")
|
||||
},
|
||||
}
|
||||
_ => warn!("Cannot expand passive match"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -341,34 +368,50 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
|
|||
}
|
||||
}
|
||||
|
||||
impl <'a, S: KeyboardManager, C: ClipboardManager,
|
||||
M: ConfigManager<'a>, U: UIManager, R: Renderer> ActionEventReceiver for Engine<'a, S, C, M, U, R>{
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: KeyboardManager,
|
||||
C: ClipboardManager,
|
||||
M: ConfigManager<'a>,
|
||||
U: UIManager,
|
||||
R: Renderer,
|
||||
> ActionEventReceiver for Engine<'a, S, C, M, U, R>
|
||||
{
|
||||
fn on_action_event(&self, e: ActionType) {
|
||||
let config = self.config_manager.default_config();
|
||||
match e {
|
||||
ActionType::IconClick => {
|
||||
self.ui_manager.show_menu(self.build_menu());
|
||||
},
|
||||
}
|
||||
ActionType::ExitWorker => {
|
||||
info!("terminating worker process");
|
||||
self.ui_manager.cleanup();
|
||||
exit(0);
|
||||
},
|
||||
}
|
||||
ActionType::Exit => {
|
||||
send_command_or_warn(Service::Daemon, config.clone(), IPCCommand::exit());
|
||||
},
|
||||
}
|
||||
ActionType::RestartWorker => {
|
||||
send_command_or_warn(Service::Daemon, config.clone(), IPCCommand::restart_worker());
|
||||
},
|
||||
send_command_or_warn(
|
||||
Service::Daemon,
|
||||
config.clone(),
|
||||
IPCCommand::restart_worker(),
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a, S: KeyboardManager, C: ClipboardManager,
|
||||
M: ConfigManager<'a>, U: UIManager, R: Renderer> SystemEventReceiver for Engine<'a, S, C, M, U, R>{
|
||||
|
||||
impl<
|
||||
'a,
|
||||
S: KeyboardManager,
|
||||
C: ClipboardManager,
|
||||
M: ConfigManager<'a>,
|
||||
U: UIManager,
|
||||
R: Renderer,
|
||||
> SystemEventReceiver for Engine<'a, S, C, M, U, R>
|
||||
{
|
||||
fn on_system_event(&self, e: SystemEvent) {
|
||||
match e {
|
||||
// MacOS specific
|
||||
|
@ -379,16 +422,16 @@ impl <'a, S: KeyboardManager, C: ClipboardManager,
|
|||
if config.secure_input_notification && config.show_notifications {
|
||||
self.ui_manager.notify_delay(&format!("{} has activated SecureInput. Espanso won't work until you disable it.", app_name), 5000);
|
||||
}
|
||||
},
|
||||
}
|
||||
SystemEvent::SecureInputDisabled => {
|
||||
info!("SecureInput has been disabled.");
|
||||
},
|
||||
}
|
||||
SystemEvent::NotifyRequest(message) => {
|
||||
let config = self.config_manager.default_config();
|
||||
if config.show_notifications {
|
||||
self.ui_manager.notify(&message);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,7 +17,7 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::event::{KeyEventReceiver, ActionEventReceiver, Event, SystemEventReceiver};
|
||||
use crate::event::{ActionEventReceiver, Event, KeyEventReceiver, SystemEventReceiver};
|
||||
use std::sync::mpsc::Receiver;
|
||||
|
||||
pub trait EventManager {
|
||||
|
@ -32,14 +32,17 @@ pub struct DefaultEventManager<'a> {
|
|||
}
|
||||
|
||||
impl<'a> DefaultEventManager<'a> {
|
||||
pub fn new(receive_channel: Receiver<Event>, key_receivers: Vec<&'a dyn KeyEventReceiver>,
|
||||
pub fn new(
|
||||
receive_channel: Receiver<Event>,
|
||||
key_receivers: Vec<&'a dyn KeyEventReceiver>,
|
||||
action_receivers: Vec<&'a dyn ActionEventReceiver>,
|
||||
system_receivers: Vec<&'a dyn SystemEventReceiver>) -> DefaultEventManager<'a> {
|
||||
system_receivers: Vec<&'a dyn SystemEventReceiver>,
|
||||
) -> DefaultEventManager<'a> {
|
||||
DefaultEventManager {
|
||||
receive_channel,
|
||||
key_receivers,
|
||||
action_receivers,
|
||||
system_receivers
|
||||
system_receivers,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -48,17 +51,21 @@ impl <'a> EventManager for DefaultEventManager<'a> {
|
|||
fn eventloop(&self) {
|
||||
loop {
|
||||
match self.receive_channel.recv() {
|
||||
Ok(event) => {
|
||||
match event {
|
||||
Ok(event) => match event {
|
||||
Event::Key(key_event) => {
|
||||
self.key_receivers.iter().for_each(move |&receiver| receiver.on_key_event(key_event.clone()));
|
||||
},
|
||||
self.key_receivers
|
||||
.iter()
|
||||
.for_each(move |&receiver| receiver.on_key_event(key_event.clone()));
|
||||
}
|
||||
Event::Action(action_event) => {
|
||||
self.action_receivers.iter().for_each(|&receiver| receiver.on_action_event(action_event.clone()));
|
||||
self.action_receivers
|
||||
.iter()
|
||||
.for_each(|&receiver| receiver.on_action_event(action_event.clone()));
|
||||
}
|
||||
Event::System(system_event) => {
|
||||
self.system_receivers.iter().for_each(move |&receiver| receiver.on_system_event(system_event.clone()));
|
||||
}
|
||||
self.system_receivers.iter().for_each(move |&receiver| {
|
||||
receiver.on_system_event(system_event.clone())
|
||||
});
|
||||
}
|
||||
},
|
||||
Err(e) => panic!("Broken event channel {}", e),
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
pub(crate) mod manager;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -60,7 +60,7 @@ impl From<i32> for ActionType {
|
|||
pub enum KeyEvent {
|
||||
Char(String),
|
||||
Modifier(KeyModifier),
|
||||
Other
|
||||
Other,
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
|
@ -99,44 +99,24 @@ impl KeyModifier {
|
|||
match config {
|
||||
KeyModifier::CTRL => {
|
||||
current == &LEFT_CTRL || current == &RIGHT_CTRL || current == &CTRL
|
||||
},
|
||||
}
|
||||
KeyModifier::SHIFT => {
|
||||
current == &LEFT_SHIFT || current == &RIGHT_SHIFT || current == &SHIFT
|
||||
},
|
||||
KeyModifier::ALT => {
|
||||
current == &LEFT_ALT || current == &RIGHT_ALT || current == &ALT
|
||||
},
|
||||
}
|
||||
KeyModifier::ALT => current == &LEFT_ALT || current == &RIGHT_ALT || current == &ALT,
|
||||
KeyModifier::META => {
|
||||
current == &LEFT_META || current == &RIGHT_META || current == &META
|
||||
},
|
||||
KeyModifier::BACKSPACE => {
|
||||
current == &BACKSPACE
|
||||
},
|
||||
KeyModifier::LEFT_CTRL => {
|
||||
current == &LEFT_CTRL
|
||||
},
|
||||
KeyModifier::RIGHT_CTRL => {
|
||||
current == &RIGHT_CTRL
|
||||
},
|
||||
KeyModifier::LEFT_ALT => {
|
||||
current == &LEFT_ALT
|
||||
},
|
||||
KeyModifier::RIGHT_ALT => {
|
||||
current == &RIGHT_ALT
|
||||
},
|
||||
KeyModifier::LEFT_META => {
|
||||
current == &LEFT_META
|
||||
},
|
||||
KeyModifier::RIGHT_META => {
|
||||
current == &RIGHT_META
|
||||
},
|
||||
KeyModifier::LEFT_SHIFT => {
|
||||
current == &LEFT_SHIFT
|
||||
},
|
||||
KeyModifier::RIGHT_SHIFT => {
|
||||
current == &RIGHT_SHIFT
|
||||
},
|
||||
_ => {false},
|
||||
}
|
||||
KeyModifier::BACKSPACE => current == &BACKSPACE,
|
||||
KeyModifier::LEFT_CTRL => current == &LEFT_CTRL,
|
||||
KeyModifier::RIGHT_CTRL => current == &RIGHT_CTRL,
|
||||
KeyModifier::LEFT_ALT => current == &LEFT_ALT,
|
||||
KeyModifier::RIGHT_ALT => current == &RIGHT_ALT,
|
||||
KeyModifier::LEFT_META => current == &LEFT_META,
|
||||
KeyModifier::RIGHT_META => current == &RIGHT_META,
|
||||
KeyModifier::LEFT_SHIFT => current == &LEFT_SHIFT,
|
||||
KeyModifier::RIGHT_SHIFT => current == &RIGHT_SHIFT,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +129,7 @@ pub enum SystemEvent {
|
|||
SecureInputDisabled,
|
||||
|
||||
// Notification
|
||||
NotifyRequest(String)
|
||||
NotifyRequest(String),
|
||||
}
|
||||
|
||||
// Receivers
|
||||
|
@ -170,8 +150,8 @@ pub trait SystemEventReceiver {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::KeyModifier::*;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_shallow_equals_ctrl() {
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde_yaml::{Mapping};
|
||||
use crate::clipboard::ClipboardManager;
|
||||
use serde_yaml::Mapping;
|
||||
|
||||
pub struct ClipboardExtension {
|
||||
clipboard_manager: Box<dyn ClipboardManager>,
|
||||
|
@ -26,9 +26,7 @@ pub struct ClipboardExtension {
|
|||
|
||||
impl ClipboardExtension {
|
||||
pub fn new(clipboard_manager: Box<dyn ClipboardManager>) -> ClipboardExtension {
|
||||
ClipboardExtension{
|
||||
clipboard_manager
|
||||
}
|
||||
ClipboardExtension { clipboard_manager }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use chrono::{DateTime, Local};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
|
||||
pub struct DateExtension {}
|
||||
|
||||
|
|
|
@ -17,15 +17,15 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde_yaml::Mapping;
|
||||
use crate::clipboard::ClipboardManager;
|
||||
use serde_yaml::Mapping;
|
||||
|
||||
mod date;
|
||||
mod shell;
|
||||
mod script;
|
||||
mod random;
|
||||
mod clipboard;
|
||||
mod date;
|
||||
pub mod dummy;
|
||||
mod random;
|
||||
mod script;
|
||||
mod shell;
|
||||
|
||||
pub trait Extension {
|
||||
fn name(&self) -> String;
|
||||
|
|
|
@ -17,9 +17,9 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use log::{error, warn};
|
||||
use rand::seq::SliceRandom;
|
||||
use log::{warn, error};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
|
||||
pub struct RandomExtension {}
|
||||
|
||||
|
@ -38,13 +38,14 @@ impl super::Extension for RandomExtension {
|
|||
let choices = params.get(&Value::from("choices"));
|
||||
if choices.is_none() {
|
||||
warn!("No 'choices' parameter specified for random variable");
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
let choices = choices.unwrap().as_sequence();
|
||||
if let Some(choices) = choices {
|
||||
let str_choices = choices.iter().map(|arg| {
|
||||
arg.as_str().unwrap_or_default().to_string()
|
||||
}).collect::<Vec<String>>();
|
||||
let str_choices = choices
|
||||
.iter()
|
||||
.map(|arg| arg.as_str().unwrap_or_default().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
// Select a random choice between the possibilities
|
||||
let choice = str_choices.choose(&mut rand::thread_rng());
|
||||
|
@ -54,14 +55,13 @@ impl super::Extension for RandomExtension {
|
|||
// Render arguments
|
||||
let output = crate::render::utils::render_args(output, args);
|
||||
|
||||
return Some(output)
|
||||
},
|
||||
return Some(output);
|
||||
}
|
||||
None => {
|
||||
error!("Could not select a random choice.");
|
||||
return None
|
||||
},
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
error!("choices array have an invalid format '{:?}'", choices);
|
||||
|
@ -77,11 +77,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_random_basic() {
|
||||
let mut params = Mapping::new();
|
||||
let choices = vec!(
|
||||
"first",
|
||||
"second",
|
||||
"third",
|
||||
);
|
||||
let choices = vec!["first", "second", "third"];
|
||||
params.insert(Value::from("choices"), Value::from(choices.clone()));
|
||||
|
||||
let extension = RandomExtension::new();
|
||||
|
@ -97,11 +93,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_random_with_args() {
|
||||
let mut params = Mapping::new();
|
||||
let choices = vec!(
|
||||
"first $0$",
|
||||
"second $0$",
|
||||
"$0$ third",
|
||||
);
|
||||
let choices = vec!["first $0$", "second $0$", "$0$ third"];
|
||||
params.insert(Value::from("choices"), Value::from(choices.clone()));
|
||||
|
||||
let extension = RandomExtension::new();
|
||||
|
@ -111,11 +103,7 @@ mod tests {
|
|||
|
||||
let output = output.unwrap();
|
||||
|
||||
let rendered_choices = vec!(
|
||||
"first test",
|
||||
"second test",
|
||||
"test third",
|
||||
);
|
||||
let rendered_choices = vec!["first test", "second test", "test third"];
|
||||
|
||||
assert!(rendered_choices.iter().any(|x| x == &output));
|
||||
}
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use log::{error, warn};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use std::process::Command;
|
||||
use log::{warn, error};
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
pub struct ScriptExtension {}
|
||||
|
||||
|
@ -39,17 +39,21 @@ impl super::Extension for ScriptExtension {
|
|||
let args = params.get(&Value::from("args"));
|
||||
if args.is_none() {
|
||||
warn!("No 'args' parameter specified for script variable");
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
let args = args.unwrap().as_sequence();
|
||||
if let Some(args) = args {
|
||||
let mut str_args = args.iter().map(|arg| {
|
||||
arg.as_str().unwrap_or_default().to_string()
|
||||
}).collect::<Vec<String>>();
|
||||
let mut str_args = args
|
||||
.iter()
|
||||
.map(|arg| arg.as_str().unwrap_or_default().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
// The user has to enable argument concatenation explicitly
|
||||
let inject_args = params.get(&Value::from("inject_args"))
|
||||
.unwrap_or(&Value::from(false)).as_bool().unwrap_or(false);
|
||||
let inject_args = params
|
||||
.get(&Value::from("inject_args"))
|
||||
.unwrap_or(&Value::from(false))
|
||||
.as_bool()
|
||||
.unwrap_or(false);
|
||||
if inject_args {
|
||||
str_args.extend(user_args.clone());
|
||||
}
|
||||
|
@ -71,17 +75,12 @@ impl super::Extension for ScriptExtension {
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
let output = if str_args.len() > 1 {
|
||||
Command::new(&str_args[0])
|
||||
.args(&str_args[1..])
|
||||
.output()
|
||||
Command::new(&str_args[0]).args(&str_args[1..]).output()
|
||||
} else {
|
||||
Command::new(&str_args[0])
|
||||
.output()
|
||||
Command::new(&str_args[0]).output()
|
||||
};
|
||||
|
||||
|
||||
match output {
|
||||
Ok(output) => {
|
||||
let output_str = String::from_utf8_lossy(output.stdout.as_slice());
|
||||
|
@ -94,12 +93,12 @@ impl super::Extension for ScriptExtension {
|
|||
warn!("Script command reported error: \n{}", error_str);
|
||||
}
|
||||
|
||||
return Some(output_str.into_owned())
|
||||
},
|
||||
return Some(output_str.into_owned());
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Could not execute script '{:?}', error: {}", args, e);
|
||||
return None
|
||||
},
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +116,10 @@ mod tests {
|
|||
#[cfg(not(target_os = "windows"))]
|
||||
fn test_script_basic() {
|
||||
let mut params = Mapping::new();
|
||||
params.insert(Value::from("args"), Value::from(vec!["echo", "hello world"]));
|
||||
params.insert(
|
||||
Value::from("args"),
|
||||
Value::from(vec!["echo", "hello world"]),
|
||||
);
|
||||
|
||||
let extension = ScriptExtension::new();
|
||||
let output = extension.calculate(¶ms, &vec![]);
|
||||
|
@ -130,7 +132,10 @@ mod tests {
|
|||
#[cfg(not(target_os = "windows"))]
|
||||
fn test_script_inject_args_off() {
|
||||
let mut params = Mapping::new();
|
||||
params.insert(Value::from("args"), Value::from(vec!["echo", "hello world"]));
|
||||
params.insert(
|
||||
Value::from("args"),
|
||||
Value::from(vec!["echo", "hello world"]),
|
||||
);
|
||||
|
||||
let extension = ScriptExtension::new();
|
||||
let output = extension.calculate(¶ms, &vec!["jon".to_owned()]);
|
||||
|
@ -143,7 +148,10 @@ mod tests {
|
|||
#[cfg(not(target_os = "windows"))]
|
||||
fn test_script_inject_args_on() {
|
||||
let mut params = Mapping::new();
|
||||
params.insert(Value::from("args"), Value::from(vec!["echo", "hello world"]));
|
||||
params.insert(
|
||||
Value::from("args"),
|
||||
Value::from(vec!["echo", "hello world"]),
|
||||
);
|
||||
params.insert(Value::from("inject_args"), Value::from(true));
|
||||
|
||||
let extension = ScriptExtension::new();
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use log::{error, warn};
|
||||
use regex::{Captures, Regex};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use std::process::{Command, Output};
|
||||
use log::{warn, error};
|
||||
use regex::{Regex, Captures};
|
||||
|
||||
lazy_static! {
|
||||
static ref POS_ARG_REGEX: Regex = if cfg!(target_os = "windows") {
|
||||
|
@ -41,31 +41,13 @@ pub enum Shell {
|
|||
impl Shell {
|
||||
fn execute_cmd(&self, cmd: &str) -> std::io::Result<Output> {
|
||||
match self {
|
||||
Shell::Cmd => {
|
||||
Command::new("cmd")
|
||||
.args(&["/C", &cmd])
|
||||
.output()
|
||||
},
|
||||
Shell::Powershell => {
|
||||
Command::new("powershell")
|
||||
Shell::Cmd => Command::new("cmd").args(&["/C", &cmd]).output(),
|
||||
Shell::Powershell => Command::new("powershell")
|
||||
.args(&["-Command", &cmd])
|
||||
.output()
|
||||
},
|
||||
Shell::WSL => {
|
||||
Command::new("wsl")
|
||||
.args(&["bash", "-c", &cmd])
|
||||
.output()
|
||||
},
|
||||
Shell::Bash => {
|
||||
Command::new("bash")
|
||||
.args(&["-c", &cmd])
|
||||
.output()
|
||||
},
|
||||
Shell::Sh => {
|
||||
Command::new("sh")
|
||||
.args(&["-c", &cmd])
|
||||
.output()
|
||||
},
|
||||
.output(),
|
||||
Shell::WSL => Command::new("wsl").args(&["bash", "-c", &cmd]).output(),
|
||||
Shell::Bash => Command::new("bash").args(&["-c", &cmd]).output(),
|
||||
Shell::Sh => Command::new("sh").args(&["-c", &cmd]).output(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,7 +58,7 @@ impl Shell {
|
|||
"wsl" => Some(Shell::WSL),
|
||||
"bash" => Some(Shell::Bash),
|
||||
"sh" => Some(Shell::Sh),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -112,12 +94,13 @@ impl super::Extension for ShellExtension {
|
|||
let cmd = params.get(&Value::from("cmd"));
|
||||
if cmd.is_none() {
|
||||
warn!("No 'cmd' parameter specified for shell variable");
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
let cmd = cmd.unwrap().as_str().unwrap();
|
||||
|
||||
// Render positional parameters in args
|
||||
let cmd = POS_ARG_REGEX.replace_all(&cmd, |caps: &Captures| {
|
||||
let cmd = POS_ARG_REGEX
|
||||
.replace_all(&cmd, |caps: &Captures| {
|
||||
let position_str = caps.name("pos").unwrap().as_str();
|
||||
let position = position_str.parse::<i32>().unwrap_or(-1);
|
||||
if position >= 0 && position < args.len() as i32 {
|
||||
|
@ -125,7 +108,8 @@ impl super::Extension for ShellExtension {
|
|||
} else {
|
||||
"".to_owned()
|
||||
}
|
||||
}).to_string();
|
||||
})
|
||||
.to_string();
|
||||
|
||||
let shell_param = params.get(&Value::from("shell"));
|
||||
let shell = if let Some(shell_param) = shell_param {
|
||||
|
@ -169,11 +153,11 @@ impl super::Extension for ShellExtension {
|
|||
}
|
||||
|
||||
Some(output_str)
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Could not execute cmd '{}', error: {}", cmd, e);
|
||||
None
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -216,7 +200,10 @@ mod tests {
|
|||
#[test]
|
||||
fn test_shell_trimmed_2() {
|
||||
let mut params = Mapping::new();
|
||||
params.insert(Value::from("cmd"), Value::from("echo \" hello world \""));
|
||||
params.insert(
|
||||
Value::from("cmd"),
|
||||
Value::from("echo \" hello world \""),
|
||||
);
|
||||
|
||||
params.insert(Value::from("trim"), Value::from(true));
|
||||
|
||||
|
|
|
@ -17,14 +17,13 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::ffi::CString;
|
||||
use crate::bridge::linux::*;
|
||||
use super::PasteShortcut;
|
||||
use log::error;
|
||||
use crate::bridge::linux::*;
|
||||
use crate::config::Configs;
|
||||
use log::error;
|
||||
use std::ffi::CString;
|
||||
|
||||
pub struct LinuxKeyboardManager {
|
||||
}
|
||||
pub struct LinuxKeyboardManager {}
|
||||
|
||||
impl super::KeyboardManager for LinuxKeyboardManager {
|
||||
fn send_string(&self, active_config: &Configs, s: &str) {
|
||||
|
@ -36,8 +35,8 @@ impl super::KeyboardManager for LinuxKeyboardManager {
|
|||
} else {
|
||||
send_string(cstr.as_ptr());
|
||||
}
|
||||
}
|
||||
Err(e) => panic!(e.to_string())
|
||||
},
|
||||
Err(e) => panic!(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,21 +17,22 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::ffi::CString;
|
||||
use crate::bridge::macos::*;
|
||||
use super::PasteShortcut;
|
||||
use log::error;
|
||||
use crate::bridge::macos::*;
|
||||
use crate::config::Configs;
|
||||
use log::error;
|
||||
use std::ffi::CString;
|
||||
|
||||
pub struct MacKeyboardManager {
|
||||
}
|
||||
pub struct MacKeyboardManager {}
|
||||
|
||||
impl super::KeyboardManager for MacKeyboardManager {
|
||||
fn send_string(&self, _: &Configs, s: &str) {
|
||||
let res = CString::new(s);
|
||||
match res {
|
||||
Ok(cstr) => unsafe { send_string(cstr.as_ptr()); }
|
||||
Err(e) => panic!(e.to_string())
|
||||
Ok(cstr) => unsafe {
|
||||
send_string(cstr.as_ptr());
|
||||
},
|
||||
Err(e) => panic!(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use crate::config::Configs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
|
|
|
@ -17,28 +17,24 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use widestring::{U16CString};
|
||||
use crate::bridge::windows::*;
|
||||
use super::PasteShortcut;
|
||||
use log::error;
|
||||
use crate::bridge::windows::*;
|
||||
use crate::config::Configs;
|
||||
use log::error;
|
||||
use widestring::U16CString;
|
||||
|
||||
pub struct WindowsKeyboardManager {
|
||||
}
|
||||
pub struct WindowsKeyboardManager {}
|
||||
|
||||
impl super::KeyboardManager for WindowsKeyboardManager {
|
||||
fn send_string(&self, _: &Configs, s: &str) {
|
||||
let res = U16CString::from_str(s);
|
||||
match res {
|
||||
Ok(s) => {
|
||||
unsafe {
|
||||
Ok(s) => unsafe {
|
||||
send_string(s.as_ptr());
|
||||
},
|
||||
Err(e) => println!("Error while sending string: {}", e.to_string()),
|
||||
}
|
||||
}
|
||||
Err(e) => println!("Error while sending string: {}", e.to_string())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn send_enter(&self, _: &Configs) {
|
||||
unsafe {
|
||||
|
@ -63,9 +59,7 @@ impl super::KeyboardManager for WindowsKeyboardManager {
|
|||
}
|
||||
|
||||
fn delete_string(&self, config: &Configs, count: i32) {
|
||||
unsafe {
|
||||
delete_string(count, config.backspace_delay)
|
||||
}
|
||||
unsafe { delete_string(count, config.backspace_delay) }
|
||||
}
|
||||
|
||||
fn move_cursor_left(&self, _: &Configs, count: i32) {
|
||||
|
|
326
src/main.rs
326
src/main.rs
|
@ -20,56 +20,56 @@
|
|||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
use regex::Regex;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::process::exit;
|
||||
use std::sync::{Arc, mpsc};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::mpsc::{Receiver, Sender, RecvError};
|
||||
use std::sync::mpsc::channel;
|
||||
use std::sync::mpsc::{Receiver, RecvError, Sender};
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::process::{Command, Stdio};
|
||||
use notify::{RecommendedWatcher, Watcher, RecursiveMode, DebouncedEvent};
|
||||
use std::sync::mpsc::channel;
|
||||
use regex::Regex;
|
||||
|
||||
use clap::{App, Arg, ArgMatches, SubCommand, AppSettings};
|
||||
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
||||
use fs2::FileExt;
|
||||
use log::{info, LevelFilter, warn, error};
|
||||
use simplelog::{CombinedLogger, SharedLogger, TerminalMode, TermLogger, WriteLogger};
|
||||
use log::{error, info, warn, LevelFilter};
|
||||
use simplelog::{CombinedLogger, SharedLogger, TermLogger, TerminalMode, WriteLogger};
|
||||
|
||||
use crate::config::{ConfigManager, ConfigSet, Configs};
|
||||
use crate::config::runtime::RuntimeConfigManager;
|
||||
use crate::config::{ConfigManager, ConfigSet, Configs};
|
||||
use crate::engine::Engine;
|
||||
use crate::event::*;
|
||||
use crate::event::manager::{DefaultEventManager, EventManager};
|
||||
use crate::event::*;
|
||||
use crate::matcher::scrolling::ScrollingMatcher;
|
||||
use crate::package::{InstallResult, PackageManager, RemoveResult, UpdateResult};
|
||||
use crate::package::default::DefaultPackageManager;
|
||||
use crate::package::zip::ZipPackageResolver;
|
||||
use crate::package::{InstallResult, PackageManager, RemoveResult, UpdateResult};
|
||||
use crate::protocol::*;
|
||||
use crate::system::SystemManager;
|
||||
use crate::ui::UIManager;
|
||||
|
||||
mod ui;
|
||||
mod edit;
|
||||
mod event;
|
||||
mod check;
|
||||
mod utils;
|
||||
mod bridge;
|
||||
mod engine;
|
||||
mod config;
|
||||
mod render;
|
||||
mod system;
|
||||
mod context;
|
||||
mod matcher;
|
||||
mod process;
|
||||
mod package;
|
||||
mod keyboard;
|
||||
mod protocol;
|
||||
mod check;
|
||||
mod clipboard;
|
||||
mod config;
|
||||
mod context;
|
||||
mod edit;
|
||||
mod engine;
|
||||
mod event;
|
||||
mod extension;
|
||||
mod keyboard;
|
||||
mod matcher;
|
||||
mod package;
|
||||
mod process;
|
||||
mod protocol;
|
||||
mod render;
|
||||
mod sysdaemon;
|
||||
mod system;
|
||||
mod ui;
|
||||
mod utils;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
const LOG_FILE: &str = "espanso.log";
|
||||
|
@ -77,24 +77,25 @@ const LOG_FILE: &str = "espanso.log";
|
|||
fn main() {
|
||||
let install_subcommand = SubCommand::with_name("install")
|
||||
.about("Install a package. Equivalent to 'espanso package install'")
|
||||
.arg(Arg::with_name("external")
|
||||
.arg(
|
||||
Arg::with_name("external")
|
||||
.short("e")
|
||||
.long("external")
|
||||
.required(false)
|
||||
.takes_value(false)
|
||||
.help("Allow installing packages from non-verified repositories."))
|
||||
.arg(Arg::with_name("package_name")
|
||||
.help("Package name"))
|
||||
.arg(Arg::with_name("repository_url")
|
||||
.help("Allow installing packages from non-verified repositories."),
|
||||
)
|
||||
.arg(Arg::with_name("package_name").help("Package name"))
|
||||
.arg(
|
||||
Arg::with_name("repository_url")
|
||||
.help("(Optional) Link to GitHub repository")
|
||||
.required(false)
|
||||
.default_value("hub")
|
||||
.default_value("hub"),
|
||||
);
|
||||
|
||||
let uninstall_subcommand = SubCommand::with_name("uninstall")
|
||||
.about("Remove an installed package. Equivalent to 'espanso package uninstall'")
|
||||
.arg(Arg::with_name("package_name")
|
||||
.help("Package name"));
|
||||
.arg(Arg::with_name("package_name").help("Package name"));
|
||||
|
||||
let mut clap_instance = App::new("espanso")
|
||||
.version(VERSION)
|
||||
|
@ -191,7 +192,6 @@ fn main() {
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
let log_level = matches.occurrences_of("v") as i32;
|
||||
|
||||
// Load the configuration
|
||||
|
@ -299,7 +299,9 @@ fn main() {
|
|||
}
|
||||
|
||||
// Defaults help print
|
||||
clap_instance.print_long_help().expect("Unable to print help");
|
||||
clap_instance
|
||||
.print_long_help()
|
||||
.expect("Unable to print help");
|
||||
println!();
|
||||
}
|
||||
|
||||
|
@ -314,8 +316,8 @@ fn init_logger(config_set: &ConfigSet, reset: bool) {
|
|||
let mut log_outputs: Vec<Box<dyn SharedLogger>> = Vec::new();
|
||||
|
||||
// Initialize terminal output
|
||||
let terminal_out = TermLogger::new(log_level,
|
||||
simplelog::Config::default(), TerminalMode::Mixed);
|
||||
let terminal_out =
|
||||
TermLogger::new(log_level, simplelog::Config::default(), TerminalMode::Mixed);
|
||||
if let Some(terminal_out) = terminal_out {
|
||||
log_outputs.push(terminal_out);
|
||||
}
|
||||
|
@ -338,9 +340,7 @@ fn init_logger(config_set: &ConfigSet, reset: bool) {
|
|||
let file_out = WriteLogger::new(LevelFilter::Info, simplelog::Config::default(), log_file);
|
||||
log_outputs.push(file_out);
|
||||
|
||||
CombinedLogger::init(
|
||||
log_outputs
|
||||
).expect("Error opening log destination");
|
||||
CombinedLogger::init(log_outputs).expect("Error opening log destination");
|
||||
|
||||
// Activate logging for panics
|
||||
log_panics::init();
|
||||
|
@ -360,26 +360,42 @@ fn daemon_main(config_set: ConfigSet) {
|
|||
init_logger(&config_set, true);
|
||||
|
||||
info!("espanso version {}", VERSION);
|
||||
info!("using config path: {}", context::get_config_dir().to_string_lossy());
|
||||
info!("using package path: {}", context::get_package_dir().to_string_lossy());
|
||||
info!(
|
||||
"using config path: {}",
|
||||
context::get_config_dir().to_string_lossy()
|
||||
);
|
||||
info!(
|
||||
"using package path: {}",
|
||||
context::get_package_dir().to_string_lossy()
|
||||
);
|
||||
|
||||
let (send_channel, receive_channel) = mpsc::channel();
|
||||
|
||||
let ipc_server = protocol::get_ipc_server(Service::Daemon, config_set.default.clone(), send_channel.clone());
|
||||
let ipc_server = protocol::get_ipc_server(
|
||||
Service::Daemon,
|
||||
config_set.default.clone(),
|
||||
send_channel.clone(),
|
||||
);
|
||||
ipc_server.start();
|
||||
|
||||
info!("spawning worker process...");
|
||||
|
||||
let espanso_path = std::env::current_exe().expect("unable to obtain espanso path location");
|
||||
crate::process::spawn_process(&espanso_path.to_string_lossy().to_string(), &vec!("worker".to_owned()));
|
||||
crate::process::spawn_process(
|
||||
&espanso_path.to_string_lossy().to_string(),
|
||||
&vec!["worker".to_owned()],
|
||||
);
|
||||
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
|
||||
if config_set.default.auto_restart {
|
||||
let send_channel_clone = send_channel.clone();
|
||||
thread::Builder::new().name("watcher_background".to_string()).spawn(move || {
|
||||
thread::Builder::new()
|
||||
.name("watcher_background".to_string())
|
||||
.spawn(move || {
|
||||
watcher_background(send_channel_clone);
|
||||
}).expect("Unable to spawn watcher background thread");
|
||||
})
|
||||
.expect("Unable to spawn watcher background thread");
|
||||
}
|
||||
|
||||
loop {
|
||||
|
@ -388,33 +404,48 @@ fn daemon_main(config_set: ConfigSet) {
|
|||
match event {
|
||||
Event::Action(ActionType::RestartWorker) => {
|
||||
// Terminate the worker process
|
||||
send_command_or_warn(Service::Worker, config_set.default.clone(), IPCCommand::exit_worker());
|
||||
send_command_or_warn(
|
||||
Service::Worker,
|
||||
config_set.default.clone(),
|
||||
IPCCommand::exit_worker(),
|
||||
);
|
||||
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
|
||||
// Restart the worker process
|
||||
crate::process::spawn_process(&espanso_path.to_string_lossy().to_string(), &vec!("worker".to_owned(), "--reload".to_owned()));
|
||||
},
|
||||
crate::process::spawn_process(
|
||||
&espanso_path.to_string_lossy().to_string(),
|
||||
&vec!["worker".to_owned(), "--reload".to_owned()],
|
||||
);
|
||||
}
|
||||
Event::Action(ActionType::Exit) => {
|
||||
send_command_or_warn(Service::Worker, config_set.default.clone(), IPCCommand::exit_worker());
|
||||
send_command_or_warn(
|
||||
Service::Worker,
|
||||
config_set.default.clone(),
|
||||
IPCCommand::exit_worker(),
|
||||
);
|
||||
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
|
||||
info!("terminating espanso.");
|
||||
std::process::exit(0);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
// Forward the command to the worker
|
||||
let command = IPCCommand::from(event);
|
||||
if let Some(command) = command {
|
||||
send_command_or_warn(Service::Worker, config_set.default.clone(), command);
|
||||
send_command_or_warn(
|
||||
Service::Worker,
|
||||
config_set.default.clone(),
|
||||
command,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
warn!("error while reading event in daemon process: {}", e);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -423,13 +454,18 @@ fn watcher_background(sender: Sender<Event>) {
|
|||
// Create a channel to receive the events.
|
||||
let (tx, rx) = channel();
|
||||
|
||||
let mut watcher: RecommendedWatcher = Watcher::new(tx, Duration::from_secs(1)).expect("unable to create file watcher");
|
||||
let mut watcher: RecommendedWatcher =
|
||||
Watcher::new(tx, Duration::from_secs(1)).expect("unable to create file watcher");
|
||||
|
||||
let config_path = crate::context::get_config_dir();
|
||||
watcher.watch(&config_path, RecursiveMode::Recursive).expect("unable to start watcher");
|
||||
|
||||
info!("watching for changes in path: {}", config_path.to_string_lossy());
|
||||
watcher
|
||||
.watch(&config_path, RecursiveMode::Recursive)
|
||||
.expect("unable to start watcher");
|
||||
|
||||
info!(
|
||||
"watching for changes in path: {}",
|
||||
config_path.to_string_lossy()
|
||||
);
|
||||
|
||||
loop {
|
||||
let should_reload = match rx.recv() {
|
||||
|
@ -443,7 +479,8 @@ fn watcher_background(sender: Sender<Event>) {
|
|||
};
|
||||
|
||||
if let Some(path) = path {
|
||||
if path.extension().unwrap_or_default() == "yml" { // Only load yml files
|
||||
if path.extension().unwrap_or_default() == "yml" {
|
||||
// Only load yml files
|
||||
true
|
||||
} else {
|
||||
false
|
||||
|
@ -451,7 +488,7 @@ fn watcher_background(sender: Sender<Event>) {
|
|||
} else {
|
||||
false
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("error while watching files: {:?}", e);
|
||||
false
|
||||
|
@ -469,11 +506,12 @@ fn watcher_background(sender: Sender<Event>) {
|
|||
sender.send(event).unwrap_or_else(|e| {
|
||||
warn!("unable to communicate with daemon thread: {}", e);
|
||||
})
|
||||
},
|
||||
}
|
||||
Err(error) => {
|
||||
error!("Unable to reload configuration due to an error: {}", error);
|
||||
let event = Event::System(SystemEvent::NotifyRequest(
|
||||
"Unable to reload config due to an error, see the logs for more details.".to_owned()
|
||||
"Unable to reload config due to an error, see the logs for more details."
|
||||
.to_owned(),
|
||||
));
|
||||
sender.send(event).unwrap_or_else(|e| {
|
||||
warn!("unable to communicate with daemon thread: {}", e);
|
||||
|
@ -502,21 +540,34 @@ fn worker_main(config_set: ConfigSet, matches: &ArgMatches) {
|
|||
// we could reinterpret the characters we are injecting
|
||||
let is_injecting = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
|
||||
let context = context::new(config_set.default.clone(), send_channel.clone(), is_injecting.clone());
|
||||
let context = context::new(
|
||||
config_set.default.clone(),
|
||||
send_channel.clone(),
|
||||
is_injecting.clone(),
|
||||
);
|
||||
|
||||
let config_set_copy = config_set.clone();
|
||||
thread::Builder::new().name("daemon_background".to_string()).spawn(move || {
|
||||
thread::Builder::new()
|
||||
.name("daemon_background".to_string())
|
||||
.spawn(move || {
|
||||
worker_background(receive_channel, config_set_copy, is_injecting, is_reloading);
|
||||
}).expect("Unable to spawn daemon background thread");
|
||||
})
|
||||
.expect("Unable to spawn daemon background thread");
|
||||
|
||||
let ipc_server = protocol::get_ipc_server(Service::Worker, config_set.default, send_channel.clone());
|
||||
let ipc_server =
|
||||
protocol::get_ipc_server(Service::Worker, config_set.default, send_channel.clone());
|
||||
ipc_server.start();
|
||||
|
||||
context.eventloop();
|
||||
}
|
||||
|
||||
/// Background thread worker for the daemon
|
||||
fn worker_background(receive_channel: Receiver<Event>, config_set: ConfigSet, is_injecting: Arc<AtomicBool>, is_reloading: bool) {
|
||||
fn worker_background(
|
||||
receive_channel: Receiver<Event>,
|
||||
config_set: ConfigSet,
|
||||
is_injecting: Arc<AtomicBool>,
|
||||
is_reloading: bool,
|
||||
) {
|
||||
let system_manager = system::get_manager();
|
||||
let config_manager = RuntimeConfigManager::new(config_set, system_manager);
|
||||
|
||||
|
@ -535,10 +586,11 @@ fn worker_background(receive_channel: Receiver<Event>, config_set: ConfigSet, is
|
|||
|
||||
let extensions = extension::get_extensions(Box::new(clipboard::get_manager()));
|
||||
|
||||
let renderer = render::default::DefaultRenderer::new(extensions,
|
||||
config_manager.default_config().clone());
|
||||
let renderer =
|
||||
render::default::DefaultRenderer::new(extensions, config_manager.default_config().clone());
|
||||
|
||||
let engine = Engine::new(&keyboard_manager,
|
||||
let engine = Engine::new(
|
||||
&keyboard_manager,
|
||||
&clipboard_manager,
|
||||
&config_manager,
|
||||
&ui_manager,
|
||||
|
@ -550,9 +602,9 @@ fn worker_background(receive_channel: Receiver<Event>, config_set: ConfigSet, is
|
|||
|
||||
let event_manager = DefaultEventManager::new(
|
||||
receive_channel,
|
||||
vec!(&matcher),
|
||||
vec!(&engine, &matcher),
|
||||
vec!(&engine),
|
||||
vec![&matcher],
|
||||
vec![&engine, &matcher],
|
||||
vec![&engine],
|
||||
);
|
||||
|
||||
info!("worker is running!");
|
||||
|
@ -610,8 +662,8 @@ fn start_daemon(config_set: ConfigSet) {
|
|||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn start_daemon(config_set: ConfigSet) {
|
||||
use std::process::{Command, Stdio};
|
||||
use crate::sysdaemon::{verify, VerifyResult};
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
// Check if Systemd is available in the system
|
||||
let status = Command::new("systemctl")
|
||||
|
@ -622,11 +674,7 @@ fn start_daemon(config_set: ConfigSet) {
|
|||
|
||||
// If Systemd is not available in the system, espanso should default to unmanaged mode
|
||||
// See issue https://github.com/federico-terzi/espanso/issues/139
|
||||
let force_unmanaged = if let Err(_) = status {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let force_unmanaged = if let Err(_) = status { true } else { false };
|
||||
|
||||
if config_set.default.use_system_agent && !force_unmanaged {
|
||||
// Make sure espanso is currently registered in systemd
|
||||
|
@ -634,12 +682,12 @@ fn start_daemon(config_set: ConfigSet) {
|
|||
match res {
|
||||
VerifyResult::EnabledAndValid => {
|
||||
// Do nothing, everything is ok!
|
||||
},
|
||||
}
|
||||
VerifyResult::EnabledButInvalidPath => {
|
||||
eprintln!("Updating espanso service file with new path...");
|
||||
unregister_main(config_set.clone());
|
||||
register_main(config_set);
|
||||
},
|
||||
}
|
||||
VerifyResult::NotEnabled => {
|
||||
use dialoguer::Confirmation;
|
||||
if Confirmation::new()
|
||||
|
@ -656,7 +704,7 @@ fn start_daemon(config_set: ConfigSet) {
|
|||
|
||||
std::process::exit(4);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Start the espanso service
|
||||
|
@ -690,7 +738,8 @@ fn fork_daemon(config_set: ConfigSet) {
|
|||
println!("Unable to fork.");
|
||||
exit(4);
|
||||
}
|
||||
if pid > 0 { // Parent process exit
|
||||
if pid > 0 {
|
||||
// Parent process exit
|
||||
println!("daemon started!");
|
||||
exit(0);
|
||||
}
|
||||
|
@ -728,7 +777,6 @@ fn status_main() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// Stop subcommand, used to stop the daemon.
|
||||
fn stop_main(config_set: ConfigSet) {
|
||||
// Try to acquire lock file
|
||||
|
@ -748,7 +796,11 @@ fn restart_main(config_set: ConfigSet) {
|
|||
let lock_file = acquire_lock();
|
||||
if lock_file.is_none() {
|
||||
// Terminate the current espanso daemon
|
||||
send_command_or_warn(Service::Daemon, config_set.default.clone(), IPCCommand::exit());
|
||||
send_command_or_warn(
|
||||
Service::Daemon,
|
||||
config_set.default.clone(),
|
||||
IPCCommand::exit(),
|
||||
);
|
||||
} else {
|
||||
release_lock(lock_file.unwrap());
|
||||
}
|
||||
|
@ -773,9 +825,15 @@ fn detect_main() {
|
|||
let mut last_exec: String = "".to_owned();
|
||||
|
||||
loop {
|
||||
let curr_title = system_manager.get_current_window_title().unwrap_or_default();
|
||||
let curr_class = system_manager.get_current_window_class().unwrap_or_default();
|
||||
let curr_exec = system_manager.get_current_window_executable().unwrap_or_default();
|
||||
let curr_title = system_manager
|
||||
.get_current_window_title()
|
||||
.unwrap_or_default();
|
||||
let curr_class = system_manager
|
||||
.get_current_window_class()
|
||||
.unwrap_or_default();
|
||||
let curr_exec = system_manager
|
||||
.get_current_window_executable()
|
||||
.unwrap_or_default();
|
||||
|
||||
// Check if a change occurred
|
||||
if curr_title != last_title || curr_class != last_class || curr_exec != last_exec {
|
||||
|
@ -800,13 +858,15 @@ fn detect_main() {
|
|||
#[cfg(target_os = "macos")]
|
||||
fn detect_main() {
|
||||
thread::spawn(|| {
|
||||
use std::io::Write;
|
||||
use std::io::stdout;
|
||||
use std::io::Write;
|
||||
|
||||
let system_manager = system::get_manager();
|
||||
|
||||
println!("Listening for changes, now focus the window you want to analyze.");
|
||||
println!("Warning: stay on the window for a few seconds, as it may take a while to register.");
|
||||
println!(
|
||||
"Warning: stay on the window for a few seconds, as it may take a while to register."
|
||||
);
|
||||
println!("You can terminate with CTRL+C\n");
|
||||
|
||||
let mut last_title: String = "".to_owned();
|
||||
|
@ -814,9 +874,15 @@ fn detect_main() {
|
|||
let mut last_exec: String = "".to_owned();
|
||||
|
||||
loop {
|
||||
let curr_title = system_manager.get_current_window_title().unwrap_or_default();
|
||||
let curr_class = system_manager.get_current_window_class().unwrap_or_default();
|
||||
let curr_exec = system_manager.get_current_window_executable().unwrap_or_default();
|
||||
let curr_title = system_manager
|
||||
.get_current_window_title()
|
||||
.unwrap_or_default();
|
||||
let curr_class = system_manager
|
||||
.get_current_window_class()
|
||||
.unwrap_or_default();
|
||||
let curr_exec = system_manager
|
||||
.get_current_window_executable()
|
||||
.unwrap_or_default();
|
||||
|
||||
// Check if a change occurred
|
||||
if curr_title != last_title || curr_class != last_class || curr_exec != last_exec {
|
||||
|
@ -922,26 +988,25 @@ fn install_main(_config_set: ConfigSet, matches: &ArgMatches) {
|
|||
|
||||
let mut package_manager = DefaultPackageManager::new_default(Some(package_resolver));
|
||||
|
||||
let res = if repository == "hub" { // Installation from the Hub
|
||||
let res = if repository == "hub" {
|
||||
// Installation from the Hub
|
||||
if package_manager.is_index_outdated() {
|
||||
println!("Updating package index...");
|
||||
let res = package_manager.update_index(false);
|
||||
|
||||
match res {
|
||||
Ok(update_result) => {
|
||||
match update_result {
|
||||
Ok(update_result) => match update_result {
|
||||
UpdateResult::NotOutdated => {
|
||||
eprintln!("Index was already up to date");
|
||||
},
|
||||
}
|
||||
UpdateResult::Updated => {
|
||||
println!("Index updated!");
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
exit(2);
|
||||
},
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("Using cached package index, run 'espanso package refresh' to update it.")
|
||||
|
@ -968,23 +1033,24 @@ fn install_main(_config_set: ConfigSet, matches: &ArgMatches) {
|
|||
};
|
||||
|
||||
match res {
|
||||
Ok(install_result) => {
|
||||
match install_result {
|
||||
Ok(install_result) => match install_result {
|
||||
InstallResult::NotFoundInIndex => {
|
||||
eprintln!("Package not found");
|
||||
},
|
||||
}
|
||||
InstallResult::NotFoundInRepo => {
|
||||
eprintln!("Package not found in repository, are you sure the folder exist in the repo?");
|
||||
},
|
||||
eprintln!(
|
||||
"Package not found in repository, are you sure the folder exist in the repo?"
|
||||
);
|
||||
}
|
||||
InstallResult::UnableToParsePackageInfo => {
|
||||
eprintln!("Unable to parse Package info from README.md");
|
||||
},
|
||||
}
|
||||
InstallResult::MissingPackageVersion => {
|
||||
eprintln!("Missing package version");
|
||||
},
|
||||
}
|
||||
InstallResult::AlreadyInstalled => {
|
||||
eprintln!("{} already installed!", package_name);
|
||||
},
|
||||
}
|
||||
InstallResult::BlockedExternalPackage(repo_url) => {
|
||||
eprintln!("Warning: the requested package is hosted on an external repository:");
|
||||
eprintln!();
|
||||
|
@ -1011,12 +1077,11 @@ fn install_main(_config_set: ConfigSet, matches: &ArgMatches) {
|
|||
println!();
|
||||
println!("You need to restart espanso for changes to take effect, using:");
|
||||
println!(" espanso restart");
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1031,22 +1096,20 @@ fn remove_package_main(_config_set: ConfigSet, matches: &ArgMatches) {
|
|||
let res = package_manager.remove_package(package_name);
|
||||
|
||||
match res {
|
||||
Ok(remove_result) => {
|
||||
match remove_result {
|
||||
Ok(remove_result) => match remove_result {
|
||||
RemoveResult::NotFound => {
|
||||
eprintln!("{} package was not installed.", package_name);
|
||||
},
|
||||
}
|
||||
RemoveResult::Removed => {
|
||||
println!("{} successfully removed!", package_name);
|
||||
println!();
|
||||
println!("You need to restart espanso for changes to take effect, using:");
|
||||
println!(" espanso restart");
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1056,20 +1119,18 @@ fn update_index_main(_config_set: ConfigSet) {
|
|||
let res = package_manager.update_index(true);
|
||||
|
||||
match res {
|
||||
Ok(update_result) => {
|
||||
match update_result {
|
||||
Ok(update_result) => match update_result {
|
||||
UpdateResult::NotOutdated => {
|
||||
eprintln!("Index was already up to date");
|
||||
},
|
||||
}
|
||||
UpdateResult::Updated => {
|
||||
println!("Index updated!");
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
exit(2);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1117,11 +1178,11 @@ fn edit_main(matches: &ArgMatches) {
|
|||
let config_dir = crate::context::get_config_dir();
|
||||
|
||||
let config_path = match config {
|
||||
"default" => {
|
||||
config_dir.join(crate::config::DEFAULT_CONFIG_FILE_NAME)
|
||||
},
|
||||
name => { // Otherwise, search in the user/ config folder
|
||||
config_dir.join(crate::config::USER_CONFIGS_FOLDER_NAME)
|
||||
"default" => config_dir.join(crate::config::DEFAULT_CONFIG_FILE_NAME),
|
||||
name => {
|
||||
// Otherwise, search in the user/ config folder
|
||||
config_dir
|
||||
.join(crate::config::USER_CONFIGS_FOLDER_NAME)
|
||||
.join(name.to_owned() + ".yml")
|
||||
}
|
||||
};
|
||||
|
@ -1134,12 +1195,17 @@ fn edit_main(matches: &ArgMatches) {
|
|||
// Get the last modified date, so that we can detect if the user actually edits the file
|
||||
// before reloading
|
||||
let metadata = std::fs::metadata(&config_path).expect("cannot gather file metadata");
|
||||
let last_modified = metadata.modified().expect("cannot read file last modified date");
|
||||
let last_modified = metadata
|
||||
.modified()
|
||||
.expect("cannot read file last modified date");
|
||||
|
||||
let result = crate::edit::open_editor(&config_path);
|
||||
if result {
|
||||
let new_metadata = std::fs::metadata(&config_path).expect("cannot gather file metadata");
|
||||
let new_last_modified = new_metadata.modified().expect("cannot read file last modified date");
|
||||
let new_metadata =
|
||||
std::fs::metadata(&config_path).expect("cannot gather file metadata");
|
||||
let new_last_modified = new_metadata
|
||||
.modified()
|
||||
.expect("cannot read file last modified date");
|
||||
|
||||
if last_modified != new_last_modified {
|
||||
println!("File has been modified, reloading configuration");
|
||||
|
@ -1203,7 +1269,7 @@ fn acquire_custom_lock(name: &str) -> Option<File> {
|
|||
let res = file.try_lock_exclusive();
|
||||
|
||||
if res.is_ok() {
|
||||
return Some(file)
|
||||
return Some(file);
|
||||
}
|
||||
|
||||
None
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde::{Serialize, Deserialize, Deserializer};
|
||||
use crate::event::{KeyEvent, KeyModifier};
|
||||
use crate::event::KeyEventReceiver;
|
||||
use serde_yaml::Mapping;
|
||||
use crate::event::{KeyEvent, KeyModifier};
|
||||
use regex::Regex;
|
||||
use std::path::PathBuf;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde_yaml::Mapping;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub(crate) mod scrolling;
|
||||
|
||||
|
@ -62,9 +62,10 @@ pub struct ImageContent {
|
|||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for Match {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
|
||||
D: Deserializer<'de> {
|
||||
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let auto_match = AutoMatch::deserialize(deserializer)?;
|
||||
Ok(Match::from(&auto_match))
|
||||
}
|
||||
|
@ -79,7 +80,7 @@ impl<'a> From<&'a AutoMatch> for Match{
|
|||
let mut triggers = if !other.triggers.is_empty() {
|
||||
other.triggers.clone()
|
||||
} else if !other.trigger.is_empty() {
|
||||
vec!(other.trigger.clone())
|
||||
vec![other.trigger.clone()]
|
||||
} else {
|
||||
panic!("Match does not have any trigger defined: {:?}", other)
|
||||
};
|
||||
|
@ -89,44 +90,48 @@ impl<'a> From<&'a AutoMatch> for Match{
|
|||
// "hello", "Hello", "HELLO"
|
||||
if other.propagate_case {
|
||||
// List with first letter capitalized
|
||||
let first_capitalized : Vec<String> = triggers.iter().map(|trigger| {
|
||||
let first_capitalized: Vec<String> = triggers
|
||||
.iter()
|
||||
.map(|trigger| {
|
||||
let capitalized = trigger.clone();
|
||||
let mut v: Vec<char> = capitalized.chars().collect();
|
||||
|
||||
// Capitalize the first alphabetic letter
|
||||
// See issue #244
|
||||
let first_alphabetic = v.iter().position(|c| {
|
||||
c.is_alphabetic()
|
||||
}).unwrap_or(0);
|
||||
let first_alphabetic = v.iter().position(|c| c.is_alphabetic()).unwrap_or(0);
|
||||
|
||||
v[first_alphabetic] = v[first_alphabetic].to_uppercase().nth(0).unwrap();
|
||||
v.into_iter().collect()
|
||||
}).collect();
|
||||
})
|
||||
.collect();
|
||||
|
||||
let all_capitalized : Vec<String> = triggers.iter().map(|trigger| {
|
||||
trigger.to_uppercase()
|
||||
}).collect();
|
||||
let all_capitalized: Vec<String> = triggers
|
||||
.iter()
|
||||
.map(|trigger| trigger.to_uppercase())
|
||||
.collect();
|
||||
|
||||
triggers.extend(first_capitalized);
|
||||
triggers.extend(all_capitalized);
|
||||
}
|
||||
|
||||
let trigger_sequences = triggers.iter().map(|trigger| {
|
||||
let trigger_sequences = triggers
|
||||
.iter()
|
||||
.map(|trigger| {
|
||||
// Calculate the trigger sequence
|
||||
let mut trigger_sequence = Vec::new();
|
||||
let trigger_chars: Vec<char> = trigger.chars().collect();
|
||||
trigger_sequence.extend(trigger_chars.into_iter().map(|c| {
|
||||
TriggerEntry::Char(c)
|
||||
}));
|
||||
if other.word { // If it's a word match, end with a word separator
|
||||
trigger_sequence.extend(trigger_chars.into_iter().map(|c| TriggerEntry::Char(c)));
|
||||
if other.word {
|
||||
// If it's a word match, end with a word separator
|
||||
trigger_sequence.push(TriggerEntry::WordSeparator);
|
||||
}
|
||||
|
||||
trigger_sequence
|
||||
}).collect();
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
let content = if let Some(replace) = &other.replace { // Text match
|
||||
let content = if let Some(replace) = &other.replace {
|
||||
// Text match
|
||||
let new_replace = replace.clone();
|
||||
|
||||
// Check if the match contains variables
|
||||
|
@ -139,7 +144,8 @@ impl<'a> From<&'a AutoMatch> for Match{
|
|||
};
|
||||
|
||||
MatchContentType::Text(content)
|
||||
}else if let Some(image_path) = &other.image_path { // Image match
|
||||
} else if let Some(image_path) = &other.image_path {
|
||||
// Image match
|
||||
// On Windows, we have to replace the forward / with the backslash \ in the path
|
||||
let new_path = if cfg!(target_os = "windows") {
|
||||
image_path.replace("/", "\\")
|
||||
|
@ -162,7 +168,7 @@ impl<'a> From<&'a AutoMatch> for Match{
|
|||
};
|
||||
|
||||
let content = ImageContent {
|
||||
path: PathBuf::from(new_path)
|
||||
path: PathBuf::from(new_path),
|
||||
};
|
||||
|
||||
MatchContentType::Image(content)
|
||||
|
@ -214,15 +220,33 @@ struct AutoMatch {
|
|||
pub force_clipboard: bool,
|
||||
}
|
||||
|
||||
fn default_trigger() -> String {"".to_owned()}
|
||||
fn default_triggers() -> Vec<String> {Vec::new()}
|
||||
fn default_vars() -> Vec<MatchVariable> {Vec::new()}
|
||||
fn default_word() -> bool {false}
|
||||
fn default_passive_only() -> bool {false}
|
||||
fn default_replace() -> Option<String> {None}
|
||||
fn default_image_path() -> Option<String> {None}
|
||||
fn default_propagate_case() -> bool {false}
|
||||
fn default_force_clipboard() -> bool {false}
|
||||
fn default_trigger() -> String {
|
||||
"".to_owned()
|
||||
}
|
||||
fn default_triggers() -> Vec<String> {
|
||||
Vec::new()
|
||||
}
|
||||
fn default_vars() -> Vec<MatchVariable> {
|
||||
Vec::new()
|
||||
}
|
||||
fn default_word() -> bool {
|
||||
false
|
||||
}
|
||||
fn default_passive_only() -> bool {
|
||||
false
|
||||
}
|
||||
fn default_replace() -> Option<String> {
|
||||
None
|
||||
}
|
||||
fn default_image_path() -> Option<String> {
|
||||
None
|
||||
}
|
||||
fn default_propagate_case() -> bool {
|
||||
false
|
||||
}
|
||||
fn default_force_clipboard() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct MatchVariable {
|
||||
|
@ -235,12 +259,14 @@ pub struct MatchVariable {
|
|||
pub params: Mapping,
|
||||
}
|
||||
|
||||
fn default_params() -> Mapping {Mapping::new()}
|
||||
fn default_params() -> Mapping {
|
||||
Mapping::new()
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
|
||||
pub enum TriggerEntry {
|
||||
Char(char),
|
||||
WordSeparator
|
||||
WordSeparator,
|
||||
}
|
||||
|
||||
pub trait MatchReceiver {
|
||||
|
@ -260,17 +286,16 @@ impl <M: Matcher> KeyEventReceiver for M {
|
|||
match e {
|
||||
KeyEvent::Char(c) => {
|
||||
self.handle_char(&c);
|
||||
},
|
||||
}
|
||||
KeyEvent::Modifier(m) => {
|
||||
self.handle_modifier(m);
|
||||
},
|
||||
}
|
||||
KeyEvent::Other => {
|
||||
self.handle_other();
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TESTS
|
||||
|
||||
|
@ -290,10 +315,10 @@ mod tests {
|
|||
match _match.content {
|
||||
MatchContentType::Text(content) => {
|
||||
assert_eq!(content._has_vars, false);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
assert!(false);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,10 +334,10 @@ mod tests {
|
|||
match _match.content {
|
||||
MatchContentType::Text(content) => {
|
||||
assert_eq!(content._has_vars, true);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
assert!(false);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -328,10 +353,10 @@ mod tests {
|
|||
match _match.content {
|
||||
MatchContentType::Text(content) => {
|
||||
assert_eq!(content._has_vars, true);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
assert!(false);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -379,10 +404,10 @@ mod tests {
|
|||
match _match.content {
|
||||
MatchContentType::Image(content) => {
|
||||
assert_eq!(content.path, PathBuf::from("/path/to/file"));
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
assert!(false);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -447,7 +472,10 @@ mod tests {
|
|||
|
||||
let _match: Match = serde_yaml::from_str(match_str).unwrap();
|
||||
|
||||
assert_eq!(_match.triggers, vec!["hello", "hi", "Hello", "Hi", "HELLO", "HI"])
|
||||
assert_eq!(
|
||||
_match.triggers,
|
||||
vec!["hello", "hi", "Hello", "Hi", "HELLO", "HI"]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -17,13 +17,13 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::matcher::{Match, MatchReceiver, TriggerEntry};
|
||||
use std::cell::{RefCell};
|
||||
use crate::event::{KeyModifier, ActionEventReceiver, ActionType};
|
||||
use crate::config::ConfigManager;
|
||||
use crate::event::KeyModifier::{BACKSPACE, LEFT_SHIFT, RIGHT_SHIFT, CAPS_LOCK};
|
||||
use std::time::SystemTime;
|
||||
use crate::event::KeyModifier::{BACKSPACE, CAPS_LOCK, LEFT_SHIFT, RIGHT_SHIFT};
|
||||
use crate::event::{ActionEventReceiver, ActionType, KeyModifier};
|
||||
use crate::matcher::{Match, MatchReceiver, TriggerEntry};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub struct ScrollingMatcher<'a, R: MatchReceiver, M: ConfigManager<'a>> {
|
||||
config_manager: &'a M,
|
||||
|
@ -40,7 +40,7 @@ struct MatchEntry<'a> {
|
|||
start: usize,
|
||||
count: usize,
|
||||
trigger_offset: usize, // The index of the trigger in the Match that matched
|
||||
_match: &'a Match
|
||||
_match: &'a Match,
|
||||
}
|
||||
|
||||
impl<'a, R: MatchReceiver, M: ConfigManager<'a>> ScrollingMatcher<'a, R, M> {
|
||||
|
@ -74,14 +74,16 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> ScrollingMatcher<'a, R, M> {
|
|||
self.receiver.on_enable_update(*is_enabled);
|
||||
}
|
||||
|
||||
fn is_matching(mtc: &Match, current_char: &str, start: usize, trigger_offset: usize, is_current_word_separator: bool) -> bool {
|
||||
fn is_matching(
|
||||
mtc: &Match,
|
||||
current_char: &str,
|
||||
start: usize,
|
||||
trigger_offset: usize,
|
||||
is_current_word_separator: bool,
|
||||
) -> bool {
|
||||
match mtc._trigger_sequences[trigger_offset][start] {
|
||||
TriggerEntry::Char(c) => {
|
||||
current_char.starts_with(c)
|
||||
},
|
||||
TriggerEntry::WordSeparator => {
|
||||
is_current_word_separator
|
||||
},
|
||||
TriggerEntry::Char(c) => current_char.starts_with(c),
|
||||
TriggerEntry::WordSeparator => is_current_word_separator,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -98,9 +100,9 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
let active_config = self.config_manager.active_config();
|
||||
|
||||
// Check if the current char is a word separator
|
||||
let mut is_current_word_separator = active_config.word_separators.contains(
|
||||
&c.chars().nth(0).unwrap_or_default()
|
||||
);
|
||||
let mut is_current_word_separator = active_config
|
||||
.word_separators
|
||||
.contains(&c.chars().nth(0).unwrap_or_default());
|
||||
|
||||
// Workaround needed on macos to consider espanso replacement key presses as separators.
|
||||
if cfg!(target_os = "macos") {
|
||||
|
@ -118,11 +120,12 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
for m in active_config.matches.iter() {
|
||||
// only active-enabled matches are considered
|
||||
if m.passive_only {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
for trigger_offset in 0..m._trigger_sequences.len() {
|
||||
let mut result = Self::is_matching(m, c, 0, trigger_offset, is_current_word_separator);
|
||||
let mut result =
|
||||
Self::is_matching(m, c, 0, trigger_offset, is_current_word_separator);
|
||||
|
||||
if m.word {
|
||||
result = result && *was_previous_word_separator
|
||||
|
@ -133,7 +136,7 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
start: 1,
|
||||
count: m._trigger_sequences[trigger_offset].len(),
|
||||
trigger_offset,
|
||||
_match: &m
|
||||
_match: &m,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -142,22 +145,29 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
|
||||
let combined_matches: Vec<MatchEntry> = match current_set_queue.back_mut() {
|
||||
Some(last_matches) => {
|
||||
let mut updated: Vec<MatchEntry> = last_matches.iter()
|
||||
let mut updated: Vec<MatchEntry> = last_matches
|
||||
.iter()
|
||||
.filter(|&x| {
|
||||
Self::is_matching(x._match, c, x.start, x.trigger_offset, is_current_word_separator)
|
||||
Self::is_matching(
|
||||
x._match,
|
||||
c,
|
||||
x.start,
|
||||
x.trigger_offset,
|
||||
is_current_word_separator,
|
||||
)
|
||||
})
|
||||
.map(|x| MatchEntry {
|
||||
start: x.start + 1,
|
||||
count: x.count,
|
||||
trigger_offset: x.trigger_offset,
|
||||
_match: &x._match
|
||||
_match: &x._match,
|
||||
})
|
||||
.collect();
|
||||
|
||||
updated.extend(new_matches);
|
||||
updated
|
||||
},
|
||||
None => {new_matches},
|
||||
}
|
||||
None => new_matches,
|
||||
};
|
||||
|
||||
let mut found_entry = None;
|
||||
|
@ -171,7 +181,9 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
|
||||
current_set_queue.push_back(combined_matches);
|
||||
|
||||
if current_set_queue.len() as i32 > (self.config_manager.default_config().backspace_limit + 1) {
|
||||
if current_set_queue.len() as i32
|
||||
> (self.config_manager.default_config().backspace_limit + 1)
|
||||
{
|
||||
current_set_queue.pop_front();
|
||||
}
|
||||
|
||||
|
@ -194,15 +206,16 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
match as_char {
|
||||
Some(c) => {
|
||||
Some(c) // Current char is the trailing separator
|
||||
},
|
||||
None => {None},
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
};
|
||||
|
||||
// Force espanso to consider the last char as a separator
|
||||
*was_previous_word_separator = true;
|
||||
|
||||
self.receiver.on_match(mtc, trailing_separator, entry.trigger_offset);
|
||||
self.receiver
|
||||
.on_match(mtc, trailing_separator, entry.trigger_offset);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -213,8 +226,10 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
// study a mechanism to avoid this problem
|
||||
|
||||
if KeyModifier::shallow_equals(&m, &config.toggle_key) {
|
||||
check_interval(&self.toggle_press_time,
|
||||
u128::from(config.toggle_interval), || {
|
||||
check_interval(
|
||||
&self.toggle_press_time,
|
||||
u128::from(config.toggle_interval),
|
||||
|| {
|
||||
self.toggle();
|
||||
|
||||
let is_enabled = self.is_enabled.borrow();
|
||||
|
@ -222,12 +237,16 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
if !*is_enabled {
|
||||
self.current_set_queue.borrow_mut().clear();
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
} else if KeyModifier::shallow_equals(&m, &config.passive_key) {
|
||||
check_interval(&self.passive_press_time,
|
||||
u128::from(config.toggle_interval), || {
|
||||
check_interval(
|
||||
&self.passive_press_time,
|
||||
u128::from(config.toggle_interval),
|
||||
|| {
|
||||
self.receiver.on_passive();
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Backspace handling, basically "rewinding history"
|
||||
|
@ -238,7 +257,8 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
|
||||
// Consider modifiers as separators to improve word matches reliability
|
||||
if m != LEFT_SHIFT && m != RIGHT_SHIFT && m != CAPS_LOCK {
|
||||
let mut was_previous_char_word_separator = self.was_previous_char_word_separator.borrow_mut();
|
||||
let mut was_previous_char_word_separator =
|
||||
self.was_previous_char_word_separator.borrow_mut();
|
||||
*was_previous_char_word_separator = true;
|
||||
}
|
||||
}
|
||||
|
@ -246,29 +266,35 @@ impl <'a, R: MatchReceiver, M: ConfigManager<'a>> super::Matcher for ScrollingMa
|
|||
fn handle_other(&self) {
|
||||
// When receiving "other" type of events, we mark them as valid separators.
|
||||
// This dramatically improves the reliability of word matches
|
||||
let mut was_previous_char_word_separator = self.was_previous_char_word_separator.borrow_mut();
|
||||
let mut was_previous_char_word_separator =
|
||||
self.was_previous_char_word_separator.borrow_mut();
|
||||
*was_previous_char_word_separator = true;
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a, R: MatchReceiver, M: ConfigManager<'a>> ActionEventReceiver for ScrollingMatcher<'a, R, M> {
|
||||
impl<'a, R: MatchReceiver, M: ConfigManager<'a>> ActionEventReceiver
|
||||
for ScrollingMatcher<'a, R, M>
|
||||
{
|
||||
fn on_action_event(&self, e: ActionType) {
|
||||
match e {
|
||||
ActionType::Toggle => {
|
||||
self.toggle();
|
||||
},
|
||||
}
|
||||
ActionType::Enable => {
|
||||
self.set_enabled(true);
|
||||
},
|
||||
}
|
||||
ActionType::Disable => {
|
||||
self.set_enabled(false);
|
||||
},
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_interval<F>(state_var: &RefCell<SystemTime>, interval: u128, elapsed_callback: F) where F:Fn() {
|
||||
fn check_interval<F>(state_var: &RefCell<SystemTime>, interval: u128, elapsed_callback: F)
|
||||
where
|
||||
F: Fn(),
|
||||
{
|
||||
let mut press_time = state_var.borrow_mut();
|
||||
if let Ok(elapsed) = press_time.elapsed() {
|
||||
if elapsed.as_millis() < interval {
|
||||
|
|
|
@ -17,18 +17,20 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::path::{PathBuf, Path};
|
||||
use crate::package::{PackageIndex, UpdateResult, Package, InstallResult, RemoveResult, PackageResolver};
|
||||
use std::error::Error;
|
||||
use std::fs::{File, create_dir};
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use crate::package::UpdateResult::{NotOutdated, Updated};
|
||||
use crate::package::InstallResult::{NotFoundInIndex, AlreadyInstalled, BlockedExternalPackage};
|
||||
use std::fs;
|
||||
use regex::Regex;
|
||||
use crate::package::InstallResult::{AlreadyInstalled, BlockedExternalPackage, NotFoundInIndex};
|
||||
use crate::package::RemoveResult::Removed;
|
||||
use crate::package::UpdateResult::{NotOutdated, Updated};
|
||||
use crate::package::{
|
||||
InstallResult, Package, PackageIndex, PackageResolver, RemoveResult, UpdateResult,
|
||||
};
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::fs::{create_dir, File};
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
const DEFAULT_PACKAGE_INDEX_FILE: &str = "package_index.json";
|
||||
|
||||
|
@ -42,18 +44,24 @@ pub struct DefaultPackageManager {
|
|||
}
|
||||
|
||||
impl DefaultPackageManager {
|
||||
pub fn new(package_dir: PathBuf, data_dir: PathBuf, package_resolver: Option<Box<dyn PackageResolver>>) -> DefaultPackageManager {
|
||||
pub fn new(
|
||||
package_dir: PathBuf,
|
||||
data_dir: PathBuf,
|
||||
package_resolver: Option<Box<dyn PackageResolver>>,
|
||||
) -> DefaultPackageManager {
|
||||
let local_index = Self::load_local_index(&data_dir);
|
||||
|
||||
DefaultPackageManager {
|
||||
package_dir,
|
||||
data_dir,
|
||||
package_resolver,
|
||||
local_index
|
||||
local_index,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_default(package_resolver: Option<Box<dyn PackageResolver>>) -> DefaultPackageManager {
|
||||
pub fn new_default(
|
||||
package_resolver: Option<Box<dyn PackageResolver>>,
|
||||
) -> DefaultPackageManager {
|
||||
DefaultPackageManager::new(
|
||||
crate::context::get_package_dir(),
|
||||
crate::context::get_data_dir(),
|
||||
|
@ -72,7 +80,7 @@ impl DefaultPackageManager {
|
|||
let local_index = serde_json::from_reader(reader);
|
||||
|
||||
if let Ok(local_index) = local_index {
|
||||
return local_index
|
||||
return local_index;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,7 +89,8 @@ impl DefaultPackageManager {
|
|||
|
||||
fn request_index() -> Result<super::PackageIndex, Box<dyn Error>> {
|
||||
let client = reqwest::Client::new();
|
||||
let request = client.get("https://hub.espanso.org/json/")
|
||||
let request = client
|
||||
.get("https://hub.espanso.org/json/")
|
||||
.header("User-Agent", format!("espanso/{}", crate::VERSION));
|
||||
|
||||
let mut res = request.send()?;
|
||||
|
@ -93,7 +102,8 @@ impl DefaultPackageManager {
|
|||
|
||||
fn parse_package_from_readme(readme_path: &Path) -> Option<Package> {
|
||||
lazy_static! {
|
||||
static ref FIELD_REGEX: Regex = Regex::new(r###"^\s*(.*?)\s*:\s*"?(.*?)"?$"###).unwrap();
|
||||
static ref FIELD_REGEX: Regex =
|
||||
Regex::new(r###"^\s*(.*?)\s*:\s*"?(.*?)"?$"###).unwrap();
|
||||
}
|
||||
|
||||
// Read readme line by line
|
||||
|
@ -109,7 +119,7 @@ impl DefaultPackageManager {
|
|||
let line = line.unwrap();
|
||||
if line.contains("---") {
|
||||
if started {
|
||||
break
|
||||
break;
|
||||
} else {
|
||||
started = true;
|
||||
}
|
||||
|
@ -119,20 +129,23 @@ impl DefaultPackageManager {
|
|||
let property = caps.get(1);
|
||||
let value = caps.get(2);
|
||||
if property.is_some() && value.is_some() {
|
||||
fields.insert(property.unwrap().as_str().to_owned(),
|
||||
value.unwrap().as_str().to_owned());
|
||||
fields.insert(
|
||||
property.unwrap().as_str().to_owned(),
|
||||
value.unwrap().as_str().to_owned(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !fields.contains_key("package_name") ||
|
||||
!fields.contains_key("package_title") ||
|
||||
!fields.contains_key("package_version") ||
|
||||
!fields.contains_key("package_repo") ||
|
||||
!fields.contains_key("package_desc") ||
|
||||
!fields.contains_key("package_author") {
|
||||
return None
|
||||
if !fields.contains_key("package_name")
|
||||
|| !fields.contains_key("package_title")
|
||||
|| !fields.contains_key("package_version")
|
||||
|| !fields.contains_key("package_repo")
|
||||
|| !fields.contains_key("package_desc")
|
||||
|| !fields.contains_key("package_author")
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let original_repo = if fields.contains_key("package_original_repo") {
|
||||
|
@ -159,7 +172,7 @@ impl DefaultPackageManager {
|
|||
desc: fields.get("package_desc").unwrap().clone(),
|
||||
author: fields.get("package_author").unwrap().clone(),
|
||||
is_core,
|
||||
original_repo
|
||||
original_repo,
|
||||
};
|
||||
|
||||
Some(package)
|
||||
|
@ -170,7 +183,7 @@ impl DefaultPackageManager {
|
|||
|
||||
fn local_index_timestamp(&self) -> u64 {
|
||||
if let Some(local_index) = &self.local_index {
|
||||
return local_index.last_update
|
||||
return local_index.last_update;
|
||||
}
|
||||
|
||||
0
|
||||
|
@ -198,7 +211,8 @@ impl DefaultPackageManager {
|
|||
|
||||
fn cache_local_index(&self) {
|
||||
if let Some(local_index) = &self.local_index {
|
||||
let serialized = serde_json::to_string(local_index).expect("Unable to serialize local index");
|
||||
let serialized =
|
||||
serde_json::to_string(local_index).expect("Unable to serialize local index");
|
||||
let local_index_file = self.data_dir.join(DEFAULT_PACKAGE_INDEX_FILE);
|
||||
std::fs::write(local_index_file, serialized).expect("Unable to cache local index");
|
||||
}
|
||||
|
@ -207,7 +221,9 @@ impl DefaultPackageManager {
|
|||
|
||||
impl super::PackageManager for DefaultPackageManager {
|
||||
fn is_index_outdated(&self) -> bool {
|
||||
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards");
|
||||
let current_timestamp = current_time.as_secs();
|
||||
|
||||
let local_index_timestamp = self.local_index_timestamp();
|
||||
|
@ -232,18 +248,23 @@ impl super::PackageManager for DefaultPackageManager {
|
|||
|
||||
fn get_package(&self, name: &str) -> Option<Package> {
|
||||
if let Some(local_index) = &self.local_index {
|
||||
let result = local_index.packages.iter().find(|package| {
|
||||
package.name == name
|
||||
});
|
||||
let result = local_index
|
||||
.packages
|
||||
.iter()
|
||||
.find(|package| package.name == name);
|
||||
if let Some(package) = result {
|
||||
return Some(package.clone())
|
||||
return Some(package.clone());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn install_package(&self, name: &str, allow_external: bool) -> Result<InstallResult, Box<dyn Error>> {
|
||||
fn install_package(
|
||||
&self,
|
||||
name: &str,
|
||||
allow_external: bool,
|
||||
) -> Result<InstallResult, Box<dyn Error>> {
|
||||
let package = self.get_package(name);
|
||||
match package {
|
||||
Some(package) => {
|
||||
|
@ -252,21 +273,28 @@ impl super::PackageManager for DefaultPackageManager {
|
|||
} else {
|
||||
Ok(BlockedExternalPackage(package.original_repo))
|
||||
}
|
||||
},
|
||||
None => {
|
||||
Ok(NotFoundInIndex)
|
||||
},
|
||||
}
|
||||
None => Ok(NotFoundInIndex),
|
||||
}
|
||||
}
|
||||
|
||||
fn install_package_from_repo(&self, name: &str, repo_url: &str) -> Result<InstallResult, Box<dyn Error>> {
|
||||
fn install_package_from_repo(
|
||||
&self,
|
||||
name: &str,
|
||||
repo_url: &str,
|
||||
) -> Result<InstallResult, Box<dyn Error>> {
|
||||
// Check if package is already installed
|
||||
let packages = self.list_local_packages_names();
|
||||
if packages.iter().any(|p| p == name) { // Package already installed
|
||||
if packages.iter().any(|p| p == name) {
|
||||
// Package already installed
|
||||
return Ok(AlreadyInstalled);
|
||||
}
|
||||
|
||||
let temp_dir = self.package_resolver.as_ref().unwrap().clone_repo_to_temp(repo_url)?;
|
||||
let temp_dir = self
|
||||
.package_resolver
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.clone_repo_to_temp(repo_url)?;
|
||||
|
||||
let temp_package_dir = temp_dir.path().join(name);
|
||||
if !temp_package_dir.exists() {
|
||||
|
@ -329,15 +357,16 @@ impl super::PackageManager for DefaultPackageManager {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::{TempDir, NamedTempFile};
|
||||
use std::path::Path;
|
||||
use crate::package::zip::ZipPackageResolver;
|
||||
use crate::package::InstallResult::*;
|
||||
use crate::package::PackageManager;
|
||||
use std::fs::{create_dir, create_dir_all};
|
||||
use crate::package::InstallResult::*;
|
||||
use crate::package::zip::ZipPackageResolver;
|
||||
use std::path::Path;
|
||||
use tempfile::{NamedTempFile, TempDir};
|
||||
|
||||
const OUTDATED_INDEX_CONTENT: &str = include_str!("../res/test/outdated_index.json");
|
||||
const INDEX_CONTENT_WITHOUT_UPDATE: &str = include_str!("../res/test/index_without_update.json");
|
||||
const INDEX_CONTENT_WITHOUT_UPDATE: &str =
|
||||
include_str!("../res/test/index_without_update.json");
|
||||
const GET_PACKAGE_INDEX: &str = include_str!("../res/test/get_package_index.json");
|
||||
const INSTALL_PACKAGE_INDEX: &str = include_str!("../res/test/install_package_index.json");
|
||||
|
||||
|
@ -347,7 +376,10 @@ mod tests {
|
|||
package_manager: DefaultPackageManager,
|
||||
}
|
||||
|
||||
fn create_temp_package_manager<F>(setup: F) -> TempPackageManager where F: Fn(&Path, &Path) -> (){
|
||||
fn create_temp_package_manager<F>(setup: F) -> TempPackageManager
|
||||
where
|
||||
F: Fn(&Path, &Path) -> (),
|
||||
{
|
||||
let package_dir = TempDir::new().expect("unable to create temp directory");
|
||||
let data_dir = TempDir::new().expect("unable to create temp directory");
|
||||
|
||||
|
@ -362,7 +394,7 @@ mod tests {
|
|||
TempPackageManager {
|
||||
package_dir,
|
||||
data_dir,
|
||||
package_manager
|
||||
package_manager,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -389,26 +421,38 @@ mod tests {
|
|||
fn test_up_to_date_index_should_not_be_updated() {
|
||||
let mut temp = create_temp_package_manager(|_, data_dir| {
|
||||
let index_file = data_dir.join(DEFAULT_PACKAGE_INDEX_FILE);
|
||||
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards");
|
||||
let current_timestamp = current_time.as_secs();
|
||||
let new_contents = INDEX_CONTENT_WITHOUT_UPDATE.replace("XXXX", &format!("{}", current_timestamp));
|
||||
let new_contents =
|
||||
INDEX_CONTENT_WITHOUT_UPDATE.replace("XXXX", &format!("{}", current_timestamp));
|
||||
std::fs::write(index_file, new_contents).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.update_index(false).unwrap(), UpdateResult::NotOutdated);
|
||||
assert_eq!(
|
||||
temp.package_manager.update_index(false).unwrap(),
|
||||
UpdateResult::NotOutdated
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_up_to_date_index_with_force_should_be_updated() {
|
||||
let mut temp = create_temp_package_manager(|_, data_dir| {
|
||||
let index_file = data_dir.join(DEFAULT_PACKAGE_INDEX_FILE);
|
||||
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Time went backwards");
|
||||
let current_timestamp = current_time.as_secs();
|
||||
let new_contents = INDEX_CONTENT_WITHOUT_UPDATE.replace("XXXX", &format!("{}", current_timestamp));
|
||||
let new_contents =
|
||||
INDEX_CONTENT_WITHOUT_UPDATE.replace("XXXX", &format!("{}", current_timestamp));
|
||||
std::fs::write(index_file, new_contents).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.update_index(true).unwrap(), UpdateResult::Updated);
|
||||
assert_eq!(
|
||||
temp.package_manager.update_index(true).unwrap(),
|
||||
UpdateResult::Updated
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -418,15 +462,25 @@ mod tests {
|
|||
std::fs::write(index_file, OUTDATED_INDEX_CONTENT).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.update_index(false).unwrap(), UpdateResult::Updated);
|
||||
assert_eq!(
|
||||
temp.package_manager.update_index(false).unwrap(),
|
||||
UpdateResult::Updated
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_update_index_should_create_file() {
|
||||
let mut temp = create_temp_package_manager(|_, _| {});
|
||||
|
||||
assert_eq!(temp.package_manager.update_index(false).unwrap(), UpdateResult::Updated);
|
||||
assert!(temp.data_dir.path().join(DEFAULT_PACKAGE_INDEX_FILE).exists())
|
||||
assert_eq!(
|
||||
temp.package_manager.update_index(false).unwrap(),
|
||||
UpdateResult::Updated
|
||||
);
|
||||
assert!(temp
|
||||
.data_dir
|
||||
.path()
|
||||
.join(DEFAULT_PACKAGE_INDEX_FILE)
|
||||
.exists())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -436,7 +490,13 @@ mod tests {
|
|||
std::fs::write(index_file, GET_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.get_package("italian-accents").unwrap().title, "Italian Accents");
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.get_package("italian-accents")
|
||||
.unwrap()
|
||||
.title,
|
||||
"Italian Accents"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -470,7 +530,12 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("doesnotexist", false).unwrap(), NotFoundInIndex);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("doesnotexist", false)
|
||||
.unwrap(),
|
||||
NotFoundInIndex
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -481,7 +546,12 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("italian-accents", false).unwrap(), AlreadyInstalled);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("italian-accents", false)
|
||||
.unwrap(),
|
||||
AlreadyInstalled
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -491,10 +561,23 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("dummy-package", false).unwrap(), Installed);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("dummy-package", false)
|
||||
.unwrap(),
|
||||
Installed
|
||||
);
|
||||
assert!(temp.package_dir.path().join("dummy-package").exists());
|
||||
assert!(temp.package_dir.path().join("dummy-package/README.md").exists());
|
||||
assert!(temp.package_dir.path().join("dummy-package/package.yml").exists());
|
||||
assert!(temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/README.md")
|
||||
.exists());
|
||||
assert!(temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/package.yml")
|
||||
.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -504,7 +587,12 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("not-existing", false).unwrap(), NotFoundInRepo);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("not-existing", false)
|
||||
.unwrap(),
|
||||
NotFoundInRepo
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -514,7 +602,12 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("dummy-package2", false).unwrap(), MissingPackageVersion);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("dummy-package2", false)
|
||||
.unwrap(),
|
||||
MissingPackageVersion
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -524,7 +617,12 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("dummy-package3", false).unwrap(), UnableToParsePackageInfo);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("dummy-package3", false)
|
||||
.unwrap(),
|
||||
UnableToParsePackageInfo
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -534,7 +632,12 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("dummy-package4", false).unwrap(), UnableToParsePackageInfo);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("dummy-package4", false)
|
||||
.unwrap(),
|
||||
UnableToParsePackageInfo
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -544,10 +647,23 @@ mod tests {
|
|||
std::fs::write(index_file, INSTALL_PACKAGE_INDEX).unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(temp.package_manager.install_package("dummy-package", false).unwrap(), Installed);
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.install_package("dummy-package", false)
|
||||
.unwrap(),
|
||||
Installed
|
||||
);
|
||||
assert!(temp.package_dir.path().join("dummy-package").exists());
|
||||
assert!(temp.package_dir.path().join("dummy-package/README.md").exists());
|
||||
assert!(temp.package_dir.path().join("dummy-package/package.yml").exists());
|
||||
assert!(temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/README.md")
|
||||
.exists());
|
||||
assert!(temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/package.yml")
|
||||
.exists());
|
||||
|
||||
let list = temp.package_manager.list_local_packages();
|
||||
assert_eq!(list.len(), 1);
|
||||
|
@ -564,25 +680,51 @@ mod tests {
|
|||
});
|
||||
|
||||
assert!(temp.package_dir.path().join("dummy-package").exists());
|
||||
assert!(temp.package_dir.path().join("dummy-package/README.md").exists());
|
||||
assert!(temp.package_dir.path().join("dummy-package/package.yml").exists());
|
||||
assert_eq!(temp.package_manager.remove_package("dummy-package").unwrap(), RemoveResult::Removed);
|
||||
assert!(temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/README.md")
|
||||
.exists());
|
||||
assert!(temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/package.yml")
|
||||
.exists());
|
||||
assert_eq!(
|
||||
temp.package_manager
|
||||
.remove_package("dummy-package")
|
||||
.unwrap(),
|
||||
RemoveResult::Removed
|
||||
);
|
||||
assert!(!temp.package_dir.path().join("dummy-package").exists());
|
||||
assert!(!temp.package_dir.path().join("dummy-package/README.md").exists());
|
||||
assert!(!temp.package_dir.path().join("dummy-package/package.yml").exists());
|
||||
assert!(!temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/README.md")
|
||||
.exists());
|
||||
assert!(!temp
|
||||
.package_dir
|
||||
.path()
|
||||
.join("dummy-package/package.yml")
|
||||
.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_package_not_found() {
|
||||
let temp = create_temp_package_manager(|_, _| {});
|
||||
|
||||
assert_eq!(temp.package_manager.remove_package("not-existing").unwrap(), RemoveResult::NotFound);
|
||||
assert_eq!(
|
||||
temp.package_manager.remove_package("not-existing").unwrap(),
|
||||
RemoveResult::NotFound
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_package_from_readme() {
|
||||
let file = NamedTempFile::new().unwrap();
|
||||
fs::write(file.path(), r###"
|
||||
fs::write(
|
||||
file.path(),
|
||||
r###"
|
||||
---
|
||||
package_name: "italian-accents"
|
||||
package_title: "Italian Accents"
|
||||
|
@ -592,7 +734,9 @@ mod tests {
|
|||
package_repo: "https://github.com/federico-terzi/espanso-hub-core"
|
||||
is_core: true
|
||||
---
|
||||
"###).unwrap();
|
||||
"###,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let package = DefaultPackageManager::parse_package_from_readme(file.path()).unwrap();
|
||||
|
||||
|
@ -613,7 +757,9 @@ mod tests {
|
|||
#[test]
|
||||
fn test_parse_package_from_readme_with_bad_metadata() {
|
||||
let file = NamedTempFile::new().unwrap();
|
||||
fs::write(file.path(), r###"
|
||||
fs::write(
|
||||
file.path(),
|
||||
r###"
|
||||
---
|
||||
package_name: italian-accents
|
||||
package_title: "Italian Accents"
|
||||
|
@ -624,7 +770,9 @@ mod tests {
|
|||
is_core: true
|
||||
---
|
||||
Readme text
|
||||
"###).unwrap();
|
||||
"###,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let package = DefaultPackageManager::parse_package_from_readme(file.path()).unwrap();
|
||||
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
pub(crate) mod zip;
|
||||
pub(crate) mod default;
|
||||
pub(crate) mod zip;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
use tempfile::TempDir;
|
||||
|
||||
|
@ -30,8 +30,16 @@ pub trait PackageManager {
|
|||
|
||||
fn get_package(&self, name: &str) -> Option<Package>;
|
||||
|
||||
fn install_package(&self, name: &str, allow_external: bool) -> Result<InstallResult, Box<dyn Error>>;
|
||||
fn install_package_from_repo(&self, name: &str, repo_url: &str) -> Result<InstallResult, Box<dyn Error>>;
|
||||
fn install_package(
|
||||
&self,
|
||||
name: &str,
|
||||
allow_external: bool,
|
||||
) -> Result<InstallResult, Box<dyn Error>>;
|
||||
fn install_package_from_repo(
|
||||
&self,
|
||||
name: &str,
|
||||
repo_url: &str,
|
||||
) -> Result<InstallResult, Box<dyn Error>>;
|
||||
|
||||
fn remove_package(&self, name: &str) -> Result<RemoveResult, Box<dyn Error>>;
|
||||
|
||||
|
@ -57,18 +65,21 @@ pub struct Package {
|
|||
pub original_repo: String,
|
||||
}
|
||||
|
||||
fn default_is_core() -> bool {false}
|
||||
fn default_original_repo() -> String {"".to_owned()}
|
||||
fn default_is_core() -> bool {
|
||||
false
|
||||
}
|
||||
fn default_original_repo() -> String {
|
||||
"".to_owned()
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||
pub struct PackageIndex {
|
||||
#[serde(rename = "lastUpdate")]
|
||||
pub last_update: u64,
|
||||
|
||||
pub packages: Vec<Package>
|
||||
pub packages: Vec<Package>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum UpdateResult {
|
||||
NotOutdated,
|
||||
|
@ -83,11 +94,11 @@ pub enum InstallResult {
|
|||
MissingPackageVersion,
|
||||
AlreadyInstalled,
|
||||
Installed,
|
||||
BlockedExternalPackage(String)
|
||||
BlockedExternalPackage(String),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum RemoveResult {
|
||||
NotFound,
|
||||
Removed
|
||||
Removed,
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
use tempfile::TempDir;
|
||||
use std::error::Error;
|
||||
use std::io::{Cursor, copy};
|
||||
use std::{fs, io};
|
||||
use log::debug;
|
||||
use std::error::Error;
|
||||
use std::io::{copy, Cursor};
|
||||
use std::{fs, io};
|
||||
use tempfile::TempDir;
|
||||
|
||||
pub struct ZipPackageResolver;
|
||||
|
||||
|
@ -54,10 +54,19 @@ impl super::PackageResolver for ZipPackageResolver {
|
|||
}
|
||||
|
||||
if (&*file.name()).ends_with('/') {
|
||||
debug!("File {} extracted to \"{}\"", i, outpath.as_path().display());
|
||||
debug!(
|
||||
"File {} extracted to \"{}\"",
|
||||
i,
|
||||
outpath.as_path().display()
|
||||
);
|
||||
fs::create_dir_all(&outpath).unwrap();
|
||||
} else {
|
||||
debug!("File {} extracted to \"{}\" ({} bytes)", i, outpath.as_path().display(), file.size());
|
||||
debug!(
|
||||
"File {} extracted to \"{}\" ({} bytes)",
|
||||
i,
|
||||
outpath.as_path().display(),
|
||||
file.size()
|
||||
);
|
||||
if let Some(p) = outpath.parent() {
|
||||
if !p.exists() {
|
||||
fs::create_dir_all(&p).unwrap();
|
||||
|
@ -74,13 +83,15 @@ impl super::PackageResolver for ZipPackageResolver {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::PackageResolver;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_clone_temp_repository() {
|
||||
let resolver = ZipPackageResolver::new();
|
||||
let cloned_dir = resolver.clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core").unwrap();
|
||||
let cloned_dir = resolver
|
||||
.clone_repo_to_temp("https://github.com/federico-terzi/espanso-hub-core")
|
||||
.unwrap();
|
||||
assert!(cloned_dir.path().join("LICENSE").exists());
|
||||
}
|
||||
}
|
|
@ -22,9 +22,7 @@ use widestring::WideCString;
|
|||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn spawn_process(cmd: &str, args: &Vec<String>) {
|
||||
let quoted_args: Vec<String> = args.iter().map(|arg| {
|
||||
format!("\"{}\"", arg)
|
||||
}).collect();
|
||||
let quoted_args: Vec<String> = args.iter().map(|arg| format!("\"{}\"", arg)).collect();
|
||||
let quoted_args = quoted_args.join(" ");
|
||||
let final_cmd = format!("\"{}\" {}", cmd, quoted_args);
|
||||
unsafe {
|
||||
|
|
|
@ -17,14 +17,14 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::mpsc::Sender;
|
||||
use crate::event::{Event, SystemEvent};
|
||||
use crate::event::ActionType;
|
||||
use std::io::{BufReader, Read, Write};
|
||||
use std::error::Error;
|
||||
use log::error;
|
||||
use crate::config::Configs;
|
||||
use crate::event::ActionType;
|
||||
use crate::event::{Event, SystemEvent};
|
||||
use log::error;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::error::Error;
|
||||
use std::io::{BufReader, Read, Write};
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
|
@ -47,7 +47,6 @@ pub fn send_command_or_warn(service: Service, configs: Configs, command: IPCComm
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct IPCCommand {
|
||||
pub id: String,
|
||||
|
@ -59,41 +58,50 @@ pub struct IPCCommand {
|
|||
impl IPCCommand {
|
||||
fn to_event(&self) -> Option<Event> {
|
||||
match self.id.as_ref() {
|
||||
"exit" => {
|
||||
Some(Event::Action(ActionType::Exit))
|
||||
},
|
||||
"wexit" => {
|
||||
Some(Event::Action(ActionType::ExitWorker))
|
||||
},
|
||||
"toggle" => {
|
||||
Some(Event::Action(ActionType::Toggle))
|
||||
},
|
||||
"enable" => {
|
||||
Some(Event::Action(ActionType::Enable))
|
||||
},
|
||||
"disable" => {
|
||||
Some(Event::Action(ActionType::Disable))
|
||||
},
|
||||
"restartworker" => {
|
||||
Some(Event::Action(ActionType::RestartWorker))
|
||||
},
|
||||
"notify" => {
|
||||
Some(Event::System(SystemEvent::NotifyRequest(self.payload.clone())))
|
||||
},
|
||||
_ => None
|
||||
"exit" => Some(Event::Action(ActionType::Exit)),
|
||||
"wexit" => Some(Event::Action(ActionType::ExitWorker)),
|
||||
"toggle" => Some(Event::Action(ActionType::Toggle)),
|
||||
"enable" => Some(Event::Action(ActionType::Enable)),
|
||||
"disable" => Some(Event::Action(ActionType::Disable)),
|
||||
"restartworker" => Some(Event::Action(ActionType::RestartWorker)),
|
||||
"notify" => Some(Event::System(SystemEvent::NotifyRequest(
|
||||
self.payload.clone(),
|
||||
))),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from(event: Event) -> Option<IPCCommand> {
|
||||
match event {
|
||||
Event::Action(ActionType::Exit) => Some(IPCCommand{id: "exit".to_owned(), payload: "".to_owned()}),
|
||||
Event::Action(ActionType::ExitWorker) => Some(IPCCommand{id: "wexit".to_owned(), payload: "".to_owned()}),
|
||||
Event::Action(ActionType::Toggle) => Some(IPCCommand{id: "toggle".to_owned(), payload: "".to_owned()}),
|
||||
Event::Action(ActionType::Enable) => Some(IPCCommand{id: "enable".to_owned(), payload: "".to_owned()}),
|
||||
Event::Action(ActionType::Disable) => Some(IPCCommand{id: "disable".to_owned(), payload: "".to_owned()}),
|
||||
Event::Action(ActionType::RestartWorker) => Some(IPCCommand{id: "restartworker".to_owned(), payload: "".to_owned()}),
|
||||
Event::System(SystemEvent::NotifyRequest(message)) => Some(IPCCommand{id: "notify".to_owned(), payload: message}),
|
||||
_ => None
|
||||
Event::Action(ActionType::Exit) => Some(IPCCommand {
|
||||
id: "exit".to_owned(),
|
||||
payload: "".to_owned(),
|
||||
}),
|
||||
Event::Action(ActionType::ExitWorker) => Some(IPCCommand {
|
||||
id: "wexit".to_owned(),
|
||||
payload: "".to_owned(),
|
||||
}),
|
||||
Event::Action(ActionType::Toggle) => Some(IPCCommand {
|
||||
id: "toggle".to_owned(),
|
||||
payload: "".to_owned(),
|
||||
}),
|
||||
Event::Action(ActionType::Enable) => Some(IPCCommand {
|
||||
id: "enable".to_owned(),
|
||||
payload: "".to_owned(),
|
||||
}),
|
||||
Event::Action(ActionType::Disable) => Some(IPCCommand {
|
||||
id: "disable".to_owned(),
|
||||
payload: "".to_owned(),
|
||||
}),
|
||||
Event::Action(ActionType::RestartWorker) => Some(IPCCommand {
|
||||
id: "restartworker".to_owned(),
|
||||
payload: "".to_owned(),
|
||||
}),
|
||||
Event::System(SystemEvent::NotifyRequest(message)) => Some(IPCCommand {
|
||||
id: "notify".to_owned(),
|
||||
payload: message,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,17 +135,18 @@ fn process_event<R: Read, E: Error>(event_channel: &Sender<Event>, stream: Resul
|
|||
let res = buf_reader.read_to_string(&mut json_str);
|
||||
|
||||
if res.is_ok() {
|
||||
let command : Result<IPCCommand, serde_json::Error> = serde_json::from_str(&json_str);
|
||||
let command: Result<IPCCommand, serde_json::Error> =
|
||||
serde_json::from_str(&json_str);
|
||||
match command {
|
||||
Ok(command) => {
|
||||
let event = command.to_event();
|
||||
if let Some(event) = event {
|
||||
event_channel.send(event).expect("Broken event channel");
|
||||
}
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error deserializing JSON command: {}", e);
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -147,7 +156,10 @@ fn process_event<R: Read, E: Error>(event_channel: &Sender<Event>, stream: Resul
|
|||
}
|
||||
}
|
||||
|
||||
fn send_command<W: Write, E: Error>(command: IPCCommand, stream: Result<W, E>) -> Result<(), String>{
|
||||
fn send_command<W: Write, E: Error>(
|
||||
command: IPCCommand,
|
||||
stream: Result<W, E>,
|
||||
) -> Result<(), String> {
|
||||
match stream {
|
||||
Ok(mut stream) => {
|
||||
let json_str = serde_json::to_string(&command);
|
||||
|
@ -155,12 +167,10 @@ fn send_command<W: Write, E: Error>(command: IPCCommand, stream: Result<W, E>) -
|
|||
stream.write_all(json_str.as_bytes()).unwrap_or_else(|e| {
|
||||
println!("Can't write to IPC socket: {}", e);
|
||||
});
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
return Err(format!("Can't connect to daemon: {}", e))
|
||||
}
|
||||
Err(e) => return Err(format!("Can't connect to daemon: {}", e)),
|
||||
}
|
||||
|
||||
Err("Can't send command".to_owned())
|
||||
|
@ -173,7 +183,11 @@ pub enum Service {
|
|||
|
||||
// UNIX IMPLEMENTATION
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
pub fn get_ipc_server(service: Service, _: Configs, event_channel: Sender<Event>) -> impl IPCServer {
|
||||
pub fn get_ipc_server(
|
||||
service: Service,
|
||||
_: Configs,
|
||||
event_channel: Sender<Event>,
|
||||
) -> impl IPCServer {
|
||||
unix::UnixIPCServer::new(service, event_channel)
|
||||
}
|
||||
|
||||
|
@ -184,7 +198,11 @@ pub fn get_ipc_client(service: Service, _: Configs) -> impl IPCClient {
|
|||
|
||||
// WINDOWS IMPLEMENTATION
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn get_ipc_server(service: Service, config: Configs, event_channel: Sender<Event>) -> impl IPCServer {
|
||||
pub fn get_ipc_server(
|
||||
service: Service,
|
||||
config: Configs,
|
||||
event_channel: Sender<Event>,
|
||||
) -> impl IPCServer {
|
||||
windows::WindowsIPCServer::new(service, config, event_channel)
|
||||
}
|
||||
|
||||
|
|
|
@ -17,15 +17,15 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::os::unix::net::{UnixStream,UnixListener};
|
||||
use log::{info, warn};
|
||||
use std::sync::mpsc::Sender;
|
||||
use super::IPCCommand;
|
||||
use log::{info, warn};
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
use super::Service;
|
||||
use crate::context;
|
||||
use crate::event::*;
|
||||
use crate::protocol::{process_event, send_command};
|
||||
use super::Service;
|
||||
|
||||
const DAEMON_UNIX_SOCKET_NAME: &str = "espanso.sock";
|
||||
const WORKER_UNIX_SOCKET_NAME: &str = "worker.sock";
|
||||
|
@ -39,15 +39,15 @@ impl UnixIPCServer {
|
|||
pub fn new(service: Service, event_channel: Sender<Event>) -> UnixIPCServer {
|
||||
UnixIPCServer {
|
||||
service,
|
||||
event_channel
|
||||
event_channel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_unix_name(service: &Service) -> String {
|
||||
match service {
|
||||
Service::Daemon => {DAEMON_UNIX_SOCKET_NAME.to_owned()},
|
||||
Service::Worker => {WORKER_UNIX_SOCKET_NAME.to_owned()},
|
||||
Service::Daemon => DAEMON_UNIX_SOCKET_NAME.to_owned(),
|
||||
Service::Worker => WORKER_UNIX_SOCKET_NAME.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,21 +55,28 @@ impl super::IPCServer for UnixIPCServer {
|
|||
fn start(&self) {
|
||||
let event_channel = self.event_channel.clone();
|
||||
let socket_name = get_unix_name(&self.service);
|
||||
std::thread::Builder::new().name("ipc_server".to_string()).spawn(move || {
|
||||
std::thread::Builder::new()
|
||||
.name("ipc_server".to_string())
|
||||
.spawn(move || {
|
||||
let espanso_dir = context::get_data_dir();
|
||||
let unix_socket = espanso_dir.join(socket_name);
|
||||
|
||||
std::fs::remove_file(unix_socket.clone()).unwrap_or_else(|e| {
|
||||
warn!("Unable to delete Unix socket: {}", e);
|
||||
});
|
||||
let listener = UnixListener::bind(unix_socket.clone()).expect("Can't bind to Unix Socket");
|
||||
let listener =
|
||||
UnixListener::bind(unix_socket.clone()).expect("Can't bind to Unix Socket");
|
||||
|
||||
info!("Binded to IPC unix socket: {}", unix_socket.as_path().display());
|
||||
info!(
|
||||
"Binded to IPC unix socket: {}",
|
||||
unix_socket.as_path().display()
|
||||
);
|
||||
|
||||
for stream in listener.incoming() {
|
||||
process_event(&event_channel, stream);
|
||||
}
|
||||
}).expect("Unable to spawn IPC server thread");
|
||||
})
|
||||
.expect("Unable to spawn IPC server thread");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,14 +17,14 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use log::{info};
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use super::IPCCommand;
|
||||
use log::info;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
use crate::config::Configs;
|
||||
use crate::event::*;
|
||||
use crate::protocol::{process_event, send_command, Service};
|
||||
use crate::config::{Configs};
|
||||
|
||||
pub struct WindowsIPCServer {
|
||||
service: Service,
|
||||
|
@ -34,15 +34,23 @@ pub struct WindowsIPCServer {
|
|||
|
||||
fn to_port(config: &Configs, service: &Service) -> u16 {
|
||||
let port = match service {
|
||||
Service::Daemon => {config.ipc_server_port},
|
||||
Service::Worker => {config.worker_ipc_server_port},
|
||||
Service::Daemon => config.ipc_server_port,
|
||||
Service::Worker => config.worker_ipc_server_port,
|
||||
};
|
||||
port as u16
|
||||
}
|
||||
|
||||
impl WindowsIPCServer {
|
||||
pub fn new(service: Service, config: Configs, event_channel: Sender<Event>) -> WindowsIPCServer {
|
||||
WindowsIPCServer {service, config, event_channel}
|
||||
pub fn new(
|
||||
service: Service,
|
||||
config: Configs,
|
||||
event_channel: Sender<Event>,
|
||||
) -> WindowsIPCServer {
|
||||
WindowsIPCServer {
|
||||
service,
|
||||
config,
|
||||
event_channel,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,17 +58,22 @@ impl super::IPCServer for WindowsIPCServer {
|
|||
fn start(&self) {
|
||||
let event_channel = self.event_channel.clone();
|
||||
let server_port = to_port(&self.config, &self.service);
|
||||
std::thread::Builder::new().name("ipc_server".to_string()).spawn(move || {
|
||||
let listener = TcpListener::bind(
|
||||
format!("127.0.0.1:{}", server_port)
|
||||
).expect("Error binding to IPC server port");
|
||||
std::thread::Builder::new()
|
||||
.name("ipc_server".to_string())
|
||||
.spawn(move || {
|
||||
let listener = TcpListener::bind(format!("127.0.0.1:{}", server_port))
|
||||
.expect("Error binding to IPC server port");
|
||||
|
||||
info!("Binded to IPC tcp socket: {}", listener.local_addr().unwrap().to_string());
|
||||
info!(
|
||||
"Binded to IPC tcp socket: {}",
|
||||
listener.local_addr().unwrap().to_string()
|
||||
);
|
||||
|
||||
for stream in listener.incoming() {
|
||||
process_event(&event_channel, stream);
|
||||
}
|
||||
}).expect("Unable to spawn IPC server thread");
|
||||
})
|
||||
.expect("Unable to spawn IPC server thread");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,9 +91,7 @@ impl WindowsIPCClient {
|
|||
impl super::IPCClient for WindowsIPCClient {
|
||||
fn send_command(&self, command: IPCCommand) -> Result<(), String> {
|
||||
let port = to_port(&self.config, &self.service);
|
||||
let stream = TcpStream::connect(
|
||||
("127.0.0.1", port)
|
||||
);
|
||||
let stream = TcpStream::connect(("127.0.0.1", port));
|
||||
|
||||
send_command(command, stream)
|
||||
}
|
||||
|
|
|
@ -17,14 +17,14 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use serde_yaml::{Value};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use regex::{Regex, Captures};
|
||||
use log::{warn, error};
|
||||
use super::*;
|
||||
use crate::matcher::{Match, MatchContentType};
|
||||
use crate::config::Configs;
|
||||
use crate::extension::Extension;
|
||||
use crate::matcher::{Match, MatchContentType};
|
||||
use log::{error, warn};
|
||||
use regex::{Captures, Regex};
|
||||
use serde_yaml::Value;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
lazy_static! {
|
||||
static ref VAR_REGEX: Regex = Regex::new("\\{\\{\\s*(?P<name>\\w+)\\s*\\}\\}").unwrap();
|
||||
|
@ -47,8 +47,7 @@ impl DefaultRenderer {
|
|||
}
|
||||
|
||||
// Compile the regexes
|
||||
let passive_match_regex = Regex::new(&config.passive_match_regex)
|
||||
.unwrap_or_else(|e| {
|
||||
let passive_match_regex = Regex::new(&config.passive_match_regex).unwrap_or_else(|e| {
|
||||
panic!("Invalid passive match regex: {:?}", e);
|
||||
});
|
||||
|
||||
|
@ -69,7 +68,6 @@ impl DefaultRenderer {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
result
|
||||
|
@ -77,7 +75,13 @@ impl DefaultRenderer {
|
|||
}
|
||||
|
||||
impl super::Renderer for DefaultRenderer {
|
||||
fn render_match(&self, m: &Match, trigger_offset: usize, config: &Configs, args: Vec<String>) -> RenderResult {
|
||||
fn render_match(
|
||||
&self,
|
||||
m: &Match,
|
||||
trigger_offset: usize,
|
||||
config: &Configs,
|
||||
args: Vec<String>,
|
||||
) -> RenderResult {
|
||||
// Manage the different types of matches
|
||||
match &m.content {
|
||||
// Text Match
|
||||
|
@ -88,7 +92,7 @@ impl super::Renderer for DefaultRenderer {
|
|||
for caps in VAR_REGEX.captures_iter(&content.replace) {
|
||||
let var_name = caps.name("name").unwrap().as_str();
|
||||
target_vars.insert(var_name.to_owned());
|
||||
};
|
||||
}
|
||||
|
||||
let target_string = if target_vars.len() > 0 {
|
||||
let mut output_map = HashMap::new();
|
||||
|
@ -106,24 +110,32 @@ impl super::Renderer for DefaultRenderer {
|
|||
// Extract the match trigger from the variable params
|
||||
let trigger = variable.params.get(&Value::from("trigger"));
|
||||
if trigger.is_none() {
|
||||
warn!("Missing param 'trigger' in match variable: {}", variable.name);
|
||||
warn!(
|
||||
"Missing param 'trigger' in match variable: {}",
|
||||
variable.name
|
||||
);
|
||||
continue;
|
||||
}
|
||||
let trigger = trigger.unwrap();
|
||||
|
||||
// Find the given match from the active configs
|
||||
let inner_match = DefaultRenderer::find_match(config, trigger.as_str().unwrap_or(""));
|
||||
let inner_match =
|
||||
DefaultRenderer::find_match(config, trigger.as_str().unwrap_or(""));
|
||||
|
||||
if inner_match.is_none() {
|
||||
warn!("Could not find inner match with trigger: '{}'", trigger.as_str().unwrap_or("undefined"));
|
||||
continue
|
||||
warn!(
|
||||
"Could not find inner match with trigger: '{}'",
|
||||
trigger.as_str().unwrap_or("undefined")
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
let (inner_match, trigger_offset) = inner_match.unwrap();
|
||||
|
||||
// Render the inner match
|
||||
// TODO: inner arguments
|
||||
let result = self.render_match(&inner_match, trigger_offset, config, vec![]);
|
||||
let result =
|
||||
self.render_match(&inner_match, trigger_offset, config, vec![]);
|
||||
|
||||
// Inner matches are only supported for text-expansions, warn the user otherwise
|
||||
match result {
|
||||
|
@ -134,7 +146,8 @@ impl super::Renderer for DefaultRenderer {
|
|||
warn!("Inner matches must be of TEXT type. Mixing images is not supported yet.")
|
||||
},
|
||||
}
|
||||
}else{ // Normal extension variables
|
||||
} else {
|
||||
// Normal extension variables
|
||||
let extension = self.extension_map.get(&variable.var_type);
|
||||
if let Some(extension) = extension {
|
||||
let ext_out = extension.calculate(&variable.params, &args);
|
||||
|
@ -142,10 +155,16 @@ impl super::Renderer for DefaultRenderer {
|
|||
output_map.insert(variable.name.clone(), output);
|
||||
} else {
|
||||
output_map.insert(variable.name.clone(), "".to_owned());
|
||||
warn!("Could not generate output for variable: {}", variable.name);
|
||||
warn!(
|
||||
"Could not generate output for variable: {}",
|
||||
variable.name
|
||||
);
|
||||
}
|
||||
} else {
|
||||
error!("No extension found for variable type: {}", variable.var_type);
|
||||
error!(
|
||||
"No extension found for variable type: {}",
|
||||
variable.var_type
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -158,14 +177,14 @@ impl super::Renderer for DefaultRenderer {
|
|||
});
|
||||
|
||||
result.to_string()
|
||||
}else{ // No variables, simple text substitution
|
||||
} else {
|
||||
// No variables, simple text substitution
|
||||
content.replace.clone()
|
||||
};
|
||||
|
||||
// Unescape any brackets (needed to be able to insert double brackets in replacement
|
||||
// text, without triggering the variable system). See issue #187
|
||||
let target_string = target_string.replace("\\{", "{")
|
||||
.replace("\\}", "}");
|
||||
let target_string = target_string.replace("\\{", "{").replace("\\}", "}");
|
||||
|
||||
// Render any argument that may be present
|
||||
let target_string = utils::render_args(&target_string, &args);
|
||||
|
@ -177,9 +196,8 @@ impl super::Renderer for DefaultRenderer {
|
|||
// The check should be carried out from the position of the first
|
||||
// alphabetic letter
|
||||
// See issue #244
|
||||
let first_alphabetic = trigger.chars().position(|c| {
|
||||
c.is_alphabetic()
|
||||
}).unwrap_or(0);
|
||||
let first_alphabetic =
|
||||
trigger.chars().position(|c| c.is_alphabetic()).unwrap_or(0);
|
||||
|
||||
let first_char = trigger.chars().nth(first_alphabetic);
|
||||
let second_char = trigger.chars().nth(first_alphabetic + 1);
|
||||
|
@ -207,11 +225,13 @@ impl super::Renderer for DefaultRenderer {
|
|||
let mut v: Vec<char> = target_string.chars().collect();
|
||||
v[0] = v[0].to_uppercase().nth(0).unwrap();
|
||||
v.into_iter().collect()
|
||||
},
|
||||
2 => { // Full capitalization
|
||||
}
|
||||
2 => {
|
||||
// Full capitalization
|
||||
target_string.to_uppercase()
|
||||
},
|
||||
_ => { // Noop
|
||||
}
|
||||
_ => {
|
||||
// Noop
|
||||
target_string
|
||||
}
|
||||
}
|
||||
|
@ -220,7 +240,7 @@ impl super::Renderer for DefaultRenderer {
|
|||
};
|
||||
|
||||
RenderResult::Text(target_string)
|
||||
},
|
||||
}
|
||||
|
||||
// Image Match
|
||||
MatchContentType::Image(content) => {
|
||||
|
@ -231,20 +251,21 @@ impl super::Renderer for DefaultRenderer {
|
|||
error!("Image not found in path: {:?}", content.path);
|
||||
RenderResult::Error
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_passive(&self, text: &str, config: &Configs) -> RenderResult {
|
||||
// Render the matches
|
||||
let result = self.passive_match_regex.replace_all(&text, |caps: &Captures| {
|
||||
let result = self
|
||||
.passive_match_regex
|
||||
.replace_all(&text, |caps: &Captures| {
|
||||
let match_name = if let Some(name) = caps.name("name") {
|
||||
name.as_str()
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
|
||||
// Get the original matching string, useful to return the match untouched
|
||||
let original_match = caps.get(0).unwrap().as_str();
|
||||
|
||||
|
@ -262,21 +283,19 @@ impl super::Renderer for DefaultRenderer {
|
|||
} else {
|
||||
""
|
||||
};
|
||||
let args : Vec<String> = utils::split_args(match_args,
|
||||
let args: Vec<String> = utils::split_args(
|
||||
match_args,
|
||||
config.passive_arg_delimiter,
|
||||
config.passive_arg_escape);
|
||||
config.passive_arg_escape,
|
||||
);
|
||||
|
||||
let (m, trigger_offset) = m.unwrap();
|
||||
// Render the actual match
|
||||
let result = self.render_match(&m, trigger_offset, &config, args);
|
||||
|
||||
match result {
|
||||
RenderResult::Text(out) => {
|
||||
out
|
||||
},
|
||||
_ => {
|
||||
original_match.to_owned()
|
||||
}
|
||||
RenderResult::Text(out) => out,
|
||||
_ => original_match.to_owned(),
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -291,7 +310,10 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
fn get_renderer(config: Configs) -> DefaultRenderer {
|
||||
DefaultRenderer::new(vec![Box::new(crate::extension::dummy::DummyExtension::new())], config)
|
||||
DefaultRenderer::new(
|
||||
vec![Box::new(crate::extension::dummy::DummyExtension::new())],
|
||||
config,
|
||||
)
|
||||
}
|
||||
|
||||
fn get_config_for(s: &str) -> Configs {
|
||||
|
@ -303,10 +325,8 @@ mod tests {
|
|||
match rendered {
|
||||
RenderResult::Text(rendered) => {
|
||||
assert_eq!(rendered, target);
|
||||
},
|
||||
_ => {
|
||||
assert!(false)
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -316,11 +336,13 @@ mod tests {
|
|||
this text contains no matches
|
||||
"###;
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: test
|
||||
replace: result
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -333,11 +355,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_no_args() {
|
||||
let text = "this is a :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':test'
|
||||
replace: result
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -350,11 +374,13 @@ mod tests {
|
|||
fn test_render_passive_multiple_match_no_args() {
|
||||
let text = "this is a :test and then another :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':test'
|
||||
replace: result
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -373,11 +399,13 @@ mod tests {
|
|||
result
|
||||
"###;
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':test'
|
||||
replace: result
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -390,7 +418,8 @@ mod tests {
|
|||
fn test_render_passive_nested_matches_no_args() {
|
||||
let text = ":greet";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':greet'
|
||||
replace: "hi {{name}}"
|
||||
|
@ -402,7 +431,8 @@ mod tests {
|
|||
|
||||
- trigger: ':name'
|
||||
replace: john
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -415,11 +445,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_with_args() {
|
||||
let text = ":greet/Jon/";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':greet'
|
||||
replace: "Hi $0$"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -432,11 +464,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_with_multiple_args() {
|
||||
let text = ":greet/Jon/Snow/";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':greet'
|
||||
replace: "Hi $0$, there is $1$ outside"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -449,11 +483,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_with_escaped_args() {
|
||||
let text = ":greet/Jon/10\\/12/";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':greet'
|
||||
replace: "Hi $0$, today is $1$"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -466,11 +502,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_with_args_not_closed() {
|
||||
let text = ":greet/Jon/Snow";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':greet'
|
||||
replace: "Hi $0$"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -483,7 +521,8 @@ mod tests {
|
|||
fn test_render_passive_local_var() {
|
||||
let text = "this is :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':test'
|
||||
replace: "my {{output}}"
|
||||
|
@ -492,7 +531,8 @@ mod tests {
|
|||
type: dummy
|
||||
params:
|
||||
echo: "result"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -505,7 +545,8 @@ mod tests {
|
|||
fn test_render_passive_global_var() {
|
||||
let text = "this is :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
global_vars:
|
||||
- name: output
|
||||
type: dummy
|
||||
|
@ -515,7 +556,8 @@ mod tests {
|
|||
- trigger: ':test'
|
||||
replace: "my {{output}}"
|
||||
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -528,7 +570,8 @@ mod tests {
|
|||
fn test_render_passive_global_var_is_overridden_by_local() {
|
||||
let text = "this is :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
global_vars:
|
||||
- name: output
|
||||
type: dummy
|
||||
|
@ -543,7 +586,8 @@ mod tests {
|
|||
params:
|
||||
echo: "local"
|
||||
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -556,11 +600,13 @@ mod tests {
|
|||
fn test_render_match_with_unknown_variable_does_not_crash() {
|
||||
let text = "this is :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':test'
|
||||
replace: "my {{unknown}}"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -573,11 +619,13 @@ mod tests {
|
|||
fn test_render_escaped_double_brackets_should_not_consider_them_variable() {
|
||||
let text = "this is :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: ':test'
|
||||
replace: "my \\{\\{unknown\\}\\}"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -590,11 +638,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_multi_trigger_no_args() {
|
||||
let text = "this is a :yolo and :test";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- triggers: [':test', ':yolo']
|
||||
replace: result
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -607,11 +657,13 @@ mod tests {
|
|||
fn test_render_passive_simple_match_multi_trigger_with_args() {
|
||||
let text = ":yolo/Jon/";
|
||||
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- triggers: [':greet', ':yolo']
|
||||
replace: "Hi $0$"
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -622,12 +674,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_render_match_case_propagation_no_case() {
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: 'test'
|
||||
replace: result
|
||||
propagate_case: true
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -642,12 +696,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_render_match_case_propagation_first_capital() {
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: 'test'
|
||||
replace: result
|
||||
propagate_case: true
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
@ -662,12 +718,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_render_match_case_propagation_all_capital() {
|
||||
let config = get_config_for(r###"
|
||||
let config = get_config_for(
|
||||
r###"
|
||||
matches:
|
||||
- trigger: 'test'
|
||||
replace: result
|
||||
propagate_case: true
|
||||
"###);
|
||||
"###,
|
||||
);
|
||||
|
||||
let renderer = get_renderer(config.clone());
|
||||
|
||||
|
|
|
@ -17,16 +17,22 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::path::PathBuf;
|
||||
use crate::matcher::{Match};
|
||||
use crate::config::Configs;
|
||||
use crate::matcher::Match;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub(crate) mod default;
|
||||
pub(crate) mod utils;
|
||||
|
||||
pub trait Renderer {
|
||||
// Render a match output
|
||||
fn render_match(&self, m: &Match, trigger_offset: usize, config: &Configs, args: Vec<String>) -> RenderResult;
|
||||
fn render_match(
|
||||
&self,
|
||||
m: &Match,
|
||||
trigger_offset: usize,
|
||||
config: &Configs,
|
||||
args: Vec<String>,
|
||||
) -> RenderResult;
|
||||
|
||||
// Render a passive expansion text
|
||||
fn render_passive(&self, text: &str, config: &Configs) -> RenderResult;
|
||||
|
@ -35,5 +41,5 @@ pub trait Renderer {
|
|||
pub enum RenderResult {
|
||||
Text(String),
|
||||
Image(PathBuf),
|
||||
Error
|
||||
Error,
|
||||
}
|
|
@ -17,7 +17,7 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use regex::{Regex, Captures};
|
||||
use regex::{Captures, Regex};
|
||||
|
||||
lazy_static! {
|
||||
static ref ARG_REGEX: Regex = Regex::new("\\$(?P<pos>\\d+)\\$").unwrap();
|
||||
|
@ -43,7 +43,7 @@ pub fn split_args(text: &str, delimiter: char, escape: char) -> Vec<String> {
|
|||
|
||||
// Make sure the text is not empty
|
||||
if text.is_empty() {
|
||||
return output
|
||||
return output;
|
||||
}
|
||||
|
||||
let mut last = String::from("");
|
||||
|
@ -80,25 +80,28 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_render_args_no_args() {
|
||||
let args = vec!("hello".to_owned());
|
||||
let args = vec!["hello".to_owned()];
|
||||
assert_eq!(render_args("no args", &args), "no args")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_args_one_arg() {
|
||||
let args = vec!("jon".to_owned());
|
||||
let args = vec!["jon".to_owned()];
|
||||
assert_eq!(render_args("hello $0$", &args), "hello jon")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_args_one_multiple_args() {
|
||||
let args = vec!("jon".to_owned(), "snow".to_owned());
|
||||
assert_eq!(render_args("hello $0$, the $1$ is white", &args), "hello jon, the snow is white")
|
||||
let args = vec!["jon".to_owned(), "snow".to_owned()];
|
||||
assert_eq!(
|
||||
render_args("hello $0$, the $1$ is white", &args),
|
||||
"hello jon, the snow is white"
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_render_args_out_of_range() {
|
||||
let args = vec!("jon".to_owned());
|
||||
let args = vec!["jon".to_owned()];
|
||||
assert_eq!(render_args("hello $10$", &args), "hello ")
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
// This functions are used to register/unregister espanso from the system daemon manager.
|
||||
|
||||
use crate::config::ConfigSet;
|
||||
use crate::sysdaemon::VerifyResult::{EnabledAndValid, NotEnabled, EnabledButInvalidPath};
|
||||
use crate::sysdaemon::VerifyResult::{EnabledAndValid, EnabledButInvalidPath, NotEnabled};
|
||||
|
||||
// INSTALLATION
|
||||
|
||||
|
@ -45,13 +45,21 @@ pub fn register(_config_set: ConfigSet) {
|
|||
|
||||
let plist_file = agents_dir.join(MAC_PLIST_FILENAME);
|
||||
if !plist_file.exists() {
|
||||
println!("Creating LaunchAgents entry: {}", plist_file.to_str().unwrap_or_default());
|
||||
println!(
|
||||
"Creating LaunchAgents entry: {}",
|
||||
plist_file.to_str().unwrap_or_default()
|
||||
);
|
||||
|
||||
let espanso_path = std::env::current_exe().expect("Could not get espanso executable path");
|
||||
println!("Entry will point to: {}", espanso_path.to_str().unwrap_or_default());
|
||||
println!(
|
||||
"Entry will point to: {}",
|
||||
espanso_path.to_str().unwrap_or_default()
|
||||
);
|
||||
|
||||
let plist_content = String::from(MAC_PLIST_CONTENT)
|
||||
.replace("{{{espanso_path}}}", espanso_path.to_str().unwrap_or_default());
|
||||
let plist_content = String::from(MAC_PLIST_CONTENT).replace(
|
||||
"{{{espanso_path}}}",
|
||||
espanso_path.to_str().unwrap_or_default(),
|
||||
);
|
||||
|
||||
std::fs::write(plist_file.clone(), plist_content).expect("Unable to write plist file");
|
||||
|
||||
|
@ -110,7 +118,7 @@ const LINUX_SERVICE_FILENAME : &str = "espanso.service";
|
|||
#[cfg(target_os = "linux")]
|
||||
pub fn register(_: ConfigSet) {
|
||||
use std::fs::create_dir_all;
|
||||
use std::process::{Command};
|
||||
use std::process::Command;
|
||||
|
||||
// Check if espanso service is already registered
|
||||
let res = Command::new("systemctl")
|
||||
|
@ -154,15 +162,24 @@ pub fn register(_: ConfigSet) {
|
|||
|
||||
let service_file = user_dir.join(LINUX_SERVICE_FILENAME);
|
||||
if !service_file.exists() {
|
||||
println!("Creating service entry: {}", service_file.to_str().unwrap_or_default());
|
||||
println!(
|
||||
"Creating service entry: {}",
|
||||
service_file.to_str().unwrap_or_default()
|
||||
);
|
||||
|
||||
let espanso_path = std::env::current_exe().expect("Could not get espanso executable path");
|
||||
println!("Entry will point to: {}", espanso_path.to_str().unwrap_or_default());
|
||||
println!(
|
||||
"Entry will point to: {}",
|
||||
espanso_path.to_str().unwrap_or_default()
|
||||
);
|
||||
|
||||
let service_content = String::from(LINUX_SERVICE_CONTENT)
|
||||
.replace("{{{espanso_path}}}", espanso_path.to_str().unwrap_or_default());
|
||||
let service_content = String::from(LINUX_SERVICE_CONTENT).replace(
|
||||
"{{{espanso_path}}}",
|
||||
espanso_path.to_str().unwrap_or_default(),
|
||||
);
|
||||
|
||||
std::fs::write(service_file.clone(), service_content).expect("Unable to write service file");
|
||||
std::fs::write(service_file.clone(), service_content)
|
||||
.expect("Unable to write service file");
|
||||
|
||||
println!("Service file created correctly!")
|
||||
}
|
||||
|
@ -191,7 +208,7 @@ pub enum VerifyResult {
|
|||
#[cfg(target_os = "linux")]
|
||||
pub fn verify() -> VerifyResult {
|
||||
use regex::Regex;
|
||||
use std::process::{Command};
|
||||
use std::process::Command;
|
||||
|
||||
// Check if espanso service is already registered
|
||||
let res = Command::new("systemctl")
|
||||
|
@ -201,7 +218,7 @@ pub fn verify() -> VerifyResult {
|
|||
let output = String::from_utf8_lossy(res.stdout.as_slice());
|
||||
let output = output.trim();
|
||||
if !res.status.success() || output != "enabled" {
|
||||
return NotEnabled
|
||||
return NotEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -219,10 +236,11 @@ pub fn verify() -> VerifyResult {
|
|||
if res.status.success() {
|
||||
let caps = EXEC_PATH_REGEX.captures(output).unwrap();
|
||||
let path = caps.get(1).map_or("", |m| m.as_str());
|
||||
let espanso_path = std::env::current_exe().expect("Could not get espanso executable path");
|
||||
let espanso_path =
|
||||
std::env::current_exe().expect("Could not get espanso executable path");
|
||||
|
||||
if espanso_path.to_string_lossy() != path {
|
||||
return EnabledButInvalidPath
|
||||
return EnabledButInvalidPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -232,12 +250,13 @@ pub fn verify() -> VerifyResult {
|
|||
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn unregister(_: ConfigSet) {
|
||||
use std::process::{Command};
|
||||
use std::process::Command;
|
||||
|
||||
// Disable the service first
|
||||
Command::new("systemctl")
|
||||
.args(&["--user", "disable", "espanso"])
|
||||
.status().expect("Unable to invoke systemctl");
|
||||
.status()
|
||||
.expect("Unable to invoke systemctl");
|
||||
|
||||
// Then delete the espanso.service entry
|
||||
let config_dir = dirs::config_dir().expect("Could not get configuration directory");
|
||||
|
@ -251,11 +270,14 @@ pub fn unregister(_: ConfigSet) {
|
|||
Ok(_) => {
|
||||
println!("Deleted entry at {}", service_file.to_string_lossy());
|
||||
println!("Service unregistered successfully!");
|
||||
},
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error, could not delete service entry at {} with error {}",
|
||||
service_file.to_string_lossy(), e);
|
||||
},
|
||||
println!(
|
||||
"Error, could not delete service entry at {} with error {}",
|
||||
service_file.to_string_lossy(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
eprintln!("Error, could not find espanso service file");
|
||||
|
|
|
@ -19,7 +19,9 @@
|
|||
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use crate::bridge::linux::{get_active_window_name, get_active_window_class, get_active_window_executable};
|
||||
use crate::bridge::linux::{
|
||||
get_active_window_class, get_active_window_executable, get_active_window_name,
|
||||
};
|
||||
use std::ffi::CStr;
|
||||
|
||||
pub struct LinuxSystemManager {}
|
||||
|
|
|
@ -19,12 +19,12 @@
|
|||
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use crate::bridge::macos::{
|
||||
get_active_app_bundle, get_active_app_identifier, get_path_from_pid, get_secure_input_process,
|
||||
};
|
||||
use std::ffi::CStr;
|
||||
use crate::bridge::macos::{get_active_app_bundle, get_active_app_identifier, get_secure_input_process, get_path_from_pid};
|
||||
|
||||
pub struct MacSystemManager {
|
||||
|
||||
}
|
||||
pub struct MacSystemManager {}
|
||||
|
||||
impl super::SystemManager for MacSystemManager {
|
||||
fn get_current_window_title(&self) -> Option<String> {
|
||||
|
@ -70,9 +70,7 @@ impl super::SystemManager for MacSystemManager {
|
|||
|
||||
impl MacSystemManager {
|
||||
pub fn new() -> MacSystemManager {
|
||||
MacSystemManager{
|
||||
|
||||
}
|
||||
MacSystemManager {}
|
||||
}
|
||||
|
||||
/// Check whether an application is currently holding the Secure Input.
|
||||
|
|
|
@ -17,12 +17,10 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use widestring::U16CString;
|
||||
use crate::bridge::windows::*;
|
||||
use widestring::U16CString;
|
||||
|
||||
pub struct WindowsSystemManager {
|
||||
|
||||
}
|
||||
pub struct WindowsSystemManager {}
|
||||
|
||||
impl WindowsSystemManager {
|
||||
pub fn new() -> WindowsSystemManager {
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::process::Command;
|
||||
use super::MenuItem;
|
||||
use log::{error, info};
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
const LINUX_ICON_CONTENT: &[u8] = include_bytes!("../res/linux/icon.png");
|
||||
|
||||
|
@ -35,8 +35,14 @@ impl super::UIManager for LinuxUIManager {
|
|||
|
||||
fn notify_delay(&self, message: &str, duration: i32) {
|
||||
let res = Command::new("notify-send")
|
||||
.args(&["-i", self.icon_path.to_str().unwrap_or_default(),
|
||||
"-t", &duration.to_string(), "espanso", message])
|
||||
.args(&[
|
||||
"-i",
|
||||
self.icon_path.to_str().unwrap_or_default(),
|
||||
"-t",
|
||||
&duration.to_string(),
|
||||
"espanso",
|
||||
message,
|
||||
])
|
||||
.output();
|
||||
|
||||
if let Err(e) = res {
|
||||
|
@ -59,12 +65,13 @@ impl LinuxUIManager {
|
|||
let data_dir = crate::context::get_data_dir();
|
||||
let icon_path = data_dir.join("icon.png");
|
||||
if !icon_path.exists() {
|
||||
info!("Creating espanso icon in '{}'", icon_path.to_str().unwrap_or_default());
|
||||
info!(
|
||||
"Creating espanso icon in '{}'",
|
||||
icon_path.to_str().unwrap_or_default()
|
||||
);
|
||||
std::fs::write(&icon_path, LINUX_ICON_CONTENT).expect("Unable to copy espanso icon");
|
||||
}
|
||||
|
||||
LinuxUIManager{
|
||||
icon_path
|
||||
}
|
||||
LinuxUIManager { icon_path }
|
||||
}
|
||||
}
|
|
@ -17,21 +17,21 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::{fs, io};
|
||||
use std::io::{Cursor};
|
||||
use crate::bridge::macos::{show_context_menu, MacMenuItem};
|
||||
use crate::context;
|
||||
use crate::ui::{MenuItem, MenuItemType};
|
||||
use log::{debug, info, warn};
|
||||
use std::ffi::CString;
|
||||
use log::{info, warn, debug};
|
||||
use std::io::Cursor;
|
||||
use std::os::raw::c_char;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use crate::ui::{MenuItem, MenuItemType};
|
||||
use crate::bridge::macos::{MacMenuItem, show_context_menu};
|
||||
use std::os::raw::c_char;
|
||||
use crate::context;
|
||||
use std::{fs, io};
|
||||
|
||||
const NOTIFY_HELPER_BINARY: &'static [u8] = include_bytes!("../res/mac/EspansoNotifyHelper.zip");
|
||||
|
||||
pub struct MacUIManager {
|
||||
notify_helper_path: PathBuf
|
||||
notify_helper_path: PathBuf,
|
||||
}
|
||||
|
||||
impl super::UIManager for MacUIManager {
|
||||
|
@ -66,8 +66,8 @@ impl super::UIManager for MacUIManager {
|
|||
}
|
||||
|
||||
let menu_type = match item.item_type {
|
||||
MenuItemType::Button => {1},
|
||||
MenuItemType::Separator => {2},
|
||||
MenuItemType::Button => 1,
|
||||
MenuItemType::Separator => 2,
|
||||
};
|
||||
|
||||
let raw_item = MacMenuItem {
|
||||
|
@ -79,7 +79,9 @@ impl super::UIManager for MacUIManager {
|
|||
raw_menu.push(raw_item);
|
||||
}
|
||||
|
||||
unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); }
|
||||
unsafe {
|
||||
show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32);
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup(&self) {
|
||||
|
@ -91,15 +93,16 @@ impl MacUIManager {
|
|||
pub fn new() -> MacUIManager {
|
||||
let notify_helper_path = MacUIManager::initialize_notify_helper();
|
||||
|
||||
MacUIManager{
|
||||
notify_helper_path
|
||||
}
|
||||
MacUIManager { notify_helper_path }
|
||||
}
|
||||
|
||||
fn initialize_notify_helper() -> PathBuf {
|
||||
let espanso_dir = context::get_data_dir();
|
||||
|
||||
info!("Initializing EspansoNotifyHelper in {}", espanso_dir.as_path().display());
|
||||
info!(
|
||||
"Initializing EspansoNotifyHelper in {}",
|
||||
espanso_dir.as_path().display()
|
||||
);
|
||||
|
||||
let espanso_target = espanso_dir.join("EspansoNotifyHelper.app");
|
||||
|
||||
|
@ -123,10 +126,19 @@ impl MacUIManager {
|
|||
}
|
||||
|
||||
if (&*file.name()).ends_with('/') {
|
||||
debug!("File {} extracted to \"{}\"", i, outpath.as_path().display());
|
||||
debug!(
|
||||
"File {} extracted to \"{}\"",
|
||||
i,
|
||||
outpath.as_path().display()
|
||||
);
|
||||
fs::create_dir_all(&outpath).unwrap();
|
||||
} else {
|
||||
debug!("File {} extracted to \"{}\" ({} bytes)", i, outpath.as_path().display(), file.size());
|
||||
debug!(
|
||||
"File {} extracted to \"{}\" ({} bytes)",
|
||||
i,
|
||||
outpath.as_path().display(),
|
||||
file.size()
|
||||
);
|
||||
if let Some(p) = outpath.parent() {
|
||||
if !p.exists() {
|
||||
fs::create_dir_all(&p).unwrap();
|
||||
|
|
|
@ -17,16 +17,18 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::bridge::windows::{show_notification, close_notification, WindowsMenuItem, show_context_menu, cleanup_ui};
|
||||
use widestring::U16CString;
|
||||
use std::{thread, time};
|
||||
use log::{debug};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::Arc;
|
||||
use crate::bridge::windows::{
|
||||
cleanup_ui, close_notification, show_context_menu, show_notification, WindowsMenuItem,
|
||||
};
|
||||
use crate::ui::{MenuItem, MenuItemType};
|
||||
use log::debug;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::{thread, time};
|
||||
use widestring::U16CString;
|
||||
|
||||
pub struct WindowsUIManager {
|
||||
id: Arc<Mutex<i32>>
|
||||
id: Arc<Mutex<i32>>,
|
||||
}
|
||||
|
||||
impl super::UIManager for WindowsUIManager {
|
||||
|
@ -45,7 +47,9 @@ impl super::UIManager for WindowsUIManager {
|
|||
|
||||
// Setup a timeout to close the notification
|
||||
let id = Arc::clone(&self.id);
|
||||
let _ = thread::Builder::new().name("notification_thread".to_string()).spawn(move || {
|
||||
let _ = thread::Builder::new()
|
||||
.name("notification_thread".to_string())
|
||||
.spawn(move || {
|
||||
for _ in 1..10 {
|
||||
let duration = time::Duration::from_millis(step as u64);
|
||||
thread::sleep(duration);
|
||||
|
@ -67,7 +71,6 @@ impl super::UIManager for WindowsUIManager {
|
|||
let message = U16CString::from_str(message).unwrap();
|
||||
show_notification(message.as_ptr());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn show_menu(&self, menu: Vec<MenuItem>) {
|
||||
|
@ -81,8 +84,8 @@ impl super::UIManager for WindowsUIManager {
|
|||
}
|
||||
|
||||
let menu_type = match item.item_type {
|
||||
MenuItemType::Button => {1},
|
||||
MenuItemType::Separator => {2},
|
||||
MenuItemType::Button => 1,
|
||||
MenuItemType::Separator => 2,
|
||||
};
|
||||
|
||||
let raw_item = WindowsMenuItem {
|
||||
|
@ -94,7 +97,9 @@ impl super::UIManager for WindowsUIManager {
|
|||
raw_menu.push(raw_item);
|
||||
}
|
||||
|
||||
unsafe { show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32); }
|
||||
unsafe {
|
||||
show_context_menu(raw_menu.as_ptr(), raw_menu.len() as i32);
|
||||
}
|
||||
}
|
||||
|
||||
fn cleanup(&self) {
|
||||
|
@ -108,9 +113,7 @@ impl WindowsUIManager {
|
|||
pub fn new() -> WindowsUIManager {
|
||||
let id = Arc::new(Mutex::new(0));
|
||||
|
||||
let manager = WindowsUIManager {
|
||||
id
|
||||
};
|
||||
let manager = WindowsUIManager { id };
|
||||
|
||||
manager
|
||||
}
|
||||
|
|
13
src/utils.rs
13
src/utils.rs
|
@ -17,9 +17,9 @@
|
|||
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::path::Path;
|
||||
use std::error::Error;
|
||||
use std::fs::create_dir;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn copy_dir(source_dir: &Path, dest_dir: &Path) -> Result<(), Box<dyn Error>> {
|
||||
for entry in std::fs::read_dir(source_dir)? {
|
||||
|
@ -31,7 +31,8 @@ pub fn copy_dir(source_dir: &Path, dest_dir: &Path) -> Result<(), Box<dyn Error>
|
|||
create_dir(&target_dir)?;
|
||||
copy_dir(&entry, &target_dir)?;
|
||||
} else if entry.is_file() {
|
||||
let target_entry = dest_dir.join(entry.file_name().expect("Error obtaining the filename"));
|
||||
let target_entry =
|
||||
dest_dir.join(entry.file_name().expect("Error obtaining the filename"));
|
||||
std::fs::copy(entry, target_entry)?;
|
||||
}
|
||||
}
|
||||
|
@ -42,8 +43,8 @@ pub fn copy_dir(source_dir: &Path, dest_dir: &Path) -> Result<(), Box<dyn Error>
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
use std::fs::create_dir;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn test_copy_dir_into() {
|
||||
|
@ -88,7 +89,9 @@ mod tests {
|
|||
assert!(dest_tmp_dir.path().join("source/file2.txt").exists());
|
||||
|
||||
assert!(dest_tmp_dir.path().join("source/nested").exists());
|
||||
assert!(dest_tmp_dir.path().join("source/nested/nestedfile.txt").exists());
|
||||
assert!(dest_tmp_dir
|
||||
.path()
|
||||
.join("source/nested/nestedfile.txt")
|
||||
.exists());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user