Skip to content

Commit

Permalink
Add a feature flag for derive_more impls
Browse files Browse the repository at this point in the history
This adds a feature flag to allow opting out of the `derive_more`
dependency and derivations added in e063091. `derive_more` brings in
heavy proc macro dependencies and isn't crucial for the core functioning
of crossterm, so it should be possible for a crossterm dependent to opt
out of bringing in the transitive dependency.
  • Loading branch information
the-mikedavis committed Feb 15, 2025
1 parent 2c18768 commit dfd45c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/lib.rs"
all-features = true

[features]
default = ["bracketed-paste", "events", "windows"]
default = ["bracketed-paste", "events", "windows", "derive-more"]

#! ### Default features
## Enables triggering [`Event::Paste`](event::Event::Paste) when pasting text into the terminal.
Expand All @@ -44,9 +44,12 @@ serde = ["dep:serde", "bitflags/serde"]
## Enables raw file descriptor polling / selecting instead of mio.
use-dev-tty = ["filedescriptor", "rustix/process"]

## Enables `is_*` helper functions for event enums.
derive-more = ["dep:derive_more"]

[dependencies]
bitflags = { version = "2.3" }
derive_more = { version = "1.0.0", features = ["is_variant"] }
derive_more = { version = "1.0.0", features = ["is_variant"], optional = true }
document-features = "0.2.10"
futures-core = { version = "0.3", optional = true, default-features = false }
parking_lot = "0.12"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ features = ["event-stream"]
| `serde` | (De)serializing of events. |
| `events` | Reading input/system events (enabled by default) |
| `filedescriptor` | Use raw filedescriptor for all events rather then mio dependency |
| `derive-more` | Adds `is_*` helper functions for event types |


To use crossterm as a very thin layer you can disable the `events` feature or use `filedescriptor` feature.
Expand All @@ -170,6 +171,7 @@ This can disable `mio` / `signal-hook` / `signal-hook-mio` dependencies.
| `winapi` | Used for low-level windows system calls which ANSI codes can't replace | windows only |
| `futures-core` | For async stream of events | only with `event-stream` feature flag |
| `serde` | ***ser***ializing and ***de***serializing of events | only with `serde` feature flag |
| `derive_more` | Adds `is_*` helper functions for event types | optional (`derive-more` feature), included by default |

### Other Resources

Expand Down
31 changes: 19 additions & 12 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub(crate) mod stream;
pub(crate) mod sys;
pub(crate) mod timeout;

#[cfg(feature = "derive-more")]
use derive_more::derive::IsVariant;
#[cfg(feature = "event-stream")]
pub use stream::EventStream;
Expand Down Expand Up @@ -543,8 +544,9 @@ impl Command for PopKeyboardEnhancementFlags {

/// Represents an event.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
#[cfg_attr(not(feature = "bracketed-paste"), derive(Copy))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash, IsVariant)]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)]
pub enum Event {
/// The terminal gained focus
FocusGained,
Expand Down Expand Up @@ -793,7 +795,8 @@ pub struct MouseEvent {
/// `MouseEventKind::Up` and `MouseEventKind::Drag` events. `MouseButton::Left`
/// is returned if we don't know which button was used.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum MouseEventKind {
/// Pressed mouse button. Contains the button that was pressed.
Down(MouseButton),
Expand All @@ -815,7 +818,8 @@ pub enum MouseEventKind {

/// Represents a mouse button.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum MouseButton {
/// Left mouse button.
Left,
Expand Down Expand Up @@ -895,7 +899,8 @@ impl Display for KeyModifiers {

/// Represents a keyboard event kind.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
pub enum KeyEventKind {
Press,
Repeat,
Expand Down Expand Up @@ -1002,17 +1007,17 @@ impl KeyEvent {

/// Returns whether the key event is a press event.
pub fn is_press(&self) -> bool {
self.kind.is_press()
matches!(self.kind, KeyEventKind::Press)
}

/// Returns whether the key event is a release event.
pub fn is_release(&self) -> bool {
self.kind.is_release()
matches!(self.kind, KeyEventKind::Release)
}

/// Returns whether the key event is a repeat event.
pub fn is_repeat(&self) -> bool {
self.kind.is_repeat()
matches!(self.kind, KeyEventKind::Repeat)
}
}

Expand Down Expand Up @@ -1214,7 +1219,8 @@ impl Display for ModifierKeyCode {
}

/// Represents a key.
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KeyCode {
/// Backspace key (Delete on macOS, Backspace on other platforms).
Expand Down Expand Up @@ -1248,12 +1254,12 @@ pub enum KeyCode {
/// F key.
///
/// `KeyCode::F(1)` represents F1 key, etc.
#[is_variant(ignore)]
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
F(u8),
/// A character.
///
/// `KeyCode::Char('c')` represents `c` character, etc.
#[is_variant(ignore)]
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
Char(char),
/// Null.
Null,
Expand Down Expand Up @@ -1306,15 +1312,15 @@ pub enum KeyCode {
/// **Note:** these keys can only be read if
/// [`KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES`] has been enabled with
/// [`PushKeyboardEnhancementFlags`].
#[is_variant(ignore)]
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
Media(MediaKeyCode),
/// A modifier key.
///
/// **Note:** these keys can only be read if **both**
/// [`KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES`] and
/// [`KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES`] have been enabled with
/// [`PushKeyboardEnhancementFlags`].
#[is_variant(ignore)]
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
Modifier(ModifierKeyCode),
}

Expand Down Expand Up @@ -1632,6 +1638,7 @@ mod tests {
modifiers: KeyModifiers::empty(),
};

#[cfg(feature = "derive-more")]
#[test]
fn event_is() {
let event = Event::FocusGained;
Expand Down

0 comments on commit dfd45c4

Please sign in to comment.