-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6bee633
Allowed font_style to be an array
John-Toohey 4d2cdb9
Prevent double `;` in LS_COLORS
John-Toohey 2c9d5ca
Allowed font_style to be an array
John-Toohey cd2298e
Prevent double `;` in LS_COLORS
John-Toohey 958402f
Merge branch 'multiple_font_styles' of github.com:John-Toohey/vivid i…
John-Toohey 473097e
Got all the tests working
John-Toohey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to use https://doc.rust-lang.org/std/primitive.slice.html#method.join here?
There was a problem hiding this comment.
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
str
s.There was a problem hiding this comment.
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)