diff options
author | a-kenji <aks.kenji@protonmail.com> | 2021-03-26 23:33:36 +0100 |
---|---|---|
committer | a-kenji <aks.kenji@protonmail.com> | 2021-03-26 23:33:36 +0100 |
commit | 84488a35aa2cef3505e9b40f7f0345cff73d62c8 (patch) | |
tree | 880d5019c40675177ea0402a186f6826d2bffa6f /zellij-tile/src | |
parent | 68737f78ac247814f2ad79e27d4f0da7c960b4a9 (diff) | |
parent | d818661c727a59312e1a43432e84fbeec79fc145 (diff) |
Merge branch 'main' of https://github.com/zellij-org/zellij into config-file
Diffstat (limited to 'zellij-tile/src')
-rw-r--r-- | zellij-tile/src/data.rs | 84 | ||||
-rw-r--r-- | zellij-tile/src/lib.rs | 49 | ||||
-rw-r--r-- | zellij-tile/src/prelude.rs | 3 | ||||
-rw-r--r-- | zellij-tile/src/shim.rs | 95 |
4 files changed, 123 insertions, 108 deletions
diff --git a/zellij-tile/src/data.rs b/zellij-tile/src/data.rs new file mode 100644 index 000000000..85d12245b --- /dev/null +++ b/zellij-tile/src/data.rs @@ -0,0 +1,84 @@ +use serde::{Deserialize, Serialize}; +use strum_macros::{EnumDiscriminants, EnumIter, EnumString, ToString}; + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Key { + Backspace, + Left, + Right, + Up, + Down, + Home, + End, + PageUp, + PageDown, + BackTab, + Delete, + Insert, + F(u8), + Char(char), + Alt(char), + Ctrl(char), + Null, + Esc, +} + +#[derive(Debug, Clone, EnumDiscriminants, ToString, Serialize, Deserialize)] +#[strum_discriminants(derive(EnumString, Hash, Serialize, Deserialize))] +#[strum_discriminants(name(EventType))] +pub enum Event { + ModeUpdate(ModeInfo), + TabUpdate(Vec<TabInfo>), + KeyPress(Key), +} + +/// Describes the different input modes, which change the way that keystrokes will be interpreted. +#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter, Serialize, Deserialize)] +pub enum InputMode { + /// In `Normal` mode, input is always written to the terminal, except for the shortcuts leading + /// to other modes + #[serde(alias = "normal")] + Normal, + /// In `Locked` mode, input is always written to the terminal and all shortcuts are disabled + /// except the one leading back to normal mode + #[serde(alias = "locked")] + Locked, + /// `Resize` mode allows resizing the different existing panes. + #[serde(alias = "resize")] + Resize, + /// `Pane` mode allows creating and closing panes, as well as moving between them. + #[serde(alias = "pane")] + Pane, + /// `Tab` mode allows creating and closing tabs, as well as moving between them. + #[serde(alias = "tab")] + Tab, + /// `Scroll` mode allows scrolling up and down within a pane. + #[serde(alias = "scroll")] + Scroll, + #[serde(alias = "renametab")] + RenameTab, +} + +impl Default for InputMode { + fn default() -> InputMode { + InputMode::Normal + } +} + +/// Represents the contents of the help message that is printed in the status bar, +/// which indicates the current [`InputMode`] and what the keybinds for that mode +/// are. Related to the default `status-bar` plugin. +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct ModeInfo { + pub mode: InputMode, + // FIXME: This should probably return Keys and Actions, then sort out strings plugin-side + pub keybinds: Vec<(String, String)>, // <shortcut> => <shortcut description> +} + +#[derive(Debug, Default, Clone, Deserialize, Serialize)] +pub struct TabInfo { + /* subset of fields to publish to plugins */ + pub position: usize, + pub name: String, + pub active: bool, +} diff --git a/zellij-tile/src/lib.rs b/zellij-tile/src/lib.rs index de87ee9c6..72211764b 100644 --- a/zellij-tile/src/lib.rs +++ b/zellij-tile/src/lib.rs @@ -1,14 +1,14 @@ -mod shim; +pub mod data; +pub mod prelude; +pub mod shim; + +use data::*; -pub use shim::*; #[allow(unused_variables)] pub trait ZellijTile { - fn init(&mut self) {} - fn draw(&mut self, rows: usize, cols: usize) {} - fn handle_key(&mut self, key: Key) {} - fn handle_global_key(&mut self, key: Key) {} - fn update_tabs(&mut self) {} - fn handle_tab_rename_keypress(&mut self, key: Key) {} + fn load(&mut self) {} + fn update(&mut self, event: Event) {} + fn render(&mut self, rows: usize, cols: usize) {} } #[macro_export] @@ -20,45 +20,22 @@ macro_rules! register_tile { fn main() { STATE.with(|state| { - state.borrow_mut().init(); + state.borrow_mut().load(); }); } #[no_mangle] - pub fn draw(rows: i32, cols: i32) { + pub fn update() { STATE.with(|state| { - state.borrow_mut().draw(rows as usize, cols as usize); + state.borrow_mut().update($crate::shim::object_from_stdin()); }); } #[no_mangle] - pub fn handle_key() { + pub fn render(rows: i32, cols: i32) { STATE.with(|state| { - state.borrow_mut().handle_key($crate::get_key()); + state.borrow_mut().render(rows as usize, cols as usize); }); } - - #[no_mangle] - pub fn handle_global_key() { - STATE.with(|state| { - state.borrow_mut().handle_global_key($crate::get_key()); - }); - } - - #[no_mangle] - pub fn update_tabs() { - STATE.with(|state| { - state.borrow_mut().update_tabs(); - }) - } - - #[no_mangle] - pub fn handle_tab_rename_keypress() { - STATE.with(|state| { - state - .borrow_mut() - .handle_tab_rename_keypress($crate::get_key()); - }) - } }; } diff --git a/zellij-tile/src/prelude.rs b/zellij-tile/src/prelude.rs new file mode 100644 index 000000000..2dd24a471 --- /dev/null +++ b/zellij-tile/src/prelude.rs @@ -0,0 +1,3 @@ +pub use crate::data::*; +pub use crate::shim::*; +pub use crate::*; diff --git a/zellij-tile/src/shim.rs b/zellij-tile/src/shim.rs index b7beb7afe..42a646a1f 100644 --- a/zellij-tile/src/shim.rs +++ b/zellij-tile/src/shim.rs @@ -1,105 +1,56 @@ -use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use serde::de::DeserializeOwned; use std::{io, path::Path}; -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub enum Key { - Backspace, - Left, - Right, - Up, - Down, - Home, - End, - PageUp, - PageDown, - BackTab, - Delete, - Insert, - F(u8), - Char(char), - Alt(char), - Ctrl(char), - Null, - Esc, -} - -// TODO: use same struct from main crate? -#[derive(Default, Debug, Clone, Serialize, Deserialize)] -pub struct Help { - pub mode: InputMode, - pub keybinds: Vec<(String, String)>, -} +use crate::data::*; -// TODO: use same struct from main crate? -#[derive(Debug, Clone, Deserialize, Serialize)] -pub enum InputMode { - Normal, - Locked, - Resize, - Pane, - Tab, - RenameTab, - Scroll, - Exiting, -} +// Subscription Handling -#[derive(Debug, Clone, Deserialize, Serialize)] -pub struct TabData { - /* subset of fields to publish to plugins */ - pub position: usize, - pub name: String, - pub active: bool, +pub fn subscribe(event_types: &[EventType]) { + println!("{}", serde_json::to_string(event_types).unwrap()); + unsafe { host_subscribe() }; } -impl Default for InputMode { - fn default() -> InputMode { - InputMode::Normal - } +pub fn unsubscribe(event_types: &[EventType]) { + println!("{}", serde_json::to_string(event_types).unwrap()); + unsafe { host_unsubscribe() }; } -pub fn get_key() -> Key { - deserialize_from_stdin().unwrap() -} - -pub fn open_file(path: &Path) { - println!("{}", path.to_string_lossy()); - unsafe { host_open_file() }; -} +// Plugin Settings pub fn set_max_height(max_height: i32) { unsafe { host_set_max_height(max_height) }; } pub fn set_invisible_borders(invisible_borders: bool) { - let invisible_borders = if invisible_borders { 1 } else { 0 }; - unsafe { host_set_invisible_borders(invisible_borders) }; + unsafe { host_set_invisible_borders(if invisible_borders { 1 } else { 0 }) }; } pub fn set_selectable(selectable: bool) { - let selectable = if selectable { 1 } else { 0 }; - unsafe { host_set_selectable(selectable) }; + unsafe { host_set_selectable(if selectable { 1 } else { 0 }) }; } -pub fn get_help() -> Help { - unsafe { host_get_help() }; - deserialize_from_stdin().unwrap_or_default() -} +// Host Functions -pub fn get_tabs() -> Vec<TabData> { - deserialize_from_stdin().unwrap_or_default() +pub fn open_file(path: &Path) { + println!("{}", path.to_string_lossy()); + unsafe { host_open_file() }; } -fn deserialize_from_stdin<T: DeserializeOwned>() -> Option<T> { +// Internal Functions + +#[doc(hidden)] +pub fn object_from_stdin<T: DeserializeOwned>() -> T { let mut json = String::new(); io::stdin().read_line(&mut json).unwrap(); - serde_json::from_str(&json).ok() + serde_json::from_str(&json).unwrap() } #[link(wasm_import_module = "zellij")] extern "C" { + fn host_subscribe(); + fn host_unsubscribe(); fn host_open_file(); fn host_set_max_height(max_height: i32); fn host_set_selectable(selectable: i32); fn host_set_invisible_borders(invisible_borders: i32); - fn host_get_help(); } |