wip: move Palette out of zellij-tile

This commit is contained in:
denis 2021-03-27 15:30:53 +02:00
parent b71315b036
commit 6e276ae386
9 changed files with 108 additions and 96 deletions

3
Cargo.lock generated
View file

@ -2305,6 +2305,7 @@ dependencies = [
"walkdir",
"wasmer",
"wasmer-wasi",
"xrdb",
"zellij-tile",
]
@ -2312,10 +2313,8 @@ dependencies = [
name = "zellij-tile"
version = "0.5.0"
dependencies = [
"colors-transform",
"serde",
"serde_json",
"strum",
"strum_macros",
"xrdb",
]

View file

@ -37,6 +37,7 @@ lazy_static = "1.4.0"
wasmer = "1.0.0"
wasmer-wasi = "1.0.0"
interprocess = "1.0.1"
xrdb = "0.1.1"
colors-transform = "0.2.5"
zellij-tile = { path = "zellij-tile/", version = "0.5.0" }

View file

@ -1,7 +1,8 @@
use crate::common::{colors, Palette};
use crate::tab::Pane;
use ansi_term::Colour::RGB;
use std::collections::HashMap;
use zellij_tile::data::{colors, InputMode, Palette};
use zellij_tile::data::InputMode;
use std::fmt::{Display, Error, Formatter};
pub mod boundary_type {

View file

@ -1,7 +1,9 @@
//! `Tab`s holds multiple panes. It tracks their coordinates (x/y) and size,
//! as well as how they should be resized
use crate::common::{input::handler::parse_keys, AppInstruction, SenderWithContext};
use crate::common::{
colors, input::handler::parse_keys, AppInstruction, Palette, SenderWithContext,
};
use crate::layout::Layout;
use crate::panes::{PaneId, PositionAndSize, TerminalPane};
use crate::pty_bus::{PtyInstruction, VteEvent};
@ -14,7 +16,7 @@ use std::{
collections::{BTreeMap, HashSet},
};
use std::{io::Write, sync::mpsc::channel};
use zellij_tile::data::{colors, Event, InputMode, Palette};
use zellij_tile::data::{Event, InputMode};
const CURSOR_HEIGHT_WIDTH_RATIO: usize = 4; // this is not accurate and kind of a magic number, TODO: look into this
const MIN_TERMINAL_HEIGHT: usize = 2;

View file

@ -2,7 +2,7 @@
use super::actions::Action;
use super::keybinds::get_default_keybinds;
use crate::common::{AppInstruction, SenderWithContext, OPENCALLS};
use crate::common::{AppInstruction, Palette, SenderWithContext, OPENCALLS};
use crate::errors::ContextType;
use crate::os_input_output::OsApi;
use crate::pty_bus::PtyInstruction;
@ -11,7 +11,7 @@ use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting;
use termion::input::{TermRead, TermReadEventsAndRaw};
use zellij_tile::data::{Event, InputMode, Key, ModeInfo, Palette};
use zellij_tile::data::{Event, InputMode, Key, ModeInfo};
use super::keybinds::key_to_actions;

View file

@ -24,6 +24,7 @@ use std::{
use crate::cli::CliArgs;
use crate::layout::Layout;
use crate::panes::PaneId;
use colors_transform::{Color, Rgb};
use command_is_executing::CommandIsExecuting;
use directories_next::ProjectDirs;
use errors::{AppContext, ContextType, ErrorContext, PluginContext, PtyContext, ScreenContext};
@ -37,7 +38,8 @@ use wasm_vm::PluginEnv;
use wasm_vm::{wasi_stdout, wasi_write_string, zellij_imports, PluginInstruction};
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState};
use zellij_tile::data::{EventType, InputMode, Palette};
use xrdb::Colors;
use zellij_tile::data::{EventType, InputMode};
#[derive(Serialize, Deserialize, Debug)]
pub enum ApiCommand {
@ -115,6 +117,84 @@ pub enum AppInstruction {
Error(String),
}
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
}
}
/// Start Zellij with the specified [`OsApi`] and command-line arguments.
// FIXME this should definitely be modularized and split into different functions.
pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {

View file

@ -6,6 +6,7 @@ use std::str;
use std::sync::mpsc::Receiver;
use super::{AppInstruction, SenderWithContext};
use crate::common::Palette;
use crate::os_input_output::OsApi;
use crate::panes::PositionAndSize;
use crate::pty_bus::{PtyInstruction, VteEvent};
@ -13,7 +14,7 @@ use crate::tab::Tab;
use crate::{errors::ErrorContext, wasm_vm::PluginInstruction};
use crate::{layout::Layout, panes::PaneId};
use zellij_tile::data::{Event, InputMode, Palette, TabInfo};
use zellij_tile::data::{Event, InputMode, TabInfo};
/// Instructions that can be sent to the [`Screen`].
#[derive(Debug, Clone)]

View file

@ -10,6 +10,4 @@ license = "MIT"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = "0.20.0"
strum_macros = "0.20.0"
xrdb = "0.1.1"
colors-transform = "0.2.5"
strum_macros = "0.20.0"

View file

@ -1,89 +1,5 @@
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 {
@ -142,6 +58,20 @@ impl Default for InputMode {
}
}
#[derive(Clone, Copy, Default, 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),
}
/// 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.