dotfiles

My collection of dotfiles
git clone git://git.noxz.tech/dotfiles
Log | Files | Refs

commit 532a16ba23a6b52b059193054b9e269626667006
parent b04bcc25f0b95de69ce2b36b844c343406c779ab
Author: Chris Noxz <chris@noxz.tech>
Date:   Fri, 24 Apr 2020 10:24:36 +0200

[ksh] add suport for SSH->hostname and shorten git prompt

Diffstat:
Mksh/.config/ksh/kshrc.d/90-prompt.sh | 90++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 48 insertions(+), 42 deletions(-)

diff --git a/ksh/.config/ksh/kshrc.d/90-prompt.sh b/ksh/.config/ksh/kshrc.d/90-prompt.sh @@ -1,60 +1,66 @@ #!/bin/ksh export PS1="$(printf '[xxx]%s\[%s\]%s\[%s\] \\w%s \$ ' \ - '$(_ksh_set_title)' \ - '$(tput bold)' \ - '$(tput sgr0)' \ - '$(_ksh_user_prompt)' \ - '$(_ksh_git_prompt)' \ + '$(_ksh_set_title)' \ + '$(tput bold)' \ + '$(tput sgr0)' \ + '$(_ksh_user_prompt)' \ + '$(_ksh_git_prompt)' \ )" +_ksh_var_hostname() { + [ -z "$HOSTNAME" ] && HOSTNAME=$(hostname) + printf "$HOSTNAME" +} + _ksh_user_prompt() { - # abort if me - [ $(id -u) == 1000 ] && return + # abort if me and not an SSH connection + [ $(id -u) == 1000 ] && [ -z "$SSH_CLIENT" ] && return - # print username - printf ' %s' "$(id -un)" + # print username (and hostname) + printf ' %s%s' \ + "$USER" \ + "$([ ! -z "$SSH_CLIENT" ] && echo "@$(_ksh_var_hostname)")" } _ksh_git_prompt() { - # abort if not a git repository - [ ! -f .git ] && [ ! -d .git ] && return - - # variable - local branch="" - local status="" - local flags="" - - # collect necessary data - branch="$(git branch 2>/dev/null | sed 's/* \(.*\)/\1/')" - status="$(git status 2> /dev/null)" - - # parse git status into flags - [ "${status#*renamed:}" != "$status" ] && flags=">$flags" - [ "${status#*Your branch is ahead of:}" != "$status" ] && flags="*$flags" - [ "${status#*new file:}" != "$status" ] && flags="+$flags" - [ "${status#*Untracked files:}" != "$status" ] && flags="?$flags" - [ "${status#*deleted:}" != "$status" ] && flags="x$flags" - [ "${status#*modified:}" != "$status" ] && flags="!!$flags" - [ ! "$flags" = "" ] && flags=" $flags" - - # print git prompt - printf ' \[%s\](%s%s)\[%s\]' \ - "$(tput setaf 3)" \ - "$branch" \ - "$flags" \ - "$(tput sgr0)" + # abort if not a git repository + [ ! -f .git ] && [ ! -d .git ] && return + + # variable + local branch="" + local status="" + local flags="" + + # collect necessary data + branch="$(git branch 2>/dev/null | sed 's/* \(.*\)/\1/')" + status="$(git status --short 2> /dev/null | cut -c1-3 | uniq)" + + # parse git status into flags + [ "${status#* R}" != "$status" ] && flags=">$flags" + [ "${status#* A}" != "$status" ] && flags="+$flags" + [ "${status#*??}" != "$status" ] && flags="?$flags" + [ "${status#* D}" != "$status" ] && flags="x$flags" + [ "${status#* M}" != "$status" ] && flags="!!$flags" + [ ! "$flags" = "" ] && flags=" $flags" + + # print git prompt + printf ' \[%s\](%s%s)\[%s\]' \ + "$(tput setaf 3)" \ + "$branch" \ + "$flags" \ + "$(tput sgr0)" } _ksh_set_title() { - # variable - local term="" + # variable + local term="" - # collect necessary data - term="$(cat /proc/$PPID/comm)" + # collect necessary data + term="$(cat /proc/$PPID/comm)" - # set title - printf '\[\033]2;%s (\w)\007\]' "${term##*/}" + # set title + printf '\[\033]2;%s (\w)\007\]' "${term##*/}" }