Added basic trait implementations to all Commands (#363)

- Added basic trait implementations to all Commands
- Added `Debug` to all public-facing types
- Added `Clone` to several public-facing types, where relevant
- Added `Eq` to all `PartialEq` types
- Added `Debug` and `Clone` to a few internal types
- `ResetColor` uses `&'static str` instead of `String`
- Updated crossterm-winapi dependency
This commit is contained in:
Nathan West 2020-01-28 14:20:26 -05:00 committed by Timon
parent e863b7b75f
commit f2fca91b30
16 changed files with 60 additions and 31 deletions

View File

@ -6,6 +6,8 @@
- Fixed minor syntax bug in `execute!` and `queue!`. - Fixed minor syntax bug in `execute!` and `queue!`.
- Cleaned up implementation of `execute!` and `queue!`. - Cleaned up implementation of `execute!` and `queue!`.
- **breaking change** Changed `ContentStyle::apply` to take self by value instead of reference, to prevent an unnecessary extra clone. - **breaking change** Changed `ContentStyle::apply` to take self by value instead of reference, to prevent an unnecessary extra clone.
- Added basic trait implementations (`Debug`, `Clone`, `Copy`, etc) to all of the command structs
- `ResetColor` uses `&'static str` instead of `String`
# Version 0.14.2 # Version 0.14.2
- Fix TIOCGWINSZ for FreeBSD - Fix TIOCGWINSZ for FreeBSD

View File

@ -47,7 +47,7 @@ version = "0.3.8"
features = ["winuser"] features = ["winuser"]
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
crossterm_winapi = "0.5.1" crossterm_winapi = "0.6.0"
# #
# UNIX dependencies # UNIX dependencies

View File

@ -57,6 +57,7 @@ pub(crate) mod sys;
/// ///
/// * Top left cell is represented as `0,0`. /// * Top left cell is represented as `0,0`.
/// * Commands must be executed/queued for execution otherwise they do nothing. /// * Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveTo(pub u16, pub u16); pub struct MoveTo(pub u16, pub u16);
impl Command for MoveTo { impl Command for MoveTo {
@ -78,6 +79,7 @@ impl Command for MoveTo {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveToNextLine(pub u16); pub struct MoveToNextLine(pub u16);
impl Command for MoveToNextLine { impl Command for MoveToNextLine {
@ -99,6 +101,7 @@ impl Command for MoveToNextLine {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveToPreviousLine(pub u16); pub struct MoveToPreviousLine(pub u16);
impl Command for MoveToPreviousLine { impl Command for MoveToPreviousLine {
@ -119,6 +122,7 @@ impl Command for MoveToPreviousLine {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveToColumn(pub u16); pub struct MoveToColumn(pub u16);
impl Command for MoveToColumn { impl Command for MoveToColumn {
@ -139,6 +143,7 @@ impl Command for MoveToColumn {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveUp(pub u16); pub struct MoveUp(pub u16);
impl Command for MoveUp { impl Command for MoveUp {
@ -159,6 +164,7 @@ impl Command for MoveUp {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveRight(pub u16); pub struct MoveRight(pub u16);
impl Command for MoveRight { impl Command for MoveRight {
@ -179,6 +185,7 @@ impl Command for MoveRight {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveDown(pub u16); pub struct MoveDown(pub u16);
impl Command for MoveDown { impl Command for MoveDown {
@ -199,6 +206,7 @@ impl Command for MoveDown {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MoveLeft(pub u16); pub struct MoveLeft(pub u16);
impl Command for MoveLeft { impl Command for MoveLeft {
@ -222,6 +230,7 @@ impl Command for MoveLeft {
/// ///
/// - The cursor position is stored globally. /// - The cursor position is stored globally.
/// - Commands must be executed/queued for execution otherwise they do nothing. /// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SavePosition; pub struct SavePosition;
impl Command for SavePosition { impl Command for SavePosition {
@ -245,6 +254,7 @@ impl Command for SavePosition {
/// ///
/// - The cursor position is stored globally. /// - The cursor position is stored globally.
/// - Commands must be executed/queued for execution otherwise they do nothing. /// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RestorePosition; pub struct RestorePosition;
impl Command for RestorePosition { impl Command for RestorePosition {
@ -265,6 +275,7 @@ impl Command for RestorePosition {
/// # Notes /// # Notes
/// ///
/// - Commands must be executed/queued for execution otherwise they do nothing. /// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Hide; pub struct Hide;
impl Command for Hide { impl Command for Hide {
@ -285,6 +296,7 @@ impl Command for Hide {
/// # Notes /// # Notes
/// ///
/// - Commands must be executed/queued for execution otherwise they do nothing. /// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Show; pub struct Show;
impl Command for Show { impl Command for Show {
@ -306,6 +318,7 @@ impl Command for Show {
/// ///
/// - Windows versions lower than Windows 10 do not support this functionality. /// - Windows versions lower than Windows 10 do not support this functionality.
/// - Commands must be executed/queued for execution otherwise they do nothing. /// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableBlinking; pub struct EnableBlinking;
impl Command for EnableBlinking { impl Command for EnableBlinking {
@ -327,6 +340,7 @@ impl Command for EnableBlinking {
/// ///
/// - Windows versions lower than Windows 10 do not support this functionality. /// - Windows versions lower than Windows 10 do not support this functionality.
/// - Commands must be executed/queued for execution otherwise they do nothing. /// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableBlinking; pub struct DisableBlinking;
impl Command for DisableBlinking { impl Command for DisableBlinking {

View File

@ -221,6 +221,7 @@ where
/// A command that enables mouse event capturing. /// A command that enables mouse event capturing.
/// ///
/// Mouse events can be captured with [read](./fn.read.html)/[poll](./fn.poll.html). /// Mouse events can be captured with [read](./fn.read.html)/[poll](./fn.poll.html).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableMouseCapture; pub struct EnableMouseCapture;
impl Command for EnableMouseCapture { impl Command for EnableMouseCapture {
@ -244,6 +245,7 @@ impl Command for EnableMouseCapture {
/// A command that disables mouse event capturing. /// A command that disables mouse event capturing.
/// ///
/// Mouse events can be captured with [read](./fn.read.html)/[poll](./fn.poll.html). /// Mouse events can be captured with [read](./fn.read.html)/[poll](./fn.poll.html).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableMouseCapture; pub struct DisableMouseCapture;
impl Command for DisableMouseCapture { impl Command for DisableMouseCapture {
@ -413,7 +415,7 @@ pub enum KeyCode {
/// ///
/// Encapsulates publicly available `Event` with additional internal /// Encapsulates publicly available `Event` with additional internal
/// events that shouldn't be publicly available to the crate users. /// events that shouldn't be publicly available to the crate users.
#[derive(Debug, PartialOrd, PartialEq, Hash, Clone)] #[derive(Debug, PartialOrd, PartialEq, Hash, Clone, Eq)]
pub(crate) enum InternalEvent { pub(crate) enum InternalEvent {
/// An event. /// An event.
Event(Event), Event(Event),

View File

@ -7,6 +7,7 @@ pub(crate) trait Filter: Send + Sync + 'static {
} }
#[cfg(unix)] #[cfg(unix)]
#[derive(Debug, Clone)]
pub(crate) struct CursorPositionFilter; pub(crate) struct CursorPositionFilter;
#[cfg(unix)] #[cfg(unix)]
@ -20,6 +21,7 @@ impl Filter for CursorPositionFilter {
} }
} }
#[derive(Debug, Clone)]
pub(crate) struct EventFilter; pub(crate) struct EventFilter;
impl Filter for EventFilter { impl Filter for EventFilter {
@ -38,6 +40,7 @@ impl Filter for EventFilter {
} }
} }
#[derive(Debug, Clone)]
pub(crate) struct InternalEventFilter; pub(crate) struct InternalEventFilter;
impl Filter for InternalEventFilter { impl Filter for InternalEventFilter {

View File

@ -179,6 +179,7 @@ impl EventSource for UnixInternalEventSource {
// * mimick anes Parser interface // * mimick anes Parser interface
// * move the advancing, parsing, ... stuff out of the `try_read` method // * move the advancing, parsing, ... stuff out of the `try_read` method
// //
#[derive(Debug)]
struct Parser { struct Parser {
buffer: Vec<u8>, buffer: Vec<u8>,
internal_events: VecDeque<InternalEvent>, internal_events: VecDeque<InternalEvent>,

View File

@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use crossterm_winapi::{Console, Handle, InputEventType, KeyEventRecord, MouseEvent}; use crossterm_winapi::{Console, Handle, InputEvent, KeyEventRecord, MouseEvent};
use crate::event::{sys::windows::poll::WinApiPoll, Event}; use crate::event::{sys::windows::poll::WinApiPoll, Event};
@ -33,31 +33,21 @@ impl EventSource for WindowsEventSource {
let poll_timeout = PollTimeout::new(timeout); let poll_timeout = PollTimeout::new(timeout);
loop { loop {
if let Some(event_ready) = self.poll.poll(timeout)? { if let Some(event_ready) = self.poll.poll(poll_timeout.leftover())? {
if event_ready && self.console.number_of_console_input_events()? != 0 { if event_ready && self.console.number_of_console_input_events()? != 0 {
let input = self.console.read_single_input_event()?; let event = match self.console.read_single_input_event()? {
InputEvent::KeyEvent(record) => handle_key_event(record)?,
let event = match input.event_type { InputEvent::MouseEvent(record) => handle_mouse_event(record)?,
InputEventType::KeyEvent => handle_key_event(unsafe { InputEvent::WindowBufferSizeEvent(record) => {
KeyEventRecord::from(*input.event.KeyEvent()) Some(Event::Resize(record.size.x, record.size.y))
})?,
InputEventType::MouseEvent => handle_mouse_event(unsafe {
MouseEvent::from(*input.event.MouseEvent())
})?,
InputEventType::WindowBufferSizeEvent => {
let new_size = crate::terminal::size()?;
Some(Event::Resize(new_size.0, new_size.1))
} }
InputEventType::FocusEvent | InputEventType::MenuEvent => None, _ => None,
}; };
return Ok(match event { if let Some(event) = event {
None => None, return Ok(Some(InternalEvent::Event(event)));
Some(event) => Some(InternalEvent::Event(event)), }
});
} }
} else {
return Ok(None);
} }
if poll_timeout.elapsed() { if poll_timeout.elapsed() {

View File

@ -31,6 +31,7 @@ use super::{
/// ///
/// Check the [examples](https://github.com/crossterm-rs/crossterm/tree/master/examples) folder to see how to use /// Check the [examples](https://github.com/crossterm-rs/crossterm/tree/master/examples) folder to see how to use
/// it (`event-stream-*`). /// it (`event-stream-*`).
#[derive(Debug)]
pub struct EventStream { pub struct EventStream {
poll_internal_waker: Waker, poll_internal_waker: Waker,
stream_wake_thread_spawned: Arc<AtomicBool>, stream_wake_thread_spawned: Arc<AtomicBool>,

View File

@ -11,6 +11,7 @@ use crate::{ErrorKind, Result};
/// ///
/// It allows to retrieve raw file descriptor, write to the file descriptor and /// It allows to retrieve raw file descriptor, write to the file descriptor and
/// mainly it closes the file descriptor once dropped. /// mainly it closes the file descriptor once dropped.
#[derive(Debug)]
pub struct FileDesc { pub struct FileDesc {
fd: RawFd, fd: RawFd,
close_on_drop: bool, close_on_drop: bool,

View File

@ -6,6 +6,7 @@ use mio::{Evented, Poll, PollOpt, Ready, Registration, SetReadiness, Token};
use crate::Result; use crate::Result;
#[derive(Debug)]
struct WakerInner { struct WakerInner {
registration: Registration, registration: Registration,
set_readiness: SetReadiness, set_readiness: SetReadiness,
@ -33,7 +34,7 @@ impl WakerInner {
} }
/// Allows to wake up the `mio::Poll::poll()` method. /// Allows to wake up the `mio::Poll::poll()` method.
#[derive(Clone)] #[derive(Clone, Debug)]
pub(crate) struct Waker { pub(crate) struct Waker {
inner: Arc<Mutex<WakerInner>>, inner: Arc<Mutex<WakerInner>>,
} }

View File

@ -15,6 +15,7 @@ use crate::Result;
#[cfg(feature = "event-stream")] #[cfg(feature = "event-stream")]
pub(crate) use super::waker::Waker; pub(crate) use super::waker::Waker;
#[derive(Debug)]
pub(crate) struct WinApiPoll { pub(crate) struct WinApiPoll {
#[cfg(feature = "event-stream")] #[cfg(feature = "event-stream")]
waker: Waker, waker: Waker,

View File

@ -5,7 +5,7 @@ use crossterm_winapi::Semaphore;
use crate::Result; use crate::Result;
/// Allows to wake up the `WinApiPoll::poll()` method. /// Allows to wake up the `WinApiPoll::poll()` method.
#[derive(Clone)] #[derive(Clone, Debug)]
pub(crate) struct Waker { pub(crate) struct Waker {
inner: Arc<Mutex<Semaphore>>, inner: Arc<Mutex<Semaphore>>,
} }

View File

@ -1,6 +1,7 @@
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
/// Keeps track of the elapsed time since the moment the polling started. /// Keeps track of the elapsed time since the moment the polling started.
#[derive(Debug, Clone)]
pub struct PollTimeout { pub struct PollTimeout {
timeout: Option<Duration>, timeout: Option<Duration>,
start: Instant, start: Instant,

View File

@ -227,6 +227,7 @@ pub fn available_color_count() -> u16 {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetForegroundColor(pub Color); pub struct SetForegroundColor(pub Color);
impl Command for SetForegroundColor { impl Command for SetForegroundColor {
@ -249,6 +250,7 @@ impl Command for SetForegroundColor {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetBackgroundColor(pub Color); pub struct SetBackgroundColor(pub Color);
impl Command for SetBackgroundColor { impl Command for SetBackgroundColor {
@ -271,6 +273,7 @@ impl Command for SetBackgroundColor {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetAttribute(pub Attribute); pub struct SetAttribute(pub Attribute);
impl Command for SetAttribute { impl Command for SetAttribute {
@ -294,6 +297,7 @@ impl Command for SetAttribute {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone)]
pub struct PrintStyledContent<D: Display + Clone>(pub StyledContent<D>); pub struct PrintStyledContent<D: Display + Clone>(pub StyledContent<D>);
impl<D> Command for PrintStyledContent<D> impl<D> Command for PrintStyledContent<D>
@ -317,13 +321,14 @@ where
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ResetColor; pub struct ResetColor;
impl Command for ResetColor { impl Command for ResetColor {
type AnsiType = String; type AnsiType = &'static str;
fn ansi_code(&self) -> Self::AnsiType { fn ansi_code(&self) -> Self::AnsiType {
ansi::RESET_CSI_SEQUENCE.to_string() ansi::RESET_CSI_SEQUENCE
} }
#[cfg(windows)] #[cfg(windows)]
@ -335,6 +340,7 @@ impl Command for ResetColor {
/// A command that prints the given displayable type. /// A command that prints the given displayable type.
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Print<T: Display + Clone>(pub T); pub struct Print<T: Display + Clone>(pub T);
impl<T: Display + Clone> Command for Print<T> { impl<T: Display + Clone> Command for Print<T> {

View File

@ -27,7 +27,7 @@ use crate::{
/// ///
/// println!("{}", styled); /// println!("{}", styled);
/// ``` /// ```
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct StyledContent<D: Display> { pub struct StyledContent<D: Display> {
/// The style (colors, content attributes). /// The style (colors, content attributes).
style: ContentStyle, style: ContentStyle,

View File

@ -141,6 +141,7 @@ pub fn size() -> Result<(u16, u16)> {
/// } /// }
/// ``` /// ```
/// ///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnterAlternateScreen; pub struct EnterAlternateScreen;
impl Command for EnterAlternateScreen { impl Command for EnterAlternateScreen {
@ -180,6 +181,7 @@ impl Command for EnterAlternateScreen {
/// } /// }
/// ``` /// ```
/// ///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LeaveAlternateScreen; pub struct LeaveAlternateScreen;
impl Command for LeaveAlternateScreen { impl Command for LeaveAlternateScreen {
@ -218,6 +220,7 @@ pub enum ClearType {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScrollUp(pub u16); pub struct ScrollUp(pub u16);
impl Command for ScrollUp { impl Command for ScrollUp {
@ -238,6 +241,7 @@ impl Command for ScrollUp {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScrollDown(pub u16); pub struct ScrollDown(pub u16);
impl Command for ScrollDown { impl Command for ScrollDown {
@ -260,6 +264,7 @@ impl Command for ScrollDown {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Clear(pub ClearType); pub struct Clear(pub ClearType);
impl Command for Clear { impl Command for Clear {
@ -286,6 +291,7 @@ impl Command for Clear {
/// # Notes /// # Notes
/// ///
/// Commands must be executed/queued for execution otherwise they do nothing. /// Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetSize(pub u16, pub u16); pub struct SetSize(pub u16, pub u16);
impl Command for SetSize { impl Command for SetSize {