feat: Add recipes/keybind - #1008
Conversation
|
Thanks for putting this together. Configurable keybindings are absolutely a real problem people run into, and I think this is a good topic for the website 👍 I think there may be a bit of scope confusion coming from the earlier discussion, so I want to clarify intent rather than push back on the idea itself. On the Ratatui website, a recipe is meant to document how to approach solving a problem in an existing app, not to prescribe a single starting template. The goal is to help readers understand the shape of the solution, the trade-offs involved, and how to integrate it into code they already have. In this case, the recipe should focus on:
Your More generally, recipes on the Ratatui site work best when they are written as guidance rather than prescriptions. They should assume readers already have an application and are looking to improve it. Explaining design goals, common patterns, and trade-offs up front helps keep the content broadly useful and avoids implying that there is a single correct architecture. As a rough guide, a structure that usually works well for recipes is something like:
I think the template works best as a nice to have at the end, along the lines of:
That keeps the recipe broadly useful, avoids locking readers into a single solution, and aligns with the overall tone of the Ratatui docs: practical guidance first, tools second. Overall, the topic is solid, the motivation is good, and I think with a small reframing this could be a really strong addition to the recipes section. |
* refact with recipe style --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: yanganto <10803111+yanganto@users.noreply.github.com>
|
Hi @joshka |
|
It seems to me that |
|
There will be a lot of possible ways to handle keybindings. If a user has a strong and clear idea about how to implement it, he will not use a crate to build it, and will do it with raw key events. These are the current ways. There are no words about right or wrong. I just provide a solution to help people if they do not have an idea, but want a way to avoid problems, and this is the majority. Most people build a tui for business task, they will not be happy to play with keybindings. I saw the pain point. (You never know how many times I say bad words when I push code, but the git tool upgraded again, and I temporarily can not use it, before I learn more what is new in the config. I do not check every config after I did all system upgraded, and I use nixOS in a roll release way, I believe the Arch user will run into the same problems) This is MIT license, I do this and will happy to use more Rust tui applications and see the ecosystem blooming. I am not native English/Romance languages speaker, I already tried AI to prevent issues. If my words is less, people will ask me to describe more, else they don't know the solution. If my words too much, people will like to think I force people should do this. As you can see, you can disable the Overall, if you think some other specific design pattern for the keybinding, or highlight others' work I am happy to add a section to do. The problem for me is should I list down the solution I never used? (Did a PR for tokio template should included async-std template at the same time?) I will be open mide and will not against it if other crate builder edit the page and want add any section about this topic in the future? Back to the topic, I want a solution to over the keybinding issues and roll out TUI application in quick and saving the developer power, let is why we do open source. Isn't it? |
* add descriptions about keybind-rs --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: yanganto <10803111+yanganto@users.noreply.github.com>
|
I did not noticed there is a really similar solution |
* update docs for crossterm-keybind 0.2 --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: yanganto <10803111+yanganto@users.noreply.github.com>
* add from scratch section --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: yanganto <10803111+yanganto@users.noreply.github.com>
|
Hi @orhun After they have more idea, they can see the configurable keybinding feature is much about building a parser for a config file. Normally, this is not a task align their business, and also they can use crate not in a blind using. If you still think we need more sections in this recipe, please let me know. I am looking forward and will actively response. |
|
Yeah that's better. I wish we could give code examples for that also. I still think that we are spending quite a large amount of time explaining concepts with references to 3rd party libs. |
* add pesudo code and trim 3rd party codes --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: yanganto <10803111+yanganto@users.noreply.github.com>
|
Hi @orhun |
|
Hi @joshka |
|
Hi there, I want to give you a good response on this, but it might be a bit delayed. If you want to get ahead of me on this, there's a bunch of background reading that would be worth brushing up on at ratatui/ratatui#627 Some quick off the shallow review points:
const BINDINGS: &[Binding] = &[
key(KeyCode::Up, Action::MoveUp),
key(KeyCode::Char('k'), Action::MoveUp),
key(KeyCode::Down, Action::MoveDown),
key(KeyCode::Char('j'), Action::MoveDown),
key(KeyCode::Left, Action::MoveLeft),
key(KeyCode::Char('h'), Action::MoveLeft),
key(KeyCode::Right, Action::MoveRight),
key(KeyCode::Char('l'), Action::MoveRight),
ctrl(KeyCode::Char('s'), Action::Save),
key(KeyCode::Char('?'), Action::Help),
shift(KeyCode::Char('h'), Action::Help),
key(KeyCode::Esc, Action::Quit),
key(KeyCode::Char('q'), Action::Quit),
ctrl(KeyCode::Char('c'), Action::Quit),
];
fn action_for_key(event: KeyEvent) -> Option<Action> {
let event = event.as_key_press_event()?;
BINDINGS.iter().find_map(|binding| {
(event.code == binding.code && event.modifiers == binding.modifiers)
.then_some(binding.action)
})
}A similar approach using a match statement would work. I also asked GPT-5.2 to review the article and it generated a pretty decent review. I don't know what your thoughts are on AI tooling, but if it's something that you don't mind, I'd recommend reading https://chatgpt.com/share/694763ed-4880-8010-a30b-beacd292d778 Side note: |
* revisit with ChatGPT 5.1 suggestions https://chatgpt.com/share/694763ed-4880-8010-a30b-beacd292d778 Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: yanganto <10803111+yanganto@users.noreply.github.com>
|
Hi @joshka Some suggestion from AI are opposite with previous review, tradeoffs is much subjective thing and no meaning because different user has different using case, so I always focus on what solutions can use now. Please kindly noted things you are talking and comparing are a discussion and implemented codes from people's work. Please kindly think again your previous helper functions shown any related thing with the title, Configurable Keybindings, of the article, or with any additional features are shown in current implementation.
There are already crates with working code over abstract discussion or promises, because Linus Torvalds says, "Show me the code". Could you open mind and accept people use the ratatui with different crates and in different way? At least author of Furthermore, I will be happy if you want me to review others work in the future, just @. For now, I believe this PR is good to go. |
No description provided.