forked from jtroo/kanata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.rs
More file actions
168 lines (147 loc) · 5.58 KB
/
args.rs
File metadata and controls
168 lines (147 loc) · 5.58 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use clap::Parser;
#[cfg(feature = "tcp_server")]
use kanata_state_machine::SocketAddrWrapper;
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(author, version, verbatim_doc_comment)]
/// kanata: an advanced software key remapper
///
/// kanata remaps key presses to other keys or complex actions depending on the
/// configuration for that key. You can find the guide for creating a config
/// file here: https://github.com/jtroo/kanata/blob/main/docs/config.adoc
///
/// If you need help, please feel welcome to create an issue or discussion in
/// the kanata repository: https://github.com/jtroo/kanata
pub struct Args {
// Display different platform specific paths based on the target OS
#[cfg_attr(
target_os = "windows",
doc = r"Configuration file(s) to use with kanata. If not specified, defaults to
kanata.kbd in the current working directory and
'C:\Users\user\AppData\Roaming\kanata\kanata.kbd'."
)]
#[cfg_attr(
target_os = "macos",
doc = "Configuration file(s) to use with kanata. If not specified, defaults to
kanata.kbd in the current working directory and
'$HOME/Library/Application Support/kanata/kanata.kbd'."
)]
#[cfg_attr(
not(any(target_os = "macos", target_os = "windows")),
doc = "Configuration file(s) to use with kanata. If not specified, defaults to
kanata.kbd in the current working directory and
'$XDG_CONFIG_HOME/kanata/kanata.kbd'."
)]
#[arg(short, long, verbatim_doc_comment)]
pub cfg: Option<Vec<PathBuf>>,
/// Read configuration from stdin instead of a file.
#[arg(long, verbatim_doc_comment)]
pub cfg_stdin: bool,
/// Port or full address (IP:PORT) to run the optional TCP server on. If blank,
/// no TCP port will be listened on.
#[cfg(feature = "tcp_server")]
#[arg(
short = 'p',
long = "port",
value_name = "PORT or IP:PORT",
verbatim_doc_comment
)]
pub tcp_server_address: Option<SocketAddrWrapper>,
/// Path for the symlink pointing to the newly-created device. If blank, no
/// symlink will be created.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[arg(short, long, verbatim_doc_comment)]
pub symlink_path: Option<String>,
/// List the keyboards available for grabbing and exit.
#[cfg(any(
target_os = "macos",
any(target_os = "linux", target_os = "android"),
all(target_os = "windows", feature = "interception_driver")
))]
#[arg(short, long)]
pub list: bool,
/// Disable logging, except for errors. Takes precedent over debug and trace.
#[arg(short, long)]
pub quiet: bool,
/// Enable debug logging.
#[arg(short, long)]
pub debug: bool,
/// Enable trace logging; implies --debug as well.
#[arg(short, long)]
pub trace: bool,
/// Remove the startup delay.
/// In some cases, removing the delay may cause keyboard issues on startup.
#[arg(short, long, verbatim_doc_comment)]
pub nodelay: bool,
/// Milliseconds to wait before attempting to register a newly connected
/// device. The default is 200.
///
/// You may wish to increase this if you have a device that is failing
/// to register - the device may be taking too long to become ready.
#[cfg(any(target_os = "linux", target_os = "android"))]
#[arg(short, long, verbatim_doc_comment)]
pub wait_device_ms: Option<u64>,
/// Validate configuration file and exit
#[arg(long, verbatim_doc_comment)]
pub check: bool,
/// Log layer changes even if the configuration file has set the defcfg
/// option to false. Useful if you are experimenting with a new
/// configuration but want to default to no logging.
#[arg(long, verbatim_doc_comment)]
pub log_layer_changes: bool,
/// Skip the "Press enter to exit" prompt and exit immediately.
/// Useful for running kanata as a background service (e.g., systemd)
/// where automatic restart on failure is desired.
#[arg(long, verbatim_doc_comment)]
pub no_wait: bool,
/// Exit code to use when emergency exit is triggered (LCtrl+Space+Escape).
/// Default is 0 (success). Set to non-zero if your service manager should
/// treat emergency exit as a failure and restart.
#[arg(long, default_value = "0", verbatim_doc_comment)]
pub emergency_exit_code: i32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_wait_flag_default_false() {
let args = Args::try_parse_from(["kanata"]).unwrap();
assert!(!args.no_wait);
}
#[test]
fn no_wait_flag_enabled() {
let args = Args::try_parse_from(["kanata", "--no-wait"]).unwrap();
assert!(args.no_wait);
}
#[test]
fn no_wait_with_other_flags() {
let args =
Args::try_parse_from(["kanata", "--no-wait", "--nodelay", "-c", "test.kbd"]).unwrap();
assert!(args.no_wait);
assert!(args.nodelay);
}
#[test]
fn emergency_exit_code_default() {
let args = Args::try_parse_from(["kanata"]).unwrap();
assert_eq!(args.emergency_exit_code, 0);
}
#[test]
fn emergency_exit_code_custom() {
let args = Args::try_parse_from(["kanata", "--emergency-exit-code", "42"]).unwrap();
assert_eq!(args.emergency_exit_code, 42);
}
#[test]
fn emergency_exit_code_with_other_flags() {
let args = Args::try_parse_from([
"kanata",
"--emergency-exit-code",
"1",
"--no-wait",
"-c",
"test.kbd",
])
.unwrap();
assert_eq!(args.emergency_exit_code, 1);
assert!(args.no_wait);
}
}