Fix formatting

This commit is contained in:
Federico Terzi 2020-06-24 22:11:01 +02:00
parent 958d0669e9
commit 45bcaee54b
11 changed files with 73 additions and 74 deletions

View File

@ -33,7 +33,12 @@ extern "C" {
pub fn get_active_window_executable(buffer: *mut c_char, size: i32) -> i32; pub fn get_active_window_executable(buffer: *mut c_char, size: i32) -> i32;
pub fn is_current_window_special() -> i32; pub fn is_current_window_special() -> i32;
pub fn register_error_callback( pub fn register_error_callback(
cb: extern "C" fn(_self: *mut c_void, error_code: c_char, request_code: c_char, minor_code: c_char), cb: extern "C" fn(
_self: *mut c_void,
error_code: c_char,
request_code: c_char,
minor_code: c_char,
),
); );
// Keyboard // Keyboard

View File

@ -17,9 +17,9 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use serde::Serialize;
use crate::config::ConfigSet; use crate::config::ConfigSet;
use crate::matcher::{Match, MatchContentType}; use crate::matcher::{Match, MatchContentType};
use serde::Serialize;
pub fn list_matches(config_set: ConfigSet, onlytriggers: bool, preserve_newlines: bool) { pub fn list_matches(config_set: ConfigSet, onlytriggers: bool, preserve_newlines: bool) {
let matches = filter_matches(config_set); let matches = filter_matches(config_set);
@ -28,19 +28,19 @@ pub fn list_matches(config_set: ConfigSet, onlytriggers: bool, preserve_newlines
for trigger in m.triggers.iter() { for trigger in m.triggers.iter() {
if onlytriggers { if onlytriggers {
println!("{}", trigger); println!("{}", trigger);
}else { } else {
match m.content { match m.content {
MatchContentType::Text(ref text) => { MatchContentType::Text(ref text) => {
let replace = if preserve_newlines { let replace = if preserve_newlines {
text.replace.to_owned() text.replace.to_owned()
}else{ } else {
text.replace.replace("\n", " ") text.replace.replace("\n", " ")
}; };
println!("{} - {}", trigger, replace) println!("{} - {}", trigger, replace)
}, }
MatchContentType::Image(_) => { MatchContentType::Image(_) => {
// Skip image matches for now // Skip image matches for now
}, }
} }
} }
} }
@ -60,15 +60,13 @@ pub fn list_matches_as_json(config_set: ConfigSet) {
for m in matches { for m in matches {
match m.content { match m.content {
MatchContentType::Text(ref text) => { MatchContentType::Text(ref text) => entries.push(JsonMatchEntry {
entries.push(JsonMatchEntry {
triggers: m.triggers, triggers: m.triggers,
replace: text.replace.clone(), replace: text.replace.clone(),
}) }),
},
MatchContentType::Image(_) => { MatchContentType::Image(_) => {
// Skip image matches for now // Skip image matches for now
}, }
} }
} }
@ -82,8 +80,8 @@ fn filter_matches(config_set: ConfigSet) -> Vec<Match> {
output.extend(config_set.default.matches); output.extend(config_set.default.matches);
// TODO: consider specific matches by class, title or exe path // TODO: consider specific matches by class, title or exe path
// for specific in config_set.specific { // for specific in config_set.specific {
// output.extend(specific.matches) // output.extend(specific.matches)
// } // }
output output
} }

View File

@ -163,5 +163,8 @@ extern "C" fn error_callback(
request_code: c_char, request_code: c_char,
minor_code: c_char, minor_code: c_char,
) { ) {
warn!("X11 reported an error code: {}, request_code: {} and minor_code: {}", error_code, request_code, minor_code); warn!(
"X11 reported an error code: {}, request_code: {} and minor_code: {}",
error_code, request_code, minor_code
);
} }

View File

@ -136,16 +136,24 @@ impl<
fn find_match_by_trigger(&self, trigger: &str) -> Option<Match> { fn find_match_by_trigger(&self, trigger: &str) -> Option<Match> {
let config = self.config_manager.active_config(); let config = self.config_manager.active_config();
if let Some(m) = config.matches.iter().find(|m| if let Some(m) = config
m.triggers.iter().any(|t| t == trigger) .matches
) { .iter()
.find(|m| m.triggers.iter().any(|t| t == trigger))
{
Some(m.clone()) Some(m.clone())
}else{ } else {
None None
} }
} }
fn inject_match(&self, m: &Match, trailing_separator: Option<char>, trigger_offset: usize, skip_delete: bool) { fn inject_match(
&self,
m: &Match,
trailing_separator: Option<char>,
trigger_offset: usize,
skip_delete: bool,
) {
let config = self.config_manager.active_config(); let config = self.config_manager.active_config();
if !config.enable_active { if !config.enable_active {
@ -455,10 +463,8 @@ impl<
match m { match m {
Some(m) => { Some(m) => {
self.inject_match(&m, None, 0, true); self.inject_match(&m, None, 0, true);
}, }
None => { None => warn!("No match found with trigger: {}", trigger),
warn!("No match found with trigger: {}", trigger)
},
} }
} }
} }

View File

@ -88,7 +88,8 @@ impl super::Extension for ScriptExtension {
match output { match output {
Ok(output) => { Ok(output) => {
let mut output_str = String::from_utf8_lossy(output.stdout.as_slice()).to_string(); let mut output_str =
String::from_utf8_lossy(output.stdout.as_slice()).to_string();
let error_str = String::from_utf8_lossy(output.stderr.as_slice()); let error_str = String::from_utf8_lossy(output.stderr.as_slice());
let error_str = error_str.to_string(); let error_str = error_str.to_string();
let error_str = error_str.trim(); let error_str = error_str.trim();
@ -103,7 +104,7 @@ impl super::Extension for ScriptExtension {
let should_trim = if let Some(value) = trim_opt { let should_trim = if let Some(value) = trim_opt {
let val = value.as_bool(); let val = value.as_bool();
val.unwrap_or(true) val.unwrap_or(true)
}else{ } else {
true true
}; };
@ -154,10 +155,7 @@ mod tests {
Value::from("args"), Value::from("args"),
Value::from(vec!["echo", "hello world"]), Value::from(vec!["echo", "hello world"]),
); );
params.insert( params.insert(Value::from("trim"), Value::from(false));
Value::from("trim"),
Value::from(false),
);
let extension = ScriptExtension::new(); let extension = ScriptExtension::new();
let output = extension.calculate(&params, &vec![]); let output = extension.calculate(&params, &vec![]);

View File

@ -46,32 +46,32 @@ impl Shell {
let mut command = Command::new("cmd"); let mut command = Command::new("cmd");
command.args(&["/C", &cmd]); command.args(&["/C", &cmd]);
command command
}, }
Shell::Powershell => { Shell::Powershell => {
let mut command = Command::new("powershell"); let mut command = Command::new("powershell");
command.args(&["-Command", &cmd]); command.args(&["-Command", &cmd]);
command command
}, }
Shell::WSL => { Shell::WSL => {
let mut command = Command::new("bash"); let mut command = Command::new("bash");
command.args(&["-c", &cmd]); command.args(&["-c", &cmd]);
command command
}, }
Shell::WSL2 => { Shell::WSL2 => {
let mut command = Command::new("wsl"); let mut command = Command::new("wsl");
command.args(&["bash", "-c", &cmd]); command.args(&["bash", "-c", &cmd]);
command command
}, }
Shell::Bash => { Shell::Bash => {
let mut command = Command::new("bash"); let mut command = Command::new("bash");
command.args(&["-c", &cmd]); command.args(&["-c", &cmd]);
command command
}, }
Shell::Sh => { Shell::Sh => {
let mut command = Command::new("sh"); let mut command = Command::new("sh");
command.args(&["-c", &cmd]); command.args(&["-c", &cmd]);
command command
}, }
}; };
// Inject the $CONFIG variable // Inject the $CONFIG variable
@ -176,7 +176,7 @@ impl super::Extension for ShellExtension {
let should_trim = if let Some(value) = trim_opt { let should_trim = if let Some(value) = trim_opt {
let val = value.as_bool(); let val = value.as_bool();
val.unwrap_or(true) val.unwrap_or(true)
}else{ } else {
true true
}; };

View File

@ -53,6 +53,7 @@ use crate::ui::UIManager;
mod bridge; mod bridge;
mod check; mod check;
mod cli;
mod clipboard; mod clipboard;
mod config; mod config;
mod context; mod context;
@ -69,7 +70,6 @@ mod render;
mod sysdaemon; mod sysdaemon;
mod system; mod system;
mod ui; mod ui;
mod cli;
mod utils; mod utils;
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
@ -519,18 +519,14 @@ fn register_signals(_: Configs) {}
fn register_signals(config: Configs) { fn register_signals(config: Configs) {
// On Unix, also listen for signals so that we can terminate the // On Unix, also listen for signals so that we can terminate the
// worker if the daemon receives a signal // worker if the daemon receives a signal
use signal_hook::{iterator::Signals, SIGTERM, SIGINT}; use signal_hook::{iterator::Signals, SIGINT, SIGTERM};
let signals = Signals::new(&[SIGTERM, SIGINT]).expect("unable to register for signals"); let signals = Signals::new(&[SIGTERM, SIGINT]).expect("unable to register for signals");
thread::Builder::new() thread::Builder::new()
.name("signal monitor".to_string()) .name("signal monitor".to_string())
.spawn(move || { .spawn(move || {
for signal in signals.forever() { for signal in signals.forever() {
info!("Received signal: {:?}, terminating worker", signal); info!("Received signal: {:?}, terminating worker", signal);
send_command_or_warn( send_command_or_warn(Service::Worker, config, IPCCommand::exit_worker());
Service::Worker,
config,
IPCCommand::exit_worker(),
);
std::thread::sleep(Duration::from_millis(200)); std::thread::sleep(Duration::from_millis(200));
@ -1268,7 +1264,6 @@ fn path_main(_config_set: ConfigSet, matches: &ArgMatches) {
} }
} }
fn match_main(config_set: ConfigSet, matches: &ArgMatches) { fn match_main(config_set: ConfigSet, matches: &ArgMatches) {
if let Some(matches) = matches.subcommand_matches("list") { if let Some(matches) = matches.subcommand_matches("list") {
let json = matches.is_present("json"); let json = matches.is_present("json");
@ -1277,10 +1272,10 @@ fn match_main(config_set: ConfigSet, matches: &ArgMatches) {
if !json { if !json {
crate::cli::list_matches(config_set, onlytriggers, preserve_newlines); crate::cli::list_matches(config_set, onlytriggers, preserve_newlines);
}else{ } else {
crate::cli::list_matches_as_json(config_set); crate::cli::list_matches_as_json(config_set);
} }
}else if let Some(matches) = matches.subcommand_matches("exec") { } else if let Some(matches) = matches.subcommand_matches("exec") {
let trigger = matches.value_of("trigger").unwrap_or_else(|| { let trigger = matches.value_of("trigger").unwrap_or_else(|| {
eprintln!("missing trigger"); eprintln!("missing trigger");
exit(1); exit(1);

View File

@ -67,9 +67,7 @@ impl IPCCommand {
"notify" => Some(Event::System(SystemEvent::NotifyRequest( "notify" => Some(Event::System(SystemEvent::NotifyRequest(
self.payload.clone(), self.payload.clone(),
))), ))),
"trigger" => Some(Event::System(SystemEvent::Trigger( "trigger" => Some(Event::System(SystemEvent::Trigger(self.payload.clone()))),
self.payload.clone(),
))),
_ => None, _ => None,
} }
} }

View File

@ -23,10 +23,10 @@ use std::net::{TcpListener, TcpStream};
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use crate::config::Configs; use crate::config::Configs;
use crate::context;
use crate::event::*; use crate::event::*;
use crate::protocol::{process_event, send_command, Service}; use crate::protocol::{process_event, send_command, Service};
use named_pipe::{PipeOptions, PipeServer, PipeClient}; use named_pipe::{PipeClient, PipeOptions, PipeServer};
use crate::context;
use std::io::Error; use std::io::Error;
use std::path::PathBuf; use std::path::PathBuf;
@ -46,12 +46,8 @@ fn get_pipe_name(service: &Service) -> String {
} }
} }
impl WindowsIPCServer { impl WindowsIPCServer {
pub fn new( pub fn new(service: Service, event_channel: Sender<Event>) -> WindowsIPCServer {
service: Service,
event_channel: Sender<Event>,
) -> WindowsIPCServer {
WindowsIPCServer { WindowsIPCServer {
service, service,
event_channel, event_channel,
@ -68,13 +64,12 @@ impl super::IPCServer for WindowsIPCServer {
.spawn(move || { .spawn(move || {
let options = PipeOptions::new(&pipe_name); let options = PipeOptions::new(&pipe_name);
info!( info!("Binding to named pipe: {}", pipe_name);
"Binding to named pipe: {}",
pipe_name
);
loop { loop {
let server = options.single().expect("unable to initialize IPC named pipe"); let server = options
.single()
.expect("unable to initialize IPC named pipe");
let pipe_server = server.wait(); let pipe_server = server.wait();
process_event(&event_channel, pipe_server); process_event(&event_channel, pipe_server);
} }

View File

@ -106,7 +106,8 @@ impl MacSystemManager {
if let Ok(path) = string { if let Ok(path) = string {
if !path.trim().is_empty() { if !path.trim().is_empty() {
let process = path.trim().to_string(); let process = path.trim().to_string();
let app_name = if let Some(name) = Self::get_app_name_from_path(&process) { let app_name =
if let Some(name) = Self::get_app_name_from_path(&process) {
name name
} else { } else {
process.to_owned() process.to_owned()
@ -138,14 +139,15 @@ impl MacSystemManager {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_get_app_name_from_path() { fn test_get_app_name_from_path() {
let app_name = MacSystemManager::get_app_name_from_path("/Applications/iTerm.app/Contents/MacOS/iTerm2"); let app_name = MacSystemManager::get_app_name_from_path(
"/Applications/iTerm.app/Contents/MacOS/iTerm2",
);
assert_eq!(app_name.unwrap(), "iTerm") assert_eq!(app_name.unwrap(), "iTerm")
} }
@ -161,4 +163,3 @@ mod tests {
assert_eq!(app_name.unwrap(), "SecurityAgent") assert_eq!(app_name.unwrap(), "SecurityAgent")
} }
} }