summaryrefslogtreecommitdiffstats
path: root/zellij-tile
diff options
context:
space:
mode:
authorhar7an <99636919+har7an@users.noreply.github.com>2022-07-06 14:06:56 +0000
committerGitHub <noreply@github.com>2022-07-06 16:06:56 +0200
commitc26a6bcf566d1a332ab795107852be004669d683 (patch)
tree3dcb45725aeedb82e37d8b0bf24801fbbd76e757 /zellij-tile
parent5a40f42652d02145f28f04586b3f62427963417b (diff)
refactor(crates): move shared contents from zellij tile to zellij utils (#1541)
* zellij-tile: Move `data` to zellij-utils The rationale behind this is that all components of zellij access the data structures defined in this module, as they define some of the most basic types in the application. However, so far zellij-tile is treated like a separate crate from the rest of the program in that it is the only one that doesn't have access to `zellij-utils`, which contains a lot of other data structures used throughout zellij. This poses issues as discussed in https://github.com/zellij-org/zellij/pull/1242 and is one of the reasons why the keybindings in the status bar default plugin can't be updated dynamically. It is also the main reason for why the keybindings are currently passed to the plugin as strings: The plugins only have access to `zellij-tile`, but since this is a dependency of `zellij-utils`, it can't import `zellij-utils` to access the keybindings. Other weird side-effect are that in some places `server` and `client` have to access the `zellij-tile` contents "through" `zellij-utils`, as in `use zellij_utils::zellij_tile::prelude::*`. By moving these central data structures to one common shared crate (`zellij-utils`), `zellij-tile` will be enabled to import `zellij-utils` like `screen` and `client` already do. This will, next to other things, allow dropping a lot of `std::fmt::Fmt` impls needed to convert core data structures into strings and as a consequence, a lot of string parsing in the first place. * utils: Integrate new `data` module, bump rust ver Integrates the `data` module that was previously part of `zellij-tile` to allow sharing the contained data structures between all components of zellij. This allows `zellij-tile` to use `utils` as a dependency. However, since `tile` is build against the wasm target, it cannot include all of `zellij-utils`, since a lot of dependencies there cannot compile with `wasm` as target (Examples include: termwiz, log4rs, async-std). Thus we make all the dependencies that cannot compile against `wasm` optional and introduce a new feature `full` that will compile the crate with all dependencies. Along with this, modify `lib.rs` to include most of the data structures only when compiling against the `full` feature. This makes the compiles of `zellij-tile` lighter, as it doesn't include all of `utils`. As a side effect, due to the dependency notation for the optional dependencies (See https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies), we bump the rust toolchain version to 1.60.0. * tile: Import `data` from zellij-utils Add `zellij-utils` as a dependency to `zellij-tile` and allow us access to the `data` module defined there. Update the re-export in the `prelude` such that from all of the plugins points of view *absolutely nothing changes*. * utils: Fix `data` module dependency Since the `data` module has been migrated from `zellij-tile` to `zellij-utils`, we import it from `zellij-utils` directly now. Also unify the imports for the `data` module members: We import all of the through `data::` now, not through a mixture of `data::` and `prelude::`. * client: Fix `data` module dependency Since the `data` module has been migrated from `zellij-tile` to `zellij-utils`, we import it from `zellij-utils` directly now. Also unify the imports for the `data` module members: We import all of the through `data::` now, not through a mixture of `data::` and `prelude::`. Add the "full" feature flag to the `zellij-utils` dependency so it includes all the components we need. * server: Fix `data` module dependency Since the `data` module has been migrated from `zellij-tile` to `zellij-utils`, we import it from `zellij-utils` directly now. Also unify the imports for the `data` module members: We import all of the through `data::` now, not through a mixture of `data::` and `prelude::`. Add the "full" feature flag to the `zellij-utils` dependency so it includes all the components we need. * tests: Fix `data` module dependency Since the `data` module has been migrated from `zellij-tile` to `zellij-utils`, we import it from `zellij-utils` directly now. * utils: Remove "full" feature in favor of conditional compilation using `target_family`. Replace the rust 1.60 method of specifying optional dependencies based on features and optionally include the dependencies only when not building for wasm instead. (I.e. `cfg(not(target_family = "wasm"))`) * cargo: Update module dependencies since `client`, `server` and `tile` now all depend on `utils` only.
Diffstat (limited to 'zellij-tile')
-rw-r--r--zellij-tile/Cargo.toml1
-rw-r--r--zellij-tile/src/data.rs302
-rw-r--r--zellij-tile/src/lib.rs3
-rw-r--r--zellij-tile/src/prelude.rs2
-rw-r--r--zellij-tile/src/shim.rs3
5 files changed, 4 insertions, 307 deletions
diff --git a/zellij-tile/Cargo.toml b/zellij-tile/Cargo.toml
index 2c4d374ef..b593c158e 100644
--- a/zellij-tile/Cargo.toml
+++ b/zellij-tile/Cargo.toml
@@ -12,3 +12,4 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = "0.20.0"
strum_macros = "0.20.0"
+zellij-utils = { path = "../zellij-utils/", version = "0.31.0" }
diff --git a/zellij-tile/src/data.rs b/zellij-tile/src/data.rs
deleted file mode 100644
index c50ef4790..000000000
--- a/zellij-tile/src/data.rs
+++ /dev/null
@@ -1,302 +0,0 @@
-use clap::ArgEnum;
-use serde::{Deserialize, Serialize};
-use std::fmt;
-use std::str::FromStr;
-use strum_macros::{EnumDiscriminants, EnumIter, EnumString, ToString};
-
-pub type ClientId = u16; // TODO: merge with crate type?
-
-pub fn client_id_to_colors(
- client_id: ClientId,
- colors: Palette,
-) -> Option<(PaletteColor, PaletteColor)> {
- // (primary color, secondary color)
- match client_id {
- 1 => Some((colors.magenta, colors.black)),
- 2 => Some((colors.blue, colors.black)),
- 3 => Some((colors.purple, colors.black)),
- 4 => Some((colors.yellow, colors.black)),
- 5 => Some((colors.cyan, colors.black)),
- 6 => Some((colors.gold, colors.black)),
- 7 => Some((colors.red, colors.black)),
- 8 => Some((colors.silver, colors.black)),
- 9 => Some((colors.pink, colors.black)),
- 10 => Some((colors.brown, colors.black)),
- _ => None,
- }
-}
-
-pub fn single_client_color(colors: Palette) -> (PaletteColor, PaletteColor) {
- (colors.green, colors.black)
-}
-
-#[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(CharOrArrow),
- Ctrl(char),
- Null,
- Esc,
-}
-
-#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
-#[serde(untagged)]
-pub enum CharOrArrow {
- Char(char),
- Direction(Direction),
-}
-
-/// The four directions (left, right, up, down).
-#[derive(Eq, Clone, Copy, Debug, PartialEq, Hash, Deserialize, Serialize)]
-pub enum Direction {
- Left,
- Right,
- Up,
- Down,
-}
-
-#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
-// FIXME: This should be extended to handle different button clicks (not just
-// left click) and the `ScrollUp` and `ScrollDown` events could probably be
-// merged into a single `Scroll(isize)` event.
-pub enum Mouse {
- ScrollUp(usize), // number of lines
- ScrollDown(usize), // number of lines
- LeftClick(isize, usize), // line and column
- RightClick(isize, usize), // line and column
- Hold(isize, usize), // line and column
- Release(isize, usize), // line and column
-}
-
-#[derive(Debug, Clone, PartialEq, EnumDiscriminants, ToString, Serialize, Deserialize)]
-#[strum_discriminants(derive(EnumString, Hash, Serialize, Deserialize))]
-#[strum_discriminants(name(EventType))]
-#[non_exhaustive]
-pub enum Event {
- ModeUpdate(ModeInfo),
- TabUpdate(Vec<TabInfo>),
- Key(Key),
- Mouse(Mouse),
- Timer(f64),
- CopyToClipboard(CopyDestination),
- SystemClipboardFailure,
- InputReceived,
- Visible(bool),
-}
-
-/// Describes the different input modes, which change the way that keystrokes will be interpreted.
-#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter, Serialize, Deserialize, ArgEnum)]
-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,
- /// `RenameTab` mode allows assigning a new name to a tab.
- #[serde(alias = "renametab")]
- RenameTab,
- /// `RenamePane` mode allows assigning a new name to a pane.
- #[serde(alias = "renamepane")]
- RenamePane,
- /// `Session` mode allows detaching sessions
- #[serde(alias = "session")]
- Session,
- /// `Move` mode allows moving the different existing panes within a tab
- #[serde(alias = "move")]
- Move,
- /// `Prompt` mode allows interacting with active prompts.
- #[serde(alias = "prompt")]
- Prompt,
- /// `Tmux` mode allows for basic tmux keybindings functionality
- #[serde(alias = "tmux")]
- Tmux,
-}
-
-impl Default for InputMode {
- fn default() -> InputMode {
- InputMode::Normal
- }
-}
-
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
-pub enum ThemeHue {
- Light,
- Dark,
-}
-impl Default for ThemeHue {
- fn default() -> ThemeHue {
- ThemeHue::Dark
- }
-}
-
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
-pub enum PaletteColor {
- Rgb((u8, u8, u8)),
- EightBit(u8),
-}
-impl Default for PaletteColor {
- fn default() -> PaletteColor {
- PaletteColor::EightBit(0)
- }
-}
-
-impl FromStr for InputMode {
- type Err = Box<dyn std::error::Error>;
-
- fn from_str(s: &str) -> Result<Self, Self::Err> {
- match s {
- "normal" => Ok(InputMode::Normal),
- "resize" => Ok(InputMode::Resize),
- "locked" => Ok(InputMode::Locked),
- "pane" => Ok(InputMode::Pane),
- "tab" => Ok(InputMode::Tab),
- "scroll" => Ok(InputMode::Scroll),
- "renametab" => Ok(InputMode::RenameTab),
- "session" => Ok(InputMode::Session),
- "move" => Ok(InputMode::Move),
- "tmux" => Ok(InputMode::Tmux),
- "prompt" => Ok(InputMode::Prompt),
- "renamepane" => Ok(InputMode::RenamePane),
- e => Err(e.to_string().into()),
- }
- }
-}
-
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
-pub enum PaletteSource {
- Default,
- Xresources,
-}
-impl Default for PaletteSource {
- fn default() -> PaletteSource {
- PaletteSource::Default
- }
-}
-#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
-pub struct Palette {
- pub source: PaletteSource,
- pub theme_hue: ThemeHue,
- pub fg: PaletteColor,
- pub bg: PaletteColor,
- pub black: PaletteColor,
- pub red: PaletteColor,
- pub green: PaletteColor,
- pub yellow: PaletteColor,
- pub blue: PaletteColor,
- pub magenta: PaletteColor,
- pub cyan: PaletteColor,
- pub white: PaletteColor,
- pub orange: PaletteColor,
- pub gray: PaletteColor,
- pub purple: PaletteColor,
- pub gold: PaletteColor,
- pub silver: PaletteColor,
- pub pink: PaletteColor,
- pub brown: PaletteColor,
-}
-
-#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
-pub struct Style {
- pub colors: Palette,
- pub rounded_corners: bool,
-}
-
-/// 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, PartialEq, Eq, Hash, 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 style: Style,
- pub capabilities: PluginCapabilities,
- pub session_name: Option<String>,
-}
-
-#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
-pub struct TabInfo {
- /* subset of fields to publish to plugins */
- pub position: usize,
- pub name: String,
- pub active: bool,
- pub panes_to_hide: usize,
- pub is_fullscreen_active: bool,
- pub is_sync_panes_active: bool,
- pub are_floating_panes_visible: bool,
- pub other_focused_clients: Vec<ClientId>,
-}
-
-#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
-pub struct PluginIds {
- pub plugin_id: u32,
- pub zellij_pid: u32,
-}
-
-/// Tag used to identify the plugin in layout and config yaml files
-#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
-pub struct PluginTag(String);
-
-impl PluginTag {
- pub fn new(url: impl Into<String>) -> Self {
- PluginTag(url.into())
- }
-}
-
-impl From<PluginTag> for String {
- fn from(tag: PluginTag) -> Self {
- tag.0
- }
-}
-
-impl fmt::Display for PluginTag {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "{}", self.0)
- }
-}
-
-#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
-pub struct PluginCapabilities {
- pub arrow_fonts: bool,
-}
-
-impl Default for PluginCapabilities {
- fn default() -> PluginCapabilities {
- PluginCapabilities { arrow_fonts: true }
- }
-}
-
-#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
-pub enum CopyDestination {
- Command,
- Primary,
- System,
-}
diff --git a/zellij-tile/src/lib.rs b/zellij-tile/src/lib.rs
index 96483c3c3..85a5bfe1f 100644
--- a/zellij-tile/src/lib.rs
+++ b/zellij-tile/src/lib.rs
@@ -1,8 +1,7 @@
-pub mod data;
pub mod prelude;
pub mod shim;
-use data::*;
+use zellij_utils::data::Event;
#[allow(unused_variables)]
pub trait ZellijPlugin {
diff --git a/zellij-tile/src/prelude.rs b/zellij-tile/src/prelude.rs
index 2dd24a471..196696a4c 100644
--- a/zellij-tile/src/prelude.rs
+++ b/zellij-tile/src/prelude.rs
@@ -1,3 +1,3 @@
-pub use crate::data::*;
pub use crate::shim::*;
pub use crate::*;
+pub use zellij_utils::data::*;
diff --git a/zellij-tile/src/shim.rs b/zellij-tile/src/shim.rs
index fe41a21b7..99920e744 100644
--- a/zellij-tile/src/shim.rs
+++ b/zellij-tile/src/shim.rs
@@ -1,7 +1,6 @@
use serde::{de::DeserializeOwned, Serialize};
use std::{io, path::Path};
-
-use crate::data::*;
+use zellij_utils::data::*;
// Subscription Handling