Skip to content

Commit 12f36ec

Browse files
Add a feature flag for derive_more impls (#970)
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.
1 parent 2c18768 commit 12f36ec

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ path = "src/lib.rs"
2121
all-features = true
2222

2323
[features]
24-
default = ["bracketed-paste", "events", "windows"]
24+
default = ["bracketed-paste", "events", "windows", "derive-more"]
2525

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

47+
## Enables `is_*` helper functions for event enums.
48+
derive-more = ["dep:derive_more"]
49+
4750
[dependencies]
4851
bitflags = { version = "2.3" }
49-
derive_more = { version = "1.0.0", features = ["is_variant"] }
52+
derive_more = { version = "1.0.0", features = ["is_variant"], optional = true }
5053
document-features = "0.2.10"
5154
futures-core = { version = "0.3", optional = true, default-features = false }
5255
parking_lot = "0.12"

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ features = ["event-stream"]
153153
| `serde` | (De)serializing of events. |
154154
| `events` | Reading input/system events (enabled by default) |
155155
| `filedescriptor` | Use raw filedescriptor for all events rather then mio dependency |
156+
| `derive-more` | Adds `is_*` helper functions for event types |
156157

157158

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

174176
### Other Resources
175177

src/event.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub(crate) mod stream;
126126
pub(crate) mod sys;
127127
pub(crate) mod timeout;
128128

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

544545
/// Represents an event.
545546
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
547+
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
546548
#[cfg_attr(not(feature = "bracketed-paste"), derive(Copy))]
547-
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash, IsVariant)]
549+
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Hash)]
548550
pub enum Event {
549551
/// The terminal gained focus
550552
FocusGained,
@@ -793,7 +795,8 @@ pub struct MouseEvent {
793795
/// `MouseEventKind::Up` and `MouseEventKind::Drag` events. `MouseButton::Left`
794796
/// is returned if we don't know which button was used.
795797
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
796-
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
798+
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
799+
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
797800
pub enum MouseEventKind {
798801
/// Pressed mouse button. Contains the button that was pressed.
799802
Down(MouseButton),
@@ -815,7 +818,8 @@ pub enum MouseEventKind {
815818

816819
/// Represents a mouse button.
817820
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
818-
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
821+
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
822+
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
819823
pub enum MouseButton {
820824
/// Left mouse button.
821825
Left,
@@ -895,7 +899,8 @@ impl Display for KeyModifiers {
895899

896900
/// Represents a keyboard event kind.
897901
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
898-
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
902+
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
903+
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
899904
pub enum KeyEventKind {
900905
Press,
901906
Repeat,
@@ -1002,17 +1007,17 @@ impl KeyEvent {
10021007

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

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

10131018
/// Returns whether the key event is a repeat event.
10141019
pub fn is_repeat(&self) -> bool {
1015-
self.kind.is_repeat()
1020+
matches!(self.kind, KeyEventKind::Repeat)
10161021
}
10171022
}
10181023

@@ -1214,7 +1219,8 @@ impl Display for ModifierKeyCode {
12141219
}
12151220

12161221
/// Represents a key.
1217-
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, IsVariant)]
1222+
#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash)]
1223+
#[cfg_attr(feature = "derive-more", derive(IsVariant))]
12181224
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12191225
pub enum KeyCode {
12201226
/// Backspace key (Delete on macOS, Backspace on other platforms).
@@ -1248,12 +1254,12 @@ pub enum KeyCode {
12481254
/// F key.
12491255
///
12501256
/// `KeyCode::F(1)` represents F1 key, etc.
1251-
#[is_variant(ignore)]
1257+
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
12521258
F(u8),
12531259
/// A character.
12541260
///
12551261
/// `KeyCode::Char('c')` represents `c` character, etc.
1256-
#[is_variant(ignore)]
1262+
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
12571263
Char(char),
12581264
/// Null.
12591265
Null,
@@ -1306,15 +1312,15 @@ pub enum KeyCode {
13061312
/// **Note:** these keys can only be read if
13071313
/// [`KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES`] has been enabled with
13081314
/// [`PushKeyboardEnhancementFlags`].
1309-
#[is_variant(ignore)]
1315+
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
13101316
Media(MediaKeyCode),
13111317
/// A modifier key.
13121318
///
13131319
/// **Note:** these keys can only be read if **both**
13141320
/// [`KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES`] and
13151321
/// [`KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES`] have been enabled with
13161322
/// [`PushKeyboardEnhancementFlags`].
1317-
#[is_variant(ignore)]
1323+
#[cfg_attr(feature = "derive-more", is_variant(ignore))]
13181324
Modifier(ModifierKeyCode),
13191325
}
13201326

@@ -1632,6 +1638,7 @@ mod tests {
16321638
modifiers: KeyModifiers::empty(),
16331639
};
16341640

1641+
#[cfg(feature = "derive-more")]
16351642
#[test]
16361643
fn event_is() {
16371644
let event = Event::FocusGained;

0 commit comments

Comments
 (0)