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 +++++++++++++++++++++++++++++++++++++++++ src/state/editor/mod.rs | 28 +++++++++++++++++++----- src/state/log.rs | 18 +++++++++------- 3 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 src/state/editor/cursorshape.rs (limited to 'src/state') 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, + } + } +} 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(()) }); } -- cgit v1.2.3-70-g09d2