Skip to content

Commit

Permalink
macos support (serl#13)
Browse files Browse the repository at this point in the history
* Document coreutils dependency

* Work with macos date command
  • Loading branch information
serl committed Feb 13, 2022
1 parent bd77700 commit d76cb93
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -11,6 +11,7 @@ An elegant(?), fast(?) and colorful(!) solution to have the complete(?) `syncthi
* bash 4+
* `curl`
* `jq`
* `coreutils` (for `numfmt`)
* works on Linux as well as on MacOS

## Env variables
Expand Down
12 changes: 11 additions & 1 deletion syncthing-quick-status.sh
Expand Up @@ -70,6 +70,16 @@ function format_time() {
echo "${COLOR_GRAY}$(echo "$1" | cut -d'.' -f1)${COLOR_RESET}"
}

function time_to_epoch() {
if date --version &>/dev/null; then
# GNU date
date --date="$1" +%s
else
# macos date
date -jf '%Y-%m-%dT%H:%M:%S' "$(echo "$1" | cut -d'.' -f1)" +%s
fi
}

function get_messages() { # $0 api_name jq_commands message_color_control_code max_age_in_seconds
call_jq "$1" "$2"
RESULT="$(jq_arg "$RESULT" '.when + " " + .message' | tail -n "$LOG_ENTRIES_LIMIT")"
Expand All @@ -82,7 +92,7 @@ function get_messages() { # $0 api_name jq_commands message_color_control_code m
[[ -z $line ]] && continue
when="$(echo "$line" | cut -d' ' -f1)"
message="$(echo "$line" | cut -d' ' -f2-)"
timestamp="$(date --date="$when" +%s)"
timestamp="$(time_to_epoch "$when")"
formatted_line="$(format_time "$when") ${message_color}$message${COLOR_RESET}"$'\n'
[[ $max_age -gt 0 ]] && [[ $timestamp -lt $min_timestamp ]] &&
continue
Expand Down

0 comments on commit d76cb93

Please sign in to comment.