diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-03 12:20:54 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-03 12:20:54 +0200 |
| commit | 92fb3e17aa413046dff0a004ab5d790d45ef90f5 (patch) | |
| tree | 9dfbb3e71423abd80e65fc73e96952427e0738b8 /src | |
| parent | 90daf295c7dd5fedc3219b8947fac110f05cc522 (diff) | |
add cursor shape handling
Diffstat (limited to 'src')
| -rw-r--r-- | src/state/editor/cursorshape.rs | 48 | ||||
| -rw-r--r-- | src/state/editor/mod.rs | 28 | ||||
| -rw-r--r-- | src/state/log.rs | 18 | ||||
| -rw-r--r-- | src/widgets/luaeditor/mod.rs | 2 |
4 files changed, 82 insertions, 14 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, + } + } +} diff --git a/src/state/editor/mod.rs b/src/state/editor/mod.rs index 47d11bc..d9c3ab5 100644 --- a/src/state/editor/mod.rs +++ b/src/state/editor/mod.rs @@ -1,18 +1,20 @@ use crate::cursor::CursorMove; -use self::{bar::EditorBarState, buffer::Buffer}; +use self::{bar::EditorBarState, buffer::Buffer, cursorshape::CursorShape}; -use super::{GlobalState, DUMMY_STATE}; +use super::{window::Window, GlobalState, DUMMY_STATE}; use mlua::{Lua, Table, UserData}; pub mod bar; pub mod buffer; +pub mod cursorshape; #[derive(Default, Debug)] pub struct EditorState { pub visible: bool, pub buffer: Buffer, pub bar: EditorBarState, + pub cursor_shape: CursorShape, } macro_rules! cfg { @@ -33,6 +35,7 @@ impl EditorState { visible: false, buffer: Buffer::new(), bar: EditorBarState::new(), + cursor_shape: CursorShape::Bar, } } @@ -48,18 +51,31 @@ impl UserData for EditorState { fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { fields.add_field_function_get("visible", |_, _| Ok(cfg!().visible)); fields.add_field_function_set("visible", |_, _, visible: bool| { - cfg_mut!().visible = visible; + let mut state = GlobalState::instance_mut(); + state.editor.visible = visible; + + if let Window::Editor = state.active_window { + if !visible { + state.set_focus(Window::View); + } + } + Ok(()) }); - fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.editor.bar)); - fields.add_field_function_get("content", |_, _| Ok(cfg!().buffer.as_string())); fields.add_field_function_set("content", |_, _, content: String| { cfg_mut!().buffer.set_lines_from_string(content); Ok(()) }); + fields.add_field_function_get("cursor_shape", |_, _| Ok(cfg!().cursor_shape)); + fields.add_field_function_set("cursor_shape", |_, _, shape: CursorShape| { + cfg_mut!().cursor_shape = shape; + Ok(()) + }); + + fields.add_field_function_get("lines", |lua, _| { let table = lua.create_table()?; @@ -80,6 +96,8 @@ impl UserData for EditorState { cfg_mut!().buffer.set_lines(vec); Ok(()) }); + + fields.add_field_function_get("bar", |_, _| Ok(DUMMY_STATE.editor.bar)); } fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { diff --git a/src/state/log.rs b/src/state/log.rs index 5afa8ca..f7311a0 100644 --- a/src/state/log.rs +++ b/src/state/log.rs @@ -1,5 +1,5 @@ use mlua::UserData; -use super::GlobalState; +use super::{window::Window, GlobalState}; #[derive(Debug, Default)] pub struct LogState { @@ -18,17 +18,19 @@ macro_rules! cfg { }; } -macro_rules! cfg_mut { - () => { - GlobalState::instance_mut().log - }; -} - impl UserData for LogState { fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { fields.add_field_function_get("visible", |_, _| Ok(cfg!().visible)); fields.add_field_function_set("visible", |_, _, visible: bool| { - cfg_mut!().visible = visible; + let mut state = GlobalState::instance_mut(); + state.log.visible = visible; + + if let Window::Log = state.active_window { + if !visible { + state.set_focus(Window::Log); + } + } + Ok(()) }); } diff --git a/src/widgets/luaeditor/mod.rs b/src/widgets/luaeditor/mod.rs index 4bcb5d2..0dcda9f 100644 --- a/src/widgets/luaeditor/mod.rs +++ b/src/widgets/luaeditor/mod.rs @@ -60,7 +60,7 @@ impl LuaEditor { buffer.cursor().y() as u16, buffer.cursor().x() as u16 + nr_width, ), - style: ratatui::crossterm::cursor::SetCursorStyle::SteadyBar, + style: state.editor.cursor_shape.into(), }) } } |