summaryrefslogtreecommitdiff
path: root/src/state/editor
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/editor')
-rw-r--r--src/state/editor/cursorshape.rs48
-rw-r--r--src/state/editor/mod.rs28
2 files changed, 71 insertions, 5 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) {