From 33a35b7589dbc35f43f8823b79591ca857bceeac Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Tue, 15 Jun 2021 13:27:53 +0800 Subject: [PATCH] Add other cursor shape --- helix-tui/src/backend/crossterm.rs | 13 ++++++++++--- helix-tui/src/backend/mod.rs | 3 ++- helix-tui/src/backend/test.rs | 3 ++- helix-tui/src/terminal.rs | 12 ++++++------ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/helix-tui/src/backend/crossterm.rs b/helix-tui/src/backend/crossterm.rs index f6703e144..189f9dce2 100644 --- a/helix-tui/src/backend/crossterm.rs +++ b/helix-tui/src/backend/crossterm.rs @@ -3,9 +3,10 @@ use crate::{ buffer::Cell, layout::Rect, style::{Color, Modifier}, + terminal::CursorKind, }; use crossterm::{ - cursor::{Hide, MoveTo, Show}, + cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show}, execute, queue, style::{ Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor, @@ -93,8 +94,14 @@ where map_error(execute!(self.buffer, Hide)) } - fn show_cursor(&mut self) -> io::Result<()> { - map_error(execute!(self.buffer, Show)) + fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> { + let shape = match kind { + CursorKind::Block => CursorShape::Block, + CursorKind::Bar => CursorShape::Line, + CursorKind::Underline => CursorShape::UnderScore, + CursorKind::Hidden => unreachable!(), + }; + map_error(execute!(self.buffer, Show, SetCursorShape(shape))) } fn get_cursor(&mut self) -> io::Result<(u16, u16)> { diff --git a/helix-tui/src/backend/mod.rs b/helix-tui/src/backend/mod.rs index ed1252008..5cf217682 100644 --- a/helix-tui/src/backend/mod.rs +++ b/helix-tui/src/backend/mod.rs @@ -2,6 +2,7 @@ use std::io; use crate::buffer::Cell; use crate::layout::Rect; +use crate::terminal::CursorKind; #[cfg(feature = "crossterm")] mod crossterm; @@ -16,7 +17,7 @@ pub trait Backend { where I: Iterator; fn hide_cursor(&mut self) -> Result<(), io::Error>; - fn show_cursor(&mut self) -> Result<(), io::Error>; + fn show_cursor(&mut self, kind: CursorKind) -> Result<(), io::Error>; fn get_cursor(&mut self) -> Result<(u16, u16), io::Error>; fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error>; fn clear(&mut self) -> Result<(), io::Error>; diff --git a/helix-tui/src/backend/test.rs b/helix-tui/src/backend/test.rs index 46b372614..4681831d1 100644 --- a/helix-tui/src/backend/test.rs +++ b/helix-tui/src/backend/test.rs @@ -2,6 +2,7 @@ use crate::{ backend::Backend, buffer::{Buffer, Cell}, layout::Rect, + terminal::CursorKind, }; use std::{fmt::Write, io}; use unicode_width::UnicodeWidthStr; @@ -122,7 +123,7 @@ impl Backend for TestBackend { Ok(()) } - fn show_cursor(&mut self) -> Result<(), io::Error> { + fn show_cursor(&mut self, _kind: CursorKind) -> Result<(), io::Error> { self.cursor = true; Ok(()) } diff --git a/helix-tui/src/terminal.rs b/helix-tui/src/terminal.rs index e8f8f3595..b9b2437a1 100644 --- a/helix-tui/src/terminal.rs +++ b/helix-tui/src/terminal.rs @@ -14,9 +14,9 @@ pub enum CursorKind { /// █ Block, /// | - // Bar, + Bar, /// _ - // Underline, + Underline, /// Hidden cursor, can set cursor position with this to let IME have correct cursor position. Hidden, } @@ -70,7 +70,7 @@ where fn drop(&mut self) { // Attempt to restore the cursor state if self.hidden_cursor { - if let Err(err) = self.show_cursor() { + if let Err(err) = self.show_cursor(CursorKind::Block) { eprintln!("Failed to show the cursor: {}", err); } } @@ -184,8 +184,8 @@ where } match cursor_kind { - CursorKind::Block => self.show_cursor()?, CursorKind::Hidden => self.hide_cursor()?, + kind => self.show_cursor(kind)?, } // Swap buffers @@ -203,8 +203,8 @@ where Ok(()) } - pub fn show_cursor(&mut self) -> io::Result<()> { - self.backend.show_cursor()?; + pub fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> { + self.backend.show_cursor(kind)?; self.hidden_cursor = false; Ok(()) }