mirror of
https://github.com/zellij-org/zellij.git
synced 2024-11-22 22:26:54 +03:00
Merge pull request #437 from a-kenji/xrdb-loading-fix
Make Xrdb Loading More Forgiving
This commit is contained in:
commit
2792a9009b
@ -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
|
||||
}
|
||||
|
@ -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<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();
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user