-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeymap_entry.rs
76 lines (71 loc) · 1.94 KB
/
keymap_entry.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::types::{GoToOrLaunch, KeymapEntry, Launch, Leaf};
use anyhow::Result;
use std::{
collections::HashMap,
process::{self, Command},
str,
};
impl Leaf {
pub(crate) fn run(&self) -> Result<()> {
match self {
Leaf::GoToOrLaunch(GoToOrLaunch {
workspace_name,
instance_match,
launch: Launch { program, args, .. },
}) => {
let mut save_tree = Command::new("i3-save-tree");
save_tree.arg("--workspace").arg(workspace_name);
let mut go_to = Command::new("i3-msg");
go_to.arg(format!("workspace {workspace_name}"));
go_to.spawn()?;
if !str::from_utf8(&save_tree.output()?.stdout)?.contains(instance_match) {
let mut program = Command::new(program);
program.args(*args);
program.spawn()?;
}
}
Leaf::Launch(Launch { program, args, .. }) => {
let mut program = Command::new(program);
program.args(*args);
program.spawn()?;
}
Leaf::LaunchNoQuit(Launch { program, args, .. }) => {
let mut program = Command::new(program);
program.args(*args);
program.spawn()?;
return Ok(());
}
Leaf::Quit => process::exit(0),
}
process::exit(0);
}
}
impl KeymapEntry {
pub(crate) fn is_mode(&self) -> bool {
match self {
KeymapEntry::Leaf(_) => false,
KeymapEntry::Node { .. } => true,
}
}
pub(crate) fn get_name(&self) -> &'static str {
match self {
KeymapEntry::Leaf(leaf) => match leaf {
Leaf::GoToOrLaunch(GoToOrLaunch {
launch: Launch { name, .. },
..
}) => name,
Leaf::Launch(Launch { name, .. }) | Leaf::LaunchNoQuit(Launch { name, .. }) => name,
Leaf::Quit => "quit",
},
KeymapEntry::Node { name, .. } => name,
}
}
}
impl Default for KeymapEntry {
fn default() -> Self {
Self::Node {
name: "top",
map: HashMap::new(),
}
}
}