Add other cursor shape

imgbot
Ivan Tham 3 years ago
parent 124514aa70
commit 33a35b7589

@ -3,9 +3,10 @@ use crate::{
buffer::Cell, buffer::Cell,
layout::Rect, layout::Rect,
style::{Color, Modifier}, style::{Color, Modifier},
terminal::CursorKind,
}; };
use crossterm::{ use crossterm::{
cursor::{Hide, MoveTo, Show}, cursor::{CursorShape, Hide, MoveTo, SetCursorShape, Show},
execute, queue, execute, queue,
style::{ style::{
Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor, Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
@ -93,8 +94,14 @@ where
map_error(execute!(self.buffer, Hide)) map_error(execute!(self.buffer, Hide))
} }
fn show_cursor(&mut self) -> io::Result<()> { fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
map_error(execute!(self.buffer, Show)) 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)> { fn get_cursor(&mut self) -> io::Result<(u16, u16)> {

@ -2,6 +2,7 @@ use std::io;
use crate::buffer::Cell; use crate::buffer::Cell;
use crate::layout::Rect; use crate::layout::Rect;
use crate::terminal::CursorKind;
#[cfg(feature = "crossterm")] #[cfg(feature = "crossterm")]
mod crossterm; mod crossterm;
@ -16,7 +17,7 @@ pub trait Backend {
where where
I: Iterator<Item = (u16, u16, &'a Cell)>; I: Iterator<Item = (u16, u16, &'a Cell)>;
fn hide_cursor(&mut self) -> Result<(), io::Error>; 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 get_cursor(&mut self) -> Result<(u16, u16), io::Error>;
fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error>; fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), io::Error>;
fn clear(&mut self) -> Result<(), io::Error>; fn clear(&mut self) -> Result<(), io::Error>;

@ -2,6 +2,7 @@ use crate::{
backend::Backend, backend::Backend,
buffer::{Buffer, Cell}, buffer::{Buffer, Cell},
layout::Rect, layout::Rect,
terminal::CursorKind,
}; };
use std::{fmt::Write, io}; use std::{fmt::Write, io};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -122,7 +123,7 @@ impl Backend for TestBackend {
Ok(()) Ok(())
} }
fn show_cursor(&mut self) -> Result<(), io::Error> { fn show_cursor(&mut self, _kind: CursorKind) -> Result<(), io::Error> {
self.cursor = true; self.cursor = true;
Ok(()) Ok(())
} }

@ -14,9 +14,9 @@ pub enum CursorKind {
/// █ /// █
Block, Block,
/// | /// |
// Bar, Bar,
/// _ /// _
// Underline, Underline,
/// Hidden cursor, can set cursor position with this to let IME have correct cursor position. /// Hidden cursor, can set cursor position with this to let IME have correct cursor position.
Hidden, Hidden,
} }
@ -70,7 +70,7 @@ where
fn drop(&mut self) { fn drop(&mut self) {
// Attempt to restore the cursor state // Attempt to restore the cursor state
if self.hidden_cursor { 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); eprintln!("Failed to show the cursor: {}", err);
} }
} }
@ -184,8 +184,8 @@ where
} }
match cursor_kind { match cursor_kind {
CursorKind::Block => self.show_cursor()?,
CursorKind::Hidden => self.hide_cursor()?, CursorKind::Hidden => self.hide_cursor()?,
kind => self.show_cursor(kind)?,
} }
// Swap buffers // Swap buffers
@ -203,8 +203,8 @@ where
Ok(()) Ok(())
} }
pub fn show_cursor(&mut self) -> io::Result<()> { pub fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> {
self.backend.show_cursor()?; self.backend.show_cursor(kind)?;
self.hidden_cursor = false; self.hidden_cursor = false;
Ok(()) Ok(())
} }

Loading…
Cancel
Save