summaryrefslogtreecommitdiff
path: root/src/state/editor/cursorshape.rs
blob: b9b45bd8c033ef32673aff9ba772cfa99e56ca05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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,
        }
    }
}