Skip to content

Commit ac122d8

Browse files
committed
fix non-ascii spaces like   are not available for keys
1 parent c4bc537 commit ac122d8

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

doc/binding_syntax.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This is the grammar of key binding representation in [W3C EBNF notation][ebnf].
3030
```ebnf
3131
key-binding ::= key-sequence
3232
key-sequence ::= key-combination ((space)+ key-combination)*
33-
space ::= ' ' | '\t'
33+
space ::= ' ' | #09 | #0A | #0C | #0D
3434
key-combination ::= (modifier '+')* key
3535
modifier ::= 'Control' | 'Ctrl' | 'Command' | 'Cmd' | 'Mod' | 'Alt' | 'Super' | 'Option' | 'Shift' |
3636
'control' | 'ctrl' | 'command' | 'cmd' | 'mod' | 'alt' | 'super' | 'option' | 'shift' |
@@ -70,7 +70,7 @@ The following modifier keys are available:
7070
- `Super`: <kbd>Windows</kbd> key on platforms other than macOS, Command key on macOS
7171
- `Alt`: <kbd>Alt</kbd> key
7272
- `Option`: An alias to <kbd>Alt</kbd> key
73-
- `Shift`: <kbd>Shift</kbd> key
73+
- `Shift`: <kbd>Shift</kbd> key (can only modify named keys)
7474

7575
> [!Caution]
7676
>

src/key.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl FromStr for Key {
7070
type Err = Error;
7171

7272
fn from_str(s: &str) -> Result<Self, Self::Err> {
73-
let s = s.trim();
73+
let s = s.trim_ascii();
7474
{
7575
let mut c = s.chars();
7676
if let (Some(c), None) = (c.next(), c.next()) {
@@ -159,7 +159,7 @@ impl FromStr for Mods {
159159
type Err = Error;
160160

161161
fn from_str(s: &str) -> Result<Self, Self::Err> {
162-
match s.trim() {
162+
match s.trim_ascii() {
163163
"Control" | "control" | "CONTROL" | "Ctrl" | "ctrl" | "CTRL" => Ok(Self::CTRL),
164164
"Command" | "command" | "COMMAND" | "Cmd" | "cmd" | "CMD" => Ok(Self::CMD),
165165
"Mod" | "mod" | "MOD" => Ok(Self::MOD),
@@ -200,7 +200,7 @@ impl FromStr for KeyInput {
200200
type Err = Error;
201201

202202
fn from_str(s: &str) -> Result<Self, Self::Err> {
203-
let mut s = s.trim().split('+');
203+
let mut s = s.trim_ascii().split('+');
204204
let mut cur = s.next().unwrap(); // Iterator by `.split()` is never empty
205205
let mut mods = Mods::NONE;
206206
loop {
@@ -282,7 +282,7 @@ impl FromStr for KeySeq {
282282
type Err = Error;
283283

284284
fn from_str(s: &str) -> Result<Self, Self::Err> {
285-
let mut keys = s.split_whitespace();
285+
let mut keys = s.split_ascii_whitespace();
286286
if let Some(key) = keys.next() {
287287
let mut seq = Self::Single(key.parse()?);
288288
for key in keys {
@@ -348,6 +348,8 @@ mod tests {
348348
),
349349
("Shift+Plus", KeyInput::new('+', Mods::SHIFT)),
350350
("Shift+Space", KeyInput::new(' ', Mods::SHIFT)),
351+
(" ", KeyInput::new(' ', Mods::NONE)),
352+
("Ctrl+ ", KeyInput::new(' ', Mods::CTRL)),
351353
];
352354

353355
for (input, expected) in tests {
@@ -397,6 +399,13 @@ mod tests {
397399
KeyInput::new('c', Mods::MOD),
398400
]),
399401
),
402+
(
403+
"  Ctrl+ ",
404+
KeySeq::from(vec![
405+
KeyInput::new(' ', Mods::NONE),
406+
KeyInput::new(' ', Mods::CTRL),
407+
]),
408+
),
400409
];
401410

402411
for (seq, expected) in tests {

src/keybind.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,20 @@ mod tests {
370370
assert_eq!(dispatcher.dispatch('a'), Some(&A::Action1));
371371
assert_eq!(dispatcher.dispatch('b'), None);
372372
}
373+
374+
#[test]
375+
fn non_ascii_space() {
376+
let binds = vec![Keybind::new(' ', A::Action1)];
377+
let mut dispatcher = KeybindDispatcher::new(binds);
378+
assert_eq!(dispatcher.dispatch(' '), Some(&A::Action1));
379+
380+
let mut dispatcher = KeybindDispatcher::default();
381+
dispatcher.bind(" ", A::Action1).unwrap();
382+
dispatcher.bind("Ctrl+ ", A::Action2).unwrap();
383+
assert_eq!(dispatcher.dispatch(' '), Some(&A::Action1));
384+
assert_eq!(
385+
dispatcher.dispatch(KeyInput::new(' ', Mods::CTRL)),
386+
Some(&A::Action2),
387+
);
388+
}
373389
}

0 commit comments

Comments
 (0)