diff options
| author | tslil clingman <> | 2020-10-30 20:48:49 -0400 |
|---|---|---|
| committer | tslil clingman <> | 2020-10-30 20:54:19 -0400 |
| commit | ceba6f8fcda9a882d3dbbfb23c004beabf99e320 (patch) | |
| tree | 68ca385bd1eb7894be829e712336064f5b87c111 /.bashprompt | |
| parent | 7c1beacef1dbed322e99d633742da6b2f655c603 (diff) | |
Merging fancy bash prompt
Diffstat (limited to '.bashprompt')
| -rw-r--r-- | .bashprompt | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/.bashprompt b/.bashprompt new file mode 100644 index 0000000..295a0f3 --- /dev/null +++ b/.bashprompt @@ -0,0 +1,85 @@ +# Based on the brilliant https://redandblack.io/blog/2020/bash-prompt-with-updating-time/ + +NEWLINE="\n" +SINGLE_SPACE=" " +PRINTING_OFF="\[" +PRINTING_ON="\]" +RESTORE_CURSOR_POSITION="\e[u" +SAVE_CURSOR_POSITION="\e[s" + +NON_COLOUR="$(tput sgr0)" +DEF_COLOUR="$(tput setaf 240)" +TIM_COLOUR="$(tput bold)$(tput setaf 32)" +DIR_COLOUR="$(tput bold)$(tput setaf 201)" +ERR_COLOUR="$(tput setaf 9)" +DOL_COLOUR="$(tput rev)$(tput setaf 34)" +VCS_COLOUR="$(tput setaf 214)" + +HOST="\H:" +PS2_PROMPT=">" +DIRECTORY="\w" +TIMESTAMP="date +%m/%H.%M" + +move_cursor_to_start_of_ps1() { + command_rows=$(history 1 | wc -l) + if [ "$command_rows" -gt 1 ]; then + let vertical_movement=$command_rows+1 + else + command=$(history 1 | sed 's/^\s*[0-9]*\s*//') + command_length=${#command} + ps1_prompt_length=${#PS1_PROMPT} + let total_length=$command_length+$ps1_prompt_length + let lines=$total_length/${COLUMNS}+1 + let vertical_movement=$lines + fi + tput cuu $vertical_movement +} + +PS0_ELEMENTS=( + "$SAVE_CURSOR_POSITION" "\$(move_cursor_to_start_of_ps1)" + "(" "$TIM_COLOUR" "\$($TIMESTAMP)" "$NON_COLOUR" ")" "$RESTORE_CURSOR_POSITION" +) +PS0=$(IFS=; echo "${PS0_ELEMENTS[*]}") + +set_ps1_prompt() { + local rval=$?; + + local PS1_SYMBOL="#"; + (( $(id -u) )) && PS1_SYMBOL=\$; + + if (( $rval )); then + local PROMPT_COLOUR="$(tput rev)$ERR_COLOUR" + local ERROR_ELEMENTS=( + "$PRINTING_OFF" "$ERR_COLOUR" "$PRINTING_ON" + "<$rval>" + "$PRINTING_OFF" "$DEF_COLOUR" "$PRINTING_ON" + ) + local ERROR_VALUE=$(IFS=; echo "${ERROR_ELEMENTS[*]}") + else + local PROMPT_COLOUR="$DOL_COLOUR" + local ERROR_VALUE="" + fi + PS1_ELEMENTS=( + "(" "$PRINTING_OFF" "$DEF_COLOUR" "$PRINTING_ON" "\$($TIMESTAMP)" + "$PRINTING_OFF" "$NON_COLOUR" "$PRINTING_ON" ")" "[" "$HOST" + "$PRINTING_OFF" "$DIR_COLOUR" "$PRINTING_ON" "$DIRECTORY" + "$PRINTING_OFF" "$NON_COLOUR" "$PRINTING_ON" "]" + "$PRINTING_OFF" "$VCS_COLOUR" "$PRINTING_ON" "\$(vcs)" + "$ERROR_VALUE" + "$PRINTING_OFF" "$PROMPT_COLOUR" "$PRINTING_ON" "$PS1_SYMBOL" + "$PRINTING_OFF" "$NON_COLOUR" "$PRINTING_ON" "$SINGLE_SPACE" + ) + PS1=$(IFS=; echo "${PS1_ELEMENTS[*]}") +} + +PROMPT_COMMAND=set_ps1_prompt + +PS2_ELEMENTS=( + "$PRINTING_OFF" "$PROMPT_COLOUR" "$PRINTING_ON" "$PS2_PROMPT" + "$SINGLE_SPACE" "$PRINTING_OFF" "$NON_COLOUR" "$PRINTING_ON" +) +PS2=$(IFS=; echo "${PS2_ELEMENTS[*]}") + +shopt -s histverify + +PS4='[$EPOCHREALTIME]' |
