Add other cursor shape

This commit is contained in:
Ivan Tham 2021-06-15 13:27:53 +08:00
parent 124514aa70
commit 33a35b7589
4 changed files with 20 additions and 11 deletions

View file

@ -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)> {

View file

@ -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<Item = (u16, u16, &'a Cell)>;
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>;

View file

@ -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(())
}

View file

@ -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(())
}