From d04094560bca17a86a5dfefdf0c4118562dc481a Mon Sep 17 00:00:00 2001 From: Pi Lanningham Date: Fri, 10 Nov 2023 23:21:09 -0500 Subject: [PATCH] Add an example usage in the check command Feel free to do this differently, I just implemented it because i'm actually using it heh --- crates/aiken/src/cmd/check.rs | 98 +++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 10 deletions(-) diff --git a/crates/aiken/src/cmd/check.rs b/crates/aiken/src/cmd/check.rs index 0372b026..59e7c5fb 100644 --- a/crates/aiken/src/cmd/check.rs +++ b/crates/aiken/src/cmd/check.rs @@ -1,5 +1,10 @@ use std::path::PathBuf; +use aiken_project::watch; +use owo_colors::{OwoColorize, Stream::Stderr}; + +use crate::Terminal; + #[derive(clap::Args)] /// Type-check an Aiken project pub struct Args { @@ -18,10 +23,14 @@ pub struct Args { #[clap(long)] debug: bool, - // When enabled, re-run the command on file changes instead of exiting + /// When enabled, re-run the command on file changes instead of exiting #[clap(long)] watch: bool, + /// When enabled, clear the screen before running + #[clap(long)] + clear: bool, + /// Only run tests if they match any of these strings. /// You can match a module with `-m aiken/list` or `-m list`. /// You can match a test with `-m "aiken/list.{map}"` or `-m "aiken/option.{flatten_1}"` @@ -47,16 +56,85 @@ pub fn exec( match_tests, exact_match, no_traces, + watch, + clear, .. }: Args, ) -> miette::Result<()> { - crate::with_project(directory, deny, |p| { - p.check( - skip_tests, - match_tests.clone(), - debug, - exact_match, - (!no_traces).into(), - ) - }) + if watch { + watch::watch_project(directory, Terminal, watch::default_filter, 500, |p| { + if clear { + println!("{esc}c", esc = 27 as char); + } + let build_result = p.check( + skip_tests, + match_tests.clone(), + debug, + exact_match, + (!no_traces).into(), + ); + + let warnings = p.warnings(); + + let warning_count = warnings.len(); + + for warning in &warnings { + warning.report() + } + + let plural = if warning_count == 1 { "" } else { "s" }; + + if let Err(errs) = build_result { + for err in &errs { + err.report() + } + + eprintln!( + "\n{}", + "Summary" + .if_supports_color(Stderr, |s| s.purple()) + .if_supports_color(Stderr, |s| s.bold()) + ); + + let warning_text = format!("{warning_count} warning{plural}"); + + let plural = if errs.len() == 1 { "" } else { "s" }; + + let error_text = format!("{} error{}", errs.len(), plural); + + let full_summary = format!( + " {}, {}", + error_text.if_supports_color(Stderr, |s| s.red()), + warning_text.if_supports_color(Stderr, |s| s.yellow()) + ); + + eprintln!("{full_summary}"); + } else { + eprintln!( + "\n{}", + "Summary" + .if_supports_color(Stderr, |s| s.purple()) + .if_supports_color(Stderr, |s| s.bold()) + ); + + let warning_text = format!("{warning_count} warning{plural}"); + + eprintln!( + " 0 errors, {}", + warning_text.if_supports_color(Stderr, |s| s.yellow()), + ); + } + Ok(()) + }) + } else { + crate::with_project(directory, deny, |p| { + p.check( + skip_tests, + match_tests.clone(), + debug, + exact_match, + (!no_traces).into(), + ) + }) + } }