Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Refactor #3

Merged
merged 9 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ libc = "0.2.51"
[dependencies]
crossterm_utils = { version = "0.3.1" }
crossterm_screen = { version = "0.3.1" }
lazy_static = "1.4"
serde = { version = "1.0", features = ["derive"], optional = true }
96 changes: 7 additions & 89 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
//! A module that contains all the actions related to reading input from the terminal.
//! Like reading a line, reading a character and reading asynchronously.

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crossterm_utils::Result;

pub use self::input::{input, TerminalInput};
#[cfg(unix)]
pub use self::unix_input::{AsyncReader, SyncReader};
pub use self::unix::{AsyncReader, SyncReader};
#[cfg(windows)]
pub use self::windows_input::{AsyncReader, SyncReader};

mod input;
pub use self::windows::{AsyncReader, SyncReader};

#[cfg(unix)]
mod unix_input;
pub(crate) mod unix;
#[cfg(windows)]
mod windows_input;
pub(crate) mod windows;

/// This trait defines the actions that can be performed with the terminal input.
/// This trait can be implemented so that a concrete implementation of the ITerminalInput can fulfill
Expand All @@ -27,7 +21,7 @@ mod windows_input;
///
/// This trait is implemented for Windows and UNIX systems.
/// Unix is using the 'TTY' and windows is using 'libc' C functions to read the input.
trait ITerminalInput {
pub(crate) trait Input {
/// Read one character from the user input
fn read_char(&self) -> Result<char>;
/// Read the input asynchronously from the user.
Expand All @@ -36,84 +30,8 @@ trait ITerminalInput {
fn read_until_async(&self, delimiter: u8) -> AsyncReader;
/// Read the input synchronously from the user.
fn read_sync(&self) -> SyncReader;
/// Start monitoring mouse events.
fn enable_mouse_mode(&self) -> Result<()>;
/// Stop monitoring mouse events.
fn disable_mouse_mode(&self) -> Result<()>;
}

/// Enum to specify which input event has occurred.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Hash, Clone)]
pub enum InputEvent {
/// A single key or a combination is pressed.
Keyboard(KeyEvent),
/// A mouse event occurred.
Mouse(MouseEvent),
/// A unsupported event has occurred.
Unsupported(Vec<u8>),
/// An unknown event has occurred.
Unknown,
}

/// Enum to specify which mouse event has occurred.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Copy)]
pub enum MouseEvent {
/// A mouse press has occurred, this contains the pressed button and the position of the press.
Press(MouseButton, u16, u16),
/// A mouse button was released.
Release(u16, u16),
/// A mouse button was hold.
Hold(u16, u16),
/// An unknown mouse event has occurred.
Unknown,
}

/// Enum to define mouse buttons.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Copy)]
pub enum MouseButton {
/// Left mouse button
Left,
/// Right mouse button
Right,
/// Middle mouse button
Middle,
/// Scroll up
WheelUp,
/// Scroll down
WheelDown,
}

/// Enum with different key or key combinations.
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum KeyEvent {
Backspace,
Enter,
Left,
Right,
Up,
Down,
Home,
End,
PageUp,
PageDown,
Tab,
BackTab,
Delete,
Insert,
F(u8),
Char(char),
Alt(char),
Ctrl(char),
Null,
Esc,
CtrlUp,
CtrlDown,
CtrlRight,
CtrlLeft,
ShiftUp,
ShiftDown,
ShiftRight,
ShiftLeft,
}
158 changes: 0 additions & 158 deletions src/input/input.rs

This file was deleted.

22 changes: 11 additions & 11 deletions src/input/unix_input.rs → src/input/unix.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
//! This is a UNIX specific implementation for input related action.

use std::char;
use std::sync::{
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver, Sender},
Arc,
};
use std::{
char,
io::{self, Read},
str, thread,
str,
sync::{
atomic::{AtomicBool, Ordering},
mpsc::{self, Receiver, Sender},
Arc,
},
thread,
};

use crossterm_utils::{csi, write_cout, ErrorKind, Result};

use crate::sys::unix::{get_tty, read_char_raw};
use crate::{input::Input, InputEvent, KeyEvent, MouseButton, MouseEvent};

use super::{ITerminalInput, InputEvent, KeyEvent, MouseButton, MouseEvent};

pub struct UnixInput;
pub(crate) struct UnixInput;

impl UnixInput {
pub fn new() -> UnixInput {
UnixInput {}
}
}

impl ITerminalInput for UnixInput {
impl Input for UnixInput {
fn read_char(&self) -> Result<char> {
read_char_raw()
}
Expand Down
Loading