This repository has been archived by the owner on Jul 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
watch
70 lines (57 loc) · 1.84 KB
/
watch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /bin/zsh
# Setup empty variables
last_error_message=""
last_error_timestamp=""
last_error_time=""
last_message=""
time_since=0
while true;
do
# Run watch
result=$(make watch)
# If non-zero exit code (i.e. an error)
if [ $? -ne 0 ]
then
# Strip colour tags
message=$(echo "$result" | sed -E "s/\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
# Send message to notification centre
terminal-notifier -message "$message" -title "Watch Error"
# Output error in console
last_error_message=$result
last_error_time=$(date +"%T")
last_error_timestamp=$(date +%s)
last_message=""
else
last_message=$result
fi
# Clear the console
clear
# Right align the time
time=$(date +"%T")
title="Watching..."
let pos=$(tput cols)-${#title} # Work out how much to pad to right align
printf "\e[34m%s\e[33m%${pos}s\e[39m\n" $title "$time"
# If there was a previous error
if [ -n "$last_error_message" ]; then
# Work out how long ago the error occurred
let time_since=$(expr $(date +%s) - $last_error_timestamp)/60
# Print how long ago the error occurred
if [ $time_since -eq 0 ]; then time_since="Less than a minute ago"; else time_since="$time_since minutes ago"; fi
# Print the error
printf "\n\n\e[31mLast Error @ %s (%s)\n\e[39m\n%s\r\n\n" "$time_since" "$last_error_time" "$last_error_message"
# Print a line across the screen
printf '=%.0s' {1..$COLUMNS}
fi
# If there was a success message
if [ -n "$last_message" ]; then
printf "\n\n\e[32mLast Message\e[39m\n\n"
printf "%s\n" "$last_message"
fi
# Accept sleep time from command line, otherwise set to 0.5 seconds
if [ -n "$1" ]
then
sleep "$1"
else
sleep 0.5
fi
done