diff options
author | Tim Oram <dev@mitmaro.ca> | 2019-07-02 09:54:28 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2019-07-02 09:54:28 -0230 |
commit | 8947002607684d6ba7fa6d99511800efe82c1c26 (patch) | |
tree | 292b4157bafe31ffc7520da3c6c1f17fb9266844 | |
parent | e892bb48335e1e744942d165779c496890e56423 (diff) |
Move window state error to module
-rw-r--r-- | src/application.rs | 30 | ||||
-rw-r--r-- | src/constants.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/view/view.rs | 35 | ||||
-rw-r--r-- | src/window_size_error/mod.rs | 4 | ||||
-rw-r--r-- | src/window_size_error/window_size_error.rs | 61 |
6 files changed, 83 insertions, 56 deletions
diff --git a/src/application.rs b/src/application.rs index 814b5c6..e58eb6e 100644 --- a/src/application.rs +++ b/src/application.rs @@ -7,11 +7,12 @@ 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::input::InputHandler; use crate::list::List; use crate::process::{HandleInputResult, ProcessModule, ProcessResult, State}; use crate::show_commit::ShowCommit; use crate::view::View; +use crate::window_size_error::WindowSizeError; pub struct Application<'a> { confirm_abort: ConfirmAbort, @@ -26,6 +27,7 @@ pub struct Application<'a> { list: List<'a>, show_commit: ShowCommit, view: View<'a>, + window_size_error: WindowSizeError, } impl<'a> Application<'a> { @@ -49,6 +51,7 @@ impl<'a> Application<'a> { list: List::new(config), show_commit: ShowCommit::new(), view, + window_size_error: WindowSizeError::new(), } } @@ -63,7 +66,7 @@ impl<'a> Application<'a> { 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(_) => {}, + State::WindowSizeError(_) => self.window_size_error.activate(state, &self.git_interactive), } } @@ -78,7 +81,7 @@ impl<'a> Application<'a> { State::Help(_) => self.help.deactivate(), State::List(_) => self.list.deactivate(), State::ShowCommit => self.show_commit.deactivate(), - State::WindowSizeError(_) => {}, + State::WindowSizeError(_) => self.window_size_error.deactivate(), } } @@ -93,7 +96,7 @@ impl<'a> Application<'a> { 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(), + State::WindowSizeError(_) => self.window_size_error.process(&mut self.git_interactive), } } @@ -113,19 +116,11 @@ impl<'a> Application<'a> { 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(), + State::WindowSizeError(_) => self.window_size_error.render(&self.view, &self.git_interactive), } self.view.refresh() } - fn draw_window_size_error(&self) { - self.view.draw_window_size_error(); - } - - pub fn get_input(&self) -> Input { - self.input_handler.get_input() - } - pub fn handle_input(&mut self, state: State) -> HandleInputResult { match state { State::ConfirmAbort => { @@ -155,14 +150,13 @@ impl<'a> Application<'a> { self.show_commit .handle_input_with_view(&self.input_handler, &mut self.git_interactive, &self.view) }, - State::WindowSizeError(_) => self.handle_window_size_error_input(), + State::WindowSizeError(_) => { + self.window_size_error + .handle_input(&self.input_handler, &mut self.git_interactive) + }, } } - pub fn handle_window_size_error_input(&mut self) -> HandleInputResult { - HandleInputResult::new(self.get_input()) - } - pub fn write_file(&self) -> Result<(), String> { self.git_interactive.write_file() } diff --git a/src/constants.rs b/src/constants.rs index 2a6ffd8..1b6e40e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -16,12 +16,12 @@ pub const VISUAL_MODE_FOOTER_COMPACT: &str = "(V) up,down,j,k,p,r,e,s,f,d,?"; pub const VISUAL_MODE_FOOTER_COMPACT_WIDTH: usize = 29; pub const HEIGHT_ERROR_MESSAGE: &str = "Window too small, increase height to continue\n"; -pub const MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH: i32 = 45; +pub const MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH: usize = 45; pub const SHORT_ERROR_MESSAGE: &str = "Window too small\n"; -pub const SHORT_ERROR_MESSAGE_WIDTH: i32 = 16; +pub const SHORT_ERROR_MESSAGE_WIDTH: usize = 16; -pub const MINIMUM_WINDOW_HEIGHT: i32 = 5; // title + pad top + line + pad bottom + help -pub const MINIMUM_COMPACT_WINDOW_WIDTH: i32 = 20; //">s ccc mmmmmmmmmmmmm".len() +pub const MINIMUM_WINDOW_HEIGHT: usize = 5; // title + pad top + line + pad bottom + help +pub const MINIMUM_COMPACT_WINDOW_WIDTH: usize = 20; //">s ccc mmmmmmmmmmmmm".len() pub const MINIMUM_FULL_WINDOW_WIDTH: usize = 34; // " > squash cccccccc mmmmmmmmmmmmm %".len() pub const NAME: &str = "interactive-rebase-tool"; diff --git a/src/main.rs b/src/main.rs index 58fafeb..17fd2d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ mod scroll; mod show_commit; mod view; mod window; +mod window_size_error; use crate::application::Application; use crate::config::Config; diff --git a/src/view/view.rs b/src/view/view.rs index d3f4ba1..898cffb 100644 --- a/src/view/view.rs +++ b/src/view/view.rs @@ -1,10 +1,6 @@ use crate::constants::{ - HEIGHT_ERROR_MESSAGE, MINIMUM_COMPACT_WINDOW_WIDTH, MINIMUM_WINDOW_HEIGHT, - MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH, - SHORT_ERROR_MESSAGE, - SHORT_ERROR_MESSAGE_WIDTH, TITLE, TITLE_HELP_INDICATOR, TITLE_HELP_INDICATOR_LENGTH, @@ -39,7 +35,7 @@ impl<'v> View<'v> { } pub fn check_window_size(&self) -> bool { - let (window_width, window_height) = self.window.get_window_size(); + let (window_width, window_height) = self.get_view_size(); !(window_width <= MINIMUM_COMPACT_WINDOW_WIDTH || window_height <= MINIMUM_WINDOW_HEIGHT) } @@ -66,35 +62,6 @@ impl<'v> View<'v> { self.window.refresh(); } - pub fn draw_window_size_error(&self) { - let (window_width, window_height) = self.window.get_window_size(); - - self.window.color(WindowColor::Foreground); - if window_width <= MINIMUM_COMPACT_WINDOW_WIDTH { - if window_width >= SHORT_ERROR_MESSAGE_WIDTH { - self.window.draw_str(SHORT_ERROR_MESSAGE); - } - else { - // not much to do if the window gets too narrow - self.window.draw_str("Size!\n"); - } - return; - } - - if window_height <= MINIMUM_WINDOW_HEIGHT { - if window_width >= MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH { - self.window.draw_str(HEIGHT_ERROR_MESSAGE); - } - else if window_width >= SHORT_ERROR_MESSAGE_WIDTH { - self.window.draw_str(SHORT_ERROR_MESSAGE); - } - else { - // not much to do if the window gets too narrow - self.window.draw_str("Size!\n"); - } - } - } - pub fn draw_view_lines(&self, lines: Vec<ViewLine>, top: usize, height: usize) { let number_of_lines = lines.len(); diff --git a/src/window_size_error/mod.rs b/src/window_size_error/mod.rs new file mode 100644 index 0000000..7ff6eb5 --- /dev/null +++ b/src/window_size_error/mod.rs @@ -0,0 +1,4 @@ +#[allow(clippy::module_inception)] +mod window_size_error; + +pub use self::window_size_error::WindowSizeError; diff --git a/src/window_size_error/window_size_error.rs b/src/window_size_error/window_size_error.rs new file mode 100644 index 0000000..6a7c17c --- /dev/null +++ b/src/window_size_error/window_size_error.rs @@ -0,0 +1,61 @@ +use crate::constants::{ + HEIGHT_ERROR_MESSAGE, + MINIMUM_COMPACT_WINDOW_WIDTH, + MINIMUM_WINDOW_HEIGHT, + MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH, + SHORT_ERROR_MESSAGE, + SHORT_ERROR_MESSAGE_WIDTH, +}; +use crate::git_interactive::GitInteractive; +use crate::input::InputHandler; +use crate::process::{HandleInputResult, ProcessModule}; +use crate::view::View; +use crate::window::WindowColor; + +pub struct WindowSizeError {} + +impl ProcessModule for WindowSizeError { + fn handle_input( + &mut self, + input_handler: &InputHandler, + _git_interactive: &mut GitInteractive, + ) -> HandleInputResult + { + HandleInputResult::new(input_handler.get_input()) + } + + fn render(&self, view: &View, _git_interactive: &GitInteractive) { + let (window_width, window_height) = view.get_view_size(); + + view.set_color(WindowColor::Foreground); + if window_width <= MINIMUM_COMPACT_WINDOW_WIDTH { + if window_width >= SHORT_ERROR_MESSAGE_WIDTH { + view.draw_str(SHORT_ERROR_MESSAGE); + } + else { + // not much to do if the window gets too narrow + view.draw_str("Size!\n"); + } + return; + } + + if window_height <= MINIMUM_WINDOW_HEIGHT { + if window_width >= MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH { + view.draw_str(HEIGHT_ERROR_MESSAGE); + } + else if window_width >= SHORT_ERROR_MESSAGE_WIDTH { + view.draw_str(SHORT_ERROR_MESSAGE); + } + else { + // not much to do if the window gets too narrow + view.draw_str("Size!\n"); + } + } + } +} + +impl WindowSizeError { + pub fn new() -> Self { + Self {} + } +} |