Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add pagination to the help menu
  • Loading branch information
skrewby committed Feb 19, 2020
commit 38a6c0477bbe312a663623602d52beb522d7e9be
20 changes: 20 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ pub struct App {
pub made_for_you_index: usize,
pub artists_list_index: usize,
pub clipboard_context: Option<ClipboardContext>,
pub help_docs_size: u32,
pub help_menu_page: u32,
pub help_menu_max_lines: u32,
pub help_menu_offset: u32,
}

impl App {
Expand Down Expand Up @@ -357,6 +361,10 @@ impl App {
user: None,
instant_since_last_current_playback_poll: Instant::now(),
clipboard_context: None,
help_docs_size: 0,
help_menu_page: 0,
help_menu_max_lines: 0,
help_menu_offset: 0,
}
}

Expand Down Expand Up @@ -1326,4 +1334,16 @@ impl App {
}
}
}

pub fn calculate_help_menu_offset(&mut self) {
let old_offset = self.help_menu_offset;

if self.help_menu_max_lines < self.help_docs_size {
self.help_menu_offset = self.help_menu_page * self.help_menu_max_lines + 1;
}
if self.help_menu_offset > self.help_docs_size {
self.help_menu_offset = old_offset;
self.help_menu_page -= 1;
}
}
}
12 changes: 11 additions & 1 deletion src/handlers/help_menu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
use crate::{app::App, event::Key};

pub fn handler(key: Key, _app: &mut App) {
pub fn handler(key: Key, app: &mut App) {
match key {
Key::Ctrl('d') => {
app.help_menu_page += 1;
app.calculate_help_menu_offset();
}
Key::Ctrl('u') => {
if app.help_menu_page > 0 {
app.help_menu_page -= 1;
app.calculate_help_menu_offset();
}
}
_ => {}
};
}
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ fn main() -> Result<(), failure::Error> {

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

app.help_docs_size = ui::help::get_help_docs().len() as u32;

// Now that spotify is ready, check if the user has already selected a device_id to
// play music on, if not send them to the device selection view
if app.client_config.device_id.is_none() {
Expand All @@ -210,6 +212,13 @@ fn main() -> Result<(), failure::Error> {
loop {
// Get the size of the screen on each loop to account for resize event
if let Ok(size) = terminal.backend().size() {
// Reset the help menu is the terminal was resized
if app.size != size {
app.help_menu_max_lines = 0;
app.help_menu_offset = 0;
app.help_menu_page = 0;
}

app.size = size;

// Based on the size of the terminal, adjust the search limit.
Expand All @@ -218,6 +227,14 @@ fn main() -> Result<(), failure::Error> {
app.large_search_limit = min((f32::from(size.height) / 1.4) as u32, max_limit);
app.small_search_limit =
min((f32::from(size.height) / 2.85) as u32, max_limit / 2);

// Based on the size of the terminal, adjust how many lines are
// dislayed in the help menu
if app.size.height > 8 {
app.help_menu_max_lines = (app.size.height as u32) - 8;
} else {
app.help_menu_max_lines = 0;
}
};

let current_route = app.get_current_route();
Expand Down
3 changes: 2 additions & 1 deletion src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod audio_analysis;
mod help;
pub mod help;
mod util;
use super::{
app::{
Expand Down Expand Up @@ -85,6 +85,7 @@ where
let header = ["Description", "Event", "Context"];

let help_docs = get_help_docs();
let help_docs = &help_docs[app.help_menu_offset as usize..];

let rows = help_docs
.iter()
Expand Down