From 92fb3e17aa413046dff0a004ab5d790d45ef90f5 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 3 Aug 2024 12:20:54 +0200 Subject: add cursor shape handling --- src/state/editor/cursorshape.rs | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/state/editor/cursorshape.rs (limited to 'src/state/editor/cursorshape.rs') diff --git a/src/state/editor/cursorshape.rs b/src/state/editor/cursorshape.rs new file mode 100644 index 0000000..b9b45bd --- /dev/null +++ b/src/state/editor/cursorshape.rs @@ -0,0 +1,48 @@ +use mlua::{FromLua, IntoLua, Value}; +use ratatui::crossterm::cursor::SetCursorStyle; + +#[derive(Default, Debug, Clone, Copy)] +pub enum CursorShape { + #[default] + Block, + Bar, + Underscore, +} + +impl<'lua> FromLua<'lua> for CursorShape { + fn from_lua( + value: mlua::prelude::LuaValue<'lua>, + _lua: &'lua mlua::prelude::Lua, + ) -> mlua::prelude::LuaResult { + if let Value::String(s) = value { + match s.to_str().unwrap().to_lowercase().as_ref() { + "block" => return Ok(CursorShape::Block), + "bar" => return Ok(CursorShape::Bar), + "underscore" => return Ok(CursorShape::Underscore), + _ => {} + } + } + + Err(mlua::Error::runtime("failed to parse cursorshape")) + } +} + +impl<'lua> IntoLua<'lua> for CursorShape { + fn into_lua(self, lua: &'lua mlua::prelude::Lua) -> mlua::prelude::LuaResult> { + match self { + CursorShape::Block => "block", + CursorShape::Bar => "bar", + CursorShape::Underscore => "underscore", + }.into_lua(lua) + } +} + +impl Into for CursorShape { + fn into(self) -> SetCursorStyle { + match self { + CursorShape::Block => SetCursorStyle::SteadyBlock, + CursorShape::Bar => SetCursorStyle::SteadyBar, + CursorShape::Underscore => SetCursorStyle::SteadyUnderScore, + } + } +} -- cgit v1.2.3-70-g09d2