wip: replace the impl with a fn load_palette instead

This commit is contained in:
denis 2021-03-28 10:51:15 +03:00
parent 6e276ae386
commit 42890d4e64
5 changed files with 59 additions and 78 deletions

View file

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

View file

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

View file

@ -2,7 +2,7 @@
use super::actions::Action; use super::actions::Action;
use super::keybinds::get_default_keybinds; use super::keybinds::get_default_keybinds;
use crate::common::{AppInstruction, Palette, SenderWithContext, OPENCALLS}; use crate::common::{load_palette, AppInstruction, SenderWithContext, OPENCALLS};
use crate::errors::ContextType; use crate::errors::ContextType;
use crate::os_input_output::OsApi; use crate::os_input_output::OsApi;
use crate::pty_bus::PtyInstruction; use crate::pty_bus::PtyInstruction;
@ -256,7 +256,7 @@ impl InputHandler {
// TODO this should probably be automatically generated in some way // 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) -> ModeInfo {
let mut keybinds: Vec<(String, String)> = vec![]; let mut keybinds: Vec<(String, String)> = vec![];
let mut palette = Palette::new(); let mut palette = load_palette();
match mode { match mode {
InputMode::Normal | InputMode::Locked => {} InputMode::Normal | InputMode::Locked => {}
InputMode::Resize => { InputMode::Resize => {

View file

@ -39,7 +39,7 @@ use wasm_vm::{wasi_stdout, wasi_write_string, zellij_imports, PluginInstruction}
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value}; use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState}; use wasmer_wasi::{Pipe, WasiState};
use xrdb::Colors; use xrdb::Colors;
use zellij_tile::data::{EventType, InputMode}; use zellij_tile::data::{EventType, InputMode, Palette};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub enum ApiCommand { pub enum ApiCommand {
@ -126,73 +126,57 @@ pub mod colors {
pub const BLACK: (u8, u8, u8) = (0, 0, 0); pub const BLACK: (u8, u8, u8) = (0, 0, 0);
} }
#[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub fn load_palette() -> Palette {
pub struct Palette { let palette = match Colors::new("xresources") {
pub fg: (u8, u8, u8), Some(colors) => {
pub bg: (u8, u8, u8), let fg = colors.fg.unwrap();
pub black: (u8, u8, u8), let fg_imm = &fg;
pub red: (u8, u8, u8), let fg_hex: &str = &fg_imm;
pub green: (u8, u8, u8), let fg = Rgb::from_hex_str(fg_hex).unwrap().as_tuple();
pub yellow: (u8, u8, u8), let fg = (fg.0 as u8, fg.1 as u8, fg.2 as u8);
pub blue: (u8, u8, u8), let bg = colors.bg.unwrap();
pub magenta: (u8, u8, u8), let bg_imm = &bg;
pub cyan: (u8, u8, u8), let bg_hex: &str = &bg_imm;
pub white: (u8, u8, u8), 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
impl Palette { .colors
pub fn new() -> Self { .iter()
let palette = match Colors::new("xresources") { .map(|c| {
Some(colors) => { let c = c.clone();
let fg = colors.fg.unwrap(); let imm_str = &c.unwrap();
let fg_imm = &fg; let hex_str: &str = &imm_str;
let fg_hex: &str = &fg_imm; let rgb = Rgb::from_hex_str(hex_str).unwrap().as_tuple();
let fg = Rgb::from_hex_str(fg_hex).unwrap().as_tuple(); (rgb.0 as u8, rgb.1 as u8, rgb.2 as u8)
let fg = (fg.0 as u8, fg.1 as u8, fg.2 as u8); })
let bg = colors.bg.unwrap(); .collect();
let bg_imm = &bg; Palette {
let bg_hex: &str = &bg_imm; fg,
let bg = Rgb::from_hex_str(bg_hex).unwrap().as_tuple(); bg,
let bg = (bg.0 as u8, bg.1 as u8, bg.2 as u8); black: colors[0],
let colors: Vec<(u8, u8, u8)> = colors red: colors[1],
.colors green: colors[2],
.iter() yellow: colors[3],
.map(|c| { blue: colors[4],
let c = c.clone(); magenta: colors[5],
let imm_str = &c.unwrap(); cyan: colors[6],
let hex_str: &str = &imm_str; white: colors[7],
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, None => Palette {
bg: colors::BLACK, fg: colors::BRIGHT_GRAY,
black: colors::BLACK, bg: colors::BLACK,
red: colors::RED, black: colors::BLACK,
green: colors::GREEN, red: colors::RED,
yellow: colors::GRAY, green: colors::GREEN,
blue: colors::GRAY, yellow: colors::GRAY,
magenta: colors::GRAY, blue: colors::GRAY,
cyan: colors::GRAY, magenta: colors::GRAY,
white: colors::WHITE, cyan: colors::GRAY,
}, white: colors::WHITE,
}; },
palette };
} palette
} }
/// Start Zellij with the specified [`OsApi`] and command-line arguments. /// Start Zellij with the specified [`OsApi`] and command-line arguments.
@ -326,7 +310,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let send_plugin_instructions = send_plugin_instructions.clone(); let send_plugin_instructions = send_plugin_instructions.clone();
let send_app_instructions = send_app_instructions.clone(); let send_app_instructions = send_app_instructions.clone();
let max_panes = opts.max_panes; let max_panes = opts.max_panes;
let colors = Palette::new(); let colors = load_palette();
// debug_log_to_file(format!("{:?}", colors)); // debug_log_to_file(format!("{:?}", colors));
move || { move || {
let mut screen = Screen::new( let mut screen = Screen::new(

View file

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