diff options
author | Tim Oram <dev@mitmaro.ca> | 2018-10-28 14:46:42 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2018-10-28 14:51:32 -0230 |
commit | 0549838e46e72f25dbc754e36e940cd8806a35b3 (patch) | |
tree | b9beb4a7f280c2eae13fac4dbccd9a709497685d | |
parent | d0311ef71f9101d2cdb9d51efd427010a9bb22bc (diff) |
Auto select next line after the action is modified
This is behind a configuration option
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | src/application.rs | 216 | ||||
-rw-r--r-- | src/color.rs | 1 | ||||
-rw-r--r-- | src/config.rs | 3 | ||||
-rw-r--r-- | src/git_config.rs | 6 | ||||
-rw-r--r-- | src/input.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/window.rs | 91 |
9 files changed, 217 insertions, 127 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c08440..edd27aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Configuration of colors - Support for the exec action +- Auto-select next line configuration ### Fixed - Windows creating a new window on run (hopefully) @@ -122,6 +122,7 @@ git config --global interactive-rebase-tool.foregroundColor black | Key | Default | Type | Description | | ------------------ | ------- | ----- | ----------- | +| `autoSelectNext` | false | bool | If true, auto select the next line after action modification | | `foregroundColor` | white | Color | Color used for most text and the UI | | `indicatorColor` | yellow | Color | Color used for text the indicates or needs to standout | | `errorColor` | red | Color | Color used for showing error messages | diff --git a/src/application.rs b/src/application.rs index 3657cf2..653f8cf 100644 --- a/src/application.rs +++ b/src/application.rs @@ -2,47 +2,63 @@ use action::Action; use git_interactive::GitInteractive; use window::{ - Input, Window }; +use input::Input; +use config::Config; const EXIT_CODE_GOOD: i32 = 0; const EXIT_CODE_WRITE_ERROR: i32 = 8; #[derive(PartialEq, Debug)] pub enum State { + ConfirmAbort, + ConfirmRebase, + Help, List, ShowCommit, - Help } pub struct Application { + config: Config, pub exit_code: Option<i32>, - window: Window, git_interactive: GitInteractive, - state: State + state: State, + window: Window, } impl Application { - pub fn new(git_interactive: GitInteractive, window: Window) -> Self { + pub fn new(git_interactive: GitInteractive, window: Window, config: Config) -> Self { Application { + config, + exit_code: None, git_interactive, + state: State::List, window, - exit_code: None, - state: State::List } } pub fn process_input(&mut self) { match self.state { + State::ConfirmAbort => self.process_confirm_abort(), + State::ConfirmRebase => self.process_confirm_rebase(), + State::Help => self.process_help_input(), State::List => self.process_list_input(), State::ShowCommit => self.process_show_commit_input(), - State::Help => self.process_help_input() } } pub fn draw(&self) { match self.state { + State::ConfirmAbort => { + self.window.draw_confirm("Are you sure you want to abort"); + }, + State::ConfirmRebase => { + self.window.draw_confirm("Are you sure you want to rebase"); + } + State::Help => { + self.window.draw_help(); + }, State::List => { self.window.draw( self.git_interactive.get_lines(), @@ -55,9 +71,6 @@ impl Application { self.git_interactive.get_git_root() ); }, - State::Help => { - self.window.draw_help(); - } } } @@ -83,79 +96,92 @@ impl Application { } fn process_help_input(&mut self) { - self.window.window.getch(); + self.window.get_input(); self.state = State::List; } fn process_show_commit_input(&mut self) { - self.window.window.getch(); + self.window.get_input(); self.state = State::List; } - + + fn process_confirm_abort(&mut self) { + if self.window.get_confirm() { + self.abort(); + } + else { + self.state = State::List; + } + } + + fn process_confirm_rebase(&mut self) { + if self.window.get_confirm() { + self.finish(); + } + else { + self.state = State::List; + } + } + fn process_list_input(&mut self) { - match self.window.window.getch() { - Some(Input::Character(c)) if c == '?' => { - self.state = State::Help; - }, - Some(Input::Character(c)) if c == 'c' => { - self.state = State::ShowCommit; - }, - Some(Input::Character(c)) - if (c == 'Q') || (c == 'q' && self.window.confirm("Are you sure you want to abort")) - => self.abort(), - Some(Input::Character(c)) - if (c == 'W') || (c == 'w' && self.window.confirm("Are you sure you want to rebase")) - => self.finish(), - Some(Input::Character(c)) - if c == 'p' => self.git_interactive.set_selected_line_action(Action::Pick), - Some(Input::Character(c)) - if c == 'r' => self.git_interactive.set_selected_line_action(Action::Reword), - Some(Input::Character(c)) - if c == 'e' => self.git_interactive.set_selected_line_action(Action::Edit), - Some(Input::Character(c)) - if c == 's' => self.git_interactive.set_selected_line_action(Action::Squash), - Some(Input::Character(c)) - if c == 'f' => self.git_interactive.set_selected_line_action(Action::Fixup), - Some(Input::Character(c)) - if c == 'd' => self.git_interactive.set_selected_line_action(Action::Drop), - Some(Input::Character(c)) if c == 'j' => { + match self.window.get_input() { + Input::Help => self.state = State::Help, + Input::ShowCommit => self.state = State::ShowCommit, + Input::Abort => self.state = State::ConfirmAbort, + Input::ForceAbort => self.abort(), + Input::Rebase => self.state = State::ConfirmRebase, + Input::ForceRebase => self.finish(), + Input::Drop => self.set_selected_line_action(Action::Drop), + Input::Edit => self.set_selected_line_action(Action::Edit), + Input::Fixup => self.set_selected_line_action(Action::Fixup), + Input::Pick => self.set_selected_line_action(Action::Pick), + Input::Reword => self.set_selected_line_action(Action::Reword), + Input::Squash => self.set_selected_line_action(Action::Squash), + Input::SwapSelectedDown => { self.git_interactive.swap_selected_down(); self.reset_top(); }, - Some(Input::Character(c)) if c == 'k' => { + Input::SwapSelectedUp => { self.git_interactive.swap_selected_up(); self.reset_top(); }, - Some(Input::KeyDown) => { + Input::MoveCursorDown => { self.git_interactive.move_cursor_down(1); self.reset_top(); }, - Some(Input::KeyUp) => { + Input::MoveCursorUp => { self.git_interactive.move_cursor_up(1); self.reset_top(); }, - Some(Input::KeyPPage) => { - self.git_interactive.move_cursor_up(5); + Input::MoveCursorPageDown => { + self.git_interactive.move_cursor_down(5); self.reset_top(); }, - Some(Input::KeyNPage) => { - self.git_interactive.move_cursor_down(5); + Input::MoveCursorPageUp => { + self.git_interactive.move_cursor_up(5); self.reset_top(); }, - Some(Input::KeyResize) => { + Input::Resize => { self.window.resize_term(); self.reset_top() }, - _ => {} + Input::Other => {} } } - + fn reset_top(&mut self) { self.window.set_top( self.git_interactive.get_lines().len(), *self.git_interactive.get_selected_line_index() ) } + + fn set_selected_line_action(&mut self, action: Action) { + self.git_interactive.set_selected_line_action(action); + if self.config.auto_select_next { + self.git_interactive.move_cursor_down(1); + } + } } #[cfg(test)] @@ -167,7 +193,7 @@ mod tests { use git_interactive::GitInteractive; use window::{ Window, - Input + PancursesInput }; use action::Action; use config::Config; @@ -178,7 +204,7 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let app = Application::new(gi, window); + let app = Application::new(gi, window, config); assert_eq!(app.git_interactive.get_lines().len(), 14); } @@ -187,8 +213,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('?'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('?'); app.process_input(); assert_eq!(app.state, State::Help); } @@ -199,8 +225,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-show-commit.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('c'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('c'); app.process_input(); assert_eq!(app.state, State::ShowCommit); } @@ -210,17 +236,17 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-long.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::KeyDown; + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::KeyDown; app.process_input(); assert_eq!(*app.git_interactive.get_selected_line_index(), 2); - app.window.window.next_char = Input::KeyUp; + app.window.window.next_char = PancursesInput::KeyUp; app.process_input(); assert_eq!(*app.git_interactive.get_selected_line_index(), 1); - app.window.window.next_char = Input::KeyNPage; + app.window.window.next_char = PancursesInput::KeyNPage; app.process_input(); assert_eq!(*app.git_interactive.get_selected_line_index(), 6); - app.window.window.next_char = Input::KeyPPage; + app.window.window.next_char = PancursesInput::KeyPPage; app.process_input(); assert_eq!(*app.git_interactive.get_selected_line_index(), 1); } @@ -230,20 +256,20 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-short.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::KeyUp; + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::KeyUp; app.process_input(); app.process_input(); - app.window.window.next_char = Input::KeyPPage; + app.window.window.next_char = PancursesInput::KeyPPage; app.process_input(); assert_eq!(*app.git_interactive.get_selected_line_index(), 1); - app.window.window.next_char = Input::KeyDown; + app.window.window.next_char = PancursesInput::KeyDown; app.process_input(); app.process_input(); app.process_input(); app.process_input(); app.process_input(); - app.window.window.next_char = Input::KeyNPage; + app.window.window.next_char = PancursesInput::KeyNPage; app.process_input(); assert_eq!(*app.git_interactive.get_selected_line_index(), 3); } @@ -253,11 +279,11 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); + let mut app = Application::new(gi, window, config); // first item is already pick - app.window.window.next_char = Input::KeyDown; + app.window.window.next_char = PancursesInput::KeyDown; app.process_input(); - app.window.window.next_char = Input::Character('p'); + app.window.window.next_char = PancursesInput::Character('p'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[1].get_action(), Action::Pick); } @@ -267,8 +293,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('r'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('r'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Reword); } @@ -278,8 +304,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('e'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('e'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Edit); } @@ -289,23 +315,23 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-exec.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('p'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('p'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Exec); - app.window.window.next_char = Input::Character('r'); + app.window.window.next_char = PancursesInput::Character('r'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Exec); - app.window.window.next_char = Input::Character('e'); + app.window.window.next_char = PancursesInput::Character('e'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Exec); - app.window.window.next_char = Input::Character('s'); + app.window.window.next_char = PancursesInput::Character('s'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Exec); - app.window.window.next_char = Input::Character('f'); + app.window.window.next_char = PancursesInput::Character('f'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Exec); - app.window.window.next_char = Input::Character('d'); + app.window.window.next_char = PancursesInput::Character('d'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Exec); } @@ -315,8 +341,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('s'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('s'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Squash); } @@ -326,8 +352,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('d'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('d'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_action(), Action::Drop); } @@ -337,8 +363,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('j'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('j'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_hash_or_command(), "bbb"); assert_eq!(*app.git_interactive.get_lines()[1].get_hash_or_command(), "aaa"); @@ -350,10 +376,10 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::KeyDown; + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::KeyDown; app.process_input(); - app.window.window.next_char = Input::Character('k'); + app.window.window.next_char = PancursesInput::Character('k'); app.process_input(); assert_eq!(*app.git_interactive.get_lines()[0].get_hash_or_command(), "bbb"); assert_eq!(*app.git_interactive.get_lines()[1].get_hash_or_command(), "aaa"); @@ -365,8 +391,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('Q'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('Q'); app.process_input(); assert_eq!(app.exit_code.unwrap(), 0); assert!(app.git_interactive.get_lines().is_empty()); @@ -377,8 +403,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-todo-all-actions.in", "#").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('W'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('W'); app.process_input(); assert_eq!(app.exit_code.unwrap(), 0); assert!(!app.git_interactive.get_lines().is_empty()); @@ -389,8 +415,8 @@ mod tests { let gi = GitInteractive::new_from_filepath("test/git-rebase-alternative-comment-character.in", "%").unwrap(); let config = Config::new(&GitConfig::new().unwrap()); let window = Window::new(config); - let mut app = Application::new(gi, window); - app.window.window.next_char = Input::Character('W'); + let mut app = Application::new(gi, window, config); + app.window.window.next_char = PancursesInput::Character('W'); app.process_input(); assert_eq!(app.exit_code.unwrap(), 0); assert!(!app.git_interactive.get_lines().is_empty()); diff --git a/src/color.rs b/src/color.rs index 80e6f5e..525cba2 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,3 +1,4 @@ +#[derive(Debug, Copy, Clone)] pub enum Color { Black, Blue, diff --git a/src/config.rs b/src/config.rs index 9542fe8..42c17de 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use color::Color; use git_config::GitConfig; +#[derive(Debug, Copy, Clone)] pub struct Config { pub foreground_color: Color, pub indicator_color: Color, @@ -14,6 +15,7 @@ pub struct Config { pub squash_color: Color, pub fixup_color: Color, pub drop_color: Color, + pub auto_select_next: bool, } fn string_to_color(color_string: &str, default_color: Color) -> Color { @@ -45,6 +47,7 @@ impl Config { squash_color: string_to_color(git_config.squash_color.as_ref(), Color::Cyan), fixup_color: string_to_color(git_config.fixup_color.as_ref(), Color::Magenta), drop_color: string_to_color(git_config.drop_color.as_ref(), Color::Red), + auto_select_next: git_config.auto_select_next, } } } diff --git a/src/git_config.rs b/src/git_config.rs index f18c99b..1779a2d 100644 --- a/src/git_config.rs +++ b/src/git_config.rs @@ -3,6 +3,7 @@ use git2::Config; use std::env; use std::path::Path; +#[derive(Debug)] pub struct GitConfig { pub comment_char: String, pub foreground_color: String, @@ -17,6 +18,7 @@ pub struct GitConfig { pub squash_color: String, pub fixup_color: String, pub drop_color: String, + pub auto_select_next: bool, } impl GitConfig { @@ -92,6 +94,10 @@ impl GitConfig { Ok(drop_color_value) => drop_color_value.to_lowercase(), Err(_msg) => String::from("") }, + auto_select_next: match config.get_bool("interactive-rebase-tool.autoSelectNext") { + Ok(auto_select_next_value) => auto_select_next_value, + Err(_msg) => false + }, }) }, Err(msg) => { diff --git a/src/input.rs b/src/input.rs new file mode 100644 index 0000000..0968290 --- /dev/null +++ b/src/input.rs @@ -0,0 +1,22 @@ +pub enum Input { + Help, + ShowCommit, + Abort, + ForceAbort, + Rebase, + ForceRebase, + Drop, + Edit, + Fixup, + Pick, + Reword, + Squash, + SwapSelectedDown, + SwapSelectedUp, + MoveCursorDown, + MoveCursorUp, + MoveCursorPageDown, + MoveCursorPageUp, + Resize, + Other, +} diff --git a/src/main.rs b/src/main.rs index 15cd709..700f6f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ mod commit; mod config; mod git_config; mod git_interactive; +mod input; mod line; mod window; #[cfg(test)] @@ -56,7 +57,7 @@ fn main() { let window = Window::new(config); - let mut application = Application::new(git_interactive, window); + let mut application = Application::new(git_interactive, window, config); while application.exit_code == None { application.draw(); diff --git a/src/window.rs b/src/window.rs index d4994fd..a50921f 100644 --- a/src/window.rs +++ b/src/window.rs @@ -12,7 +12,7 @@ use pancurses as pancurses; #[cfg(test)] use mocks::mockcurses as pancurses; -pub use pancurses::Input as Input; +pub use pancurses::Input as PancursesInput; use action::{ Action, @@ -23,6 +23,7 @@ use line::Line; use commit::Commit; use color::Color; use config::Config; +use input::Input; const COLOR_TABLE: [i16; 8] = [ pancurses::COLOR_BLACK, @@ -98,7 +99,7 @@ impl Window { } fn draw_more_indicator(&self, remaining: usize) { - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.attron(pancurses::A_DIM); self.window.attron(pancurses::A_REVERSE); self.window.addstr(&format!(" -- {} -- ", remaining)); @@ -107,7 +108,7 @@ impl Window { } fn draw_title(&self) { - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.set_dim(true); self.set_underline(true); self.window.addstr("Git Interactive Rebase ? for help\n"); @@ -116,7 +117,7 @@ impl Window { } fn draw_line(&self, line: &Line, selected: bool) { - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); if selected { self.window.addstr(" > "); } @@ -124,21 +125,21 @@ impl Window { self.window.addstr(" "); } match *line.get_action() { - Action::Pick => self.set_color(&self.config.pick_color), - Action::Reword => self.set_color(&self.config.reword_color), - Action::Edit => self.set_color(&self.config.edit_color), - Action::Exec => self.set_color(&self.config.exec_color), - Action::Squash => self.set_color(&self.config.squash_color), - Action::Fixup => self.set_color(&self.config.fixup_color), - Action::Drop => self.set_color(&self.config.drop_color) + Action::Pick => self.set_color(self.config.pick_color), + Action::Reword => self.set_color(self.config.reword_color), + Action::Edit => self.set_color(self.config.edit_color), + Action::Exec => self.set_color(self.config.exec_color), + Action::Squash => self.set_color(self.config.squash_color), + Action::Fixup => self.set_color(self.config.fixup_color), + Action::Drop => self.set_color(self.config.drop_color) } self.window.addstr(&format!("{:6}", action_to_str(line.get_action()))); - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.addstr(&format!(" {} {}\n", line.get_hash_or_command(), line.get_comment())); } fn draw_footer(&self) { - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.set_dim(true); self.window.mvaddstr( self.window.get_max_y() - 1, @@ -164,12 +165,12 @@ impl Window { self.draw_title(); match result { Ok(output) => { - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); match Commit::new(&String::from_utf8_lossy(&output.stdout)) { Ok(commit_data) => { - self.set_color(&self.config.indicator_color); + self.set_color(self.config.indicator_color); self.window.addstr(&format!("\nCommit: {}\n", commit)); - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.addstr(&format!( "Author: {} <{}>\n", commit_data.get_author_name(), commit_data.get_author_email() )); @@ -194,32 +195,32 @@ impl Window { .fold(0, |a, x| cmp::max(a, x.get_added().len())); for file_stat in commit_data.get_file_stats() { - self.set_color(&self.config.diff_add_color); + self.set_color(self.config.diff_add_color); self.window.addstr( &file_stat.get_added().pad_to_width_with_alignment(max_add_change_length, Alignment::Right) ); - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.addstr(" | "); - self.set_color(&self.config.diff_remove_color); + self.set_color(self.config.diff_remove_color); self.window.addstr( &file_stat.get_removed().pad_to_width_with_alignment(max_remove_change_length, Alignment::Left) ); - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.addstr(&format!(" {}\n", &file_stat.get_name())); } }, Err(msg) => { - self.set_color(&self.config.error_color); + self.set_color(self.config.error_color); self.window.addstr(&msg); } } }, Err(msg) => { - self.set_color(&self.config.error_color); + self.set_color(self.config.error_color); self.window.addstr(msg.description()); } } - self.set_color(&self.config.indicator_color); + self.set_color(self.config.indicator_color); self.window.addstr("\n\nHit any key to close"); self.window.refresh(); } @@ -227,7 +228,7 @@ impl Window { pub fn draw_help(&self) { self.window.clear(); self.draw_title(); - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.addstr("\n Key Action\n"); self.window.addstr(" --------------------------------------------------\n"); self.draw_help_command("Up", "Move selection up"); @@ -248,20 +249,20 @@ impl Window { self.draw_help_command("s", "Set selected commit to be squashed"); self.draw_help_command("f", "Set selected commit to be fixed-up"); self.draw_help_command("d", "Set selected commit to be dropped"); - self.set_color(&self.config.indicator_color); + self.set_color(self.config.indicator_color); self.window.addstr("\n\nHit any key to close help"); self.window.refresh(); } fn draw_help_command(&self, command: &str, help: &str) { - self.set_color(&self.config.indicator_color); + self.set_color(self.config.indicator_color); self.window.addstr(&format!(" {:9} ", command)); - self.set_color(&self.config.foreground_color); + self.set_color(self.config.foreground_color); self.window.addstr(&format!("{}\n", help)); } - fn set_color(&self, color: &Color) { - match *color { + fn set_color(&self, color: Color) { + match color { Color::Black => self.window.attrset(pancurses::COLOR_PAIR(0)), Color::Blue => self.window.attrset(pancurses::COLOR_PAIR(1)), Color::Cyan => self.window.attrset(pancurses::COLOR_PAIR(2)), @@ -291,12 +292,40 @@ impl Window { } } - pub fn confirm(&self, message: &str) -> bool { + pub fn get_input(&self) -> Input { + match self.window.getch() { + Some(PancursesInput::Character(c)) if c == '?' => Input::Help, + Some(PancursesInput::Character(c)) if c == 'c' => Input::ShowCommit, + Some(PancursesInput::Character(c)) if c == 'q' => Input::Abort, + Some(PancursesInput::Character(c)) if c == 'Q' => Input::ForceAbort, + Some(PancursesInput::Character(c)) if c == 'w' => Input::Rebase, + Some(PancursesInput::Character(c)) if c == 'W' => Input::ForceRebase, + Some(PancursesInput::Character(c)) if c == 'p' => Input::Pick, + Some(PancursesInput::Character(c)) if c == 'r' => Input::Reword, + Some(PancursesInput::Character(c)) if c == 'e' => Input::Edit, + Some(PancursesInput::Character(c)) if c == 's' => Input::Squash, + Some(PancursesInput::Character(c)) if c == 'f' => Input::Fixup, + Some(PancursesInput::Character(c)) if c == 'd' => Input::Drop, + Some(PancursesInput::Character(c)) if c == 'j' => Input::SwapSelectedDown, + Some(PancursesInput::Character(c)) if c == 'k' => Input::SwapSelectedUp, + Some(PancursesInput::KeyDown) => Input::MoveCursorDown, + Some(PancursesInput::KeyUp) => Input::MoveCursorUp, + Some(PancursesInput::KeyPPage) => Input::MoveCursorPageUp, + Some(PancursesInput::KeyNPage) => Input::MoveCursorPageDown, + Some(PancursesInput::KeyResize) => Input::Resize, + _ => Input::Other, + } + } + + pub fn draw_confirm(&self, message: &str) { self.window.clear(); self.draw_title(); self.window.addstr(&format!("\n{} (y/n)? ", message)); + } + + pub fn get_confirm(&self) -> bool { match self.window.getch() { - Some(Input::Character(c)) if c == 'y' || c == 'Y' => true, + Some(PancursesInput::Character(c)) if c == 'y' || c == 'Y' => true, _ => false } } |