|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Create a temporary helper script in /tmp |
| 4 | +PROCESS_SCRIPT="/tmp/_process_lines_$$.sh" |
| 5 | + |
| 6 | +cat > "$PROCESS_SCRIPT" <<'EOF' |
| 7 | +#!/bin/bash |
| 8 | +
|
| 9 | +last_dot_time=0 |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +RED='\033[0;31m' |
| 12 | +GREEN='\033[0;32m' |
| 13 | +
|
| 14 | +
|
| 15 | +strip_ansi() { |
| 16 | + echo "$1" | sed -E 's/\x1B\[[0-9;]*[mGKHF]//g' |
| 17 | +} |
| 18 | +
|
| 19 | +while IFS= read -r line; do |
| 20 | + now=$(date +%s) |
| 21 | + clean_line=$(strip_ansi "$line") |
| 22 | +
|
| 23 | + if [[ "$clean_line" =~ === && ! "$clean_line" =~ =========== ]]; then |
| 24 | + echo -e "${YELLOW}🟢 $clean_line\033[0m" |
| 25 | + continue |
| 26 | + fi |
| 27 | +
|
| 28 | + if [[ "$clean_line" =~ ailures\:\ 0 ]]; then |
| 29 | + echo -e "${GREEN}🟢 $clean_line\033[0m" |
| 30 | + continue |
| 31 | + fi |
| 32 | +
|
| 33 | + if [[ "$clean_line" =~ [Ff]ailures: ]]; then |
| 34 | + res_count=$(echo "$clean_line" | sed -nE 's/.*[Ff]ailures:[[:space:]]*([0-9]+).*[[:space:]]*$/\1/p') |
| 35 | + if [[ "$res_count" != "0" && -n "$res_count" ]]; then |
| 36 | + echo -e "${RED}🔴 $clean_line\033[0m" |
| 37 | + echo -en "\a" ; echo -en "\a" # Beep sound twice |
| 38 | + |
| 39 | + fi |
| 40 | + continue |
| 41 | + fi |
| 42 | +
|
| 43 | + if [[ "$clean_line" =~ [Ss]uccesses: ]]; then |
| 44 | + res_count=$(echo "$clean_line" | sed -nE 's/.*[Ss]uccesses:[[:space:]]*([0-9]+).*[[:space:]]*$/\1/p') |
| 45 | + if [[ -n "$res_count" ]]; then |
| 46 | + echo -e "${GREEN}🟢 $clean_line\033[0m" |
| 47 | + continue |
| 48 | + fi |
| 49 | + fi |
| 50 | +
|
| 51 | + # Default case: print dot every 4 seconds |
| 52 | + if (( now - last_dot_time >= 4 )); then |
| 53 | + echo -n "."; last_dot_time=$now |
| 54 | + fi |
| 55 | +done |
| 56 | +EOF |
| 57 | + |
| 58 | +chmod +x "$PROCESS_SCRIPT" |
| 59 | + |
| 60 | +# Cleanup handler |
| 61 | +cleanup() { |
| 62 | + echo -e "\n🛑 Caught Ctrl+C. Cleaning up..." |
| 63 | + [[ -n "$WATCH_PID" ]] && kill -- -"$WATCH_PID" 2>/dev/null |
| 64 | + rm -f "$PROCESS_SCRIPT" |
| 65 | + exit 0 |
| 66 | +} |
| 67 | + |
| 68 | +trap cleanup SIGINT |
| 69 | + |
| 70 | +last_seen_file="" |
| 71 | + |
| 72 | +while true; do |
| 73 | + if [[ -n "$1" ]]; then |
| 74 | + TEE_FILE="$1" |
| 75 | + else |
| 76 | + TEE_FILE=$(ls -t TEE*.ansi | head -n 1) |
| 77 | + fi |
| 78 | + |
| 79 | + if [[ -z "$TEE_FILE" ]]; then |
| 80 | + echo "❌ No matching file found (TEE*.ansi) — retrying in 2 seconds..." |
| 81 | + sleep 2 |
| 82 | + continue |
| 83 | + fi |
| 84 | + |
| 85 | + if [[ "$TEE_FILE" == "$last_seen_file" ]]; then |
| 86 | + sleep 2 |
| 87 | + continue |
| 88 | + fi |
| 89 | + |
| 90 | + echo -e "📄 Now watching: $TEE_FILE" |
| 91 | + last_seen_file="$TEE_FILE" |
| 92 | + |
| 93 | + # Run tail | process in a subshell group, and capture the group PID |
| 94 | + ( |
| 95 | + stdbuf -oL tail -n0 -F "$TEE_FILE" 2>/dev/null | "$PROCESS_SCRIPT" |
| 96 | + ) & |
| 97 | + WATCH_PID=$! |
| 98 | + |
| 99 | + # Wait until file changes or Ctrl+C |
| 100 | + while [[ "$TEE_FILE" == "$last_seen_file" ]]; do |
| 101 | + sleep 2 |
| 102 | + TEE_FILE=$(ls -t TEE*.ansi 2>/dev/null | head -n 1) |
| 103 | + done |
| 104 | + |
| 105 | + echo -e "🔁 Detected newer file: $TEE_FILE" |
| 106 | + kill -- -"$WATCH_PID" 2>/dev/null |
| 107 | +done |
| 108 | + |
0 commit comments