summaryrefslogtreecommitdiffstats
path: root/zellij-tile/src
diff options
context:
space:
mode:
authordenis <denismaximov98@gmail.com>2021-03-27 14:43:16 +0200
committerdenis <denismaximov98@gmail.com>2021-03-27 14:43:16 +0200
commitb71315b03638c630b2e70dac1b464dd79d84d781 (patch)
tree00b2661efc4306c653ba61e3f7b16ddb3d615076 /zellij-tile/src
parent2c402b0b1d6a21220dca65835335bacfe3fe43e6 (diff)
parentd818661c727a59312e1a43432e84fbeec79fc145 (diff)
wip: latest plugin system merge in
Diffstat (limited to 'zellij-tile/src')
-rw-r--r--zellij-tile/src/data.rs162
-rw-r--r--zellij-tile/src/lib.rs49
-rw-r--r--zellij-tile/src/prelude.rs3
-rw-r--r--zellij-tile/src/shim.rs113
4 files changed, 201 insertions, 126 deletions
diff --git a/zellij-tile/src/data.rs b/zellij-tile/src/data.rs
new file mode 100644
index 000000000..315bc0911
--- /dev/null
+++ b/zellij-tile/src/data.rs
@@ -0,0 +1,162 @@
+use colors_transform::{Color, Rgb};
+use serde::{Deserialize, Serialize};
+use strum_macros::{EnumDiscriminants, EnumIter, EnumString, ToString};
+use xrdb::Colors;
+pub mod colors {
+ pub const WHITE: (u8, u8, u8) = (238, 238, 238);
+ pub const GREEN: (u8, u8, u8) = (175, 255, 0);
+ pub const GRAY: (u8, u8, u8) = (68, 68, 68);
+ pub const BRIGHT_GRAY: (u8, u8, u8) = (138, 138, 138);
+ pub const RED: (u8, u8, u8) = (135, 0, 0);
+ pub const BLACK: (u8, u8, u8) = (0, 0, 0);
+}
+
+#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
+pub struct Palette {
+ pub fg: (u8, u8, u8),
+ pub bg: (u8, u8, u8),
+ pub black: (u8, u8, u8),
+ pub red: (u8, u8, u8),
+ pub green: (u8, u8, u8),
+ pub yellow: (u8, u8, u8),
+ pub blue: (u8, u8, u8),
+ pub magenta: (u8, u8, u8),
+ pub cyan: (u8, u8, u8),
+ pub white: (u8, u8, u8),
+}
+
+impl Palette {
+ pub fn new() -> Self {
+ let palette = match Colors::new("xresources") {
+ Some(colors) => {
+ let fg = colors.fg.unwrap();
+ let fg_imm = &fg;
+ let fg_hex: &str = &fg_imm;
+ let fg = Rgb::from_hex_str(fg_hex).unwrap().as_tuple();
+ let fg = (fg.0 as u8, fg.1 as u8, fg.2 as u8);
+ let bg = colors.bg.unwrap();
+ let bg_imm = &bg;
+ let bg_hex: &str = &bg_imm;
+ let bg = Rgb::from_hex_str(bg_hex).unwrap().as_tuple();
+ let bg = (bg.0 as u8, bg.1 as u8, bg.2 as u8);
+ let colors: Vec<(u8, u8, u8)> = colors
+ .colors
+ .iter()
+ .map(|c| {
+ let c = c.clone();
+ let imm_str = &c.unwrap();
+ let hex_str: &str = &imm_str;
+ let rgb = Rgb::from_hex_str(hex_str).unwrap().as_tuple();
+ (rgb.0 as u8, rgb.1 as u8, rgb.2 as u8)
+ })
+ .collect();
+ Self {
+ fg,
+ bg,
+ black: colors[0],
+ red: colors[1],
+ green: colors[2],
+ yellow: colors[3],
+ blue: colors[4],
+ magenta: colors[5],
+ cyan: colors[6],
+ white: colors[7],
+ }
+ }
+ None => Self {
+ fg: colors::BRIGHT_GRAY,
+ bg: colors::BLACK,
+ black: colors::BLACK,
+ red: colors::RED,
+ green: colors::GREEN,
+ yellow: colors::GRAY,
+ blue: colors::GRAY,
+ magenta: colors::GRAY,
+ cyan: colors::GRAY,
+ white: colors::WHITE,
+ },
+ };
+ palette
+ }
+}
+impl Default for Palette {
+ fn default() -> Palette {
+ Palette::new()
+ }
+}
+
+#[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
+ Normal,
+ /// In `Locked` mode, input is always written to the terminal and all shortcuts are disabled
+ /// except the one leading back to normal mode
+ Locked,
+ /// `Resize` mode allows resizing the different existing panes.
+ Resize,
+ /// `Pane` mode allows creating and closing panes, as well as moving between them.
+ Pane,
+ /// `Tab` mode allows creating and closing tabs, as well as moving between them.
+ Tab,
+ /// `Scroll` mode allows scrolling up and down within a pane.
+ Scroll,
+ 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>
+ pub palette: Palette,
+}
+
+#[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 bad9ffa80..42a646a1f 100644
--- a/zellij-tile/src/shim.rs
+++ b/zellij-tile/src/shim.rs
@@ -1,123 +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,
-}
+use crate::data::*;
-// TODO: use same struct from main crate?
-#[derive(Default, Debug, Clone, Serialize, Deserialize)]
-pub struct Help {
- pub mode: InputMode,
- pub keybinds: Vec<(String, String)>,
- pub palette: Palette
-}
+// Subscription Handling
-// TODO: use same struct from main crate?
-#[derive(Debug, Clone, Deserialize, Serialize)]
-pub enum InputMode {
- Normal,
- Locked,
- Resize,
- Pane,
- Tab,
- RenameTab,
- Scroll,
- Exiting,
+pub fn subscribe(event_types: &[EventType]) {
+ println!("{}", serde_json::to_string(event_types).unwrap());
+ unsafe { host_subscribe() };
}
-#[derive(Debug, Clone, Deserialize, Serialize)]
-pub struct TabData {
- /* subset of fields to publish to plugins */
- pub position: usize,
- pub name: String,
- pub active: bool,
-}
-
-#[derive(Default, Debug, Copy, Clone, Deserialize, Serialize)]
-pub struct Palette {
- pub fg: (u8, u8, u8),
- pub bg: (u8, u8, u8),
- pub black: (u8, u8, u8),
- pub red: (u8, u8, u8),
- pub green: (u8, u8, u8),
- pub yellow: (u8, u8, u8),
- pub blue: (u8, u8, u8),
- pub magenta: (u8, u8, u8),
- pub cyan: (u8, u8, u8),
- pub white: (u8, u8, u8),
-}
-
-impl Default for InputMode {
- fn default() -> InputMode {
- InputMode::Normal
- }
-}
-
-pub fn get_key() -> Key {
- deserialize_from_stdin().unwrap()
-}
-pub fn get_palette() -> Palette {
- deserialize_from_stdin().unwrap()
+pub fn unsubscribe(event_types: &[EventType]) {
+ println!("{}", serde_json::to_string(event_types).unwrap());
+ unsafe { host_unsubscribe() };
}
-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();
}