From 1d495ff7b9c0a9d638576558d409533e80ba2330 Mon Sep 17 00:00:00 2001 From: olivia Date: Mon, 26 Nov 2018 11:10:38 +0100 Subject: [PATCH] add markdown output for default command --- Cargo.toml | 1 + default_out.md | 4 ++++ src/main.rs | 22 +++++++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 default_out.md diff --git a/Cargo.toml b/Cargo.toml index bf4195dd..5ac31cde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["olivia "] clap = "2.32.0" indicatif = "0.9.0" console = "0.6.2" +syntect = "3.0.2" diff --git a/default_out.md b/default_out.md new file mode 100644 index 00000000..7cbdffcf --- /dev/null +++ b/default_out.md @@ -0,0 +1,4 @@ +## Welcome to Rustlings! + +To get started, run `rustlings verify` in order to get the first exercise. +Make sure to have your editor open! \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1a085ef0..e84dff75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,15 +2,21 @@ extern crate clap; extern crate console; extern crate indicatif; +extern crate syntect; use clap::{App, Arg, SubCommand}; use console::{style, Emoji}; use indicatif::ProgressBar; +use syntect::easy::HighlightFile; +use syntect::parsing::SyntaxSet; +use syntect::highlighting::{ThemeSet, Style}; +use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings}; use std::fs::remove_file; +use std::io::BufRead; use std::process::Command; fn main() { - let matches = App::new("r2") + let matches = App::new("rustlings") .version(crate_version!()) .author("Olivia Hugger") .about("Test") @@ -21,6 +27,9 @@ fn main() { .arg(Arg::with_name("file").required(true).index(1)), ).get_matches(); + let ss = SyntaxSet::load_defaults_newlines(); + let ts = ThemeSet::load_defaults(); + println!(r#" _ _ _ "#); println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#); println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#); @@ -130,6 +139,17 @@ fn main() { compile_only("exercises/error_handling/option1.rs"); test("exercises/error_handling/result1.rs"); } + + if let None = matches.subcommand_name() { + let mut highlighter = HighlightFile::new("default_out.md", &ss, &ts.themes["Solarized (dark)"]).unwrap(); + for maybe_line in highlighter.reader.lines() { + let line = maybe_line.unwrap(); + let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line, &ss); + println!("{}", as_24_bit_terminal_escaped(®ions[..], true)); + } + } + + println!("\x1b[0m"); } fn compile_only(filename: &str) {