Skip to content

Commit

Permalink
Add SetCursorStyle command (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonPost committed Jan 11, 2023
1 parent 05229b7 commit 614e6a7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 35 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -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
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.
Expand Down
32 changes: 24 additions & 8 deletions examples/interactive-demo/src/test/cursor.rs
Expand Up @@ -74,28 +74,43 @@ where
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
W: Write,
{
execute!(
w,
style::Print("EnableCursorBlinking"),
cursor::EnableBlinking
style::Print("Blinking Block:"),
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
W: Write,
{
execute!(
w,
style::Print("DisableCursorBlinking"),
cursor::DisableBlinking
style::Print("Blinking Underscore:"),
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<()>
where
W: Write,
Expand Down Expand Up @@ -192,8 +207,9 @@ where
w,
test_hide_cursor,
test_show_cursor,
test_enable_cursor_blinking,
test_disable_cursor_blinking,
test_cursor_blinking_bar,
test_cursor_blinking_block,
test_cursor_blinking_underscore,
test_move_cursor_left,
test_move_cursor_right,
test_move_cursor_up,
Expand Down
61 changes: 34 additions & 27 deletions src/cursor.rs
Expand Up @@ -329,71 +329,78 @@ impl Command for Show {

/// A command that enables blinking of the terminal cursor.
///
/// See `SetCursorStyle` which is more advanced and better supported.
///
/// # Notes
///
/// - Windows versions lower than Windows 10 do not support this functionality.
/// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EnableBlinking;

impl Command for EnableBlinking {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
f.write_str(csi!("?12h"))
}

#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
Ok(())
}
}


/// A command that disables blinking of the terminal cursor.
///
/// See `SetCursorStyle` which is more advanced and better supported.
///
/// # Notes
///
/// - Windows versions lower than Windows 10 do not support this functionality.
/// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DisableBlinking;

impl Command for DisableBlinking {
fn write_ansi(&self, f: &mut impl fmt::Write) -> fmt::Result {
f.write_str(csi!("?12l"))
}

#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
Ok(())
}
}

/// All supported cursor shapes
///
/// # 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
///
/// 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
///
/// - Commands must be executed/queued for execution otherwise they do nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetCursorShape(pub CursorShape);
pub enum SetCursorStyle {
/// 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 {
use CursorShape::*;
match self.0 {
UnderScore => f.write_str("\x1b[3 q"),
Line => f.write_str("\x1b[5 q"),
Block => f.write_str("\x1b[2 q"),
match self {
SetCursorStyle::DefaultUserShape => f.write_str("\x1b[?12h\x1b[0 q"),
SetCursorStyle::BlinkingBlock => f.write_str("\x1b[?12h\x1b[1 q"),
SetCursorStyle::SteadyBlock => f.write_str("\x1b[?12l\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"),
}
}

Expand All @@ -418,7 +425,7 @@ impl_display!(for Hide);
impl_display!(for Show);
impl_display!(for EnableBlinking);
impl_display!(for DisableBlinking);
impl_display!(for SetCursorShape);
impl_display!(for SetCursorStyle);

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 614e6a7

Please sign in to comment.