Skip to content

Commit

Permalink
Add other cursor shape
Browse files Browse the repository at this point in the history
  • Loading branch information
pickfire committed Jun 15, 2021
1 parent 124514a commit 33a35b7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
13 changes: 10 additions & 3 deletions helix-tui/src/backend/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)> {
Expand Down
3 changes: 2 additions & 1 deletion helix-tui/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;

use crate::buffer::Cell;
use crate::layout::Rect;
use crate::terminal::CursorKind;

#[cfg(feature = "crossterm")]
mod crossterm;
Expand All @@ -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>;
Expand Down
3 changes: 2 additions & 1 deletion helix-tui/src/backend/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
backend::Backend,
buffer::{Buffer, Cell},
layout::Rect,
terminal::CursorKind,
};
use std::{fmt::Write, io};
use unicode_width::UnicodeWidthStr;
Expand Down Expand Up @@ -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(())
}
Expand Down
12 changes: 6 additions & 6 deletions helix-tui/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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(())
}
Expand Down

0 comments on commit 33a35b7

Please sign in to comment.