New subcommand switch-workspace-or-window

timeout_old
Tassilo Horn 4 years ago
parent d7be7cecbb
commit a17271a918
  1. 1
      .gitignore
  2. 5
      src/bin/swayr.rs
  3. 22
      src/client.rs
  4. 59
      src/con.rs

1
.gitignore vendored

@ -1 +1,2 @@
/target
Cargo.lock

@ -24,6 +24,8 @@ enum SwayrCommand {
QuitWindow,
/// Switch workspace with LRU display order
SwitchWorkspace,
/// Switch workspace or window with LRU display order
SwitchWorkspaceOrWindow,
/// Select and execute a swaymsg command
ExecuteSwaymsgCommand,
}
@ -34,6 +36,9 @@ fn main() {
SwayrCommand::SwitchWindow => client::switch_window(),
SwayrCommand::QuitWindow => client::quit_window(),
SwayrCommand::SwitchWorkspace => client::switch_workspace(),
SwayrCommand::SwitchWorkspaceOrWindow => {
client::switch_workspace_or_window()
}
SwayrCommand::ExecuteSwaymsgCommand => client::exec_swaymsg_command(),
}
}

@ -26,6 +26,28 @@ pub fn switch_workspace() {
}
}
pub fn switch_workspace_or_window() {
let root = con::get_tree();
let workspaces = con::get_workspaces(&root, false);
let ws_or_wins = con::WsOrWin::from_workspaces(&workspaces);
if let Some(ws_or_win) = con::select_workspace_or_window(
"Select workspace or window",
&ws_or_wins,
) {
match ws_or_win {
con::WsOrWin::Ws { ws } => {
util::swaymsg(&["workspace", "number", ws.get_name()]);
}
con::WsOrWin::Win { win } => {
util::swaymsg(&[
format!("[con_id={}]", win.get_id()).as_str(),
"focus",
]);
}
}
}
}
pub fn quit_window() {
let root = con::get_tree();
let mut windows = con::get_windows(&root);

@ -140,18 +140,20 @@ fn build_workspaces(
) -> Vec<Workspace> {
let mut v = vec![];
for workspace in root.workspaces() {
let mut wins: Vec<Window> = workspace
.windows()
.iter()
.map(|w| Window {
node: &w,
con_props: con_props.remove(&w.id),
workspace: &workspace,
})
.collect();
wins.sort();
v.push(Workspace {
node: &workspace,
con_props: con_props.remove(&workspace.id),
windows: workspace
.windows()
.iter()
.map(|w| Window {
node: &w,
con_props: con_props.remove(&w.id),
workspace: &workspace,
})
.collect(),
windows: wins,
})
}
v
@ -246,6 +248,45 @@ pub fn select_workspace<'a>(
util::wofi_select(prompt, workspaces)
}
pub enum WsOrWin<'a> {
Ws { ws: &'a Workspace<'a> },
Win { win: &'a Window<'a> },
}
impl<'a> fmt::Display for WsOrWin<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
WsOrWin::Ws { ws } => ws.fmt(f),
WsOrWin::Win { win } => match f.write_str("\t") {
Ok(()) => win.fmt(f),
Err(e) => Err(e),
},
}
}
}
impl WsOrWin<'_> {
pub fn from_workspaces<'a>(
workspaces: &'a [Workspace],
) -> Vec<WsOrWin<'a>> {
let mut v = vec![];
for ws in workspaces {
v.push(WsOrWin::Ws { ws });
for win in &ws.windows {
v.push(WsOrWin::Win { win: &win });
}
}
v
}
}
pub fn select_workspace_or_window<'a>(
prompt: &'a str,
ws_or_wins: &'a [WsOrWin<'a>],
) -> Option<&'a WsOrWin<'a>> {
util::wofi_select(prompt, ws_or_wins)
}
pub struct Workspace<'a> {
node: &'a ipc::Node,
con_props: Option<ipc::ConProps>,

Loading…
Cancel
Save