diff options
author | Tim Oram <dev@mitmaro.ca> | 2019-07-01 14:45:57 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2019-07-01 14:45:57 -0230 |
commit | e892bb48335e1e744942d165779c496890e56423 (patch) | |
tree | f9f8a527b6f93f12ceef3c755e7fe43db1cef097 /src | |
parent | 017735fa8561e4a25b54af9f2a60c52ee8e0d347 (diff) |
Move help to module
Diffstat (limited to 'src')
-rw-r--r-- | src/application.rs | 59 | ||||
-rw-r--r-- | src/help/help.rs | 105 | ||||
-rw-r--r-- | src/help/mod.rs | 4 | ||||
-rw-r--r-- | src/list/list.rs | 31 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/view/view.rs | 54 |
6 files changed, 136 insertions, 118 deletions
diff --git a/src/application.rs b/src/application.rs index d324d01..814b5c6 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,32 +1,17 @@ use crate::config::Config; use crate::confirm_abort::ConfirmAbort; use crate::confirm_rebase::ConfirmRebase; -use crate::constants::{LIST_HELP_LINES, VISUAL_MODE_HELP_LINES}; use crate::edit::Edit; use crate::error::Error; use crate::exiting::Exiting; use crate::external_editor::ExternalEditor; use crate::git_interactive::GitInteractive; +use crate::help::Help; use crate::input::{Input, InputHandler}; use crate::list::List; -use crate::process::{HandleInputResult, HandleInputResultBuilder, ProcessModule, ProcessResult, State}; +use crate::process::{HandleInputResult, ProcessModule, ProcessResult, State}; use crate::show_commit::ShowCommit; use crate::view::View; -use core::borrow::Borrow; - -fn get_help_lines(help_state: &State) -> &[(&str, &str)] { - if let State::List(visual_mode) = *help_state { - if visual_mode { - LIST_HELP_LINES - } - else { - VISUAL_MODE_HELP_LINES - } - } - else { - &[] - } -} pub struct Application<'a> { confirm_abort: ConfirmAbort, @@ -36,6 +21,7 @@ pub struct Application<'a> { exiting: Exiting, external_editor: ExternalEditor<'a>, git_interactive: GitInteractive, + help: Help, input_handler: &'a InputHandler<'a>, list: List<'a>, show_commit: ShowCommit, @@ -58,6 +44,7 @@ impl<'a> Application<'a> { exiting: Exiting::new(), external_editor: ExternalEditor::new(config), git_interactive, + help: Help::new(), input_handler, list: List::new(config), show_commit: ShowCommit::new(), @@ -73,7 +60,7 @@ impl<'a> Application<'a> { State::Error { .. } => self.error.activate(state, &self.git_interactive), State::Exiting => self.exiting.activate(state, &self.git_interactive), State::ExternalEditor => self.external_editor.activate(state, &self.git_interactive), - State::Help(_) => {}, + State::Help(_) => self.help.activate(state, &self.git_interactive), State::List(_) => self.list.activate(state, &self.git_interactive), State::ShowCommit => self.show_commit.activate(state, &self.git_interactive), State::WindowSizeError(_) => {}, @@ -88,7 +75,7 @@ impl<'a> Application<'a> { State::Error { .. } => self.error.deactivate(), State::Exiting => self.exiting.deactivate(), State::ExternalEditor => self.external_editor.deactivate(), - State::Help(_) => {}, + State::Help(_) => self.help.deactivate(), State::List(_) => self.list.deactivate(), State::ShowCommit => self.show_commit.deactivate(), State::WindowSizeError(_) => {}, @@ -103,7 +90,7 @@ impl<'a> Application<'a> { State::Error { .. } => self.error.process(&mut self.git_interactive), State::Exiting => self.exiting.process(&mut self.git_interactive), State::ExternalEditor => self.external_editor.process(&mut self.git_interactive), - State::Help(_) => ProcessResult::new(), + State::Help(_) => self.help.process(&mut self.git_interactive), State::List(_) => self.list.process_with_view(&mut self.git_interactive, &self.view), State::ShowCommit => self.show_commit.process(&mut self.git_interactive), State::WindowSizeError(_) => ProcessResult::new(), @@ -123,7 +110,7 @@ impl<'a> Application<'a> { State::Error { .. } => self.error.render(&self.view, &self.git_interactive), State::Exiting => self.exiting.render(&self.view, &self.git_interactive), State::ExternalEditor => self.external_editor.render(&self.view, &self.git_interactive), - State::Help(help_state) => self.draw_help(help_state.borrow()), + State::Help(_) => self.help.render(&self.view, &self.git_interactive), State::List(_) => self.list.render(&self.view, &self.git_interactive), State::ShowCommit => self.show_commit.render(&self.view, &self.git_interactive), State::WindowSizeError(_) => self.draw_window_size_error(), @@ -131,10 +118,6 @@ impl<'a> Application<'a> { self.view.refresh() } - fn draw_help(&self, help_state: &State) { - self.view.draw_help(get_help_lines(help_state)) - } - fn draw_window_size_error(&self) { self.view.draw_window_size_error(); } @@ -163,11 +146,11 @@ impl<'a> Application<'a> { self.external_editor .handle_input(&self.input_handler, &mut self.git_interactive) }, - State::Help(help_state) => self.handle_help_input(help_state.borrow()), - State::List(_) => { - self.list + State::Help(_) => { + self.help .handle_input_with_view(&self.input_handler, &mut self.git_interactive, &self.view) }, + State::List(_) => self.list.handle_input(&self.input_handler, &mut self.git_interactive), State::ShowCommit => { self.show_commit .handle_input_with_view(&self.input_handler, &mut self.git_interactive, &self.view) @@ -176,26 +159,6 @@ impl<'a> Application<'a> { } } - fn handle_help_input(&mut self, help_state: &State) -> HandleInputResult { - let input = self.get_input(); - let mut result = HandleInputResultBuilder::new(input); - match input { - Input::MoveCursorDown => { - self.view.update_help_top(false, false, get_help_lines(help_state)); - }, - Input::MoveCursorUp => { - self.view.update_help_top(true, false, get_help_lines(help_state)); - }, - Input::Resize => { - self.view.update_help_top(true, true, get_help_lines(help_state)); - }, - _ => { - result = result.state(help_state.clone()); - }, - } - result.build() - } - pub fn handle_window_size_error_input(&mut self) -> HandleInputResult { HandleInputResult::new(self.get_input()) } diff --git a/src/help/help.rs b/src/help/help.rs new file mode 100644 index 0000000..3eda151 --- /dev/null +++ b/src/help/help.rs @@ -0,0 +1,105 @@ +use crate::constants::{LIST_HELP_LINES, VISUAL_MODE_HELP_LINES}; +use crate::git_interactive::GitInteractive; +use crate::input::{Input, InputHandler}; +use crate::process::{HandleInputResult, HandleInputResultBuilder, ProcessModule, State}; +use crate::scroll::ScrollPosition; +use crate::view::{LineSegment, View, ViewLine}; +use crate::window::WindowColor; + +fn get_help_lines(return_state: &State) -> &[(&str, &str)] { + if let State::List(visual_mode) = *return_state { + if visual_mode { + VISUAL_MODE_HELP_LINES + } + else { + LIST_HELP_LINES + } + } + else { + &[] + } +} + +pub struct Help { + scroll_position: ScrollPosition, + return_state: State, +} + +impl ProcessModule for Help { + fn activate(&mut self, state: State, _git_interactive: &GitInteractive) { + self.scroll_position.reset(); + if let State::Help(return_state) = state { + self.return_state = *return_state; + } + else { + panic!("Help module activated when not expected"); + } + } + + fn render(&self, view: &View, _git_interactive: &GitInteractive) { + let (view_width, view_height) = view.get_view_size(); + + let mut view_lines: Vec<ViewLine> = vec![]; + + for line in get_help_lines(&self.return_state) { + view_lines.push(ViewLine::new(vec![ + LineSegment::new_with_color(format!(" {:4} ", line.0).as_str(), WindowColor::IndicatorColor), + LineSegment::new(line.1), + ])); + } + + view.draw_title(false); + + view.set_color(WindowColor::Foreground); + view.set_style(false, true, false); + view.draw_str(" Key Action"); + if view_width > 13 { + let padding = " ".repeat(view_width - 13); + view.draw_str(padding.as_str()); + } + + view.draw_view_lines(view_lines, self.scroll_position.get_position(), view_height - 3); + + view.set_color(WindowColor::IndicatorColor); + view.draw_str("Any key to close"); + } +} + +impl Help { + pub fn new() -> Self { + Self { + return_state: State::List(false), + scroll_position: ScrollPosition::new(3, 6, 3), + } + } + + // TODO refactor to remove need for view + pub fn handle_input_with_view( + &mut self, + input_handler: &InputHandler, + _git_interactive: &mut GitInteractive, + view: &View, + ) -> HandleInputResult + { + let (_, window_height) = view.get_view_size(); + let input = input_handler.get_input(); + let mut result = HandleInputResultBuilder::new(input); + match input { + Input::MoveCursorDown => { + self.scroll_position + .scroll_down(window_height, get_help_lines(&self.return_state).len()); + }, + Input::MoveCursorUp => { + self.scroll_position + .scroll_up(window_height, get_help_lines(&self.return_state).len()); + }, + Input::Resize => { + self.scroll_position.reset(); + }, + _ => { + result = result.state(self.return_state.clone()); + }, + } + result.build() + } +} diff --git a/src/help/mod.rs b/src/help/mod.rs new file mode 100644 index 0000000..013e492 --- /dev/null +++ b/src/help/mod.rs @@ -0,0 +1,4 @@ +#[allow(clippy::module_inception)] +mod help; + +pub use self::help::Help; diff --git a/src/list/list.rs b/src/list/list.rs index e94ad02..7676837 100644 --- a/src/list/list.rs +++ b/src/list/list.rs @@ -5,13 +5,11 @@ use crate::constants::{ LIST_FOOTER_COMPACT_WIDTH, LIST_FOOTER_FULL, LIST_FOOTER_FULL_WIDTH, - LIST_HELP_LINES, MINIMUM_FULL_WINDOW_WIDTH, VISUAL_MODE_FOOTER_COMPACT, VISUAL_MODE_FOOTER_COMPACT_WIDTH, VISUAL_MODE_FOOTER_FULL, VISUAL_MODE_FOOTER_FULL_WIDTH, - VISUAL_MODE_HELP_LINES, }; use crate::git_interactive::GitInteractive; use crate::input::{Input, InputHandler}; @@ -36,6 +34,18 @@ pub struct List<'l> { } impl<'l> ProcessModule for List<'l> { + fn handle_input( + &mut self, + input_handler: &InputHandler, + git_interactive: &mut GitInteractive, + ) -> HandleInputResult + { + match self.state { + ListState::Normal => self.handle_normal_mode_input(input_handler, git_interactive), + ListState::Visual => self.handle_visual_mode_input(input_handler, git_interactive), + } + } + #[allow(clippy::nonminimal_bool)] fn render(&self, view: &View, git_interactive: &GitInteractive) { let (view_width, view_height) = view.get_view_size(); @@ -113,31 +123,16 @@ impl<'l> List<'l> { ProcessResult::new() } - pub fn handle_input_with_view( - &mut self, - input_handler: &InputHandler, - git_interactive: &mut GitInteractive, - view: &View, - ) -> HandleInputResult - { - match self.state { - ListState::Normal => self.handle_normal_mode_input(input_handler, git_interactive, view), - ListState::Visual => self.handle_visual_mode_input(input_handler, git_interactive, view), - } - } - fn handle_normal_mode_input( &mut self, input_handler: &InputHandler, git_interactive: &mut GitInteractive, - view: &View, ) -> HandleInputResult { let input = input_handler.get_input(); let mut result = HandleInputResultBuilder::new(input); match input { Input::Help => { - view.update_help_top(false, true, LIST_HELP_LINES); result = result.help(State::List(false)); }, Input::ShowCommit => { @@ -191,14 +186,12 @@ impl<'l> List<'l> { &mut self, input_handler: &InputHandler, git_interactive: &mut GitInteractive, - view: &View, ) -> HandleInputResult { let input = input_handler.get_input(); let mut result = HandleInputResultBuilder::new(input); match input { Input::Help => { - view.update_help_top(false, true, VISUAL_MODE_HELP_LINES); result = result.help(State::List(true)); }, Input::MoveCursorDown => { diff --git a/src/main.rs b/src/main.rs index 335136f..58fafeb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod error; mod exiting; mod external_editor; mod git_interactive; +mod help; mod input; mod line; mod list; diff --git a/src/view/view.rs b/src/view/view.rs index eade0da..d3f4ba1 100644 --- a/src/view/view.rs +++ b/src/view/view.rs @@ -12,22 +12,18 @@ use crate::constants::{ TITLE_SHORT, TITLE_SHORT_LENGTH, }; -use crate::scroll::{get_scroll_position, ScrollPosition}; -use crate::view::{LineSegment, ViewLine}; +use crate::scroll::get_scroll_position; +use crate::view::ViewLine; use crate::window::Window; use crate::window::WindowColor; pub struct View<'v> { - help_top: ScrollPosition, window: &'v Window<'v>, } impl<'v> View<'v> { pub fn new(window: &'v Window) -> Self { - Self { - help_top: ScrollPosition::new(3, 6, 3), - window, - } + Self { window } } pub fn draw_str(&self, s: &str) { @@ -182,50 +178,6 @@ impl<'v> View<'v> { } } - pub fn update_help_top(&self, scroll_up: bool, reset: bool, help_lines: &[(&str, &str)]) { - let (_, window_height) = self.window.get_window_size(); - if reset { - self.help_top.reset(); - } - else if scroll_up { - self.help_top.scroll_up(window_height as usize, help_lines.len()); - } - else { - self.help_top.scroll_down(window_height as usize, help_lines.len()); - } - } - - pub fn draw_help(&self, help_lines: &[(&str, &str)]) { - let (window_width, window_height) = self.window.get_window_size(); - let view_height = window_height as usize - 3; - - let mut view_lines: Vec<ViewLine> = vec![]; - - for line in help_lines { - view_lines.push(ViewLine::new(vec![ - LineSegment::new_with_color(format!(" {:4} ", line.0).as_str(), WindowColor::IndicatorColor), - LineSegment::new(line.1), - ])); - } - - self.window.set_style(false, false, false); - self.window.clear(); - self.draw_title(false); - - self.window.color(WindowColor::Foreground); - self.window.set_style(false, true, false); - self.window.draw_str(" Key Action"); - if window_width as usize > 13 { - let padding = " ".repeat(window_width as usize - 13); - self.window.draw_str(padding.as_str()); - } - - self.draw_view_lines(view_lines, self.help_top.get_position(), view_height); - - self.window.color(WindowColor::IndicatorColor); - self.window.draw_str("Any key to close"); - } - pub fn draw_prompt(&self, message: &str) { self.draw_title(false); self.window.set_style(false, false, false); |