Created
August 1, 2018 08:44
-
-
Save hfossli/9485b8c68355855490768baafb6b0f69 to your computer and use it in GitHub Desktop.
Get time difference between output line. Supports logs from fastlane as well!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CLEAR='\033[0m' | |
RED='\033[38;5;160m' | |
ORANGE='\033[38;5;202m' | |
YELLOW='\033[38;5;220m' | |
GREEN='\033[0;32m' | |
BLUE='\033[0;36m' | |
GREY='\033[38;5;237m' | |
# default values | |
THRESHOLD_LOW=1 | |
THRESHOLD_MEDIUM=5 | |
THRESHOLD_HIGH=10 | |
RESET_MARK="" | |
PARSE_FASTLANE_DATE=false | |
function usage() { | |
if [ -n "$1" ]; then | |
echo -e "${RED}👉 $1${CLEAR}\n"; | |
fi | |
echo "Usage: $0 [-l low] [-m medium] [-h high]" | |
echo " -l, --low Threshold in seconds for low duration color formatting" | |
echo " -m, --medium Threshold in seconds for medium duration color formatting" | |
echo " -h, --high Threshold in seconds for high duration color formatting" | |
echo " -r, --reset-mark String match to reset total counter" | |
echo " -p, --parse-fastlane-date Parse date from fastlane output" | |
echo "" | |
echo "Example: $0 --low 10 --medium 30 --high 60 --reset-mark \"--- Step: \w* ---\"" | |
exit 1 | |
} | |
# parse params | |
while [[ "$#" > 0 ]]; do case $1 in | |
-l|--low) THRESHOLD_LOW="$2";shift;shift;; | |
-m|--medium) THRESHOLD_MEDIUM="$2";shift;shift;; | |
-h|--high) THRESHOLD_HIGH="$2"; shift;shift;; | |
-r|--reset-mark) RESET_MARK="$2"; shift;shift;; | |
-p|--parse-fastlane-date) PARSE_FASTLANE_DATE=true; shift;; | |
*) usage "Unknown parameter passed: $1"; shift; shift;; | |
esac; done | |
# verify valid params | |
if ! [ "$THRESHOLD_LOW" -eq "$THRESHOLD_LOW" ] 2>/dev/null; then usage "Low threshold is not a number"; fi; | |
if ! [ "$THRESHOLD_MEDIUM" -eq "$THRESHOLD_MEDIUM" ] 2>/dev/null; then usage "Medium threshold is not a number"; fi; | |
if ! [ "$THRESHOLD_HIGH" -eq "$THRESHOLD_HIGH" ] 2>/dev/null; then usage "High threshold is not a number"; fi; | |
function seconds_from_date() { | |
if [[ ! -z "$@" ]]; then | |
echo "$@" | sed 's/^/((/; s/:/)*60+/g' | bc | |
fi | |
} | |
function seconds_between() { | |
echo $(($2-$1)) | |
} | |
function color_for_duration() { | |
if [ $1 -ge $THRESHOLD_HIGH ]; then | |
echo $RED | |
elif [ $1 -ge $THRESHOLD_MEDIUM ]; then | |
echo $ORANGE | |
elif [ $1 -ge $THRESHOLD_LOW ]; then | |
echo $YELLOW | |
else | |
echo $GREY | |
fi | |
} | |
# loop over piped input | |
first_time="" | |
last_time="" | |
total_diff="0" | |
while read -s -r -e; do | |
if $PARSE_FASTLANE_DATE; then | |
date=$(echo ${REPLY:1:8} | grep "\d\d:\d\d:\d\d") | |
time="$(seconds_from_date $date)" | |
else | |
time=$(gdate +%s) | |
fi | |
if [[ ! -z $RESET_MARK ]] && echo $REPLY | grep -q "$RESET_MARK"; then | |
first_time="" | |
echo -e "${BLUE}Reseting timer -----------$CLEAR" | |
fi | |
if [ "$time" != "" ]; then | |
if [ "$first_time" = "" ]; then | |
first_time="$time" | |
fi | |
if [ "$last_time" = "" ]; then | |
last_time="$time" | |
fi | |
last_diff=$(seconds_between $last_time $time) | |
total_diff=$(seconds_between $first_time $time) | |
color=$(color_for_duration $last_diff) | |
format=$(printf "$color%-14s$CLEAR $GREY= %-14s$CLEAR" "+ $last_diff seconds" "$total_diff seconds") | |
echo -e "$format $REPLY" | |
last_time="$time" | |
else | |
color=$(color_for_duration 0) | |
format=$(printf "$GREY%-14s$CLEAR $GREY= %-14s$CLEAR" "+ 0 seconds" "$total_diff seconds") | |
echo -e "$format $REPLY" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Interactive
your_command | ./time-diff-output.sh
Parse from a fastlane log file