summaryrefslogtreecommitdiffstats
path: root/.bash_prompt
diff options
context:
space:
mode:
authorMichal Minář <mic.liamg@gmail.com>2019-03-28 12:06:42 +0100
committerMichal Minář <mic.liamg@gmail.com>2019-03-28 12:06:42 +0100
commite960e1ea2d8c286eaba0a5ad8bb31cac2e44a4ca (patch)
treedfda5a2cb4382c2fe0bfa2bf498961e98df6e867 /.bash_prompt
parentbf5bae4d302a7f050f9cbe481f1a1d5962aedb5f (diff)
downloadrcs-master.zip
rcs-master.tar.gz
rcs-master.tar.xz
bash_prompt: print execution timeHEADmaster
Signed-off-by: Michal Minář <mic.liamg@gmail.com>
Diffstat (limited to '.bash_prompt')
-rw-r--r--.bash_prompt50
1 files changed, 45 insertions, 5 deletions
diff --git a/.bash_prompt b/.bash_prompt
index 3b12df6..6e08ddd 100644
--- a/.bash_prompt
+++ b/.bash_prompt
@@ -50,12 +50,44 @@ else
PROMPT_COMMAND=__prompt_command_no_color
fi
+unset __prompt_show_shell_level
+function __prompt_show_shell_level() {
+ printf '>%0.s' `seq $SHLVL`
+}
+
+unset __timer_start
+function __timer_start {
+ __START_TIME="${__START_TIME:-$SECONDS}"
+}
+
+unset __timer_stop
+function __timer_stop {
+ [[ -z "${__START_TIME:-}" ]] && return 0
+ local secs="$(($SECONDS - $__START_TIME))"
+ unset __START_TIME
+ if [[ "${secs:-0}" -le 0 ]]; then
+ __ELAPSED_TIME="0s"
+ return 0
+ fi
+ local elapsed=""
+ local hours="$(($secs / 3600))"
+ [[ "${hours:-0}" -gt 0 ]] && elapsed+="${hours}h"
+ local minutes="$((($secs / 60) % 60))"
+ [[ "${minutes:-0}" -gt 0 ]] && elapsed+="${minutes}m"
+ secs="$(($secs % 60))"
+ [[ "${secs:-0}" -gt 0 ]] && elapsed+="${secs}s"
+ __ELAPSED_TIME="${elapsed:-}"
+}
+
# Run 256colors.pl script in 256 color xterm to check out possible colors. To
# get the escape sequence run: `echo $(tput setaf ${color_index}) | hexdump -C`.
+unset __make_prompt_string
function __make_prompt_string() {
local exit_code="$1"
local context
+ local elapsed="${__ELAPSED_TIME:-}"
+ unset __ELAPSED_TIME
if [[ "${TERM256COLORS:-0}" == 1 ]]; then
local c_mark='\e[38;5;163m'
local c_local='\e[38;5;220m'
@@ -91,6 +123,7 @@ function __make_prompt_string() {
# \[ \] can be safely omitted until new line character because cursor won't
# be placed here
+ [[ -n "${elapsed:-}" ]] && PS1+="${c_mark}(${c_info}${elapsed}${c_mark}) ${c_info}"
PS1+="${exit_code:-}${context:-}${c_mark}[${c_info}"'`date +%H:%M:%S`'"${c_mark}] "
PS1+="${c_local}\u${c_at}@${c_local}\h: ${c_path}\W${c_info}"
PS1+='`declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)"` '
@@ -100,13 +133,12 @@ function __make_prompt_string() {
PS1+="\n\[${c_mark}\]\$\[${c_none}\] "
}
-function __prompt_show_shell_level() {
- printf '>%0.s' `seq $SHLVL`
-}
-
+unset __make_prompt_string_no_color
function __make_prompt_string_no_color() {
local exit_code="$1"
local context=''
+ local elapsed="${__ELAPSED_TIME:-}"
+ unset __ELAPSED_TIME
if [[ -n "${exit_code}" ]]; then
exit_code="${exit_code}}"
fi
@@ -115,13 +147,16 @@ function __make_prompt_string_no_color() {
fi
PS1=""
[[ "${HISTFILE}" == /dev/null ]] && PS1="NOHIST "
+ [[ -n "${elapsed:-}" ]] && PS1+="(${elapsed}) "
PS1+="${exit_code:-}${context:-}"'[`date +%H:%M:%S`] \u@\h: \W'
PS1+='$(declare -F __git_ps1 &>/dev/null && __git_ps1 " (%s)") '
PS1+="${c_mark}"'$(__prompt_show_shell_level) \n\$ '
}
+unset __prompt_command
function __prompt_command() {
local code="$?"
+ __timer_stop
history -a
__make_prompt_string "${code}"
# set terminal's title
@@ -134,13 +169,18 @@ function __prompt_command() {
if [[ "${host}" =~ ^([^.]+\.).+(\.[^.]+)?$ ]]; then
host="${BASH_REMATCH[1]}"$'\u2026'"${BASH_REMATCH[2]}"
fi
- echo -ne "\033]0;${USER}@${host}: ${pth}\007"
+ printf "\33]0;%s@%s: %s\7" "${USER}" "${host}" "${pth}"
+ unset __START_TIME
}
+unset __prompt_command_no_color
function __prompt_command_no_color() {
local code="$?"
+ __timer_stop
history -a
__make_prompt_string_no_color "${code}"
+ unset __START_TIME
}
unset color_prompt
+trap '__timer_start' DEBUG