on master help sign in
Add other cursor shape
commit33a35b7589dbc35f43f8823b79591ca857bceeac authorIvan Tham <pickfire@riseup.net> dateTue 15 Jun 2021, 13:27 (5 years ago) parent124514a
4 files changed+20−11
helix-tui/src/backend/crossterm.rs+10−3 helix-tui/src/backend/mod.rs+2−1 helix-tui/src/backend/test.rs+2−1 helix-tui/src/terminal.rs+6−6
helix-tui/src/backend/crossterm.rs+10−3
@@ -3,9 +3,10 @@ use crate::{
33 buffer::Cell,
44 layout::Rect,
55 style::{Color, Modifier},
6+ terminal::CursorKind,
67};
78use crossterm::{
8− cursor::{Hide, MoveTo, Show},
9+ cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
910 execute, queue,
1011 style::{
1112 Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
@@ -93,8 +94,14 @@ where
9394 map_error(execute!(self.buffer, Hide))
9495 }
9596
96− fn show_cursor(&mut self) -> io::Result<()> {
97− map_error(execute!(self.buffer, Show))
97+ fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
98+ let shape = match kind {
99+ CursorKind::Block => CursorShape::Block,
100+ CursorKind::Bar => CursorShape::Line,
101+ CursorKind::Underline => CursorShape::UnderScore,
102+ CursorKind::Hidden => unreachable!(),
103+ };
104+ map_error(execute!(self.buffer, Show, SetCursorShape(shape)))
98105 }
99106
100107 fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
helix-tui/src/backend/mod.rs+2−1
@@ -2,6 +2,7 @@ use std::io;
22
33use crate::buffer::Cell;
44use crate::layout::Rect;
5+use crate::terminal::CursorKind;
56
67#[cfg(feature = "crossterm")]
78mod crossterm;
@@ -16,7 +17,7 @@ pub trait Backend {
1617 where
1718 I: Iterator<Item = (u16, u16, &'a Cell)>;
1819 fn hide_cursor(&mut self) -> Result<(), io::Error>;
19− fn show_cursor(&mut self) -> Result<(), io::Error>;
20+ fn show_cursor(&mut self, kind: CursorKind) -> Result<(), io::Error>;
2021 fn get_cursor(&mut self) -> Result<(u16, u16), io::Error>;
2122 fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error>;
2223 fn clear(&mut self) -> Result<(), io::Error>;
helix-tui/src/backend/test.rs+2−1
@@ -2,6 +2,7 @@ use crate::{
22 backend::Backend,
33 buffer::{Buffer, Cell},
44 layout::Rect,
5+ terminal::CursorKind,
56};
67use std::{fmt::Write, io};
78use unicode_width::UnicodeWidthStr;
@@ -122,7 +123,7 @@ impl Backend for TestBackend {
122123 Ok(())
123124 }
124125
125− fn show_cursor(&mut self) -> Result<(), io::Error> {
126+ fn show_cursor(&mut self, _kind: CursorKind) -> Result<(), io::Error> {
126127 self.cursor = true;
127128 Ok(())
128129 }
helix-tui/src/terminal.rs+6−6
@@ -14,9 +14,9 @@ pub enum CursorKind {
1414 /// █
1515 Block,
1616 /// |
17− // Bar,
17+ Bar,
1818 /// _
19− // Underline,
19+ Underline,
2020 /// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
2121 Hidden,
2222}
@@ -70,7 +70,7 @@ where
7070 fn drop(&mut self) {
7171 // Attempt to restore the cursor state
7272 if self.hidden_cursor {
73− if let Err(err) = self.show_cursor() {
73+ if let Err(err) = self.show_cursor(CursorKind::Block) {
7474 eprintln!("Failed to show the cursor: {}", err);
7575 }
7676 }
@@ -184,8 +184,8 @@ where
184184 }
185185
186186 match cursor_kind {
187− CursorKind::Block => self.show_cursor()?,
188187 CursorKind::Hidden => self.hide_cursor()?,
188+ kind => self.show_cursor(kind)?,
189189 }
190190
191191 // Swap buffers
@@ -203,8 +203,8 @@ where
203203 Ok(())
204204 }
205205
206− pub fn show_cursor(&mut self) -> io::Result<()> {
207− self.backend.show_cursor()?;
206+ pub fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
207+ self.backend.show_cursor(kind)?;
208208 self.hidden_cursor = false;
209209 Ok(())
210210 }