diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-23 15:07:27 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-23 15:07:27 +1200 |
commit | 4d378ca7e44461c1f477efd060accfe243b333f0 (patch) | |
tree | ecd0783b4e3deb7d6c0f5ab179f27eab80789aff | |
parent | Add comment to vague block (diff) | |
parent | Set POSIX PS1 before loading subscripts (diff) | |
download | dotfiles-4d378ca7e44461c1f477efd060accfe243b333f0.tar.gz dotfiles-4d378ca7e44461c1f477efd060accfe243b333f0.zip |
Merge branch 'openbsd'
-rw-r--r-- | pdksh/pdkshrc.d/prompt.pdksh | 55 | ||||
-rw-r--r-- | sh/shrc | 5 | ||||
-rw-r--r-- | sh/shrc.d/prompt.sh | 3 |
3 files changed, 35 insertions, 28 deletions
diff --git a/pdksh/pdkshrc.d/prompt.pdksh b/pdksh/pdkshrc.d/prompt.pdksh index f9dfa584..05e3eced 100644 --- a/pdksh/pdkshrc.d/prompt.pdksh +++ b/pdksh/pdkshrc.d/prompt.pdksh @@ -15,13 +15,19 @@ prompt() { # Turn complex, colored PS1 and debugging PS4 prompts on on) - # Set up prompt, including optional PROMPT_PREFIX and PROMPT_SUFFIX - # variables - PS1='\u@\h:\w' - PS1=$PS1'$(prompt vcs)' - PS1=$PS1'$(prompt job)' - PS1='${PROMPT_PREFIX}'$PS1 - PS1=$PS1'${PROMPT_SUFFIX}' + # Basic prompt shape depends on whether we're in SSH or not + PS1= + [[ -n $SSH_CONNECTION ]] && + PS1=$PS1'\u@\h:' + PS1=$PS1'\w' + + # Add sub-commands; VCS, job, and return status checks + PS1=$PS1'$(prompt vcs)$(prompt job)' + + # Add prefix and suffix + PS1='${PROMPT_PREFIX}'$PS1'${PROMPT_SUFFIX}' + + # Add terminating "$" or "#" sign PS1=$PS1'\$' # Count available colors @@ -79,10 +85,10 @@ prompt() { # Git prompt function git) - # Bail if we have no git(1) - if ! hash git 2>/dev/null ; then - return 1 - fi + # Bail if we're not in a work tree--or, implicitly, if we don't + # have git(1). + [[ $(git rev-parse --is-inside-work-tree 2>/dev/null) = true ]] || + return # Attempt to determine git branch, bail if we can't typeset branch @@ -102,12 +108,14 @@ prompt() { typeset state # Upstream HEAD has commits after local HEAD; we're "behind" - (($(git rev-list --count 'HEAD..@{u}' 2>/dev/null) > 0)) && - state=${state}\< + typeset -i behind + behind=$(git rev-list --count 'HEAD..@{u}' 2>/dev/null) + ((behind)) && state=${state}'<' # Local HEAD has commits after upstream HEAD; we're "ahead" - (($(git rev-list --count '@{u}..HEAD' 2>/dev/null) > 0)) && - state=${state}\> + typeset -i ahead + ahead=$(git rev-list --count '@{u}..HEAD' 2>/dev/null) + ((ahead)) && state=${state}'>' # Tracked files are modified git diff-files --quiet || @@ -115,19 +123,20 @@ prompt() { # Changes are staged git diff-index --cached --quiet HEAD || - state=${state}\+ + state=${state}'+' # There are some untracked and unignored files [[ -n $(git ls-files --others --exclude-standard) ]] && - state=${state}\? + state=${state}'?' # There are stashed changes git rev-parse --quiet --verify refs/stash >/dev/null && - state=${state}\^ + state=${state}'^' # Print the status in brackets; add a git: prefix only if there # might be another VCS prompt (because PROMPT_VCS is set) - printf '(%s%s%s)' "${PROMPT_VCS:+git:}" "${branch:-unknown}" "$state" + printf '(%s%s%s)' \ + "${PROMPT_VCS:+git:}" "${branch:-unknown}" "$state" ;; # Revert to simple inexpensive prompts @@ -142,9 +151,7 @@ prompt() { vcs) typeset vcs for vcs in "${PROMPT_VCS[@]:-git}" ; do - if prompt "$vcs" ; then - return - fi + prompt "$vcs" && return done ;; @@ -152,9 +159,7 @@ prompt() { job) typeset -i jobc jobc=$(jobs -p | sed -n '$=') - if ((jobc > 0)) ; then - printf '{%u}' "$jobc" - fi + ((jobc)) && printf '{%u}' "$jobc" ;; # Print error @@ -4,6 +4,11 @@ case $- in *) return ;; esac +# Basic PS1 for POSIX shell; if we're using something more advanced, something +# in ~/.shrc.d should overrule this. Does every POSIX shell support these? dash +# does, at least. +PS1=$(printf '%s@%s$ ' "$(whoami)" "$(hostname -s)") + # Load all the POSIX-compatible functions from ~/.shrc.d; more advanced shells # like bash will have their own functions for sh in "$HOME"/.shrc.d/*.sh ; do diff --git a/sh/shrc.d/prompt.sh b/sh/shrc.d/prompt.sh deleted file mode 100644 index dff42f05..00000000 --- a/sh/shrc.d/prompt.sh +++ /dev/null @@ -1,3 +0,0 @@ -# Basic PS1 for POSIX shell -# Does every POSIX shell support these? dash does, at least. -PS1=$(printf '%s@%s$ ' "$(whoami)" "$(hostname -s)") |