Skip to content

Commit e4e057b

Browse files
authored
Cleanup before v1.0.0 release (#31)
* docs: rephrase wording in README.md * chore: add script to test release-please * chore: move modes.rs example outside examples/crossterm/ dir
1 parent 82a8f06 commit e4e057b

File tree

7 files changed

+153
-68
lines changed

7 files changed

+153
-68
lines changed

.github/scripts/release.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
5+
TARGET_BRANCH="${1:-main}"
6+
7+
# An overview of how release-please works:
8+
#
9+
# It only runs against the target remote branch of the repository. Any changes made locally will
10+
# not trigger a release.
11+
#
12+
# By default, only commits start with "feat:", "fix:", or "deps:" will trigger a new release.
13+
release-please release-pr \
14+
--token "$(gh auth token)" \
15+
--repo-url rezigned/keymap-rs \
16+
--config-file .github/prerelease-please-config.json \
17+
--manifest-file .github/.release-please-manifest.json \
18+
--target-branch "${TARGET_BRANCH}" \
19+
--debug \
20+
--dry-run

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ on:
55
branches: ["main"]
66
paths-ignore:
77
- '**.md'
8+
- '.github/scripts/**'
89
pull_request:
910
branches: ["main"]
1011
paths-ignore:
1112
- '**.md'
13+
- '.github/scripts/**'
1214

1315
env:
1416
CARGO_TERM_COLOR: always

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ name = "crossterm_derived_config"
6060
path = "examples/crossterm/derived_config.rs"
6161
required-features = ["crossterm", "derive"]
6262

63-
[[example]]
64-
name = "crossterm_modes"
65-
path = "examples/crossterm/modes.rs"
66-
required-features = ["crossterm", "derive"]
67-
6863
[[example]]
6964
name = "termion"
7065
required-features = ["termion"]
@@ -78,3 +73,8 @@ required-features = ["termion", "derive"]
7873
name = "termion_derived_config"
7974
path = "examples/termion/derived_config.rs"
8075
required-features = ["termion", "derive"]
76+
77+
[[example]]
78+
name = "modes"
79+
path = "examples/modes.rs"
80+
required-features = ["crossterm", "derive"]

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ match config.get(&key) {
9898

9999
### 2. Using External Configuration
100100

101-
You can also load keymaps from external files (e.g., `config.toml`). This is useful for user-configurable keybindings.
101+
Keymaps can also be loaded from external files (e.g., `config.toml`). This is useful for user-configurable keybindings.
102102

103103
**Example `config.toml`:**
104104

@@ -108,7 +108,7 @@ Jump = { keys = ["j", "up"], description = "Jump with 'j' or up arrow!" }
108108
Quit = { keys = ["@any"], description = "Quit on any key press." }
109109
```
110110

111-
You have two ways to load this configuration:
111+
This configuration can be loaded in two ways:
112112

113113
#### `Config<T>`: Load from File Only
114114

@@ -143,7 +143,7 @@ let config: DerivedConfig<Action> = toml::from_str(config_str).unwrap();
143143

144144
### 3. Compile-Time Validation
145145

146-
The `keymap_derive` macro validates all key strings at **compile time**, so you get immediate feedback on invalid syntax.
146+
The `keymap_derive` macro validates all key strings at **compile time**, providing immediate feedback on invalid syntax.
147147

148148
**Invalid Key Example:**
149149

@@ -170,7 +170,7 @@ error: Invalid key "enter2": Parse error at position 5: expect end of input, fou
170170

171171
### 4. Direct Key Parsing
172172

173-
You can also parse key strings directly into a `KeyMap` or a backend-specific key event.
173+
Key strings can also be parsed directly into a `KeyMap` or a backend-specific key event.
174174

175175
```rust
176176
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

examples/crossterm/modes.rs

Lines changed: 0 additions & 56 deletions
This file was deleted.

examples/crossterm/utils.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use std::io;
2+
13
#[cfg(feature = "crossterm")]
24
use crossterm::{cursor, execute, style::Print};
5+
use crossterm::{
6+
event::{read, Event, KeyEvent},
7+
terminal::{disable_raw_mode, enable_raw_mode},
8+
};
39

4-
#[allow(unused)]
10+
#[allow(dead_code)]
511
#[cfg(feature = "crossterm")]
612
pub(crate) fn output<T: std::fmt::Display>() -> impl FnMut(T) -> std::io::Result<()> {
7-
use std::fmt::Display;
8-
913
use crossterm::terminal::{Clear, ClearType};
1014

1115
let mut stdout = std::io::stdout();
@@ -19,5 +23,36 @@ pub(crate) fn output<T: std::fmt::Display>() -> impl FnMut(T) -> std::io::Result
1923
}
2024
}
2125

26+
#[allow(dead_code)]
27+
pub(crate) fn print(s: &str) -> bool {
28+
println!("{s}\r");
29+
false
30+
}
31+
32+
#[allow(dead_code)]
33+
pub(crate) fn quit(s: &str) -> bool {
34+
println!("{s}\r");
35+
true
36+
}
37+
38+
#[allow(dead_code)]
39+
pub(crate) fn run<F>(mut f: F) -> io::Result<()>
40+
where
41+
F: FnMut(KeyEvent) -> bool,
42+
{
43+
enable_raw_mode()?;
44+
45+
loop {
46+
if let Event::Key(key) = read()? {
47+
let quit = f(key);
48+
if quit {
49+
break;
50+
}
51+
}
52+
}
53+
54+
disable_raw_mode()
55+
}
56+
2257
#[allow(unused)]
2358
fn main() {}

examples/modes.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::collections::HashMap;
2+
3+
use keymap::DerivedConfig;
4+
use serde::Deserialize;
5+
6+
use crate::crossterm_utils::{print, quit, run};
7+
8+
#[path = "./crossterm/utils.rs"]
9+
mod crossterm_utils;
10+
11+
#[derive(keymap::KeyMap, Deserialize, Debug, Hash, Eq, PartialEq)]
12+
enum HomeAction {
13+
#[key("esc")]
14+
Quit,
15+
#[key("e")]
16+
Edit,
17+
}
18+
19+
#[derive(keymap::KeyMap, Deserialize, Debug, Hash, Eq, PartialEq)]
20+
enum EditAction {
21+
#[key("esc")]
22+
Exit,
23+
}
24+
25+
#[derive(Deserialize, Debug)]
26+
#[serde(untagged)]
27+
enum Actions {
28+
Home(DerivedConfig<HomeAction>),
29+
Edit(DerivedConfig<EditAction>),
30+
}
31+
32+
type Modes = HashMap<String, Actions>;
33+
34+
#[allow(unused)]
35+
pub(crate) const CONFIG: &str = r#"
36+
[home]
37+
Quit = { keys = ["esc", "q"], description = "Quit the app" }
38+
Edit = { keys = ["e"], description = "Enter edit mode" }
39+
40+
[edit]
41+
Exit = { keys = ["esc", "q"], description = "Exit edit mode" }
42+
"#;
43+
44+
fn main() -> std::io::Result<()> {
45+
let modes: Modes = toml::from_str(CONFIG).unwrap();
46+
let mut mode = "home";
47+
48+
println!("mode: {mode}\r");
49+
50+
run(move |key| match mode {
51+
"home" => {
52+
let Some(Actions::Home(config)) = modes.get(mode) else {
53+
return false;
54+
};
55+
56+
match config.get(&key) {
57+
Some(action) => match action {
58+
HomeAction::Quit => quit("quit!"),
59+
HomeAction::Edit => {
60+
mode = "edit";
61+
print("enter edit mode!")
62+
}
63+
},
64+
None => print(&format!("{}", key.code)),
65+
}
66+
}
67+
"edit" => {
68+
let Some(Actions::Edit(config)) = modes.get(mode) else {
69+
return false;
70+
};
71+
72+
match config.get(&key) {
73+
Some(action) => match action {
74+
EditAction::Exit => {
75+
mode = "home";
76+
print("exit edit mode!")
77+
}
78+
},
79+
None => print(&format!("{}", key.code)),
80+
}
81+
}
82+
_ => unreachable!(),
83+
})
84+
}

0 commit comments

Comments
 (0)