Merge pull request #437 from a-kenji/xrdb-loading-fix

Make Xrdb Loading More Forgiving
This commit is contained in:
a-kenji 2021-05-03 18:26:45 +02:00 committed by GitHub
commit 2792a9009b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 27 deletions

View file

@ -1,5 +1,5 @@
use crate::panes::PositionAndSize; 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::fcntl::{fcntl, FcntlArg, OFlag};
use nix::pty::{forkpty, Winsize}; use nix::pty::{forkpty, Winsize};
use nix::sys::signal::{kill, Signal}; use nix::sys::signal::{kill, Signal};
@ -14,7 +14,7 @@ use std::path::PathBuf;
use std::process::{Child, Command}; use std::process::{Child, Command};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use xrdb::Colors; use xrdb::Colors;
use zellij_tile::data::{Palette, PaletteSource, Theme}; use zellij_tile::data::{Palette, PaletteSource};
use signal_hook::{consts::signal::*, iterator::Signals}; use signal_hook::{consts::signal::*, iterator::Signals};
@ -268,10 +268,27 @@ impl OsApi for OsInputOutput {
fn load_palette(&self) -> Palette { fn load_palette(&self) -> Palette {
let palette = match Colors::new("xresources") { let palette = match Colors::new("xresources") {
Some(palette) => { Some(palette) => {
let fg = hex_to_rgb(&palette.fg); let fg = if let Some(foreground) = palette.fg.as_deref().map(hex_to_rgb) {
let bg = hex_to_rgb(&palette.bg); foreground
let colors: Vec<(u8, u8, u8)> = } else {
palette.colors.iter().map(|c| hex_to_rgb(c)).collect(); 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); let theme = detect_theme(bg);
Palette { Palette {
source: PaletteSource::Xresources, source: PaletteSource::Xresources,
@ -289,21 +306,7 @@ impl OsApi for OsInputOutput {
orange: colors[9], orange: colors[9],
} }
} }
None => Palette { None => default_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 palette
} }

View file

@ -5,7 +5,7 @@ use std::{iter, str::from_utf8};
use strip_ansi_escapes::strip; use strip_ansi_escapes::strip;
use colors_transform::{Color, Rgb}; use colors_transform::{Color, Rgb};
use zellij_tile::data::Theme; use zellij_tile::data::{Palette, PaletteSource, Theme};
fn ansi_len(s: &str) -> usize { fn ansi_len(s: &str) -> usize {
from_utf8(&strip(s.as_bytes()).unwrap()) from_utf8(&strip(s.as_bytes()).unwrap())
@ -43,14 +43,31 @@ pub mod colors {
pub const BLACK: (u8, u8, u8) = (0, 0, 0); pub const BLACK: (u8, u8, u8) = (0, 0, 0);
} }
pub fn hex_to_rgb(hex: &Option<String>) -> (u8, u8, u8) { pub fn hex_to_rgb(hex: &str) -> (u8, u8, u8) {
let c = hex.clone(); let rgb = Rgb::from_hex_str(hex)
let imm_str = &c.unwrap(); .expect("The passed argument must be a valid hex color")
let hex_str: &str = &imm_str; .as_tuple();
let rgb = Rgb::from_hex_str(hex_str).unwrap().as_tuple();
(rgb.0 as u8, rgb.1 as u8, rgb.2 as u8) (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 // Dark magic
pub fn detect_theme(bg: (u8, u8, u8)) -> Theme { pub fn detect_theme(bg: (u8, u8, u8)) -> Theme {
let (r, g, b) = bg; let (r, g, b) = bg;