summaryrefslogtreecommitdiffstats
path: root/src/input/input_handler.rs
blob: aeefe002a05a725bf678df4e82374f744a4815ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::input::Input;
use crate::window::Window;
use pancurses::Input as PancursesInput;

pub struct InputHandler<'i> {
	window: &'i Window<'i>,
}

impl<'i> InputHandler<'i> {
	pub fn new(window: &'i Window) -> Self {
		Self { window }
	}

	pub fn get_input(&self) -> Input {
		// ignore None's, since they are not really valid input
		let c = loop {
			let c = self.window.getch();
			if c.is_some() {
				break c.unwrap();
			}
		};

		match c {
			PancursesInput::Character(c) if c == '?' => Input::Help,
			PancursesInput::Character(c) if c == 'c' => Input::ShowCommit,
			PancursesInput::Character(c) if c == 'q' => Input::Abort,
			PancursesInput::Character(c) if c == 'Q' => Input::ForceAbort,
			PancursesInput::Character(c) if c == 'w' => Input::Rebase,
			PancursesInput::Character(c) if c == 'W' => Input::ForceRebase,
			PancursesInput::Character(c) if c == 'p' => Input::ActionPick,
			PancursesInput::Character(c) if c == 'b' => Input::ActionBreak,
			PancursesInput::Character(c) if c == 'r' => Input::ActionReword,
			PancursesInput::Character(c) if c == 'e' => Input::ActionEdit,
			PancursesInput::Character(c) if c == 's' => Input::ActionSquash,
			PancursesInput::Character(c) if c == 'f' => Input::ActionFixup,
			PancursesInput::Character(c) if c == 'd' => Input::ActionDrop,
			PancursesInput::Character(c) if c == 'E' => Input::Edit,
			PancursesInput::Character(c) if c == 'v' => Input::ToggleVisualMode,
			PancursesInput::Character(c) if c == 'j' => Input::SwapSelectedDown,
			PancursesInput::Character(c) if c == 'k' => Input::SwapSelectedUp,
			PancursesInput::KeyDown => Input::MoveCursorDown,
			PancursesInput::KeyUp => Input::MoveCursorUp,
			PancursesInput::KeyPPage => Input::MoveCursorPageUp,
			PancursesInput::KeyNPage => Input::MoveCursorPageDown,
			PancursesInput::KeyResize => Input::Resize,
			PancursesInput::Character(c) if c == '!' => Input::OpenInEditor,
			_ => Input::Other,
		}
	}

	pub fn get_confirm(&self) -> Input {
		match self.window.getch() {
			Some(PancursesInput::Character(c)) if c == 'y' || c == 'Y' => Input::Yes,
			Some(PancursesInput::KeyResize) => Input::Resize,
			_ => Input::No,
		}
	}

	pub fn get_character(&self) -> Input {
		loop {
			let c = loop {
				let c = self.window.getch();
				if c.is_some() {
					break c.unwrap();
				}
			};

			match c {
				PancursesInput::Character(c) if c == '\n' => break Input::Enter,
				PancursesInput::Character(c) => break Input::Character(c),
				PancursesInput::KeyEnter => break Input::Enter,
				PancursesInput::KeyBackspace => break Input::Backspace,
				PancursesInput::KeyDC => break Input::Delete,
				PancursesInput::KeyRight => break Input::MoveCursorRight,
				PancursesInput::KeyLeft => break Input::MoveCursorLeft,
				PancursesInput::KeyResize => break Input::Resize,
				_ => {},
			};
		}
	}
}