diff --git a/Cargo.toml b/Cargo.toml index 0f18f54..a8d454c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,6 @@ user-hooks = [] libc = "0.2.43" lazy_static = "1.1" semver = "0.9.0" + +[build-dependencies] +colored = "1" diff --git a/build.rs b/build.rs index e3b499c..61ea9df 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,5 @@ +extern crate colored; +use colored::*; use fs::File; use io::{BufRead, Read, Write}; use path::{Path, PathBuf}; @@ -103,7 +105,7 @@ fn hook_already_exists(hook: &Path) -> bool { fn write_script(w: &mut W) -> Result<()> { macro_rules! raw_cmd { ($c:expr) => { - concat!("\necho '+", $c, "'\n", $c) + concat!("\nrun ", $c) }; } @@ -153,13 +155,42 @@ fn write_script(w: &mut W) -> Result<()> { # Output at {} # -set -e +animation() {{ + sp='/-\|' + printf ' ' + sleep 0.1 + while true; do + printf '\b%.1s' "$sp" + sp=${{sp#?}}${{sp%???}} + sleep 0.1 + done +}} + +run() {{ + printf "%s" "{} '$*' " + animation & + animation_pid="$!" + output="$("$@" 2>&1)" + ret=$? + kill "$animation_pid" + wait "$animation_pid" 2>/dev/null + if [ $ret -eq 0 ]; then + printf "\b- {}\n" + else + printf "\b- {}\n" + printf "%s" "$output" + exit $ret + fi +}} {}"#, env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_HOMEPAGE"), env!("CARGO_MANIFEST_DIR"), path::MAIN_SEPARATOR, env::var("OUT_DIR").unwrap_or_else(|_| "".to_string()), + "Running".bold(), + "Ok".bold().green(), + "Failed".bold().red(), script )?; Ok(()) diff --git a/test/test.rs b/test/test.rs index f3b3748..843b97e 100644 --- a/test/test.rs +++ b/test/test.rs @@ -147,7 +147,10 @@ fn default_behavior() { .unwrap() .contains(format!("set by cargo-husky v{}", env!("CARGO_PKG_VERSION")).as_str())); assert_eq!( - script.lines().filter(|l| *l == "cargo test --all").count(), + script + .lines() + .filter(|l| *l == "run cargo test --all") + .count(), 1 ); assert!(script.lines().all(|l| !l.contains("cargo clippy"))); @@ -186,19 +189,22 @@ fn change_features() { assert_eq!(get_hook_script(&root, "pre-push"), None); let script = get_hook_script(&root, "pre-commit").unwrap(); - assert!(script.lines().all(|l| l != "cargo test")); + assert!(script.lines().all(|l| l != "run cargo test")); assert_eq!( script .lines() - .filter(|l| *l == "cargo clippy -- -D warnings") + .filter(|l| *l == "run cargo clippy -- -D warnings") .count(), 1 ); - assert_eq!(script.lines().filter(|l| *l == "cargo check").count(), 1); + assert_eq!( + script.lines().filter(|l| *l == "run cargo check").count(), + 1 + ); assert_eq!( script .lines() - .filter(|l| *l == "cargo fmt -- --check") + .filter(|l| *l == "run cargo fmt -- --check") .count(), 1 ); @@ -218,24 +224,30 @@ fn change_features_using_run_for_all() { let script = get_hook_script(&root, "pre-commit").unwrap(); assert_eq!( - script.lines().filter(|l| *l == "cargo test --all").count(), + script + .lines() + .filter(|l| *l == "run cargo test --all") + .count(), 1 ); assert_eq!( script .lines() - .filter(|l| *l == "cargo clippy --all -- -D warnings") + .filter(|l| *l == "run cargo clippy --all -- -D warnings") .count(), 1 ); assert_eq!( - script.lines().filter(|l| *l == "cargo check --all").count(), + script + .lines() + .filter(|l| *l == "run cargo check --all") + .count(), 1 ); assert_eq!( script .lines() - .filter(|l| *l == "cargo fmt --all -- --check") + .filter(|l| *l == "run cargo fmt --all -- --check") .count(), 1 );