chore: cleanup, fix: the tests finally pass

This commit is contained in:
denis 2021-05-01 14:44:43 +03:00
parent eabecde90b
commit f85822972a
14 changed files with 140 additions and 98 deletions

1
Cargo.lock generated
View file

@ -2254,6 +2254,7 @@ dependencies = [
name = "zellij-tile"
version = "1.1.0"
dependencies = [
"ansi_term 0.12.1",
"serde",
"serde_json",
"strum",

View file

@ -119,19 +119,6 @@ impl ColoredElements {
}
}
macro_rules! rgb {
($a:expr) => {
RGB($a.0, $a.1, $a.2)
};
}
macro_rules! style {
($a:expr, $b:expr) => {
Style::new()
.fg(RGB($a.0, $a.1, $a.2))
.on(RGB($b.0, $b.1, $b.2))
};
}
// I really hate this, but I can't come up with a good solution for this,
// we need different colors from palette for the default theme
// plus here we can add new sources in the future, like Theme

View file

@ -223,13 +223,25 @@ fn quicknav_short(palette: Palette) -> LinePart {
part: format!(
"{}{}{}{}{}{}{}{}",
text_first_part,
Style::new().fg(RGB(palette.orange.0, palette.orange.1, palette.orange.2)).bold().paint(alt),
Style::new()
.fg(RGB(palette.orange.0, palette.orange.1, palette.orange.2))
.bold()
.paint(alt),
text_second_part,
Style::new().fg(RGB(palette.green.0, palette.green.1, palette.green.2)).bold().paint(new_pane_shortcut),
Style::new()
.fg(RGB(palette.green.0, palette.green.1, palette.green.2))
.bold()
.paint(new_pane_shortcut),
text_third_part,
Style::new().fg(RGB(palette.green.0, palette.green.1, palette.green.2)).bold().paint(brackets_navigation),
Style::new()
.fg(RGB(palette.green.0, palette.green.1, palette.green.2))
.bold()
.paint(brackets_navigation),
text_fifth_part,
Style::new().fg(RGB(palette.green.0, palette.green.1, palette.green.2)).bold().paint(hjkl_navigation),
Style::new()
.fg(RGB(palette.green.0, palette.green.1, palette.green.2))
.bold()
.paint(hjkl_navigation),
),
len,
}

View file

@ -1,15 +1,7 @@
use ansi_term::{ANSIStrings, Color::RGB, Style};
use ansi_term::ANSIStrings;
use crate::{LinePart, ARROW_SEPARATOR};
use zellij_tile::data::{colors::*, Palette};
macro_rules! style {
($a:expr, $b:expr) => {
Style::new()
.fg(RGB($a.0, $a.1, $a.2))
.on(RGB($b.0, $b.1, $b.2))
};
}
use zellij_tile::prelude::*;
fn get_current_title_len(current_title: &[LinePart]) -> usize {
current_title

View file

@ -59,7 +59,7 @@ impl ZellijPlugin for State {
t.active,
t.position,
t.is_sync_panes_active,
self.mode_info.palette
self.mode_info.palette,
);
all_tabs.push(tab);
}

View file

@ -1,14 +1,6 @@
use crate::{LinePart, ARROW_SEPARATOR};
use ansi_term::{ANSIStrings, Color::RGB, Style};
use zellij_tile::data::Palette;
macro_rules! style {
($a:expr, $b:expr) => {
Style::new()
.fg(RGB($a.0, $a.1, $a.2))
.on(RGB($b.0, $b.1, $b.2))
};
}
use zellij_tile::prelude::*;
pub fn active_tab(text: String, palette: Palette) -> LinePart {
let left_separator = style!(palette.bg, palette.green).paint(ARROW_SEPARATOR);
@ -49,7 +41,7 @@ pub fn tab_style(
is_active_tab: bool,
position: usize,
is_sync_panes_active: bool,
palette: Palette
palette: Palette,
) -> LinePart {
let mut tab_text = if text.is_empty() {
format!("Tab #{}", position + 1)

View file

@ -2,7 +2,7 @@
use super::actions::Action;
use super::keybinds::Keybinds;
use crate::common::input::config::Config;
use crate::common::{input::config::Config};
use crate::common::{AppInstruction, SenderWithContext, OPENCALLS};
use crate::errors::ContextType;
use crate::os_input_output::OsApi;
@ -11,9 +11,8 @@ use crate::screen::ScreenInstruction;
use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting;
use crate::common::utils::shared::load_palette;
use termion::input::{TermRead, TermReadEventsAndRaw};
use zellij_tile::data::{Event, InputMode, Key, ModeInfo};
use zellij_tile::data::{Event, InputMode, Key, ModeInfo, Palette};
/// Handles the dispatching of [`Action`]s according to the current
/// [`InputMode`], and keep tracks of the current [`InputMode`].
@ -132,11 +131,14 @@ impl InputHandler {
self.send_plugin_instructions
.send(PluginInstruction::Update(
None,
Event::ModeUpdate(get_mode_info(mode)),
Event::ModeUpdate(get_mode_info(mode, self.os_input.load_palette())),
))
.unwrap();
self.send_screen_instructions
.send(ScreenInstruction::ChangeMode(get_mode_info(mode)))
.send(ScreenInstruction::ChangeMode(get_mode_info(
mode,
self.os_input.load_palette(),
)))
.unwrap();
self.send_screen_instructions
.send(ScreenInstruction::Render)
@ -285,9 +287,8 @@ impl InputHandler {
/// 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
pub fn get_mode_info(mode: InputMode) -> ModeInfo {
pub fn get_mode_info(mode: InputMode, palette: Palette) -> ModeInfo {
let mut keybinds: Vec<(String, String)> = vec![];
let palette = load_palette();
match mode {
InputMode::Normal | InputMode::Locked => {}
InputMode::Resize => {

View file

@ -39,7 +39,7 @@ use os_input_output::OsApi;
use pty_bus::{PtyBus, PtyInstruction};
use screen::{Screen, ScreenInstruction};
use serde::{Deserialize, Serialize};
use utils::{consts::ZELLIJ_IPC_PIPE, shared::load_palette};
use utils::consts::ZELLIJ_IPC_PIPE;
use wasm_vm::{wasi_read_string, wasi_write_object, zellij_exports, PluginEnv, PluginInstruction};
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState};
@ -267,7 +267,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let send_plugin_instructions = send_plugin_instructions.clone();
let send_app_instructions = send_app_instructions.clone();
let max_panes = opts.max_panes;
let colors = load_palette();
let colors = os_input.load_palette();
// debug_log_to_file(format!("{:?}", colors));
move || {
let mut screen = Screen::new(

View file

@ -1,4 +1,5 @@
use crate::panes::PositionAndSize;
use crate::utils::shared::{colors, detect_theme, hex_to_rgb};
use nix::fcntl::{fcntl, FcntlArg, OFlag};
use nix::pty::{forkpty, Winsize};
use nix::sys::signal::{kill, Signal};
@ -12,6 +13,8 @@ use std::os::unix::io::RawFd;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex};
use xrdb::Colors;
use zellij_tile::data::{Palette, PaletteSource, Theme};
use signal_hook::{consts::signal::*, iterator::Signals};
@ -191,6 +194,7 @@ pub trait OsApi: Send + Sync {
/// Returns a [`Box`] pointer to this [`OsApi`] struct.
fn box_clone(&self) -> Box<dyn OsApi>;
fn receive_sigwinch(&self, cb: Box<dyn Fn()>);
fn load_palette(&self) -> Palette;
}
impl OsApi for OsInputOutput {
@ -261,6 +265,48 @@ impl OsApi for OsInputOutput {
}
}
}
fn load_palette(&self) -> Palette {
let palette = match Colors::new("xresources") {
Some(palette) => {
let fg = hex_to_rgb(&palette.fg);
let bg = hex_to_rgb(&palette.bg);
let colors: Vec<(u8, u8, u8)> =
palette.colors.iter().map(|c| hex_to_rgb(c)).collect();
let theme = detect_theme(bg);
Palette {
source: PaletteSource::Xresources,
theme,
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],
orange: colors[9],
}
}
None => Palette {
source: PaletteSource::Default,
theme: Theme::Dark,
fg: colors::BRIGHT_GRAY,
bg: colors::GRAY,
black: colors::BLACK,
red: colors::RED,
green: colors::GREEN,
yellow: colors::GRAY,
blue: colors::GRAY,
magenta: colors::GRAY,
cyan: colors::GRAY,
white: colors::WHITE,
orange: colors::ORANGE,
},
};
palette
}
}
impl Clone for Box<dyn OsApi> {

View file

@ -5,8 +5,7 @@ use std::{iter, str::from_utf8};
use strip_ansi_escapes::strip;
use colors_transform::{Color, Rgb};
use xrdb::Colors;
use zellij_tile::data::{Palette, PaletteSource, Theme};
use zellij_tile::data::{Theme};
fn ansi_len(s: &str) -> usize {
from_utf8(&strip(s.as_bytes()).unwrap())
@ -65,45 +64,3 @@ pub fn detect_theme(bg: (u8, u8, u8)) -> Theme {
false => Theme::Dark,
}
}
pub fn load_palette() -> Palette {
let palette = match Colors::new("xresources") {
Some(palette) => {
let fg = hex_to_rgb(&palette.fg);
let bg = hex_to_rgb(&palette.bg);
let colors: Vec<(u8, u8, u8)> = palette.colors.iter().map(|c| hex_to_rgb(c)).collect();
let theme = detect_theme(bg);
Palette {
source: PaletteSource::Xresources,
theme,
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],
orange: colors[9]
}
}
None => Palette {
source: PaletteSource::Default,
theme: Theme::Dark,
fg: colors::BRIGHT_GRAY,
bg: colors::GRAY,
black: colors::BLACK,
red: colors::RED,
green: colors::GREEN,
yellow: colors::GRAY,
blue: colors::GRAY,
magenta: colors::GRAY,
cyan: colors::GRAY,
white: colors::WHITE,
orange: colors::ORANGE
},
};
palette
}

View file

@ -8,6 +8,8 @@ use std::time::{Duration, Instant};
use crate::os_input_output::OsApi;
use crate::tests::possible_tty_inputs::{get_possible_tty_inputs, Bytes};
use crate::utils::shared::colors;
use zellij_tile::data::{Palette, PaletteSource, Theme};
use crate::tests::utils::commands::{QUIT, SLEEP};
@ -73,6 +75,7 @@ pub struct FakeInputOutput {
last_snapshot_time: Arc<Mutex<Instant>>,
should_trigger_sigwinch: Arc<(Mutex<bool>, Condvar)>,
sigwinch_event: Option<PositionAndSize>,
palette: Arc<Mutex<Palette>>,
}
impl FakeInputOutput {
@ -81,6 +84,21 @@ impl FakeInputOutput {
let last_snapshot_time = Arc::new(Mutex::new(Instant::now()));
let stdout_writer = FakeStdoutWriter::new(last_snapshot_time.clone());
win_sizes.insert(0, winsize); // 0 is the current terminal
let palette: Palette = Palette {
source: PaletteSource::Default,
theme: Theme::Dark,
fg: colors::BRIGHT_GRAY,
bg: colors::GRAY,
black: colors::BLACK,
red: colors::RED,
green: colors::GREEN,
yellow: colors::GRAY,
blue: colors::GRAY,
magenta: colors::GRAY,
cyan: colors::GRAY,
white: colors::WHITE,
orange: colors::ORANGE,
};
FakeInputOutput {
read_buffers: Arc::new(Mutex::new(HashMap::new())),
stdin_writes: Arc::new(Mutex::new(HashMap::new())),
@ -93,6 +111,7 @@ impl FakeInputOutput {
possible_tty_inputs: get_possible_tty_inputs(),
should_trigger_sigwinch: Arc::new((Mutex::new(false), Condvar::new())),
sigwinch_event: None,
palette: Arc::new(Mutex::new(palette)),
}
}
pub fn with_tty_inputs(mut self, tty_inputs: HashMap<u16, Bytes>) -> Self {
@ -235,4 +254,22 @@ impl OsApi for FakeInputOutput {
cb();
}
}
fn load_palette(&self) -> Palette {
let palette: Palette = Palette {
source: PaletteSource::Default,
theme: Theme::Dark,
fg: colors::BRIGHT_GRAY,
bg: colors::GRAY,
black: colors::BLACK,
red: colors::RED,
green: colors::GREEN,
yellow: colors::GRAY,
blue: colors::GRAY,
magenta: colors::GRAY,
cyan: colors::GRAY,
white: colors::WHITE,
orange: colors::ORANGE,
};
palette
}
}

View file

@ -7,6 +7,7 @@ description = "A small client-side library for writing Zellij plugins"
license = "MIT"
[dependencies]
ansi_term = "0.12.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
strum = "0.20.0"

View file

@ -67,13 +67,13 @@ impl Default for InputMode {
}
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum Theme {
Light,
Dark,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum PaletteSource {
Default,
Xresources,
@ -88,7 +88,7 @@ pub mod colors {
pub const BLACK: (u8, u8, u8) = (0, 0, 0);
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct Palette {
pub source: PaletteSource,
pub theme: Theme,
@ -102,7 +102,7 @@ pub struct Palette {
pub magenta: (u8, u8, u8),
pub cyan: (u8, u8, u8),
pub white: (u8, u8, u8),
pub orange: (u8, u8, u8)
pub orange: (u8, u8, u8),
}
impl Default for Palette {
@ -120,7 +120,7 @@ impl Default for Palette {
magenta: colors::GRAY,
cyan: colors::GRAY,
white: colors::WHITE,
orange: colors::ORANGE
orange: colors::ORANGE,
}
}
}

View file

@ -39,3 +39,19 @@ macro_rules! register_plugin {
}
};
}
#[macro_export]
macro_rules! rgb {
($a:expr) => {
ansi_term::Color::RGB($a.0, $a.1, $a.2)
};
}
#[macro_export]
macro_rules! style {
($a:expr, $b:expr) => {
ansi_term::Style::new()
.fg(ansi_term::Color::RGB($a.0, $a.1, $a.2))
.on(ansi_term::Color::RGB($b.0, $b.1, $b.2))
};
}