This commit is contained in:
liv 2019-01-09 20:33:58 +01:00
parent a388bb3798
commit e03a98cbf6
3 changed files with 25 additions and 28 deletions

View file

@ -1,19 +1,19 @@
use clap::{App, Arg, SubCommand, crate_version}; use crate::run::run;
use syntect::easy::HighlightFile; use crate::verify::verify;
use syntect::parsing::SyntaxSet; use clap::{crate_version, App, Arg, SubCommand};
use syntect::highlighting::{ThemeSet, Style}; use notify::DebouncedEvent;
use syntect::util::{as_24_bit_terminal_escaped}; use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::io::BufRead; use std::io::BufRead;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::time::Duration; use std::time::Duration;
use notify::DebouncedEvent; use syntect::easy::HighlightFile;
use notify::{RecommendedWatcher, Watcher, RecursiveMode}; use syntect::highlighting::{Style, ThemeSet};
use crate::verify::verify; use syntect::parsing::SyntaxSet;
use crate::run::run; use syntect::util::as_24_bit_terminal_escaped;
mod run; mod run;
mod verify;
mod util; mod util;
mod verify;
fn main() { fn main() {
let matches = App::new("rustlings") let matches = App::new("rustlings")
@ -26,11 +26,12 @@ fn main() {
SubCommand::with_name("run") SubCommand::with_name("run")
.alias("r") .alias("r")
.arg(Arg::with_name("file").required(true).index(1)), .arg(Arg::with_name("file").required(true).index(1)),
).get_matches(); )
.get_matches();
let ss = SyntaxSet::load_defaults_newlines(); let ss = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults(); let ts = ThemeSet::load_defaults();
println!(r#" _ _ _ "#); println!(r#" _ _ _ "#);
println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#); println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#); println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
@ -55,7 +56,8 @@ fn main() {
} }
if let None = matches.subcommand_name() { if let None = matches.subcommand_name() {
let mut highlighter = HighlightFile::new("default_out.md", &ss, &ts.themes["base16-eighties.dark"]).unwrap(); let mut highlighter =
HighlightFile::new("default_out.md", &ss, &ts.themes["base16-eighties.dark"]).unwrap();
for maybe_line in highlighter.reader.lines() { for maybe_line in highlighter.reader.lines() {
let line = maybe_line.unwrap(); let line = maybe_line.unwrap();
let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line, &ss); let regions: Vec<(Style, &str)> = highlighter.highlight_lines.highlight(&line, &ss);
@ -76,14 +78,11 @@ fn watch() -> notify::Result<()> {
loop { loop {
match rx.recv() { match rx.recv() {
Ok(event) => { Ok(event) => match event {
match event { DebouncedEvent::Chmod(_) | DebouncedEvent::Write(_) => {
DebouncedEvent::Chmod(_) let _ignored = verify();
| DebouncedEvent::Write(_) => {
let _ignored = verify();
}
_ => {}
} }
_ => {}
}, },
Err(e) => println!("watch error: {:?}", e), Err(e) => println!("watch error: {:?}", e),
} }

View file

@ -1,7 +1,7 @@
use crate::util::clean;
use console::{style, Emoji}; use console::{style, Emoji};
use indicatif::ProgressBar; use indicatif::ProgressBar;
use std::process::Command; use std::process::Command;
use crate::util::clean;
pub fn run(matches: clap::ArgMatches) { pub fn run(matches: clap::ArgMatches) {
if let Some(filename) = matches.value_of("file") { if let Some(filename) = matches.value_of("file") {
@ -16,19 +16,17 @@ pub fn run(matches: clap::ArgMatches) {
if compilecmd.status.success() { if compilecmd.status.success() {
let runcmd = Command::new("./temp").output().expect("fail"); let runcmd = Command::new("./temp").output().expect("fail");
bar.finish_and_clear(); bar.finish_and_clear();
if runcmd.status.success() { if runcmd.status.success() {
println!("{}", String::from_utf8_lossy(&runcmd.stdout)); println!("{}", String::from_utf8_lossy(&runcmd.stdout));
let formatstr = let formatstr = format!("{} Successfully ran {}", Emoji("", ""), filename);
format!("{} Successfully ran {}", Emoji("", ""), filename);
println!("{}", style(formatstr).green()); println!("{}", style(formatstr).green());
clean(); clean();
} else { } else {
println!("{}", String::from_utf8_lossy(&runcmd.stdout)); println!("{}", String::from_utf8_lossy(&runcmd.stdout));
println!("{}", String::from_utf8_lossy(&runcmd.stderr)); println!("{}", String::from_utf8_lossy(&runcmd.stderr));
let formatstr = let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
println!("{}", style(formatstr).red()); println!("{}", style(formatstr).red());
clean(); clean();
} }

View file

@ -1,7 +1,7 @@
use crate::util::clean;
use console::{style, Emoji}; use console::{style, Emoji};
use indicatif::ProgressBar; use indicatif::ProgressBar;
use std::process::Command; use std::process::Command;
use crate::util::clean;
pub fn verify() -> Result<(), ()> { pub fn verify() -> Result<(), ()> {
compile_only("exercises/ex1.rs")?; compile_only("exercises/ex1.rs")?;