diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 15:51:45 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 15:51:45 +0200 |
| commit | d1492a10cdaf714074d29ad3366ab9c169d95b75 (patch) | |
| tree | 4192aa6825479f56332b3cd40d56913e48b95222 /src/state | |
| parent | c920f258f6c9b0623a841b7c27561fa1d09cef72 (diff) | |
add bartheme and barstate to sheetview
Diffstat (limited to 'src/state')
| -rw-r--r-- | src/state/bar.rs | 60 | ||||
| -rw-r--r-- | src/state/mod.rs | 1 | ||||
| -rw-r--r-- | src/state/view/bar.rs | 7 | ||||
| -rw-r--r-- | src/state/view/mod.rs | 11 | ||||
| -rw-r--r-- | src/state/view/mode.rs | 12 |
5 files changed, 83 insertions, 8 deletions
diff --git a/src/state/bar.rs b/src/state/bar.rs new file mode 100644 index 0000000..6462449 --- /dev/null +++ b/src/state/bar.rs @@ -0,0 +1,60 @@ +macro_rules! BarState { + ($name:ident, $cfg:expr, $cfg_mut:expr) => { + use crate::lua::evalsto::EvalTo; + use crate::state::view::mode::Mode; + use crate::widgets::statusbar::StatusBar; + use mlua::{Lua, UserData}; + + #[derive(Debug, Default, Clone)] + pub struct $name { + pub left: EvalTo<String, Mode>, + pub middle: EvalTo<String, Mode>, + pub right: EvalTo<String, Mode>, + } + + impl $name { + pub const fn new() -> Self { + Self { + left: EvalTo::Value(String::new()), + middle: EvalTo::Value(String::new()), + right: EvalTo::Value(String::new()), + } + } + + pub fn apply(bar: &mut StatusBar, mode: Mode, lua: &Lua) { + let (left, middle, right) = ( + $cfg.left.get(mode, lua).unwrap_or_default(), + $cfg.middle.get(mode, lua).unwrap_or_default(), + $cfg.right.get(mode, lua).unwrap_or_default(), + ); + bar.set_left(left); + bar.set_middle(middle); + bar.set_right(right); + } + } + + impl UserData for $name { + fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { + fields.add_field_function_get("left", |_, _| Ok($cfg.left.clone())); + fields.add_field_function_set("left", |_, _, style: EvalTo<String, Mode>| { + $cfg_mut.left = style; + Ok(()) + }); + + fields.add_field_function_get("middle", |_, _| Ok($cfg.middle.clone())); + fields.add_field_function_set("middle", |_, _, style: EvalTo<String, Mode>| { + $cfg_mut.middle = style; + Ok(()) + }); + + fields.add_field_function_get("right", |_, _| Ok($cfg.right.clone())); + fields.add_field_function_set("right", |_, _, style: EvalTo<String, Mode>| { + $cfg_mut.right = style; + Ok(()) + }); + } + } + }; +} + +pub(super) use BarState; diff --git a/src/state/mod.rs b/src/state/mod.rs index 5200da9..1f84716 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -8,6 +8,7 @@ pub mod editor; pub mod log; pub mod view; pub mod window; +pub(self) mod bar; #[derive(Debug, Default)] pub struct GlobalState { diff --git a/src/state/view/bar.rs b/src/state/view/bar.rs new file mode 100644 index 0000000..cc59e36 --- /dev/null +++ b/src/state/view/bar.rs @@ -0,0 +1,7 @@ +use crate::state::{bar::BarState, GlobalState}; + +BarState!( + SheetViewBarState, + GlobalState::instance().sheetview.bar, + GlobalState::instance_mut().sheetview.bar +); diff --git a/src/state/view/mod.rs b/src/state/view/mod.rs index 543644c..e45a3f4 100644 --- a/src/state/view/mod.rs +++ b/src/state/view/mod.rs @@ -2,9 +2,9 @@ use std::sync::{Arc, RwLock}; use mlua::{IntoLua, UserData, Value}; -use self::mode::Mode; +use self::{bar::SheetViewBarState, mode::Mode}; -use super::GlobalState; +use super::{GlobalState, DUMMY_STATE}; use crate::{ cursor::{Cursor, CursorMove}, sheet::{ @@ -15,6 +15,7 @@ use crate::{ }; pub mod mode; +pub mod bar; #[derive(Default, Debug)] pub struct SheetViewState { @@ -22,6 +23,7 @@ pub struct SheetViewState { active_sheet: Option<SheetId>, pub mode: Mode, pub selection_anchor: Option<(usize, usize)>, + pub bar: SheetViewBarState, } impl SheetViewState { @@ -31,6 +33,7 @@ impl SheetViewState { active_sheet: None, mode: Mode::Normal, selection_anchor: None, + bar: SheetViewBarState::new(), } } @@ -167,6 +170,10 @@ impl UserData for SheetViewState { } this.mode = mode; Ok(()) + }); + + fields.add_field_function_get("bar", |_, _| { + Ok(DUMMY_STATE.sheetview.bar) }) } diff --git a/src/state/view/mode.rs b/src/state/view/mode.rs index 3fa9c63..63e6e5b 100644 --- a/src/state/view/mode.rs +++ b/src/state/view/mode.rs @@ -18,15 +18,15 @@ impl<'lua> FromLua<'lua> for Mode { match value.as_str().unwrap().to_lowercase().as_ref() { "normal" => Ok(Mode::Normal), "visual" => Ok(Mode::Visual), - "insert" => Ok(Mode::Visual), - "command" => Ok(Mode::Visual), + "insert" => Ok(Mode::Insert), + "command" => Ok(Mode::Command), _ => Err(mlua::Error::runtime( - "mode needs to be 'normal', 'visual' or 'insert'", + "mode needs to be 'normal', 'visual', 'insert' or 'command'", )), } } else { Err(mlua::Error::runtime( - "mode needs to be 'normal', 'visual' or 'insert'", + "mode needs to be 'normal', 'visual' 'insert', or 'command'", )) } } @@ -39,8 +39,8 @@ impl<'lua> IntoLua<'lua> for Mode { ) -> mlua::prelude::LuaResult<mlua::prelude::LuaValue<'lua>> { match self { Mode::Normal => "normal", - Mode::Visual => "insert", - Mode::Insert => "visual", + Mode::Visual => "visual", + Mode::Insert => "insert", Mode::Command => "command", } .into_lua(lua) |