on master help sign in
Add other cursor shape
33a35b7Ivan Tham
helix-tui/src/backend/crossterm.rs+10−3
@@ -3,9 +3,10 @@ use crate::{
3 buffer::Cell,
3 buffer::Cell,
4 layout::Rect,
4 layout::Rect,
5 style::{Color, Modifier},
5 style::{Color, Modifier},
6+ terminal::CursorKind,
6};
7};
7use crossterm::{
8use crossterm::{
8− cursor::{Hide, MoveTo, Show},
9+ cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
9 execute, queue,
10 execute, queue,
10 style::{
11 style::{
11 Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
12 Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
@@ -93,8 +94,14 @@ where
93 map_error(execute!(self.buffer, Hide))
94 map_error(execute!(self.buffer, Hide))
94 }
95 }
95
96
96− fn show_cursor(&mut self) -> io::Result<()> {
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+ };
97− map_error(execute!(self.buffer, Show))
104+ map_error(execute!(self.buffer, Show, SetCursorShape(shape)))
98 }
105 }
99
106
100 fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
107 fn get_cursor(&mut self) -> io::Result<(u16, u16)> {
helix-tui/src/backend/mod.rs+2−1
@@ -2,6 +2,7 @@ use std::io;
2
2
3use crate::buffer::Cell;
3use crate::buffer::Cell;
4use crate::layout::Rect;
4use crate::layout::Rect;
5+use crate::terminal::CursorKind;
5
6
6#[cfg(feature = "crossterm")]
7#[cfg(feature = "crossterm")]
7mod crossterm;
8mod crossterm;
@@ -16,7 +17,7 @@ pub trait Backend {
16 where
17 where
17 I: Iterator<Item = (u16, u16, &'a Cell)>;
18 I: Iterator<Item = (u16, u16, &'a Cell)>;
18 fn hide_cursor(&mut self) -> Result<(), io::Error>;
19 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>;
20 fn get_cursor(&mut self) -> Result<(u16, u16), io::Error>;
21 fn get_cursor(&mut self) -> Result<(u16, u16), io::Error>;
21 fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error>;
22 fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error>;
22 fn clear(&mut self) -> Result<(), io::Error>;
23 fn clear(&mut self) -> Result<(), io::Error>;
helix-tui/src/backend/test.rs+2−1
@@ -2,6 +2,7 @@ use crate::{
2 backend::Backend,
2 backend::Backend,
3 buffer::{Buffer, Cell},
3 buffer::{Buffer, Cell},
4 layout::Rect,
4 layout::Rect,
5+ terminal::CursorKind,
5};
6};
6use std::{fmt::Write, io};
7use std::{fmt::Write, io};
7use unicode_width::UnicodeWidthStr;
8use unicode_width::UnicodeWidthStr;
@@ -122,7 +123,7 @@ impl Backend for TestBackend {
122 Ok(())
123 Ok(())
123 }
124 }
124
125
125− fn show_cursor(&mut self) -> Result<(), io::Error> {
126+ fn show_cursor(&mut self, _kind: CursorKind) -> Result<(), io::Error> {
126 self.cursor = true;
127 self.cursor = true;
127 Ok(())
128 Ok(())
128 }
129 }
helix-tui/src/terminal.rs+6−6
@@ -14,9 +14,9 @@ pub enum CursorKind {
14 /// █
14 /// █
15 Block,
15 Block,
16 /// |
16 /// |
17− // Bar,
17+ Bar,
18 /// _
18 /// _
19− // Underline,
19+ Underline,
20 /// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
20 /// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
21 Hidden,
21 Hidden,
22}
22}
@@ -70,7 +70,7 @@ where
70 fn drop(&mut self) {
70 fn drop(&mut self) {
71 // Attempt to restore the cursor state
71 // Attempt to restore the cursor state
72 if self.hidden_cursor {
72 if self.hidden_cursor {
73− if let Err(err) = self.show_cursor() {
73+ if let Err(err) = self.show_cursor(CursorKind::Block) {
74 eprintln!("Failed to show the cursor: {}", err);
74 eprintln!("Failed to show the cursor: {}", err);
75 }
75 }
76 }
76 }
@@ -184,8 +184,8 @@ where
184 }
184 }
185
185
186 match cursor_kind {
186 match cursor_kind {
187− CursorKind::Block => self.show_cursor()?,
188 CursorKind::Hidden => self.hide_cursor()?,
187 CursorKind::Hidden => self.hide_cursor()?,
188+ kind => self.show_cursor(kind)?,
189 }
189 }
190
190
191 // Swap buffers
191 // Swap buffers
@@ -203,8 +203,8 @@ where
203 Ok(())
203 Ok(())
204 }
204 }
205
205
206− pub fn show_cursor(&mut self) -> io::Result<()> {
206+ pub fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
207− self.backend.show_cursor()?;
207+ self.backend.show_cursor(kind)?;
208 self.hidden_cursor = false;
208 self.hidden_cursor = false;
209 Ok(())
209 Ok(())
210 }
210 }