Move formatter to separate module

This commit is contained in:
Danilo Bargen 2016-01-06 18:37:49 +01:00
commit d4a43212cd
2 changed files with 25 additions and 18 deletions

22
src/formatter.rs Normal file
View file

@ -0,0 +1,22 @@
//! Functions related to formatting and printing lines from a `Tokenizer`.
use std::io::BufRead;
use ansi_term::Colour;
use tokenizer::Tokenizer;
use types::LineType;
/// 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() {
match token {
LineType::Empty => println!(""),
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::Other(text) => debug!("Unknown line type: {:?}", text),
}
}
}

View file

@ -10,22 +10,22 @@ extern crate tempdir;
extern crate curl;
extern crate rustc_serialize;
use std::io::{BufRead, BufReader};
use std::io::BufReader;
use std::fs::File;
use std::process;
use ansi_term::Colour;
use docopt::Docopt;
mod types;
mod tokenizer;
mod formatter;
mod updater;
mod error;
use types::LineType;
use tokenizer::Tokenizer;
use updater::Updater;
use error::TldrError;
use formatter::print_lines;
const USAGE: &'static str = "
@ -83,21 +83,6 @@ fn get_file_reader(filepath: &str) -> Result<BufReader<File>, String> {
}
/// Print a token stream to an ANSI terminal.
fn print_lines<R>(tokenizer: &mut Tokenizer<R>) where R: BufRead {
while let Some(token) = tokenizer.next() {
match token {
LineType::Empty => println!(""),
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::Other(text) => debug!("Unknown line type: {:?}", text),
}
}
}
#[cfg(feature = "logging")]
fn init_log() {
env_logger::init().unwrap();