Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7f5e297

Browse files
Merge pull request clearloop#17 from hulufei/colors
Improve `pick` command to display color styles
2 parents c8d9f95 + b0351a5 commit 7f5e297

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

‎Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ rand = "0.7.2"
3131
serde = { version = "1.0.104", features = ["derive"] }
3232
serde_json = "1.0.44"
3333
toml = "0.5.5"
34+
regex = "1"
3435

3536
[dependencies.diesel]
3637
version = "1.4.3"

‎src/helper.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@ mod filter {
9393
/// Render html to command-line
9494
mod html {
9595
// use crate::Error;
96-
use colored::Colorize;
96+
use colored::{Color,Colorize};
9797
use escaper::decode_html;
98+
use regex::Regex;
9899
pub enum Token {
99100
Plain(String),
100101
Bold(String),
101102
Sup(String),
102103
Sub(String),
104+
Font((String, Color)),
103105
Eof(String),
104106
}
105107

@@ -176,6 +178,8 @@ mod html {
176178
let mut bold = false;
177179
let mut sup = false;
178180
let mut sub = false;
181+
let mut color: Option<Color> = None;
182+
let re_color = Regex::new(r#"color=['"]([^'"]+)"#).unwrap();
179183
for (i, e) in tks.chars().enumerate() {
180184
match e {
181185
'<' => {
@@ -188,6 +192,9 @@ mod html {
188192
} else if sub {
189193
output.push(Token::Sub(tks[ptr..i].to_string()));
190194
sub = false;
195+
} else if color.is_some() {
196+
output.push(Token::Font((tks[ptr..i].to_string(), color.unwrap())));
197+
color = None;
191198
} else {
192199
output.push(Token::Plain(tks[ptr..i].to_string()));
193200
}
@@ -200,6 +207,12 @@ mod html {
200207
"b" | "strong" => bold = true,
201208
"sup" => sup = true,
202209
"sub" => sub = true,
210+
s if s.starts_with("font") => {
211+
color = re_color
212+
.captures(s)
213+
.and_then(|caps| caps.get(1))
214+
.and_then(|cap| cap.as_str().parse().ok());
215+
}
203216
_ => {}
204217
},
205218
}
@@ -243,6 +256,7 @@ mod html {
243256
Ok(n) => subscript(n),
244257
_ => s,
245258
}),
259+
Token::Font((s, color)) => tks.push(s.color(color).to_string()),
246260
Token::Eof(s) => tks.push(s.normal().to_string()),
247261
}
248262
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /