Skip to content

Instantly share code, notes, and snippets.

@shepting
Created February 6, 2019 22:51
Show Gist options
  • Save shepting/74f556e95f235e33ae18993c3d83c3bf to your computer and use it in GitHub Desktop.
Save shepting/74f556e95f235e33ae18993c3d83c3bf to your computer and use it in GitHub Desktop.
Bash Command Timer
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Copyright (C) 2014 Chuan Ji <ji@chu4n.com> #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# A simple Bash script for printing timing information for each command line
# executed.
#
# For the most up-to-date version, as well as further information and
# installation instructions, please visit the GitHub project page at
# https://github.com/jichuan89/bash-command-timer
# SETTINGS
# ========
# The color of the output.
#
# This should be a color string usable in a VT100 escape sequence (see
# http://en.wikipedia.org/wiki/ANSI_escape_code#Colors), without the
# escape sequence prefix and suffix. For example, bold red would be '1;31'.
#
# If empty, disable colored output. Set it to empty if your terminal does not
# support VT100 escape sequences.
BCT_COLOR='1;30'
# The display format of the current time.
#
# This is a strftime format string (see http://strftime.org/). To tweak the
# display format of the current time, change the following line to your desired
# pattern.
#
# If empty, disables printing of current time.
BCT_TIME_FORMAT='%b %d %I:%M%p'
# IMPLEMENTATION
# ==============
# BCTTime:
#
# Command to print out the current time in nanoseconds. This is required
# because the "date" command in OS X and BSD do not support the %N sequence.
BCTTime="gdate '+%s%N'"
function BCTPrintTime() {
gdate --date="@$1" +"$BCT_TIME_FORMAT"
}
# The debug trap is invoked before the execution of each command typed by the
# user (once for every command in a composite command) and again before the
# execution of PROMPT_COMMAND after the user's command finishes. Thus, to be
# able to preserve the timestamp before the execution of the first command, we
# set the BCT_AT_PROMPT flag in PROMPT_COMMAND, only set the start time if the
# flag is set and clear it after the first execution.
BCT_AT_PROMPT=1
function BCTPreCommand() {
if [ -z "$BCT_AT_PROMPT" ]; then
return
fi
unset BCT_AT_PROMPT
BCT_COMMAND_START_TIME=$(eval $BCTTime)
}
trap 'BCTPreCommand' DEBUG
# Flag to prevent printing out the time upon first login.
BCT_FIRST_PROMPT=1
# This is executed before printing out the prompt.
function BCTPostCommand() {
SUCCESS=$?
BCT_AT_PROMPT=1
if [ -n "$BCT_FIRST_PROMPT" ]; then
unset BCT_FIRST_PROMPT
return
fi
# BCTTime prints out time in nanoseconds.
local MSEC=1000000
local SEC=$(($MSEC * 1000))
local MIN=$((60 * $SEC))
local HOUR=$((60 * $MIN))
local DAY=$((24 * $HOUR))
local command_start_time=$BCT_COMMAND_START_TIME
local command_end_time=$(eval $BCTTime)
local command_time=$(($command_end_time - $command_start_time))
local num_days=$(($command_time / $DAY))
local num_hours=$(($command_time % $DAY / $HOUR))
local num_mins=$(($command_time % $HOUR / $MIN))
local num_secs=$(($command_time % $MIN / $SEC))
local num_msecs=$(($command_time % $SEC / $MSEC))
local time_str=""
if [ $num_days -gt 0 ]; then
time_str="${time_str}${num_days}d "
fi
if [ $num_hours -gt 0 ]; then
time_str="${time_str}${num_hours}h "
fi
if [ $num_mins -gt 0 ]; then
time_str="${time_str}${num_mins}m "
fi
local num_msecs_pretty=$(printf '%01d' $num_msecs)
time_str="Last command: ${time_str}${num_secs}.${num_msecs_pretty}s Exit Code: ${SUCCESS}"
echo -e "\033[${BCT_COLOR}m${time_str}\033[0m"
}
PROMPT_COMMAND='BCTPostCommand'
@xianwen
Copy link

xianwen commented May 17, 2019

brew install coreutils is needed since this script relies on gdate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment