refactor(tui): use Direction enum for indicating the cursor position

This commit is contained in:
Orhun Parmaksız 2022-01-30 18:55:10 +03:00
parent f351b80906
commit 21b3aed659
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 11 additions and 9 deletions

View file

@ -345,12 +345,12 @@ impl<'a> App<'a> {
}
Command::MoveCursor(direction) => {
if let Some(input) = &self.input {
if direction == 0 {
if direction == Direction::Right {
if let Some(cursor_position) = self.input_cursor.checked_sub(1) {
self.input_cursor = cursor_position as u16;
}
} else if self.input_cursor != input.width() as u16 {
self.input_cursor += direction as u16;
self.input_cursor += 1;
}
}
}

View file

@ -11,8 +11,8 @@ pub enum Command {
Set(String, String),
/// Scroll the widget.
Scroll(ScrollArea, Direction, u8),
/// Move cursor to right/left.
MoveCursor(u8),
/// Move cursor..
MoveCursor(Direction),
/// Enable the search mode.
Search,
/// Process the input.
@ -71,8 +71,8 @@ impl Command {
Key::Char(c) => Command::UpdateInput(c),
Key::Backspace => Command::ClearInput(false),
Key::Delete => Command::ClearInput(true),
Key::Left => Command::MoveCursor(1),
Key::Right => Command::MoveCursor(0),
Key::Left => Command::MoveCursor(Direction::Left),
Key::Right => Command::MoveCursor(Direction::Right),
Key::Esc => Command::Exit,
_ => Command::Nothing,
}
@ -150,8 +150,8 @@ mod tests {
Key::Char('a') => Command::UpdateInput('a'),
Key::Backspace => Command::ClearInput(false),
Key::Delete => Command::ClearInput(true),
Key::Left => Command::MoveCursor(1),
Key::Right => Command::MoveCursor(0),
Key::Left => Command::MoveCursor(Direction::Left),
Key::Right => Command::MoveCursor(Direction::Right),
Key::Esc => Command::Exit,
}
assert_command_parser! {

View file

@ -50,7 +50,9 @@ generate_option!(
generate_option!(
Direction,
Up => "up",
Right => "right",
Down => "down",
Left => "left",
Top => "top",
Bottom => "bottom",
);

View file

@ -101,7 +101,7 @@ fn test_tui() -> Result<()> {
)?;
app.run_command(Command::ClearInput(false))?;
app.run_command(Command::MoveCursor(2))?;
app.run_command(Command::MoveCursor(Direction::Left))?;
app.run_command(Command::ClearInput(true))?;
app.run_command(Command::ClearInput(true))?;
"kill"