Underline parameters in example code

This commit is contained in:
Danilo Bargen 2016-01-26 10:22:03 +01:00
commit 152343eaef

View file

@ -2,11 +2,27 @@
use std::io::BufRead;
use ansi_term::Colour;
use ansi_term::{Colour, ANSIStrings};
use tokenizer::Tokenizer;
use types::LineType;
/// Provide formatting for {{ curly braces }} in ExampleCode lines
fn format_braces(text: &str) -> String {
let parts = text.split("{{").flat_map(|s| s.split("}}"))
.enumerate()
.map(|(i, v)| {
if i % 2 == 0 {
Colour::Cyan.paint(v)
} else {
Colour::Cyan.underline().paint(v)
}
})
.collect::<Vec<_>>();
return ANSIStrings(&parts).to_string()
}
/// Print a token stream to an ANSI terminal.
pub fn print_lines<R>(tokenizer: &mut Tokenizer<R>) where R: BufRead {
while let Some(token) = tokenizer.next() {
@ -15,7 +31,7 @@ pub fn print_lines<R>(tokenizer: &mut Tokenizer<R>) where R: BufRead {
LineType::Title(_) => debug!("Ignoring title"),
LineType::Description(text) => println!(" {}", text),
LineType::ExampleText(text) => println!(" {}", Colour::Green.paint(format!("- {}", text))),
LineType::ExampleCode(text) => println!(" {}", Colour::Cyan.paint(format!(" {}", text))),
LineType::ExampleCode(text) => println!(" {}", &format_braces(&text)),
LineType::Other(text) => debug!("Unknown line type: {:?}", text),
}
}