Skip to content

Commit

Permalink
Wow, help scrolling is finally working!
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 4, 2019
1 parent 31a90d7 commit 09373b2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 89 deletions.
15 changes: 7 additions & 8 deletions src/interactive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,13 @@ impl TerminalApp {

fn scroll_help(&mut self, direction: CursorDirection) {
use CursorDirection::*;
if let Some(HelpPaneState { ref mut scroll }) = self.state.help_pane {
*scroll = match direction {
Down => scroll.saturating_add(1),
Up => scroll.saturating_sub(1),
PageDown => scroll.saturating_add(10),
PageUp => scroll.saturating_sub(10),
};
}
let scroll = self.draw_state.help_scroll;
self.draw_state.help_scroll = match direction {
Down => scroll.saturating_add(1),
Up => scroll.saturating_sub(1),
PageDown => scroll.saturating_add(10),
PageUp => scroll.saturating_sub(10),
};
}

fn change_entry_selection(&mut self, direction: CursorDirection) {
Expand Down
139 changes: 61 additions & 78 deletions src/interactive/widgets/help.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::Cell;
use std::cell::{Cell, RefCell};
use tui::style::Color;
use tui::{
buffer::Buffer,
Expand All @@ -8,45 +8,75 @@ use tui::{
};

#[derive(Default, Copy, Clone)]
pub struct HelpPaneState {
pub scroll: u16,
}
pub struct HelpPaneState;

pub struct HelpPane {
pub state: HelpPaneState,
pub scroll: u16,
pub border_style: Style,
}

impl Widget for HelpPane {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let lines = Cell::new(0u16);
let count = |n| lines.set(lines.get() + n);
let spacer = || {
count(2);
[Text::Raw("\n\n".into())]
};
let title = |name| {
count(2);
[Text::Styled(
format!("{}\n\n", name).into(),
Style {
modifier: Modifier::BOLD | Modifier::UNDERLINED,
..Default::default()
},
)]
};
let hotkey = |keys, description| {
count(1);
[
Text::Styled(
let (texts, num_lines) = {
let num_lines = Cell::new(0u16);
let count = |n| num_lines.set(num_lines.get() + n);
let lines = RefCell::new(Vec::with_capacity(30));

let spacer = || {
count(2);
lines.borrow_mut().push(Text::Raw("\n\n".into()));
};
let title = |name| {
count(2);
lines.borrow_mut().push(Text::Styled(
format!("{}\n\n", name).into(),
Style {
modifier: Modifier::BOLD | Modifier::UNDERLINED,
..Default::default()
},
));
};
let hotkey = |keys, description| {
count(1);
lines.borrow_mut().push(Text::Styled(
format!("{:>10}", keys).into(),
Style {
fg: Color::Green,
..Default::default()
},
),
Text::Styled(format!(" => {}\n", description).into(), Style::default()),
]
));
lines.borrow_mut().push(Text::Styled(
format!(" => {}\n", description).into(),
Style::default(),
));
};
title("Keys for pane control");
hotkey(
"q",
"close the current pain. Closes the application if no pane is open.",
);
hotkey("<tab>", "Cycle between all open panes");
hotkey("?", "Show the help pane");
spacer();
title("Keys for Navigation");
hotkey("j", "move down an entry");
hotkey("k", "move up an entry");
hotkey("o", "descent into the selected directory");
hotkey("u", "ascent one level into the parent directory");
hotkey("Ctrl + d", "move down 10 entries at once");
hotkey("Ctrl + u", "move up 10 entries at once");
spacer();
title("Keys for sorting");
hotkey("s", "toggle sort by size ascending/descending");
spacer();
title("Keys for entry operations");
hotkey("Shift + o", "Open the entry with the associated program");
spacer();
title("Keys for application control");
hotkey("Ctrl + c", "close the application. No questions asked!");
spacer();
(lines.into_inner(), num_lines.get())
};

let mut block = Block::default()
Expand All @@ -56,56 +86,9 @@ impl Widget for HelpPane {
block.draw(area, buf);

let area = block.inner(area).inner(1);
self.state.scroll = self.state.scroll.min(lines.get());
Paragraph::new(
title("Keys for pane control")
.iter()
.chain(
hotkey(
"q",
"close the current pain. Closes the application if no pane is open.",
)
.iter(),
)
.chain(hotkey("<tab>", "Cycle between all open panes").iter())
.chain(hotkey("?", "Show the help pane").iter())
.chain(spacer().iter())
.chain(
title("Keys for Navigation")
.iter()
.chain(hotkey("j", "move down an entry").iter())
.chain(hotkey("k", "move up an entry").iter())
.chain(hotkey("o", "descent into the selected directory").iter())
.chain(hotkey("u", "ascent one level into the parent directory").iter())
.chain(hotkey("Ctrl + d", "move down 10 entries at once").iter())
.chain(hotkey("Ctrl + u", "move up 10 entries at once").iter())
.chain(spacer().iter()),
)
.chain(
title("Keys for sorting")
.iter()
.chain(hotkey("s", "toggle sort by size ascending/descending").iter())
.chain(spacer().iter()),
)
.chain(
title("Keys for entry operations")
.iter()
.chain(
hotkey("Shift + o", "Open the entry with the associated program")
.iter(),
)
.chain(spacer().iter()),
)
.chain(
title("Keys for application control")
.iter()
.chain(
hotkey("Ctrl + c", "close the application. No questions asked!").iter(),
)
.chain(spacer().iter()),
),
)
.scroll(self.state.scroll)
.draw(area, buf);
self.scroll = self.scroll.min(num_lines.saturating_sub(area.height));
Paragraph::new(texts.iter())
.scroll(self.scroll)
.draw(area, buf);
}
}
9 changes: 6 additions & 3 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tui::{
#[derive(Default)]
pub struct DrawState {
entries_list: ListState,
pub help_scroll: u16,
}

pub struct MainWindow<'a, 'b, 'c> {
Expand Down Expand Up @@ -86,11 +87,13 @@ impl<'a, 'b, 'c> Widget for MainWindow<'a, 'b, 'c> {
.draw(entries_area, buf);

if let Some((help_area, state)) = help_area_state {
HelpPane {
let mut pane = HelpPane {
scroll: draw_state.help_scroll,
state,
border_style: help_style,
}
.draw(help_area, buf);
};
pane.draw(help_area, buf);
draw_state.help_scroll = pane.scroll;
}

Footer {
Expand Down

0 comments on commit 09373b2

Please sign in to comment.