diff options
Diffstat (limited to 'src/state/editor/cursorshape.rs')
| -rw-r--r-- | src/state/editor/cursorshape.rs | 48 |
1 files changed, 48 insertions, 0 deletions
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<Self> { + 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<Value<'lua>> { + match self { + CursorShape::Block => "block", + CursorShape::Bar => "bar", + CursorShape::Underscore => "underscore", + }.into_lua(lua) + } +} + +impl Into<SetCursorStyle> for CursorShape { + fn into(self) -> SetCursorStyle { + match self { + CursorShape::Block => SetCursorStyle::SteadyBlock, + CursorShape::Bar => SetCursorStyle::SteadyBar, + CursorShape::Underscore => SetCursorStyle::SteadyUnderScore, + } + } +} |