From 152343eaeff4a363aede2e9b987be60d7c2b96cd Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Tue, 26 Jan 2016 10:22:03 +0100 Subject: [PATCH] Underline parameters in example code --- src/formatter.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 00d8dc6..2568fd6 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -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::>(); + return ANSIStrings(&parts).to_string() +} + /// Print a token stream to an ANSI terminal. pub fn print_lines(tokenizer: &mut Tokenizer) where R: BufRead { while let Some(token) = tokenizer.next() { @@ -15,7 +31,7 @@ pub fn print_lines(tokenizer: &mut Tokenizer) 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), } }