Skip to content

Commit 6bd5ad0

Browse files
authored
Merge pull request #302 from pedrohva/help-menu-scrolling
Add pagination to the help menu
2 parents de7e29c + 019394b commit 6bd5ad0

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/app.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ pub struct App {
287287
pub made_for_you_index: usize,
288288
pub artists_list_index: usize,
289289
pub clipboard_context: Option<ClipboardContext>,
290+
pub help_docs_size: u32,
291+
pub help_menu_page: u32,
292+
pub help_menu_max_lines: u32,
293+
pub help_menu_offset: u32,
290294
}
291295

292296
impl App {
@@ -357,6 +361,10 @@ impl App {
357361
user: None,
358362
instant_since_last_current_playback_poll: Instant::now(),
359363
clipboard_context: None,
364+
help_docs_size: 0,
365+
help_menu_page: 0,
366+
help_menu_max_lines: 0,
367+
help_menu_offset: 0,
360368
}
361369
}
362370

@@ -1326,4 +1334,16 @@ impl App {
13261334
}
13271335
}
13281336
}
1337+
1338+
pub fn calculate_help_menu_offset(&mut self) {
1339+
let old_offset = self.help_menu_offset;
1340+
1341+
if self.help_menu_max_lines < self.help_docs_size {
1342+
self.help_menu_offset = self.help_menu_page * self.help_menu_max_lines;
1343+
}
1344+
if self.help_menu_offset > self.help_docs_size {
1345+
self.help_menu_offset = old_offset;
1346+
self.help_menu_page -= 1;
1347+
}
1348+
}
13291349
}

src/handlers/help_menu.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
use super::common_key_events;
12
use crate::{app::App, event::Key};
23

3-
pub fn handler(key: Key, _app: &mut App) {
4+
#[derive(PartialEq)]
5+
enum Direction {
6+
UP,
7+
DOWN,
8+
}
9+
10+
pub fn handler(key: Key, app: &mut App) {
411
match key {
12+
k if common_key_events::down_event(k) => {
13+
move_page(Direction::DOWN, app);
14+
}
15+
k if common_key_events::up_event(k) => {
16+
move_page(Direction::UP, app);
17+
}
18+
Key::Ctrl('d') => {
19+
move_page(Direction::DOWN, app);
20+
}
21+
Key::Ctrl('u') => {
22+
move_page(Direction::UP, app);
23+
}
524
_ => {}
625
};
726
}
27+
28+
fn move_page(direction: Direction, app: &mut App) {
29+
if direction == Direction::UP {
30+
if app.help_menu_page > 0 {
31+
app.help_menu_page -= 1;
32+
}
33+
} else if direction == Direction::DOWN {
34+
app.help_menu_page += 1;
35+
}
36+
app.calculate_help_menu_offset();
37+
}

src/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ fn main() -> Result<(), failure::Error> {
199199

200200
app.clipboard_context = clipboard::ClipboardProvider::new().ok();
201201

202+
app.help_docs_size = ui::help::get_help_docs().len() as u32;
203+
202204
// Now that spotify is ready, check if the user has already selected a device_id to
203205
// play music on, if not send them to the device selection view
204206
if app.client_config.device_id.is_none() {
@@ -210,6 +212,13 @@ fn main() -> Result<(), failure::Error> {
210212
loop {
211213
// Get the size of the screen on each loop to account for resize event
212214
if let Ok(size) = terminal.backend().size() {
215+
// Reset the help menu is the terminal was resized
216+
if app.size != size {
217+
app.help_menu_max_lines = 0;
218+
app.help_menu_offset = 0;
219+
app.help_menu_page = 0;
220+
}
221+
213222
app.size = size;
214223

215224
// Based on the size of the terminal, adjust the search limit.
@@ -218,6 +227,14 @@ fn main() -> Result<(), failure::Error> {
218227
app.large_search_limit = min((f32::from(size.height) / 1.4) as u32, max_limit);
219228
app.small_search_limit =
220229
min((f32::from(size.height) / 2.85) as u32, max_limit / 2);
230+
231+
// Based on the size of the terminal, adjust how many lines are
232+
// dislayed in the help menu
233+
if app.size.height > 8 {
234+
app.help_menu_max_lines = (app.size.height as u32) - 8;
235+
} else {
236+
app.help_menu_max_lines = 0;
237+
}
221238
};
222239

223240
let current_route = app.get_current_route();

src/ui/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod audio_analysis;
2-
mod help;
2+
pub mod help;
33
mod util;
44
use super::{
55
app::{
@@ -85,6 +85,7 @@ where
8585
let header = ["Description", "Event", "Context"];
8686

8787
let help_docs = get_help_docs();
88+
let help_docs = &help_docs[app.help_menu_offset as usize..];
8889

8990
let rows = help_docs
9091
.iter()

0 commit comments

Comments
 (0)