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 all 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
69 changes: 69 additions & 0 deletions src/font_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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(vec![0]),
}
}
}

impl Display for FontStyle {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for (i, style) in self.0.iter().enumerate() {
if i + 1 == self.0.len() {
write!(f, "{}", style)?;
} else {
write!(f, "{};", style)?;
}
}
Comment on lines +60 to +66
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, because that would only work if the slice was a slice of strs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, right. we could use intersperse from itertools, but it's not worth a new dependency. Maybe once it is stabilized in std 😄 (rust-lang/rust#79524, https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.intersperse)

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
27 changes: 3 additions & 24 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,7 +102,7 @@ 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!(
Expand Down