1
0
mirror of https://github.com/danog/ytop.git synced 2024-11-26 20:15:03 +01:00

Implement ColorschemeRaw

This commit is contained in:
Caleb Bassi 2020-01-12 16:14:00 -08:00
parent def79e153b
commit 163dfe29c4
2 changed files with 92 additions and 17 deletions

View File

@ -3,6 +3,7 @@ use std::path::Path;
use std::str::FromStr;
use serde::Deserialize;
use tui::style::{Color, Style};
pub enum Colorschemes {
Default,
@ -29,33 +30,98 @@ impl FromStr for Colorschemes {
}
#[derive(Deserialize)]
pub struct Colorscheme {
fg: i64,
bg: i64,
pub struct ColorschemeRaw {
pub fg: i64,
pub bg: i64,
titles: i64,
borders: i64,
pub titles: i64,
pub borders: i64,
battery_lines: Vec<i64>,
pub battery_lines: Vec<i64>,
// need at least 8 entries
cpu_lines: Vec<i64>,
pub cpu_lines: Vec<i64>,
mem_main: i64,
mem_swap: i64,
pub mem_main: i64,
pub mem_swap: i64,
net_bars: i64,
pub net_bars: i64,
proc_cursor: i64,
pub proc_cursor: i64,
temp_low: i64,
temp_high: i64,
pub temp_low: i64,
pub temp_high: i64,
}
pub fn read_colorscheme(
pub struct Colorscheme {
pub text: Style,
pub titles: Style,
pub borders: Style,
pub battery_lines: Vec<Style>,
// need at least 8 entries
pub cpu_lines: Vec<Style>,
pub mem_main: Style,
pub mem_swap: Style,
pub net_bars: Style,
pub proc_cursor: Style,
pub temp_low: Style,
pub temp_high: Style,
}
impl From<ColorschemeRaw> for Colorscheme {
fn from(raw: ColorschemeRaw) -> Self {
Colorscheme {
text: Style::default()
.fg(convert_color(raw.fg))
.bg(convert_color(raw.bg)),
titles: Style::default().fg(convert_color(raw.titles)),
borders: Style::default().fg(convert_color(raw.borders)),
battery_lines: raw
.battery_lines
.into_iter()
.map(|entry| Style::default().fg(convert_color(entry)))
.collect(),
cpu_lines: raw
.cpu_lines
.into_iter()
.map(|entry| Style::default().fg(convert_color(entry)))
.collect(),
mem_main: Style::default().fg(convert_color(raw.mem_main)),
mem_swap: Style::default().fg(convert_color(raw.mem_swap)),
net_bars: Style::default().fg(convert_color(raw.net_bars)),
proc_cursor: Style::default().fg(convert_color(raw.proc_cursor)),
temp_low: Style::default().fg(convert_color(raw.temp_low)),
temp_high: Style::default().fg(convert_color(raw.temp_high)),
}
}
}
fn convert_color(raw: i64) -> Color {
if raw == -1 {
Color::Reset
} else {
Color::Indexed(raw as u8)
}
}
pub fn parse_colorscheme(
config_folder: &Path,
colorscheme: &Colorschemes,
) -> serde_json::Result<Colorscheme> {
) -> serde_json::Result<ColorschemeRaw> {
match colorscheme {
Colorschemes::Custom(name) => serde_json::from_str(
&fs::read_to_string(config_folder.join(name).with_extension("json")).unwrap(),
@ -74,3 +140,12 @@ pub fn read_colorscheme(
}
}
}
pub fn read_colorscheme(
config_folder: &Path,
colorscheme: &Colorschemes,
) -> serde_json::Result<Colorscheme> {
let raw_colorscheme = parse_colorscheme(config_folder, colorscheme)?;
Ok(Colorscheme::from(raw_colorscheme))
}

View File

@ -1,4 +1,3 @@
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders};
use crate::colorscheme::Colorscheme;
@ -6,6 +5,7 @@ use crate::colorscheme::Colorscheme;
pub fn new<'a>(colorscheme: &Colorscheme, title: &'a str) -> Block<'a> {
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Cyan))
.border_style(colorscheme.borders)
.title(title)
.title_style(colorscheme.titles)
}