aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorDenis Maximov <denismaximov98@gmail.com>2021-05-02 13:53:36 +0300
committerGitHub <noreply@github.com>2021-05-02 13:53:36 +0300
commit98d9eacd2221162c60f71b73ea7f40ff7256e225 (patch)
treebad162ac3c59a16723f75fcd32d6d0cc724be09e /src/common
parentdocs(changelog): Improved Config Loading #423 (diff)
parentchore: remove unused dependency from zellij-tile (diff)
downloadsl0ppij-98d9eacd2221162c60f71b73ea7f40ff7256e225.tar.gz
sl0ppij-98d9eacd2221162c60f71b73ea7f40ff7256e225.tar.xz
sl0ppij-98d9eacd2221162c60f71b73ea7f40ff7256e225.zip
Merge pull request #239 from zellij-org/x-colors
X colors
Diffstat (limited to 'src/common')
-rw-r--r--src/common/input/handler.rs17
-rw-r--r--src/common/mod.rs11
-rw-r--r--src/common/os_input_output.rs46
-rw-r--r--src/common/screen.rs12
-rw-r--r--src/common/utils/shared.rs36
5 files changed, 113 insertions, 9 deletions
diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs
index 62118722..7304225c 100644
--- a/src/common/input/handler.rs
+++ b/src/common/input/handler.rs
@@ -12,7 +12,7 @@ use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting;
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`].
@@ -131,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)
@@ -284,7 +287,7 @@ 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![];
match mode {
InputMode::Normal | InputMode::Locked => {}
@@ -315,7 +318,11 @@ pub fn get_mode_info(mode: InputMode) -> ModeInfo {
keybinds.push(("Enter".to_string(), "when done".to_string()));
}
}
- ModeInfo { mode, keybinds }
+ ModeInfo {
+ mode,
+ keybinds,
+ palette,
+ }
}
/// Entry point to the module. Instantiates an [`InputHandler`] and starts
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 0afc6ef2..28446d2e 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -43,7 +43,7 @@ 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};
-use zellij_tile::data::{EventType, ModeInfo};
+use zellij_tile::data::{EventType, InputMode, ModeInfo};
#[derive(Serialize, Deserialize, Debug)]
pub enum ApiCommand {
@@ -258,7 +258,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
let send_plugin_instructions = send_plugin_instructions.clone();
let send_app_instructions = send_app_instructions.clone();
let max_panes = opts.max_panes;
-
+ let colors = os_input.load_palette();
move || {
let mut screen = Screen::new(
receive_screen_instructions,
@@ -268,7 +268,12 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
&full_screen_ws,
os_input,
max_panes,
- ModeInfo::default(),
+ ModeInfo {
+ palette: colors,
+ ..ModeInfo::default()
+ },
+ InputMode::Normal,
+ colors,
);
loop {
let (event, mut err_ctx) = screen
diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs
index 35d77fdd..41a4bfe4 100644
--- a/src/common/os_input_output.rs
+++ b/src/common/os_input_output.rs
@@ -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> {
diff --git a/src/common/screen.rs b/src/common/screen.rs
index eee1166f..e6cf73f5 100644
--- a/src/common/screen.rs
+++ b/src/common/screen.rs
@@ -13,7 +13,7 @@ use crate::tab::Tab;
use crate::{errors::ErrorContext, wasm_vm::PluginInstruction};
use crate::{layout::Layout, panes::PaneId};
-use zellij_tile::data::{Event, ModeInfo, TabInfo};
+use zellij_tile::data::{Event, InputMode, ModeInfo, Palette, TabInfo};
/// Instructions that can be sent to the [`Screen`].
#[derive(Debug, Clone)]
@@ -81,6 +81,8 @@ pub struct Screen {
/// The [`OsApi`] this [`Screen`] uses.
os_api: Box<dyn OsApi>,
mode_info: ModeInfo,
+ input_mode: InputMode,
+ colors: Palette,
}
impl Screen {
@@ -96,6 +98,8 @@ impl Screen {
os_api: Box<dyn OsApi>,
max_panes: Option<usize>,
mode_info: ModeInfo,
+ input_mode: InputMode,
+ colors: Palette,
) -> Self {
Screen {
receiver: receive_screen_instructions,
@@ -108,6 +112,8 @@ impl Screen {
tabs: BTreeMap::new(),
os_api,
mode_info,
+ input_mode,
+ colors,
}
}
@@ -128,6 +134,8 @@ impl Screen {
self.max_panes,
Some(PaneId::Terminal(pane_id)),
self.mode_info.clone(),
+ self.input_mode,
+ self.colors,
);
self.active_tab_index = Some(tab_index);
self.tabs.insert(tab_index, tab);
@@ -283,6 +291,8 @@ impl Screen {
self.max_panes,
None,
self.mode_info.clone(),
+ self.input_mode,
+ self.colors,
);
tab.apply_layout(layout, new_pids);
self.active_tab_index = Some(tab_index);
diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs
index d5ba943b..3f336f7b 100644
--- a/src/common/utils/shared.rs
+++ b/src/common/utils/shared.rs
@@ -4,6 +4,9 @@ use std::{iter, str::from_utf8};
use strip_ansi_escapes::strip;
+use colors_transform::{Color, Rgb};
+use zellij_tile::data::Theme;
+
fn ansi_len(s: &str) -> usize {
from_utf8(&strip(s.as_bytes()).unwrap())
.unwrap()
@@ -28,3 +31,36 @@ pub fn adjust_to_size(s: &str, rows: usize, columns: usize) -> String {
.collect::<Vec<_>>()
.join("\n\r")
}
+
+// 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 ORANGE: (u8, u8, u8) = (215, 95, 0);
+ pub const BLACK: (u8, u8, u8) = (0, 0, 0);
+}
+
+pub fn hex_to_rgb(hex: &Option<String>) -> (u8, u8, u8) {
+ let c = hex.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)
+}
+
+// Dark magic
+pub fn detect_theme(bg: (u8, u8, u8)) -> Theme {
+ let (r, g, b) = bg;
+ // HSP, P stands for perceived brightness
+ let hsp: f64 = (0.299 * (r as f64 * r as f64)
+ + 0.587 * (g as f64 * g as f64)
+ + 0.114 * (b as f64 * b as f64))
+ .sqrt();
+ match hsp > 127.5 {
+ true => Theme::Light,
+ false => Theme::Dark,
+ }
+}