From 311a92bd126345ea6891f5e7900c0a7b528b3c25 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Wed, 3 Nov 2021 21:04:28 +0100 Subject: [PATCH] Release v0.9.0-beta.2: #: shortcuts for menu switcher cmds --- Cargo.lock | 2 +- Cargo.toml | 2 +- NEWS.md | 19 +++++++++++++------ src/cmds.rs | 37 ++++++++++++++++++++++++++++++++----- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7b6041..fc1bfbb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -360,7 +360,7 @@ dependencies = [ [[package]] name = "swayr" -version = "0.9.0-beta.1" +version = "0.9.0-beta.2" dependencies = [ "clap", "directories", diff --git a/Cargo.toml b/Cargo.toml index 945142a..76fdb95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "swayr" -version = "0.9.0-beta.1" +version = "0.9.0-beta.2" description = "A LRU window-switcher (and more) for the sway window manager" homepage = "https://sr.ht/~tsdh/swayr/" repository = "https://git.sr.ht/~tsdh/swayr" diff --git a/NEWS.md b/NEWS.md index 6cbc269..6ef858b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,12 +5,19 @@ swayr v0.9.0 show empty workspaces which makes it possible to switch to another output currently showing an empty workspace. - All menu switching commands (`switch-window`, `switch-workspace`, and - `switch-workspace-or-window`) can now create and switch to not yet existing - workspaces. Just type a digit, a name, or `:`, confirm your - input, and it'll do. The `:` format is explained in `man 5 - sway`. If that format is given, `swayr` will create the workspace using - `workspace number :`. If just a digit or name is given, the - `number` argument is not used. + `switch-workspace-or-window`) now handle non-matching input instead of doing + nothing. The input should start with any number of `#` (in order to be able + to force a non-match), a shortcut followed by a colon, and some string as + required by the shortcut. The following shortcuts are supported. + - `w:`: Switches to a possibly non-existing workspace. + `` must be a digit, a name, or `:`. The + `:` format is explained in `man 5 sway`. If that format is + given, `swayr` will create the workspace using `workspace number + :`. If just a digit or name is given, the `number` argument + is not used. + - `s:`: Executes the sway command `` using `swaymsg`. + - Any other input is assumed to be a workspace name and thus handled as + `w:` would do. swayr v0.8.0 diff --git a/src/cmds.rs b/src/cmds.rs index b596a78..390e7ae 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -395,10 +395,10 @@ pub fn switch_to_urgent_or_lru_window( lazy_static! { static ref DIGIT_AND_NAME: regex::Regex = - regex::Regex::new(r"(\d):(.*)").unwrap(); + regex::Regex::new(r"^(\d):(.*)").unwrap(); } -pub fn create_workspace(ws_name: &str) { +fn create_workspace(ws_name: &str) { if DIGIT_AND_NAME.is_match(ws_name) { run_sway_command(&["workspace", "number", ws_name]); } else { @@ -406,13 +406,36 @@ pub fn create_workspace(ws_name: &str) { } } +lazy_static! { + static ref SPECIAL_WORKSPACE: regex::Regex = + regex::Regex::new(r"^#*w:(.*)").unwrap(); + static ref SPECIAL_SWAY: regex::Regex = + regex::Regex::new(r"^#*s:(.*)").unwrap(); +} + +fn handle_non_matching_input(input: &str) { + if input.is_empty() { + return; + } + + if let Some(c) = SPECIAL_SWAY.captures(input) { + run_sway_command(&c[1].split_ascii_whitespace().collect::>()); + } else if let Some(c) = SPECIAL_WORKSPACE.captures(input) { + create_workspace(&c[1]); + } else { + create_workspace(input); + } +} + pub fn switch_window(extra_props: &HashMap) { let root = get_tree(); let windows = con::get_windows(&root, true, extra_props); match util::select_from_menu("Switch to window", &windows) { Ok(window) => focus_window_by_id(window.get_id()), - Err(ws_name) => create_workspace(&ws_name), + Err(non_matching_input) => { + handle_non_matching_input(&non_matching_input) + } } } @@ -523,7 +546,9 @@ pub fn switch_workspace(extra_props: &HashMap) { match util::select_from_menu("Switch to workspace", &workspaces) { Ok(workspace) => run_sway_command(&["workspace", workspace.get_name()]), - Err(ws_name) => create_workspace(&ws_name), + Err(non_matching_input) => { + handle_non_matching_input(&non_matching_input) + } } } @@ -538,7 +563,9 @@ pub fn switch_workspace_or_window(extra_props: &HashMap) { } con::WsOrWin::Win { win } => focus_window_by_id(win.get_id()), }, - Err(ws_name) => create_workspace(&ws_name), + Err(non_matching_input) => { + handle_non_matching_input(&non_matching_input) + } } }