From f97c3ae3a10e34b650c3993f7b29dea8407b804f Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 2 May 2021 19:19:52 +0200 Subject: [PATCH] Make Xrdb Loading More Forgiving Attempt at fixing #434. The `hex_to_rgb` function propagates the Option now. If it is None, it tries to load the default Palette now. --- src/common/os_input_output.rs | 45 +++++++++++++++++++---------------- src/common/utils/shared.rs | 29 +++++++++++++++++----- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs index 41a4bfe4..6ed402de 100644 --- a/src/common/os_input_output.rs +++ b/src/common/os_input_output.rs @@ -1,5 +1,5 @@ use crate::panes::PositionAndSize; -use crate::utils::shared::{colors, detect_theme, hex_to_rgb}; +use crate::utils::shared::{default_palette, detect_theme, hex_to_rgb}; use nix::fcntl::{fcntl, FcntlArg, OFlag}; use nix::pty::{forkpty, Winsize}; use nix::sys::signal::{kill, Signal}; @@ -14,7 +14,7 @@ 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 zellij_tile::data::{Palette, PaletteSource}; use signal_hook::{consts::signal::*, iterator::Signals}; @@ -268,10 +268,27 @@ 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 fg = if let Some(foreground) = palette.fg.as_deref().map(hex_to_rgb) { + foreground + } else { + return default_palette(); + }; + + let bg = if let Some(background) = palette.bg.as_deref().map(hex_to_rgb) { + background + } else { + return default_palette(); + }; + + // NOTE: `16` is the same as the length of `palette.colors`. + let mut colors: [(u8, u8, u8); 16] = [(0, 0, 0); 16]; + for (idx, color) in palette.colors.iter().enumerate() { + if let Some(c) = color { + colors[idx] = hex_to_rgb(c); + } else { + return default_palette(); + } + } let theme = detect_theme(bg); Palette { source: PaletteSource::Xresources, @@ -289,21 +306,7 @@ impl OsApi for OsInputOutput { 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, - }, + None => default_palette(), }; palette } diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs index 3f336f7b..c705ed0d 100644 --- a/src/common/utils/shared.rs +++ b/src/common/utils/shared.rs @@ -5,7 +5,7 @@ use std::{iter, str::from_utf8}; use strip_ansi_escapes::strip; use colors_transform::{Color, Rgb}; -use zellij_tile::data::Theme; +use zellij_tile::data::{Palette, PaletteSource, Theme}; fn ansi_len(s: &str) -> usize { from_utf8(&strip(s.as_bytes()).unwrap()) @@ -43,14 +43,31 @@ pub mod colors { pub const BLACK: (u8, u8, u8) = (0, 0, 0); } -pub fn hex_to_rgb(hex: &Option) -> (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(); +pub fn hex_to_rgb(hex: &str) -> (u8, u8, u8) { + let rgb = Rgb::from_hex_str(hex) + .expect("The passed argument must be a valid hex color") + .as_tuple(); (rgb.0 as u8, rgb.1 as u8, rgb.2 as u8) } +pub fn default_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, + } +} + // Dark magic pub fn detect_theme(bg: (u8, u8, u8)) -> Theme { let (r, g, b) = bg;