返回 CodeWhale
hints.rs
1 //! Gallery: key hints, and hints folded from a keymap.
2
3 use crossterm::event::KeyCode;
4 use ratatui::{buffer::Buffer, layout::Rect};
5
6 use super::{Entry, arrows};
7 use crate::{
8 Binding, KeyChord, KeyHint, KeyHints, KeyHintsWords, Keymap, Paint, Theme, keys::Platform,
9 };
10
11 fn key_hints(area: Rect, buf: &mut Buffer, theme: &Theme) {
12 let hints = KeyHints::new(vec![
13 KeyHint::new(arrows(theme), "move"),
14 KeyHint::new("Enter", "change"),
15 KeyHint::new("r", "reset"),
16 KeyHint::new("/", "search"),
17 KeyHint::new("Ctrl+S", "save").disabled(),
18 KeyHint::new("Esc", "close"),
19 ]);
20 hints.paint(area, buf, theme);
21 }
22
23 /// The settings screen's keys, most important first by priority: when the
24 /// row is narrow the lowest fold away and `? more` says so.
25 fn settings_keymap() -> Keymap<&'static str> {
26 Keymap::new()
27 .with(Binding::pair(KeyCode::Up, KeyCode::Down, "move", "move").priority(200))
28 .with(Binding::new(KeyCode::Enter, "change", "change").priority(250))
29 .with(Binding::new(KeyChord::char('r'), "reset", "reset").priority(100))
30 .with(Binding::new(KeyChord::char('/'), "search", "search").priority(120))
31 .with(
32 Binding::new(KeyChord::ctrl('s'), "save", "save")
33 .priority(80)
34 .disabled(),
35 )
36 .with(Binding::new(KeyCode::Esc, "close", "close").priority(240))
37 .with(Binding::help(KeyChord::char('?'), "help"))
38 }
39
40 fn keymap_hints(area: Rect, buf: &mut Buffer, theme: &Theme) {
41 let platform = Platform::current(theme.ascii());
42 let words = KeyHintsWords::default();
43 let map = settings_keymap();
44 // The row folds to the width it is given: the whole area, then a
45 // narrower one a host with a sidebar would have.
46 for (row, width) in [area.width, 34.min(area.width)].into_iter().enumerate() {
47 if row >= usize::from(area.height) {
48 break;
49 }
50 map.hints(platform, width, &words).paint(
51 Rect {
52 y: area.y + row as u16,
53 width,
54 height: 1,
55 ..area
56 },
57 buf,
58 theme,
59 );
60 }
61 }
62
63 pub(crate) fn entries() -> Vec<Entry> {
64 vec![
65 Entry {
66 name: "key-hints",
67 width: 60,
68 height: 2,
69 draw: key_hints,
70 },
71 Entry {
72 name: "keymap-hints",
73 width: 60,
74 height: 2,
75 draw: keymap_hints,
76 },
77 ]
78 }
79
79 lines RUST