mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-28 20:29:04 +03:00
using nom to create Text with inline colors. just using in a few places;
still buggy and kind of inconvenient actually.
This commit is contained in:
parent
dd9e187db6
commit
602a936440
@ -222,9 +222,7 @@ impl ID {
|
||||
|
||||
fn styled_kv(txt: &mut Text, tags: &BTreeMap<String, String>) {
|
||||
for (k, v) in tags {
|
||||
txt.add_styled_line(k.to_string(), Some(Color::RED), None, None);
|
||||
txt.append(" = ".to_string(), None);
|
||||
txt.append(v.to_string(), Some(Color::CYAN));
|
||||
txt.push(format!("[red:{}] = [cyan:{}]", k, v));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,9 +141,7 @@ impl DataVisualizer {
|
||||
} else {
|
||||
let mut txt = Text::new();
|
||||
for (k, v) in kv {
|
||||
txt.add_styled_line(k.to_string(), Some(Color::RED), None, None);
|
||||
txt.append(" = ".to_string(), None);
|
||||
txt.append(v.to_string(), Some(Color::CYAN));
|
||||
txt.push(format!("[red:{}] = [cyan:{}]", k, v));
|
||||
}
|
||||
g.draw_blocking_text(&txt, (HorizontalAlignment::Left, VerticalAlignment::Top));
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ geom = { path = "../geom" }
|
||||
glium = "0.23.0"
|
||||
glium-glyph = "0.3.0"
|
||||
glutin = "0.20.0"
|
||||
nom = "4.2.3"
|
||||
serde = "1.0.89"
|
||||
serde_derive = "1.0.89"
|
||||
simsearch = "0.1.4"
|
||||
|
@ -59,4 +59,22 @@ impl Color {
|
||||
pub const fn alpha(&self, a: f32) -> Color {
|
||||
Color([self.0[0], self.0[1], self.0[2], a])
|
||||
}
|
||||
|
||||
pub fn from_string(color: &str) -> Color {
|
||||
match color {
|
||||
"red" => Color::RED,
|
||||
"cyan" => Color::CYAN,
|
||||
_ => panic!("Unknown color {}", color),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_string(self) -> &'static str {
|
||||
if self == Color::RED {
|
||||
return "red";
|
||||
}
|
||||
if self == Color::CYAN {
|
||||
return "cyan";
|
||||
}
|
||||
panic!("Can't transform {} to a string", self);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ use geom::{Distance, Polygon, Pt2D};
|
||||
use glium_glyph::glyph_brush::rusttype::Scale;
|
||||
use glium_glyph::glyph_brush::GlyphCruncher;
|
||||
use glium_glyph::glyph_brush::{Section, SectionText, VariedSection};
|
||||
use nom::types::CompleteStr;
|
||||
use nom::{alt, char, do_parse, many1, named, separated_pair, take_till1, take_until};
|
||||
use textwrap;
|
||||
|
||||
const FG_COLOR: Color = Color::WHITE;
|
||||
@ -35,7 +37,6 @@ impl TextSpan {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO parse style from markup tags
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Text {
|
||||
// The bg_color will cover the entire block, but some lines can have extra highlighting.
|
||||
@ -64,6 +65,11 @@ impl Text {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, line: String) {
|
||||
self.lines.push((None, Vec::new()));
|
||||
parse_style(self, line);
|
||||
}
|
||||
|
||||
pub fn from_line(line: String) -> Text {
|
||||
let mut txt = Text::new();
|
||||
txt.add_line(line);
|
||||
@ -81,12 +87,6 @@ impl Text {
|
||||
txt
|
||||
}
|
||||
|
||||
pub fn pad_if_nonempty(&mut self) {
|
||||
if !self.lines.is_empty() {
|
||||
self.lines.push((None, Vec::new()));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_line(&mut self, line: String) {
|
||||
self.lines.push((None, vec![TextSpan::default_style(line)]));
|
||||
}
|
||||
@ -307,3 +307,59 @@ pub fn draw_text_bubble_mapspace(
|
||||
.borrow_mut()
|
||||
.draw_queued(g.display, g.target);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Append {
|
||||
color: Option<Color>,
|
||||
text: String,
|
||||
}
|
||||
|
||||
named!(colored<CompleteStr, Append>,
|
||||
do_parse!(
|
||||
char!('[') >>
|
||||
pair: separated_pair!(take_until!(":"), char!(':'), take_until!("]")) >>
|
||||
char!(']')
|
||||
>>
|
||||
(Append {
|
||||
color: Some(Color::from_string(&pair.0)),
|
||||
text: pair.1.to_string(),
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
fn is_left_bracket(x: char) -> bool {
|
||||
x == '['
|
||||
}
|
||||
|
||||
named!(plaintext<CompleteStr, Append>,
|
||||
do_parse!(
|
||||
txt: take_till1!(is_left_bracket)
|
||||
>>
|
||||
(Append {
|
||||
color: None,
|
||||
text: txt.to_string(),
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
named!(chunk<CompleteStr, Append>,
|
||||
alt!(colored | plaintext)
|
||||
);
|
||||
|
||||
named!(chunks<CompleteStr, Vec<Append>>,
|
||||
many1!(chunk)
|
||||
);
|
||||
|
||||
fn parse_style(txt: &mut Text, line: String) {
|
||||
match chunks(CompleteStr(&line)) {
|
||||
Ok((rest, values)) => {
|
||||
if !rest.is_empty() {
|
||||
panic!("Parsing {} had leftover {}", line, rest);
|
||||
}
|
||||
for x in values {
|
||||
txt.append(x.text, x.color);
|
||||
}
|
||||
}
|
||||
x => panic!("Parsing {} broke: {:?}", line, x),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user