From cc2cbc991162ed8e8aa406ea2817896b98eb8a61 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 28 Jun 2023 08:04:55 -0700 Subject: [PATCH] fix(whkd): allow single cmd configs This commit fixes issue #18 where a user reported that whkd would crash when the config file only contained a single hotkey. fix #18 --- src/parser.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index c96ff7b..43c10b4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -32,7 +32,7 @@ pub fn parser() -> impl Parser> { let delimiter = just(":").padded(); - let command = take_until(choice((comment, text::newline()))) + let command = take_until(choice((comment, text::newline(), end()))) .padded() .map(|c| c.0) .collect::(); @@ -45,7 +45,7 @@ pub fn parser() -> impl Parser> { let process_mapping = process_name .then_ignore(delimiter) - .then(command) + .then(command.clone()) .padded() .padded_by(comment.repeated()) .repeated() @@ -104,6 +104,29 @@ pub fn parser() -> impl Parser> { mod tests { use super::*; + #[test] + fn test_single_line_parse() { + let src = r#" +.shell pwsh # can be one of cmd | pwsh | powershell + +alt + h : echo "Hello""#; + + let output = parser().parse(src); + let expected = Whkdrc { + shell: Shell::Pwsh, + app_bindings: vec![], + bindings: vec![ + HotkeyBinding { + keys: vec![String::from("alt"), String::from("h")], + command: String::from("echo \"Hello\""), + process_name: None, + }, + ], + }; + + assert_eq!(output.unwrap(), expected); + } + #[test] fn test_parse() { let src = r#"