Skip to content

Commit e91eb18

Browse files
committed
Added -c option for colorized outut
1 parent 0abbc33 commit e91eb18

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ watcherd -v \
4848
### Usage
4949

5050
```shell
51-
Usage: watcherd -p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v]
51+
Usage: watcherd -p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v -c]
5252
watcherd --help
5353
watcherd --version
5454

@@ -83,6 +83,7 @@ Optional arguments:
8383
-i <int> When using the bash watcher, specify the interval in seconds
8484
for how often to look for directory changes.
8585
-v Verbose output.
86+
-c Colorized log output.
8687

8788
Misc arguments:
8889
--help Show this help screen.

bin/watcherd

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ IFS=$'\n'
2525

2626
# Versioning
2727
MY_NAME="watcherd"
28-
MY_DATE="2022-12-17"
28+
MY_DATE="2022-12-18"
2929
MY_URL="https://github.com/devilbox/watcherd"
3030
MY_AUTHOR="cytopia <[email protected]>"
3131
MY_GPGKEY="0xA02C56F0"
32-
MY_VERSION="1.0.6"
32+
MY_VERSION="1.0.7"
3333
MY_LICENSE="MIT"
3434

3535
# Default settings
36+
COLORIZED=0
3637
INTERVAL=1
3738
VERBOSE=0
3839
WATCHER="bash"
@@ -52,8 +53,41 @@ WITHOUT_SUBSHELL=1
5253
# Functions
5354
############################################################
5455

56+
log() {
57+
local type="${1}" # err, warn, info, ok
58+
local message="${2}" # message to log
59+
60+
# https://unix.stackexchange.com/questions/124407/what-color-codes-can-i-use-in-my-bash-ps1-prompt
61+
if [ "${COLORIZED:-}" = "1" ]; then
62+
local clr_green="\033[0;32m"
63+
local clr_yellow="\033[0;33m"
64+
local clr_red="\033[0;31m"
65+
local clr_rst="\033[0m"
66+
else
67+
local clr_green=
68+
local clr_yellow=
69+
local clr_red=
70+
local clr_rst=
71+
fi
72+
73+
if [ "${type}" = "err" ]; then
74+
printf "%s: ${clr_red}[ERR] %s${clr_rst}\n" "${MY_NAME}" "${message}" 1>&2 # stdout -> stderr
75+
fi
76+
if [ "${type}" = "warn" ]; then
77+
printf "%s: ${clr_yellow}[WARN] %s${clr_rst}\n" "${MY_NAME}" "${message}" 1>&2 # stdout -> stderr
78+
fi
79+
if [ "${VERBOSE:-}" = "1" ]; then
80+
if [ "${type}" = "info" ]; then
81+
printf "%s: [INFO] %s\n" "${MY_NAME}" "${message}"
82+
fi
83+
if [ "${type}" = "ok" ]; then
84+
printf "%s: ${clr_green}[OK] %s${clr_rst}\n" "${MY_NAME}" "${message}"
85+
fi
86+
fi
87+
}
88+
5589
function print_help() {
56-
printf "Usage: %s %s\\n" "${MY_NAME}" "-p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v]"
90+
printf "Usage: %s %s\\n" "${MY_NAME}" "-p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v -c]"
5791
printf " %s %s\\n" "${MY_NAME}" "--help"
5892
printf " %s %s\\n" "${MY_NAME}" "--version"
5993
printf "\\n"
@@ -84,6 +118,7 @@ function print_help() {
84118
printf " -i <int> %s\\n" "When using the bash watcher, specify the interval in seconds for how often"
85119
printf " %s\\n" "to look for directory changes."
86120
printf " -v %s\\n" "Verbose output."
121+
printf " -c %s\\n" "Colorized log output."
87122
printf "\\nMisc arguments:\\n"
88123
printf " --help %s\\n" "Show this help screen."
89124
printf " --version %s\\n" "Show version information."
@@ -131,13 +166,11 @@ function action() {
131166
action="${action//%n/${name}}"
132167

133168
if eval "${action}"; then
134-
if [ "${verbose}" -gt "0" ]; then
135-
printf "%s: [%s] [OK] %s succeeded: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}"
136-
fi
169+
log "ok" "${info} succeeded: ${directory}"
137170
return 0
138171
else
139-
printf "%s: [%s] [ERR] %s failed: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}" >&2
140-
printf "%s: [%s] [ERR] %s failed: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${action}" >&2
172+
log "err" "${info} failed: ${action}"
173+
log "err" "${info} failed: ${directory}"
141174
return 1
142175
fi
143176
}
@@ -152,11 +185,11 @@ function trigger() {
152185
if [ "${changes}" -eq "1" ]; then
153186
if eval "${action}"; then
154187
if [ "${verbose}" -gt "0" ]; then
155-
printf "%s: [%s] [OK] %s succeeded: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "TRG" "${action}"
188+
log "ok" "TRG succeeded: ${action}"
156189
fi
157190
return 0
158191
else
159-
printf "%s: [%s] [ERR] %s failed: %s\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )" "TRG" "${action}" >&2
192+
log "err" "TRG failed: ${action}"
160193
# Also return 0 here in order to not abort the loop
161194
return 0
162195
fi
@@ -253,6 +286,9 @@ while [ $# -gt 0 ]; do
253286
-v)
254287
VERBOSE="1"
255288
;;
289+
-c)
290+
COLORIZED="1"
291+
;;
256292
--help)
257293
print_help
258294
exit 0
@@ -290,10 +326,7 @@ fi
290326
############################################################
291327

292328
# Log startup
293-
if [ "${VERBOSE}" -gt "0" ]; then
294-
printf "%s: [%s] Starting daemon.\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
295-
fi
296-
329+
log "info" "Starting daemon: $( date '+%Y-%m-%d %H:%M:%S' )"
297330

298331
CHANGES=0
299332
ALL_DIRS="$( get_subdirs "${WATCH_DIR}" )"
@@ -320,9 +353,7 @@ CHANGES=0
320353

321354
# Use native inotify
322355
if [ "${WATCHER}" = "inotify" ]; then
323-
if [ "${VERBOSE}" -gt "0" ]; then
324-
printf "%s: [%s] Using native inotify to watch for changes.\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
325-
fi
356+
log "info" "Using native inotify to watch for changes."
326357
inotifywait \
327358
--quiet \
328359
--monitor \
@@ -345,9 +376,7 @@ if [ "${WATCHER}" = "inotify" ]; then
345376
done
346377
# Use custom inotify
347378
else
348-
if [ "${VERBOSE}" -gt "0" ]; then
349-
printf "%s: [%s] Using bash loop to watch for changes.\\n" "${MY_NAME}" "$( date '+%Y-%m-%d %H:%M:%S' )"
350-
fi
379+
log "info" "Using bash loop to watch for changes."
351380
while true; do
352381
# Get all directories
353382
NEW_DIRS="$( get_subdirs "${WATCH_DIR}" )"

tests/01.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ touch "${DIR_PATH}/file 2"
3636
### 02. Setup expected
3737
###
3838
{
39-
echo "[OK] ADD succeeded: ./dir 1"
40-
echo "[OK] ADD succeeded: ./dir 2"
41-
echo "[OK] ADD succeeded: ./dir 3"
42-
echo "[OK] ADD succeeded: ./dir 4"
39+
echo "[OK] ADD succeeded: ./dir 1"
40+
echo "[OK] ADD succeeded: ./dir 2"
41+
echo "[OK] ADD succeeded: ./dir 3"
42+
echo "[OK] ADD succeeded: ./dir 4"
4343
} > "${SCRIPT_PATH}/01.expected"
4444

4545

0 commit comments

Comments
 (0)