Introduce the EnableMouseCapture and DisableMouseCapture commands (#296)

This commit is contained in:
Canop 2019-10-29 19:47:22 +01:00 committed by Timon
parent 3ab5b170aa
commit fe6ddb90f1
5 changed files with 76 additions and 21 deletions

View File

@ -2,6 +2,7 @@
- `input` module
- Derive 'Copy' for 'KeyEvent'
- Add the `EnableMouseCapture` and `EnableMouseCapture` commands
- `cursor` module
- Remove `TerminalCursor`, `cursor`, `Crossterm::cursor()`
- Introduce static function `crossterm::cursor::position` in place of `TerminalCursor::pos`
@ -21,10 +22,10 @@
- Remove re-export terminal module types at root level, are move those to `crossterm::terminal`
- `style module`
- Rename `ObjectStyle` to `ContentStyle`. Now full names are used for methods.
- Rename `StyledObject` to `StyledContent` and made members private.
- Rename `StyledObject` to `StyledContent` and made members private.
- Rename `attr` method to `attribute`.
- Rename `Attribute::NoInverse` to `NoReverse`
# Version 0.12.1
- All the `crossterm_` crates code was moved to the `crossterm` crate

View File

@ -38,7 +38,7 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::utils::Result;
use crate::utils::{Command, Result};
#[cfg(unix)]
use self::input::unix::UnixInput;
@ -47,6 +47,7 @@ use self::input::windows::WindowsInput;
use self::input::Input;
pub use self::input::{AsyncReader, SyncReader};
mod ansi;
mod input;
mod sys;
@ -423,3 +424,40 @@ impl TerminalInput {
pub fn input() -> TerminalInput {
TerminalInput::new()
}
/// A command that enables mouse mode
///
pub struct EnableMouseCapture;
impl Command for EnableMouseCapture {
type AnsiType = String;
fn ansi_code(&self) -> Self::AnsiType {
ansi::enable_mouse_mode_csi_sequence()
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
input().enable_mouse_mode()
}
}
/// A command that disables mouse event monitoring.
///
/// Mouse events will be produced by the
/// [`AsyncReader`](struct.AsyncReader.html)/[`SyncReader`](struct.SyncReader.html).
///
pub struct DisableMouseCapture;
impl Command for DisableMouseCapture {
type AnsiType = String;
fn ansi_code(&self) -> Self::AnsiType {
ansi::disable_mouse_mode_csi_sequence()
}
#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
input().disable_mouse_mode()
}
}

23
src/input/ansi.rs Normal file
View File

@ -0,0 +1,23 @@
//! This module provides input related ANSI escape codes.
use crate::csi;
pub(crate) fn enable_mouse_mode_csi_sequence() -> String {
format!(
"{}h{}h{}h{}h",
csi!("?1000"),
csi!("?1002"),
csi!("?1015"),
csi!("?1006")
)
}
pub(crate) fn disable_mouse_mode_csi_sequence() -> String {
format!(
"{}l{}l{}l{}l",
csi!("?1006"),
csi!("?1015"),
csi!("?1002"),
csi!("?1000")
)
}

View File

@ -4,10 +4,14 @@ use std::sync::mpsc::Receiver;
use std::{char, sync::mpsc};
use crate::utils::Result;
use crate::{csi, write_cout};
use crate::write_cout;
use super::{
super::{sys::unix::internal_event_receiver, InputEvent, InternalEvent, KeyEvent},
super::{
ansi::{disable_mouse_mode_csi_sequence, enable_mouse_mode_csi_sequence},
sys::unix::internal_event_receiver,
InputEvent, InternalEvent, KeyEvent,
},
Input,
};
@ -50,24 +54,12 @@ impl Input for UnixInput {
}
fn enable_mouse_mode(&self) -> Result<()> {
write_cout!(&format!(
"{}h{}h{}h{}h",
csi!("?1000"),
csi!("?1002"),
csi!("?1015"),
csi!("?1006")
))?;
write_cout!(enable_mouse_mode_csi_sequence())?;
Ok(())
}
fn disable_mouse_mode(&self) -> Result<()> {
write_cout!(&format!(
"{}l{}l{}l{}l",
csi!("?1006"),
csi!("?1015"),
csi!("?1002"),
csi!("?1000")
))?;
write_cout!(disable_mouse_mode_csi_sequence())?;
Ok(())
}
}
@ -216,7 +208,7 @@ impl Iterator for AsyncReader {
/// `SyncReader` is an individual iterator and it doesn't use `None` to indicate that the iteration is
/// finished. You can expect additional `Some(InputEvent)` after calling `next` even if you have already
/// received `None`. Unfortunately, `None` means that an error occurred, but you're free to call `next`
/// again. This behavior will be changed in the future to avoid errors consumption.
/// again. This behavior will be changed in the future to avoid errors consumption.
///
/// # Notes
///

View File

@ -169,7 +169,8 @@
#[cfg(feature = "input")]
pub use input::{
input, AsyncReader, InputEvent, KeyEvent, MouseButton, MouseEvent, SyncReader, TerminalInput,
input, AsyncReader, DisableMouseCapture, EnableMouseCapture, InputEvent, KeyEvent, MouseButton,
MouseEvent, SyncReader, TerminalInput,
};
#[cfg(feature = "screen")]
pub use screen::{