#[cfg(feature = "cursor-style")]
use crossterm::{cursor::SetCursorStyle, execute};
#[cfg(feature = "cursor-style")]
use std::io;
use crate::canvas::modes::AppMode;
#[cfg(feature = "cursor-style")]
fn cursor_style_for_mode(mode: AppMode) -> SetCursorStyle {
match mode {
AppMode::Ins => SetCursorStyle::SteadyBlock,
AppMode::Nor => SetCursorStyle::SteadyBlock,
AppMode::Sel => SetCursorStyle::BlinkingBlock,
AppMode::General => SetCursorStyle::SteadyBlock,
AppMode::Command => SetCursorStyle::SteadyUnderScore,
}
}
pub struct CursorManager;
impl CursorManager {
#[cfg(feature = "cursor-style")]
pub fn update_for_mode(mode: AppMode) -> io::Result<()> {
#[cfg(feature = "textmode-normal")]
{
let style = SetCursorStyle::SteadyBar;
execute!(io::stdout(), style)
}
#[cfg(not(feature = "textmode-normal"))]
{
let style = cursor_style_for_mode(mode);
return execute!(io::stdout(), style);
}
}
#[cfg(not(feature = "cursor-style"))]
pub fn update_for_mode(_mode: AppMode) -> io::Result<()> {
Ok(())
}
#[cfg(feature = "cursor-style")]
pub fn reset() -> io::Result<()> {
execute!(io::stdout(), SetCursorStyle::DefaultUserShape)
}
#[cfg(not(feature = "cursor-style"))]
pub fn reset() -> io::Result<()> {
Ok(())
}
}
#[cfg(all(test, feature = "cursor-style"))]
mod tests {
use super::*;
use crossterm::Command;
fn ansi_for_mode(mode: AppMode) -> String {
let mut out = String::new();
cursor_style_for_mode(mode).write_ansi(&mut out).unwrap();
out
}
#[test]
fn insert_mode_uses_block_cursor_shape() {
assert_eq!(ansi_for_mode(AppMode::Ins), "\x1b[2 q");
}
}