From 06bce9a1fd88651a2e85c120776bc07399206806 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 9 Mar 2021 19:39:42 +0000 Subject: [PATCH] Deduplicate the WASM interface structs --- Cargo.lock | 4 +- Cargo.toml | 4 +- default-tiles/status-bar/src/main.rs | 4 +- default-tiles/strider/src/main.rs | 2 +- default-tiles/tab-bar/src/main.rs | 2 +- src/client/tab.rs | 9 ---- src/common/input/actions.rs | 4 +- src/common/input/handler.rs | 39 +--------------- src/common/input/keybinds.rs | 2 +- src/common/mod.rs | 17 ++++--- src/common/screen.rs | 4 +- src/common/wasm_vm.rs | 2 +- zellij-tile/Cargo.toml | 4 +- zellij-tile/src/data.rs | 68 ++++++++++++++++++++++++++++ zellij-tile/src/lib.rs | 15 ++++-- zellij-tile/src/prelude.rs | 3 ++ zellij-tile/src/shim.rs | 58 +----------------------- 17 files changed, 111 insertions(+), 130 deletions(-) create mode 100644 zellij-tile/src/data.rs create mode 100644 zellij-tile/src/prelude.rs diff --git a/Cargo.lock b/Cargo.lock index 1a070709..6b20bdc9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2258,7 +2258,6 @@ dependencies = [ "strip-ansi-escapes", "structopt", "strum", - "strum_macros", "termion_temporary_zellij_fork", "termios", "toml", @@ -2268,6 +2267,7 @@ dependencies = [ "walkdir", "wasmer", "wasmer-wasi", + "zellij-tile", ] [[package]] @@ -2276,4 +2276,6 @@ version = "0.5.0" dependencies = [ "serde", "serde_json", + "strum", + "strum_macros", ] diff --git a/Cargo.toml b/Cargo.toml index 1d9ac68f..b5266a8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,17 +27,17 @@ signal-hook = "0.1.10" strip-ansi-escapes = "0.1.0" structopt = "0.3" # termion = { git = "https://gitlab.com/TheLostLambda/termion.git", version = "1.6.0", features = ["serde"] } -termion = { package = "termion_temporary_zellij_fork" , version = "1.6.0", features = ["serde"]} +termion = { package = "termion_temporary_zellij_fork", version = "1.6.0", features = ["serde"]} termios = "0.3" unicode-truncate = "0.2.0" unicode-width = "0.1.8" vte = "0.8.0" strum = "0.20.0" -strum_macros = "0.20.0" lazy_static = "1.4.0" wasmer = "1.0.0" wasmer-wasi = "1.0.0" interprocess = "1.0.1" +zellij-tile = { path = "zellij-tile/", version = "0.5.0" } [dependencies.async-std] version = "1.3.0" diff --git a/default-tiles/status-bar/src/main.rs b/default-tiles/status-bar/src/main.rs index b34fa69b..68f56b7f 100644 --- a/default-tiles/status-bar/src/main.rs +++ b/default-tiles/status-bar/src/main.rs @@ -1,6 +1,6 @@ use colored::*; use std::fmt::{Display, Error, Formatter}; -use zellij_tile::*; +use zellij_tile::prelude::*; // for more of these, copy paste from: https://en.wikipedia.org/wiki/Box-drawing_character static ARROW_SEPARATOR: &str = " "; @@ -152,7 +152,7 @@ fn key_path(help: &Help) -> LinePart { len, ) } - InputMode::Normal | _ => { + InputMode::Normal => { let key_path = superkey_text.on_green(); let separator = ARROW_SEPARATOR.green().on_black(); ( diff --git a/default-tiles/strider/src/main.rs b/default-tiles/strider/src/main.rs index 415d5217..81a31ce1 100644 --- a/default-tiles/strider/src/main.rs +++ b/default-tiles/strider/src/main.rs @@ -3,7 +3,7 @@ mod state; use colored::*; use state::{FsEntry, State}; use std::{cmp::min, fs::read_dir}; -use zellij_tile::*; +use zellij_tile::prelude::*; register_tile!(State); diff --git a/default-tiles/tab-bar/src/main.rs b/default-tiles/tab-bar/src/main.rs index 4c49812f..5220509b 100644 --- a/default-tiles/tab-bar/src/main.rs +++ b/default-tiles/tab-bar/src/main.rs @@ -1,7 +1,7 @@ mod line; mod tab; -use zellij_tile::*; +use zellij_tile::prelude::*; use crate::line::tab_line; use crate::tab::tab_style; diff --git a/src/client/tab.rs b/src/client/tab.rs index 1ef0b720..8ee3775b 100644 --- a/src/client/tab.rs +++ b/src/client/tab.rs @@ -8,7 +8,6 @@ use crate::pty_bus::{PtyInstruction, VteEvent}; use crate::wasm_vm::{PluginInputType, PluginInstruction}; use crate::{boundaries::Boundaries, panes::PluginPane}; use crate::{os_input_output::OsApi, utils::shared::pad_to_size}; -use serde::{Deserialize, Serialize}; use std::os::unix::io::RawFd; use std::{ cmp::Reverse, @@ -67,14 +66,6 @@ pub struct Tab { expansion_boundary: Option, } -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -pub struct TabData { - /* subset of fields to publish to plugins */ - pub position: usize, - pub name: String, - pub active: bool, -} - // FIXME: Use a struct that has a pane_type enum, to reduce all of the duplication pub trait Pane { fn x(&self) -> usize; diff --git a/src/common/input/actions.rs b/src/common/input/actions.rs index 88a7036f..059255d3 100644 --- a/src/common/input/actions.rs +++ b/src/common/input/actions.rs @@ -1,6 +1,6 @@ //! Definition of the actions that can be bound to keys. -use super::handler; +use zellij_tile::data::InputMode; /// The four directions (left, right, up, down). #[derive(Clone, Debug)] @@ -19,7 +19,7 @@ pub enum Action { /// Write to the terminal. Write(Vec), /// Switch to the specified input mode. - SwitchToMode(handler::InputMode), + SwitchToMode(InputMode), /// Resize focus pane in specified direction. Resize(Direction), /// Switch focus to next pane in specified direction. diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 52db9b4f..e498fbb6 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -10,9 +10,8 @@ use crate::screen::ScreenInstruction; use crate::wasm_vm::{EventType, PluginInputType, PluginInstruction}; use crate::CommandIsExecuting; -use serde::{Deserialize, Serialize}; -use strum_macros::EnumIter; use termion::input::TermReadEventsAndRaw; +use zellij_tile::data::{Help, InputMode}; use super::keybinds::key_to_actions; @@ -269,42 +268,6 @@ impl InputHandler { } } -/// 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 one special input that - /// triggers the switch to [`InputMode::Command`] mode. - Normal, - /// In `Command` mode, input is bound to actions (more precisely, sequences of actions). - /// `Command` mode gives access to the other modes non-`InputMode::Normal` modes. - /// etc. - Command, - /// `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, -} - -/// 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 Help { - pub mode: InputMode, - pub keybinds: Vec<(String, String)>, // => -} - -impl Default for InputMode { - fn default() -> InputMode { - InputMode::Normal - } -} - /// Creates a [`Help`] struct indicating the current [`InputMode`] and its keybinds /// (as pairs of [`String`]s). // TODO this should probably be automatically generated in some way diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs index 46abf4ab..da0f1c15 100644 --- a/src/common/input/keybinds.rs +++ b/src/common/input/keybinds.rs @@ -1,12 +1,12 @@ //! Mapping of inputs to sequences of actions. use super::actions::{Action, Direction}; -use super::handler::InputMode; use std::collections::HashMap; use strum::IntoEnumIterator; use termion::event::Key; +use zellij_tile::data::*; type Keybinds = HashMap; type ModeKeybinds = HashMap>; diff --git a/src/common/mod.rs b/src/common/mod.rs index 7b19bcef..9c41e131 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -16,27 +16,26 @@ use std::thread; use std::{cell::RefCell, sync::mpsc::TrySendError}; use std::{collections::HashMap, fs}; -use crate::panes::PaneId; -use directories_next::ProjectDirs; -use input::handler::InputMode; -use serde::{Deserialize, Serialize}; -use termion::input::TermRead; -use wasm_vm::PluginEnv; -use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; -use wasmer_wasi::{Pipe, WasiState}; - use crate::cli::CliArgs; use crate::layout::Layout; +use crate::panes::PaneId; use command_is_executing::CommandIsExecuting; +use directories_next::ProjectDirs; use errors::{AppContext, ContextType, ErrorContext, PluginContext, PtyContext, ScreenContext}; use input::handler::input_loop; use os_input_output::OsApi; use pty_bus::{PtyBus, PtyInstruction}; use screen::{Screen, ScreenInstruction}; +use serde::{Deserialize, Serialize}; +use termion::input::TermRead; use utils::consts::{ZELLIJ_IPC_PIPE, ZELLIJ_ROOT_PLUGIN_DIR}; +use wasm_vm::PluginEnv; use wasm_vm::{ wasi_stdout, wasi_write_string, zellij_imports, EventType, PluginInputType, PluginInstruction, }; +use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; +use wasmer_wasi::{Pipe, WasiState}; +use zellij_tile::data::InputMode; #[derive(Serialize, Deserialize, Debug)] pub enum ApiCommand { diff --git a/src/common/screen.rs b/src/common/screen.rs index 199a1223..b848cc49 100644 --- a/src/common/screen.rs +++ b/src/common/screen.rs @@ -9,10 +9,12 @@ use super::{AppInstruction, SenderWithContext}; use crate::os_input_output::OsApi; use crate::panes::PositionAndSize; use crate::pty_bus::{PtyInstruction, VteEvent}; -use crate::tab::{Tab, TabData}; +use crate::tab::Tab; use crate::{errors::ErrorContext, wasm_vm::PluginInstruction}; use crate::{layout::Layout, panes::PaneId}; +use zellij_tile::data::TabData; + /// Instructions that can be sent to the [`Screen`]. #[derive(Debug, Clone)] pub enum ScreenInstruction { diff --git a/src/common/wasm_vm.rs b/src/common/wasm_vm.rs index 15b920ad..24e8d2a6 100644 --- a/src/common/wasm_vm.rs +++ b/src/common/wasm_vm.rs @@ -1,4 +1,3 @@ -use crate::tab::TabData; use serde::{Deserialize, Serialize}; use std::{ path::PathBuf, @@ -6,6 +5,7 @@ use std::{ }; use wasmer::{imports, Function, ImportObject, Store, WasmerEnv}; use wasmer_wasi::WasiEnv; +use zellij_tile::data::TabData; use super::{ input::handler::get_help, pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction, diff --git a/zellij-tile/Cargo.toml b/zellij-tile/Cargo.toml index bdabb175..eb9a2d59 100644 --- a/zellij-tile/Cargo.toml +++ b/zellij-tile/Cargo.toml @@ -8,4 +8,6 @@ license = "MIT" [dependencies] serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" +strum = "0.20.0" +strum_macros = "0.20.0" \ No newline at end of file diff --git a/zellij-tile/src/data.rs b/zellij-tile/src/data.rs new file mode 100644 index 00000000..b64f1e4e --- /dev/null +++ b/zellij-tile/src/data.rs @@ -0,0 +1,68 @@ +use serde::{Deserialize, Serialize}; +use strum_macros::EnumIter; + +#[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, +} + +/// 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 one special input that + /// triggers the switch to [`InputMode::Command`] mode. + Normal, + /// In `Command` mode, input is bound to actions (more precisely, sequences of actions). + /// `Command` mode gives access to the other modes non-`InputMode::Normal` modes. + /// etc. + Command, + /// `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 Help { + pub mode: InputMode, + pub keybinds: Vec<(String, String)>, // => +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct TabData { + /* 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 51ec9680..ea38f6c9 100644 --- a/zellij-tile/src/lib.rs +++ b/zellij-tile/src/lib.rs @@ -1,6 +1,9 @@ -mod shim; +pub mod data; +pub mod prelude; +pub mod shim; + +use data::*; -pub use shim::*; #[allow(unused_variables)] pub trait ZellijTile { fn load(&mut self) {} @@ -34,14 +37,16 @@ macro_rules! register_tile { #[no_mangle] pub fn handle_key() { STATE.with(|state| { - state.borrow_mut().handle_key($crate::get_key()); + state.borrow_mut().handle_key($crate::shim::get_key()); }); } #[no_mangle] pub fn handle_global_key() { STATE.with(|state| { - state.borrow_mut().handle_global_key($crate::get_key()); + state + .borrow_mut() + .handle_global_key($crate::shim::get_key()); }); } @@ -57,7 +62,7 @@ macro_rules! register_tile { STATE.with(|state| { state .borrow_mut() - .handle_tab_rename_keypress($crate::get_key()); + .handle_tab_rename_keypress($crate::shim::get_key()); }) } }; diff --git a/zellij-tile/src/prelude.rs b/zellij-tile/src/prelude.rs new file mode 100644 index 00000000..2dd24a47 --- /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 20185c72..bb038e82 100644 --- a/zellij-tile/src/shim.rs +++ b/zellij-tile/src/shim.rs @@ -1,61 +1,7 @@ -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)>, -} - -// TODO: use same struct from main crate? -#[derive(Debug, Clone, Deserialize, Serialize)] -pub enum InputMode { - Normal, - Command, - Resize, - Pane, - Tab, - RenameTab, - Scroll, - Exiting, -} - -#[derive(Debug, Clone, Deserialize, Serialize)] -pub struct TabData { - /* subset of fields to publish to plugins */ - pub position: usize, - pub name: String, - pub active: bool, -} - -impl Default for InputMode { - fn default() -> InputMode { - InputMode::Normal - } -} +use crate::data::*; pub fn get_key() -> Key { deserialize_from_stdin().unwrap()