Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed font_style to be an array #124

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/font_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
};

use lazy_static::lazy_static;

use yaml_rust::{yaml::Hash, Yaml};

lazy_static! {
static ref ANSI_STYLES: HashMap<&'static str, u8> = {
let mut m = HashMap::new();
m.insert("regular", 0);
m.insert("bold", 1);
m.insert("faint", 2);
m.insert("italic", 3);
m.insert("underline", 4);
m.insert("blink", 5);
m.insert("rapid-blink", 6);
m.insert("overline", 53);
m
};
}

/// A list of font styles
#[derive(Default)]
pub struct FontStyle(Vec<u8>);

impl FontStyle {
/// Creates a FontStyle from the yaml
///
/// # Panics
///
/// Panics if the yaml value is neither a string or
/// a yaml array
pub fn from_yaml(map: &Hash) -> Self {
match map.get(&Yaml::String("font-style".into())) {
Some(value) => match value {
Yaml::String(s) => Self(vec![ANSI_STYLES[s.as_str()]]),
Yaml::Array(array) => {
let mut vec = Vec::with_capacity(array.len());
for item in array {
vec.push(
ANSI_STYLES[item
.as_str()
.expect("font_style should be a string or an array of strings")],
);
}
Self(vec)
}
_ => panic!("font-style should be a string or an array of strings"),
},
None => Self::default(),
}
}
}

impl Display for FontStyle {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for style in &self.0 {
write!(f, "{};", style)?;
}
Ok(())
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod color;
mod error;
mod filetypes;
mod font_style;
mod theme;
mod types;
mod util;
Expand Down
32 changes: 4 additions & 28 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,12 @@ use std::path::Path;
use yaml_rust::yaml::YamlLoader;
use yaml_rust::Yaml;

use lazy_static::lazy_static;

use crate::color::{Color, ColorMode, ColorType};
use crate::error::{Result, VividError};
use crate::font_style::FontStyle;
use crate::types::CategoryRef;
use crate::util::{load_yaml_file, transpose};

lazy_static! {
static ref ANSI_STYLES: HashMap<&'static str, u8> = {
let mut m = HashMap::new();
m.insert("regular", 0);
m.insert("bold", 1);
m.insert("faint", 2);
m.insert("italic", 3);
m.insert("underline", 4);
m.insert("blink", 5);
m.insert("rapid-blink", 6);
m.insert("overline", 53);
m
};
}

#[derive(Debug)]
pub struct Theme {
colors: HashMap<String, Color>,
Expand Down Expand Up @@ -104,12 +88,7 @@ impl Theme {
}

if let Yaml::Hash(map) = item {
let font_style: &str = map
.get(&Yaml::String("font-style".into()))
.map(|s| s.as_str().expect("Color value should be a string"))
.unwrap_or("regular");

let font_style_ansi: &u8 = ANSI_STYLES.get(&font_style).unwrap(); // TODO
let font_style = FontStyle::from_yaml(map);

let foreground = map
.get(&Yaml::String("foreground".into()))
Expand All @@ -123,13 +102,10 @@ impl Theme {

let background = transpose(background.map(|fg| self.get_color(fg)))?;

let mut style: String = format!("{font_style}", font_style = *font_style_ansi,);
let mut style: String = format!("{font_style}");
John-Toohey marked this conversation as resolved.
Show resolved Hide resolved
if let Some(foreground) = foreground {
let foreground_code = foreground.get_style(ColorType::Foreground, self.color_mode);
style.push_str(&format!(
";{foreground_code}",
foreground_code = foreground_code
));
style.push_str(&foreground_code);
}

if let Some(background) = background {
Expand Down
Loading