Last active
May 6, 2020 15:57
-
-
Save Chambras/df1709a86c928647f2d1a39f0879f73e to your computer and use it in GitHub Desktop.
main .bashrc file. Most of the aliases were move to .bash_aliasaes. Add this at the end of your current .bashrc
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
# git | |
parse_git_branch() { | |
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' | |
} | |
# Adding last command execution time | |
# source: https://jakemccrary.com/blog/2015/05/03/put-the-last-commands-run-time-in-your-bash-prompt/ | |
# https://stackoverflow.com/a/34812608 | |
timer_now() { | |
date +%s%N | |
} | |
timer_start() { | |
timer_start=${timer_start:-$(timer_now)} | |
} | |
timer_stop() { | |
local delta_us=$((($(timer_now) - $timer_start) / 1000)) | |
local us=$((delta_us % 1000)) | |
local ms=$(((delta_us / 1000) % 1000)) | |
local s=$(((delta_us / 1000000) % 60)) | |
local m=$(((delta_us / 60000000) % 60)) | |
local h=$((delta_us / 3600000000)) | |
# Goal: always show around 3 digits of accuracy | |
if ((h > 0)); then timer_show=${h}h${m}m | |
elif ((m > 0)); then timer_show=${m}m${s}s | |
elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s | |
elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s | |
elif ((ms >= 100)); then timer_show=${ms}ms | |
elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms | |
else timer_show=${us}us | |
fi | |
unset timer_start | |
} | |
# Show timestamp on the right hand side | |
# source: https://superuser.com/questions/187455/right-align-part-of-prompt/1203400#1203400 | |
# Create a string like: "[ 16:06:06 ]" with time in RED. | |
# Strip ANSI commands before counting length | |
# From: https://www.commandlinefu.com/commands/view/12043/remove-color-special-escape-ansi-codes-from-text-with-sed | |
showTime() | |
{ | |
Green='\[\e[00;32m\]' | |
White='\[\e[01;37m\]' | |
Cyan='\[\e[00;36m\]' | |
Yellow='\[\e[00;33m\]' | |
LightBlue='\[\e[00;94m\]' | |
Reset='\[\e[00m\]' | |
printf -v PS1RHS "\e[0m[ \e[1;31m%(%H:%M:%S)T \e[0m]" -1 # -1 is current time | |
PS1RHS_stripped=$(sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" <<<"$PS1RHS") | |
# Reference: https://en.wikipedia.org/wiki/ANSI_escape_code | |
Save='\e[s' # Save cursor position | |
Rest='\e[u' # Restore cursor to save point | |
# Add the ellapsed time and current date | |
timer_stop | |
# Save cursor position, jump to right hand edge, then go left N columns where | |
# N is the length of the printable RHS string. Print the RHS string, then | |
# return to the saved position and print the LHS prompt. | |
# Note: "\[" and "\]" are used so that bash can calculate the number of | |
# printed characters so that the prompt doesn't do strange things when | |
# editing the entered text. | |
PS1="\[${Save}\e[${COLUMNS:-$(tput cols)}C\e[${#PS1RHS_stripped}D${PS1RHS}${Rest}\]" | |
PS1="$PS1[last: ${timer_show}] $Green\u$White@$Cyan\h $Yellow\W$LightBlue$(parse_git_branch)$Reset \n$ " | |
} | |
trap 'timer_start' DEBUG | |
PROMPT_COMMAND='showTime' | |
# Show timestamp on history output | |
HISTTIMEFORMAT="%F %T " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment