Skip to content

Commit

Permalink
First sketch on how help window could work
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 4, 2019
1 parent cab0ec2 commit 13dd5b2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
44 changes: 37 additions & 7 deletions src/interactive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,36 @@ impl From<WalkOptions> for DisplayOptions {
}
}

pub enum FocussedPane {
Main,
Help,
}

impl Default for FocussedPane {
fn default() -> Self {
FocussedPane::Main
}
}

#[derive(Copy, Clone)]
pub struct HelpPaneState;

#[derive(Default)]
pub struct AppState {
pub root: TreeIndex,
pub selected: Option<TreeIndex>,
pub sorting: SortMode,
pub message: Option<String>,
pub help_pane: Option<HelpPaneState>,
pub focussed: FocussedPane,
}

/// State and methods representing the interactive disk usage analyser for the terminal
pub struct TerminalApp {
pub traversal: Traversal,
pub display: DisplayOptions,
pub state: AppState,
pub widgets: DrawState,
pub draw_state: DrawState,
}

enum CursorDirection {
Expand All @@ -76,7 +93,7 @@ impl TerminalApp {
traversal,
display,
state,
ref mut widgets,
ref mut draw_state,
} = self;

terminal.draw(|mut f| {
Expand All @@ -85,7 +102,7 @@ impl TerminalApp {
traversal,
display: *display,
state: &state,
widgets,
draw_state,
}
.render(&mut f, full_screen)
})?;
Expand All @@ -107,6 +124,19 @@ impl TerminalApp {
for key in keys.filter_map(Result::ok) {
self.update_message();
match key {
Char('?') => {
use FocussedPane::*;
self.state.focussed = match self.state.focussed {
Main => {
self.state.help_pane = Some(HelpPaneState);
Help
}
Help => {
self.state.help_pane = None;
Main
}
}
}
Char('O') => self.open_that(),
Char('u') => self.exit_node(),
Char('o') => self.enter_node(),
Expand Down Expand Up @@ -208,13 +238,13 @@ impl TerminalApp {
root: traversal.root_index,
sorting: Default::default(),
message: Some("-> scanning <-".into()),
selected: None,
..Default::default()
};
MainWindow {
traversal,
display: display_options,
state: &state,
widgets: &mut Default::default(),
draw_state: &mut Default::default(),
}
.render(&mut f, full_screen)
})?;
Expand All @@ -230,12 +260,12 @@ impl TerminalApp {
state: AppState {
root,
sorting,
message: None,
selected,
..Default::default()
},
display: display_options,
traversal,
widgets: Default::default(),
draw_state: Default::default(),
})
}
}
27 changes: 21 additions & 6 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use crate::{
},
traverse::Traversal,
};
use tui::widgets::Block;
use tui::{
buffer::Buffer,
layout::{Constraint, Direction, Layout, Rect},
widgets::Widget,
};

/// The state that can be mutated while drawing
/// This is easiest compared to alternatives, but at least it's restricted to a subset of the state
#[derive(Default)]
pub struct DrawState {
entries_list: ListState,
Expand All @@ -20,7 +23,7 @@ pub struct MainWindow<'a, 'b, 'c> {
pub traversal: &'a Traversal,
pub display: DisplayOptions,
pub state: &'b AppState,
pub widgets: &'c mut DrawState,
pub draw_state: &'c mut DrawState,
}

impl<'a, 'b, 'c> Widget for MainWindow<'a, 'b, 'c> {
Expand All @@ -35,29 +38,41 @@ impl<'a, 'b, 'c> Widget for MainWindow<'a, 'b, 'c> {
},
display,
state,
ref mut widgets,
ref mut draw_state,
} = self;
let regions = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Max(256), Constraint::Length(1)].as_ref())
.split(area);
let (entries, footer) = (regions[0], regions[1]);
let (entries_area, footer_area) = (regions[0], regions[1]);
let (entries_area, help_area_state) = match state.help_pane {
Some(state) => (entries_area, Some((entries_area, state))),
None => (entries_area, None),
};
Entries {
tree: &tree,
root: state.root,
display: *display,
sorting: state.sorting,
selected: state.selected,
list: &mut widgets.entries_list,
list: &mut draw_state.entries_list,
}
.draw(entries_area, buf);

if let Some((help_area, _)) = help_area_state {
use tui::widgets::Borders;
Block::default()
.title("Help")
.borders(Borders::ALL)
.draw(help_area, buf);
}
.draw(entries, buf);

Footer {
total_bytes: *total_bytes,
entries_traversed: *entries_traversed,
format: display.byte_format,
message: state.message.clone(),
}
.draw(footer, buf);
.draw(footer_area, buf);
}
}

0 comments on commit 13dd5b2

Please sign in to comment.