Skip to content

Commit

Permalink
Change cursor types based on mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacdonald committed Dec 15, 2023
1 parent e88bf52 commit 6d604e9
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/presenters/modes/insert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::errors::*;
use crate::presenters::current_buffer_status_line_data;
use scribe::Workspace;
use crate::view::{Colors, StatusLineData, Style, View};
use crate::view::{Colors, CursorType, StatusLineData, Style, View};

pub fn display(workspace: &mut Workspace, view: &mut View) -> Result<()> {
let mut presenter = view.build_presenter()?;
Expand All @@ -21,6 +21,9 @@ pub fn display(workspace: &mut Workspace, view: &mut View) -> Result<()> {
buffer_status
]);

// Show a blinking, vertical bar indicating input.
presenter.set_cursor_type(CursorType::BlinkingBar);

// Render the changes to the screen.
presenter.present();

Expand Down
5 changes: 4 additions & 1 deletion src/presenters/modes/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use scribe::Workspace;
use scribe::buffer::Position;
use crate::presenters::{current_buffer_status_line_data, git_status_line_data};
use git2::Repository;
use crate::view::{Colors, StatusLineData, Style, View};
use crate::view::{Colors, CursorType, StatusLineData, Style, View};

pub fn display(workspace: &mut Workspace, view: &mut View, repo: &Option<Repository>) -> Result<()> {
let mut presenter = view.build_presenter()?;
Expand Down Expand Up @@ -32,6 +32,9 @@ pub fn display(workspace: &mut Workspace, view: &mut View, repo: &Option<Reposit
git_status_line_data(&repo, &buf.path)
]);

// Restore the default cursor, suggesting non-input mode.
presenter.set_cursor_type(CursorType::Block);

presenter.present();
} else {
let content = vec![
Expand Down
5 changes: 4 additions & 1 deletion src/presenters/modes/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::models::application::modes::SelectMode;
use scribe::Workspace;
use scribe::buffer::Range;
use crate::presenters::current_buffer_status_line_data;
use crate::view::{Colors, StatusLineData, Style, View};
use crate::view::{Colors, CursorType, StatusLineData, Style, View};

pub fn display(workspace: &mut Workspace, mode: &SelectMode, view: &mut View) -> Result<()> {
let mut presenter = view.build_presenter()?;
Expand All @@ -24,6 +24,9 @@ pub fn display(workspace: &mut Workspace, mode: &SelectMode, view: &mut View) ->
buffer_status
]);

// Show a vertical bar to allow unambiguous/precise selection.
presenter.set_cursor_type(CursorType::Bar);

// Render the changes to the screen.
presenter.present();

Expand Down
6 changes: 5 additions & 1 deletion src/view/presenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::view::buffer::{BufferRenderer, LexemeMapper};
use crate::view::color::{ColorMap, Colors};
use crate::view::StatusLineData;
use crate::view::style::Style;
use crate::view::terminal::{Cell, TerminalBuffer};
use crate::view::terminal::{Cell, CursorType, TerminalBuffer};
use crate::view::View;
use scribe::buffer::{Buffer, Position, Range};
use scribe::util::LineIterator;
Expand Down Expand Up @@ -55,6 +55,10 @@ impl<'p> Presenter<'p> {
self.cursor_position = position;
}

pub fn set_cursor_type(&mut self, cursor_type: CursorType) {
self.view.terminal.set_cursor_type(cursor_type);
}

pub fn present(&mut self) {
for (position, cell) in self.terminal_buffer.iter() {
self.view.terminal.print(
Expand Down
5 changes: 5 additions & 0 deletions src/view/terminal/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub enum CursorType {
Bar,
BlinkingBar,
Block,
}
3 changes: 3 additions & 0 deletions src/view/terminal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod buffer;
mod buffer_iterator;
mod cell;
mod cursor;
mod termion_terminal;

#[cfg(any(test, feature = "bench"))]
Expand All @@ -15,6 +16,7 @@ use std::sync::Arc;
pub use self::buffer::TerminalBuffer;
pub use self::buffer_iterator::TerminalBufferIterator;
pub use self::cell::Cell;
pub use self::cursor::CursorType;
pub use self::termion_terminal::TermionTerminal;

#[cfg(any(test, feature = "bench"))]
Expand All @@ -30,6 +32,7 @@ pub trait Terminal {
fn width(&self) -> usize;
fn height(&self) -> usize;
fn set_cursor(&self, _: Option<Position>);
fn set_cursor_type(&self, _: CursorType);
fn print<'a>(&self, _: &Position, _: Style, _: Colors, _: &str);
fn suspend(&self);
}
Expand Down
14 changes: 13 additions & 1 deletion src/view/terminal/termion_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::fmt::Display;
use std::ops::Drop;
use std::sync::Mutex;
use std::time::Duration;
use crate::view::{Colors, Style};
use crate::view::{Colors, CursorType, Style};
use unicode_segmentation::UnicodeSegmentation;
use signal_hook::iterator::Signals;

Expand Down Expand Up @@ -230,6 +230,18 @@ impl Terminal for TermionTerminal {
}
}

fn set_cursor_type(&self, cursor_type: CursorType) {
if let Ok(mut output) = self.output.lock() {
output.as_mut().map(|t| {
match cursor_type {
CursorType::Bar => { let _ = write!(t, "{}", cursor::SteadyBar); },
CursorType::BlinkingBar => { let _ = write!(t, "{}", cursor::BlinkingBar); },
CursorType::Block => { let _ = write!(t, "{}", cursor::SteadyBlock); },
}
});
}
}

fn print<'a>(&self, target_position: &Position, style: Style, colors: Colors, content: &str) {
self.update_style(style);
self.update_colors(colors);
Expand Down
3 changes: 2 additions & 1 deletion src/view/terminal/test_terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::models::application::Event;
use scribe::buffer::Position;
use std::sync::Mutex;
use super::Terminal;
use crate::view::{Colors, Style};
use crate::view::{Colors, CursorType, Style};

const WIDTH: usize = 10;
const HEIGHT: usize = 10;
Expand Down Expand Up @@ -86,6 +86,7 @@ impl Terminal for TestTerminal {
let mut cursor = self.cursor.lock().unwrap();
*cursor = position;
}
fn set_cursor_type(&self, _: CursorType) { }
fn suspend(&self) { }
fn print(&self, position: &Position, _: Style, colors: Colors, content: &str) {
// Ignore lines beyond visible height.
Expand Down

0 comments on commit 6d604e9

Please sign in to comment.