diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-01-15 14:00:30 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-01-15 16:11:54 +0200 |
commit | 4a81e1d3940ccf915771618e84e28870e82dfe2d (patch) | |
tree | 13ab95a9bb0416f32f41ed1fea7ff954489a95a0 /bashrc | |
parent | 87e7bacfca96ff7fe85ed4158c1b7bf502295748 (diff) | |
download | liw-dot-files-4a81e1d3940ccf915771618e84e28870e82dfe2d.tar.gz |
Refactor prompt_command
Diffstat (limited to 'bashrc')
-rw-r--r-- | bashrc | 177 |
1 files changed, 108 insertions, 69 deletions
@@ -1,18 +1,85 @@ +######################################################################## +# Colour definitions for parts of $PS1. + + +# Do we even have a terminal we can do things to? +we_have_term() +{ + [ "$TERM" ] && [ "$TERM" != dumb ] +} + + +raw_termattr() +{ + if we_have_term + then + for attr in "$@" + do + case "$attr" in + normal) + tput sgr0 + ;; + red) + tput setaf 1 + ;; + bold) + tput bold + ;; + esac + done + fi +} + + +# Output terminal attribute changing sequence, if there's a terminal. +# Also output output bash $PS1 sequences to tell bash it's +# non-printing characters. +termattr() +{ + printf '\[' + raw_termattr "$@" + printf '\]' +} + + + +######################################################################## +# Collect info to show in $PS1 + + +taskwarrior_context() +{ + task context list | awk '$NF == "yes" { print $1 }' +} + + +# Context for git and Debian packging: QvarnLabs? +QL() +{ + case "$DEBEMAIL" in + *@qvarnlabs.com) printf '[QL] ' ;; + esac +} + + find_git_root() { - (set -e - while [ "$(pwd)" != / ] - do - if [ -d .git ] - then - pwd - break + ( + set -e + while [ "$(pwd)" != / ] + do + if [ -d .git ] + then + pwd + break else cd .. - fi - done) + fi + done + ) } + is_git_repo() { case "$(find_git_root)" in @@ -21,6 +88,7 @@ is_git_repo() esac } + get_git_branch() { git branch --no-color | @@ -35,88 +103,60 @@ get_git_branch() ' } -git_prompt_part() -{ - if is_git_repo - then - echo -n "\\[$(normal_mode)\\][$(get_git_branch)]\\[$(start_bold)\\]" - fi -} -QL() -{ - case "$DEBEMAIL" in - *@qvarnlabs.com) echo '[QL] ' ;; - esac -} +######################################################################## +# Dynamically change $PS1 or output other things just before the prompt. + -task_context() +wrap_if_nonempty() { - local context="$(task context list | awk '$NF == "yes" { print $1 }')" - case "$context" in - ?*) echo "{{ $context }} " ;; - esac -} + local thing="$1" + local before="$2" + local after="$3" -iconpart="`whoami`@`hostname`: " -titlepart="`whoami`@`hostname` -- " -prompt_command() { - prompt_status=$? - if [ -r /etc/debian_chroot ] - then - debian_chroot="$(cat /etc/debian_chroot) " - else - debian_chroot="" - fi - if [ $prompt_status != 0 ] + if [ -n "$thing" ] then - echo "$(start_error)[status $prompt_status]$(normal_mode)" + printf '%s%s%s' "$before" "$thing" "$after" fi - base="`basename "$PWD"`" - PS1="\\[$(start_error)$(task_context)$(QL)$(start_bold)\\]\\u@\\h$(git_prompt_part)\\[$(basename_mode)\\](\W)\\[$(start_bold)\\]\\$ \\[$(normal_mode)\\]" } -we_have_term() -{ - [ "$TERM" ] && [ "$TERM" != dumb ] -} -basename_mode() +define_ps1() { - if we_have_term - then - tput sgr0 - tput setaf 4 - fi -} + wrap_if_nonempty "$(taskwarrior_context)" "$(termattr bold)" "$(termattr normal) " + wrap_if_nonempty "$(QL)" "$(termattr red bold)" "$(termattr normal)" -start_error() -{ - if we_have_term + if is_git_repo then - tput bold - tput setaf 1 + wrap_if_nonempty "$(get_git_branch)" "$(termattr bold)[" "]$(termattr normal)" fi + + printf '(\w)\$ ' } -start_bold() + +tell_about_exit_code() { - if we_have_term + local exit_code="$1" + if [ "$exit_code" != 0 ] then - tput sgr0 - tput bold + raw_termattr red + printf '[PREVIOUS COMMAND EXIT: %s]\n' "$exit_code" + raw_termattr normal fi } -normal_mode() -{ - if we_have_term - then - tput sgr0 - fi + +export PROMPT_COMMAND=prompt_command +prompt_command() { + tell_about_exit_code "$?" + PS1="$(define_ps1)" } + +############### + ulimit -c unlimited umask 002 @@ -130,7 +170,6 @@ export PAGER=less export LESSCHARSET=utf-8 export HISTCONTROL=ignoredups export PATH="$PATH:$HOME/bin" -export PROMPT_COMMAND=prompt_command export DEBEMAIL=liw@liw.fi export VIRSH_DEFAULT_CONNECT_URI=qemu:///system export LANG=en_GB.UTF-8 |