Compare commits
No commits in common. "input-modes" and "main" have entirely different histories.
input-mode
...
main
2 changed files with 1 additions and 43 deletions
43
src/input.rs
43
src/input.rs
|
@ -9,26 +9,14 @@ use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
|
|||
// TODO: add punctuation.
|
||||
const SEPARATORS: [char; 3] = [' ', '\t', '\n'];
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum InputMode {
|
||||
/// Insert mode. For inserting new text.
|
||||
Insert,
|
||||
|
||||
/// Normal mode. For navigation and manipulation of text. Use Escape to come back to Normal
|
||||
/// mode from any other mode.
|
||||
Normal,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InputEvent {
|
||||
None,
|
||||
Exit,
|
||||
Text(String),
|
||||
Mode(InputMode),
|
||||
}
|
||||
|
||||
pub struct Input<W: Write> {
|
||||
mode: InputMode,
|
||||
stdout: W,
|
||||
string: Vec<char>,
|
||||
clipboard: Vec<char>,
|
||||
|
@ -39,7 +27,6 @@ pub struct Input<W: Write> {
|
|||
impl<W: Write> Input<W> {
|
||||
pub fn new(stdout: W) -> Input<W> {
|
||||
Input {
|
||||
mode: InputMode::Insert,
|
||||
stdout,
|
||||
string: Vec::new(),
|
||||
clipboard: Vec::new(),
|
||||
|
@ -48,15 +35,10 @@ impl<W: Write> Input<W> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_mode(&mut self, mode: InputMode) {
|
||||
self.mode = mode
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn from(stdout: W, input: &str) -> Input<W> {
|
||||
let string: Vec<char> = input.chars().collect();
|
||||
Input {
|
||||
mode: InputMode::Insert,
|
||||
stdout,
|
||||
cursor: string.len(),
|
||||
string,
|
||||
|
@ -91,19 +73,8 @@ impl<W: Write> Input<W> {
|
|||
}
|
||||
|
||||
pub fn handle_key(&mut self, code: KeyCode, modifiers: KeyModifiers) -> Result<InputEvent> {
|
||||
// Escape always changes input mode back to Normal.
|
||||
if let KeyCode::Esc = code {
|
||||
return Ok(InputEvent::Mode(InputMode::Normal));
|
||||
}
|
||||
|
||||
match self.mode {
|
||||
InputMode::Insert => self.handle_key_mode_input(code, modifiers),
|
||||
_ => Ok(InputEvent::None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_key_mode_input(&mut self, code: KeyCode, modifiers: KeyModifiers) -> Result<InputEvent> {
|
||||
match (code, modifiers) {
|
||||
(KeyCode::Esc, _) => return Ok(InputEvent::Exit),
|
||||
(KeyCode::Char('d'), KeyModifiers::CONTROL) => return Ok(InputEvent::Exit),
|
||||
(KeyCode::Char('l'), KeyModifiers::CONTROL) => {
|
||||
self.stdout
|
||||
|
@ -354,16 +325,4 @@ mod test {
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mode_change_normal() -> Result<()> {
|
||||
let mut stdout = Vec::new();
|
||||
let mut input = Input::new(&mut stdout);
|
||||
let event = input.handle_key(KeyCode::Esc, KeyModifiers::NONE)?;
|
||||
match event {
|
||||
InputEvent::Mode(mode) => assert_eq!(mode, InputMode::Normal),
|
||||
evt => panic!("Wrong event: {evt:?}"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -149,7 +149,6 @@ impl<'a> ChatTab<'a> {
|
|||
Event::Key(event) => {
|
||||
match self.input.handle_key(event.code, event.modifiers) {
|
||||
Ok(InputEvent::Exit) => break,
|
||||
Ok(InputEvent::Mode(mode)) => self.input.with_mode(mode),
|
||||
Ok(InputEvent::Text(message)) => {
|
||||
/*
|
||||
let item = logger::Item::Message(logger::LogMessage {
|
||||
|
|
Loading…
Reference in a new issue