Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions source/bash/20_prompt.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function _dotfiles-prompt() {
local ec="$?"
local host="\h"
local host_name="${DOTFILES_HOST:-${HOSTNAME%%.*}}"
local clr_user="$CLR_BLUE"
local clr_host="$CLR_YELLOW"
local prompt="\$"
Expand All @@ -12,7 +13,7 @@ function _dotfiles-prompt() {
prompt="#"
fi

if [ "$host" = "toolbox" ]; then
if [ "$host_name" = "toolbox" ]; then
clr_host="$CLR_RED"
fi

Expand All @@ -23,11 +24,11 @@ function _dotfiles-prompt() {

# Actual prompt code
if [[ -n $supports_color ]]; then
PS1="\[$CLR_NONE\]"
PS1="$(_dotfiles-bash-np "$CLR_NONE")"
PS1+="$(_dotfiles-exit_code "$ec")"
PS1+="\[$clr_user\]\u\[$CLR_NONE\]@\[$clr_host\]$host"
PS1+="\[$CLR_NONE\]$(_dotfiles-chroot-prompt) "
PS1+="\[$CLR_BLUE\]\w"
PS1+="$(_dotfiles-bash-np "$clr_user")\u$(_dotfiles-bash-np "$CLR_NONE")@$(_dotfiles-bash-np "$clr_host")$host"
PS1+="$(_dotfiles-bash-np "$CLR_NONE")$(_dotfiles-chroot-prompt) "
PS1+="$(_dotfiles-bash-np "$CLR_BLUE")\w"

if command -v git >/dev/null 2>&1; then
PS1+="$(_dotfiles-git-prompt)"
Expand All @@ -37,21 +38,28 @@ function _dotfiles-prompt() {
if [[ -n $K8S_PROMPT ]]; then
PS1+="$(_dotfiles-k8s-prompt)"
fi
PS1+="\[$CLR_NONE\] $prompt "
PS1+="$(_dotfiles-bash-np "$CLR_NONE") $prompt "
else
PS1="\u@$host:\w$prompt "
fi
}

function _dotfiles-bash-np() {
printf '\001%s\002' "$1"
}

function _dotfiles-exit_code() {
local ec="$1"
[[ $ec != 0 ]] && printf '\[%s\]%s\[%s\] ' "$CLR_RED" "$ec" "$CLR_NONE"
[[ $ec != 0 ]] && printf '%s%s%s ' "$(_dotfiles-bash-np "$CLR_RED")" "$ec" "$(_dotfiles-bash-np "$CLR_NONE")"
}

function _dotfiles-chroot-prompt() {
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(< /etc/debian_chroot)
printf '\[%s\] (%s)\[%s\]' "$CLR_YELLOW" "$debian_chroot" "$CLR_NONE"
fi

if [[ -n $debian_chroot ]]; then
printf '%s (%s)%s' "$(_dotfiles-bash-np "$CLR_YELLOW")" "$debian_chroot" "$(_dotfiles-bash-np "$CLR_NONE")"
fi
}

Expand All @@ -75,17 +83,17 @@ function _dotfiles-git-prompt() {
if git diff-index --cached HEAD -- >/dev/null 2>&1 && [[ -n "$(git diff-index --cached HEAD -- 2>/dev/null)" ]]; then
# ls-files has no way to list files that are staged, we have to use the
# significantly slower (for large repos) diff-index command instead
indicator+='\['"$CLR_GITST_SC"'\]+' # Staged changes
indicator+="$(_dotfiles-bash-np "$CLR_GITST_SC")+" # Staged changes
fi
if [[ -n "$(git ls-files --modified 2>/dev/null)" ]]; then
indicator+='\['"$CLR_GITST_USC"'\]*' # Unstaged changes
indicator+="$(_dotfiles-bash-np "$CLR_GITST_USC")*" # Unstaged changes
fi
if [[ -n "$(git ls-files --others --exclude-standard 2>/dev/null)" ]]; then
indicator+='\['"$CLR_GITST_UT"'\]?' # Untracked files
indicator+="$(_dotfiles-bash-np "$CLR_GITST_UT")?" # Untracked files
fi

branch="$(git branch --no-color 2>/dev/null | sed -n 's/^\* //p')"
printf '\[%s\] (\[%s\]%s%s\[%s\])' "$CLR_GITST_CLS" "$CLR_GITST_BR" "$branch" "$indicator" "$CLR_GITST_CLS"
printf '%s (%s%s%s%s)' "$(_dotfiles-bash-np "$CLR_GITST_CLS")" "$(_dotfiles-bash-np "$CLR_GITST_BR")" "$branch" "$indicator" "$(_dotfiles-bash-np "$CLR_GITST_CLS")"
}

function _dotfiles-virtualenv-prompt() {
Expand All @@ -97,14 +105,14 @@ function _dotfiles-virtualenv-prompt() {
else
return 1
fi
printf '\[%s\] (%s)\[%s\]' "$CLR_YELLOW" "$environment" "$CLR_YELLOW"
printf '%s (%s)%s' "$(_dotfiles-bash-np "$CLR_YELLOW")" "$environment" "$(_dotfiles-bash-np "$CLR_NONE")"
}

function _dotfiles-k8s-prompt() {
local current_context=""
current_context="$(kubectl config current-context 2>/dev/null)" || return 1
[[ -z $current_context ]] && return 1
printf '\[%s\] (%s)\[%s\]' "$CLR_BLUE" "$current_context" "$CLR_BLUE"
printf '%s (%s)%s' "$(_dotfiles-bash-np "$CLR_BLUE")" "$current_context" "$(_dotfiles-bash-np "$CLR_NONE")"
}

PROMPT_COMMAND="_dotfiles-prompt"
14 changes: 11 additions & 3 deletions source/zsh/20_prompt.zsh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function _dotfiles-prompt() {
local host="%m"
local host_name="${DOTFILES_HOST:-$HOST}"
local clr_user="%F{blue}"
local clr_host="%F{yellow}"

Expand All @@ -8,14 +9,14 @@ function _dotfiles-prompt() {
clr_user="%F{red}"
fi

if [[ "$host" == "toolbox" ]]; then
if [[ "$host_name" == "toolbox" ]]; then
clr_host="%F{red}"
fi

# Actual prompt code
# %n = username
# Keep exit code segment zsh-native (see _dotfiles-exit_code)
print -nr -- "%f$(_dotfiles-exit_code)${clr_user}%n%f@${clr_host}${host}%f$(_dotfiles-chroot-prompt) %F{blue}${PWD/#$HOME/~}%f"
print -nr -- "%f$(_dotfiles-exit_code)${clr_user}%n%f@${clr_host}${host}%f$(_dotfiles-chroot-prompt) %F{blue}%~%f"
}

function _dotfiles-prompt2() {
Expand All @@ -36,7 +37,11 @@ function _dotfiles-chroot-prompt() {

if [[ -z "$debian_chroot" && -r /etc/debian_chroot ]]; then
debian_chroot="$(< /etc/debian_chroot)"
print -nr -- "${CLR_CHROOT_CLS} (${debian_chroot})%f"
fi

if [[ -n "$debian_chroot" ]]; then
local escaped_chroot="${debian_chroot//\%/%%}"
print -nr -- "${CLR_CHROOT_CLS} (${escaped_chroot})%f"
fi
}

Expand Down Expand Up @@ -65,6 +70,7 @@ function _dotfiles-git-prompt() {
fi

branch="$(git branch --no-color 2>/dev/null | sed -n 's/^\* //p')"
branch="${branch//\%/%%}"
print -nr -- "${CLR_GITST_CLS} (${CLR_GITST_BR}${branch}${indicator}${CLR_GITST_CLS})%f"
}

Expand All @@ -81,6 +87,7 @@ function _dotfiles-virtualenv-prompt() {
return 1
fi

environment="${environment//\%/%%}"
print -nr -- "${CLR_VE_CLS} (${CLR_VE_ENV}${environment}${CLR_VE_CLS})%f"
}

Expand All @@ -98,6 +105,7 @@ function _dotfiles-k8s-prompt() {
current_context="$(kubectl config current-context 2>/dev/null)" || return 1
[[ -z "$current_context" ]] && return 1

current_context="${current_context//\%/%%}"
print -nr -- "${CLR_K8S_CLS} (${CLR_K8S_ENV}${current_context}${CLR_K8S_CLS})%f"
}

Expand Down