Add SetCursorStyle command (#742)

This commit is contained in:
Timon 2023-01-11 21:04:02 +01:00 committed by GitHub
parent 05229b71f9
commit 614e6a73b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 35 deletions

View File

@ -1,3 +1,11 @@
# Version 0.26.0
## Added ⭐
- ⚠️ Add `SetCursorStyle` to set the cursor style.
## Breaking
- Remove `SetCursorShape` in vavour of `SetCursorStyle`.
# Version 0.25.0 # Version 0.25.0
BREAKING: `Copy` trait is removed from `Event`, you can keep it by removing the "bracked-paste" feature flag. However this flag might be standardized in the future. BREAKING: `Copy` trait is removed from `Event`, you can keep it by removing the "bracked-paste" feature flag. However this flag might be standardized in the future.

View File

@ -74,28 +74,43 @@ where
execute!(w, style::Print("ShowCursor"), cursor::Show) execute!(w, style::Print("ShowCursor"), cursor::Show)
} }
fn test_enable_cursor_blinking<W>(w: &mut W) -> Result<()> fn test_cursor_blinking_block<W>(w: &mut W) -> Result<()>
where where
W: Write, W: Write,
{ {
execute!( execute!(
w, w,
style::Print("EnableCursorBlinking"), style::Print("Blinking Block:"),
cursor::EnableBlinking cursor::MoveLeft(2),
cursor::SetCursorStyle::BlinkingBlock,
) )
} }
fn test_disable_cursor_blinking<W>(w: &mut W) -> Result<()> fn test_cursor_blinking_underscore<W>(w: &mut W) -> Result<()>
where where
W: Write, W: Write,
{ {
execute!( execute!(
w, w,
style::Print("DisableCursorBlinking"), style::Print("Blinking Underscore:"),
cursor::DisableBlinking cursor::MoveLeft(2),
cursor::SetCursorStyle::BlinkingUnderScore,
) )
} }
fn test_cursor_blinking_bar<W>(w: &mut W) -> Result<()>
where
W: Write,
{
execute!(
w,
style::Print("Blinking bar:"),
cursor::MoveLeft(2),
cursor::SetCursorStyle::BlinkingBar,
)
}
fn test_move_cursor_to<W>(w: &mut W) -> Result<()> fn test_move_cursor_to<W>(w: &mut W) -> Result<()>
where where
W: Write, W: Write,
@ -192,8 +207,9 @@ where
w, w,
test_hide_cursor, test_hide_cursor,
test_show_cursor, test_show_cursor,
test_enable_cursor_blinking, test_cursor_blinking_bar,
test_disable_cursor_blinking, test_cursor_blinking_block,
test_cursor_blinking_underscore,
test_move_cursor_left, test_move_cursor_left,
test_move_cursor_right, test_move_cursor_right,
test_move_cursor_up, test_move_cursor_up,

View File

@ -329,71 +329,78 @@ impl Command for Show {
/// A command that enables blinking of the terminal cursor. /// A command that enables blinking of the terminal cursor.
/// ///
/// See `SetCursorStyle` which is more advanced and better supported.
///
/// # Notes /// # Notes
/// ///
/// - 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableBlinking; pub struct EnableBlinking;
impl Command for EnableBlinking { impl Command for EnableBlinking {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
f.write_str(csi!("?12h")) f.write_str(csi!("?12h"))
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
Ok(()) Ok(())
} }
} }
/// A command that disables blinking of the terminal cursor. /// A command that disables blinking of the terminal cursor.
/// ///
/// See `SetCursorStyle` which is more advanced and better supported.
///
/// # Notes /// # Notes
/// ///
/// - 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)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableBlinking; pub struct DisableBlinking;
impl Command for DisableBlinking { impl Command for DisableBlinking {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
f.write_str(csi!("?12l")) f.write_str(csi!("?12l"))
} }
#[cfg(windows)] #[cfg(windows)]
fn execute_winapi(&self) -> Result<()> { fn execute_winapi(&self) -> Result<()> {
Ok(()) Ok(())
} }
} }
/// All supported cursor shapes /// A command that sets the style of the cursor.
/// /// It uses two types of escape codes, one to control blinking, and the other the shape.
/// # Note
///
/// - Used with SetCursorShape
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorShape {
UnderScore,
Line,
Block,
}
/// A command that sets the shape of the cursor
/// ///
/// # Note /// # Note
/// ///
/// - 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 enum SetCursorStyle {
pub struct SetCursorShape(pub CursorShape); /// Default cursor shape configured by the user.
DefaultUserShape,
/// A blinking block cursor shape (■).
BlinkingBlock,
/// A non blinking block cursor shape (inverse of `BlinkingBlock`).
SteadyBlock,
/// A blinking underscore cursor shape(_).
BlinkingUnderScore,
/// A non blinking underscore cursor shape (inverse of `BlinkingUnderScore`).
SteadyUnderScore,
/// A blinking cursor bar shape (|)
BlinkingBar,
/// A steady cursor bar shape (inverse of `BlinkingBar`).
SteadyBar,
}
impl Command for SetCursorShape { impl Command for SetCursorStyle {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result { fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
use CursorShape::*; match self {
match self.0 { SetCursorStyle::DefaultUserShape => f.write_str("\x1b[?12h\x1b[0 q"),
UnderScore => f.write_str("\x1b[3 q"), SetCursorStyle::BlinkingBlock => f.write_str("\x1b[?12h\x1b[1 q"),
Line => f.write_str("\x1b[5 q"), SetCursorStyle::SteadyBlock => f.write_str("\x1b[?12l\x1b[2 q"),
Block => f.write_str("\x1b[2 q"), SetCursorStyle::BlinkingUnderScore => f.write_str("\x1b[?12h\x1b[3 q"),
SetCursorStyle::SteadyUnderScore => f.write_str("\x1b[?12l\x1b[4 q"),
SetCursorStyle::BlinkingBar => f.write_str("\x1b[?12h\x1b[5 q"),
SetCursorStyle::SteadyBar => f.write_str("\x1b[?12l\x1b[6 q"),
} }
} }
@ -418,7 +425,7 @@ impl_display!(for Hide);
impl_display!(for Show); impl_display!(for Show);
impl_display!(for EnableBlinking); impl_display!(for EnableBlinking);
impl_display!(for DisableBlinking); impl_display!(for DisableBlinking);
impl_display!(for SetCursorShape); impl_display!(for SetCursorStyle);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {