From 28f676c263a2afb117931e951ee4a0caf165cff7 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 18 Jun 2018 21:43:28 -0400 Subject: [PATCH] bash_aliases: add elapsed time to prompt --- bash_aliases | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/bash_aliases b/bash_aliases index df739fb..3dc1076 100644 --- a/bash_aliases +++ b/bash_aliases @@ -31,19 +31,38 @@ function ps-color() { echo "\[\033[${codes}m\]" } -#function prompt_preexec() -#{ -# false -#} +function prompt_preexec() +{ + _command_start_time=$(date +%s) +} +prompt_preexec # Catch an enter keypress and call our preexec function. -#bind -x '"\M-\C-h1": prompt_preexec' -#bind '"\M-\C-h2": accept-line' -#bind '"\C-j": "\M-\C-h1\M-\C-h2"' -#bind '"\C-m": "\M-\C-h1\M-\C-h2"' +bind -x '"\M-\C-h1": prompt_preexec' +bind '"\M-\C-h2": accept-line' +bind '"\C-j": "\M-\C-h1\M-\C-h2"' +bind '"\C-m": "\M-\C-h1\M-\C-h2"' _last_exit_status=0 +function prompt_ps1_elapsed_time() +{ + local time="$1" + local elapsed=$(($time - $_command_start_time)) + if [ $elapsed -gt 3600 ]; then + local hours=$(($elapsed / 3600)) + local minutes=$((($elapsed % 3600) / 60)) + local seconds=$(($elapsed % 60)) + echo "${hours}h${minutes}m${seconds}s " + elif [ $elapsed -gt 60 ]; then + local minutes=$(($elapsed / 60)) + local seconds=$(($elapsed % 60)) + echo "${minutes}m${seconds}s " + elif [ $elapsed -gt 1 ]; then + echo "${elapsed}s " + fi +} + function prompt_ps1_exit_status_1() { if [ $_last_exit_status -ne 0 ]; then @@ -111,8 +130,10 @@ function prompt_ps1_svn_branch() fi } +# elapsed time +PS1="$(ps-color bold blue)\$(prompt_ps1_elapsed_time \D{%s})" # exit status -PS1="$(ps-color bold white on-red)\$(prompt_ps1_exit_status_1)$(ps-color reset)\$(prompt_ps1_exit_status_2)" +PS1="${PS1}$(ps-color bold white on-red)\$(prompt_ps1_exit_status_1)$(ps-color reset)\$(prompt_ps1_exit_status_2)" # user name and host name if [ "$USER" = "root" ]; then PS1="${PS1}$(ps-color bold red)"