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
|
use crossterm::event::{KeyEvent, KeyEventKind};
use ratatui::prelude::{
Alignment, Color, Constraint, Direction, Frame, Layout, Line, Margin, Rect,
Span, Style, Stylize,
};
use ratatui::widgets::block::{Block, BorderType, Title};
use ratatui::widgets::{
Borders, Clear, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState,
Wrap,
};
use crate::app_action::AppAction;
use crate::component::Component;
use crate::keys::key_commands::{serialize_key_event, KeyCommand};
#[derive(Default)]
pub struct GlobalKeys {
pub key_commands: Vec<KeyCommand>,
pub should_show: bool,
pub scroll: usize,
pub scroll_state: ScrollbarState,
}
impl Component for GlobalKeys {
fn init(&mut self) -> eyre::Result<()> {
self.key_commands.append(&mut vec![KeyCommand {
key_code: "?".to_string(),
description: "Toggle help menu".to_string(),
action: None,
}]);
self.scroll_state =
ScrollbarState::new(self.key_commands.len()).position(self.scroll);
Ok(())
}
fn handle_key_event(
&mut self,
key: KeyEvent,
) -> eyre::Result<Option<AppAction>> {
if key.kind == KeyEventKind::Press {
let key_event = serialize_key_event(key);
let eat_input = match key_event.as_str() {
"?" => {
self.should_show = !self.should_show;
self.scroll = 0;
true
}
"g" => {
self.scroll = 0;
true
}
"G" => {
self.scroll = self.key_commands.len() - 1;
true
}
"down" | "j" => {
if self.scroll < self.key_commands.len() - 1 {
self.scroll += 1;
}
true
}
"up" | "k" => {
if self.scroll > 0 {
self.scroll -= 1;
}
true
}
_ => false,
};
self.scroll_state = self.scroll_state.position(self.scroll);
if eat_input && self.should_show {
return Ok(None);
}
for key_command in &mut self.key_commands {
if key_command.key_code == key_event {
return Ok(key_command.action.clone());
}
}
}
Ok(None)
}
fn render(&mut self, frame: &mut Frame, rect: Rect) -> eyre::Result<()> {
let vertical_center = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(50 / 2),
Constraint::Percentage(50),
Constraint::Percentage(50 / 2),
])
.split(rect);
let center = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(50 / 2),
Constraint::Percentage(50),
Constraint::Percentage(50 / 2),
])
.split(vertical_center[1])[1];
let block = Block::default()
.title(
Title::from("Keyboard shortcuts").alignment(Alignment::Center),
)
.borders(Borders::ALL)
.border_type(BorderType::Thick);
let mut lines: Vec<Line> = vec![];
for key_command in &mut self.key_commands {
let command = Span::from(key_command.key_code.clone());
let description =
Span::from(key_command.description.clone()).italic();
let spacer = Span::from(" ");
let line = Line::from(vec![command, spacer, description]);
lines.push(line);
}
let commands = Paragraph::new(lines)
.block(block)
.wrap(Wrap { trim: true })
.scroll((u16::try_from(self.scroll)?, 0))
.style(Style::default().bg(Color::DarkGray).fg(Color::White));
if self.should_show {
frame.render_widget(Clear, center);
frame.render_widget(commands, center);
frame.render_stateful_widget(
Scrollbar::new(ScrollbarOrientation::VerticalRight),
center.inner(&Margin {
vertical: 1,
horizontal: 0,
}),
&mut self.scroll_state,
);
}
Ok(())
}
}
|