Skip to content

Commit

Permalink
feat(ps1): Adding duration timer
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 30, 2024
1 parent a3270d0 commit d102ed0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sources/_terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if command -v terraform >/dev/null 2>&1; then
local exit="${?}"

if [[ $(find . -maxdepth 1 -type f -name "*.tf" | wc -l) -gt 0 ]]; then
printf " 🔧 %s" "$(terraform workspace show)"
printf " 🔧 %s" "$(terraform workspace show)"
fi

return "${exit}"
Expand Down
2 changes: 1 addition & 1 deletion sources/kubernetes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if command -v kubectl >/dev/null 2>&1; then
K8S_CONTEXT="$(yq eval '.current-context' "${KUBECONFIG:-${HOME}/.kube/config}")"

if [[ -n ${K8S_CONTEXT} ]]; then
printf " ☸ %s" "${K8S_CONTEXT}"
printf "%s" "${K8S_CONTEXT}"
fi

return "${exit}"
Expand Down
34 changes: 28 additions & 6 deletions sources/ps1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,41 @@ _ps1_previous_status() {
fi
}

PS1="${BLUE}\u${RESET}@${RED}\h${RESET} ${GREEN}\w${RESET}"
PS1_PATTERN="${BLUE}\u${RESET}@${RED}\h${RESET} ${GREEN}\w${RESET}"
if [[ "$(type -t "__git_ps1")" == "function" ]]; then
PS1+="${YELLOW}\$(__git_ps1)${RESET}"
PS1_PATTERN+="${YELLOW}\$(__git_ps1)${RESET}"
fi

if [[ "$(type -t "__kube_ps1")" == "function" ]]; then
PS1+="${BLUE}\$(__kube_ps1)${RESET}"
PS1_PATTERN+="${BLUE}\$(__kube_ps1)${RESET}"
fi

if [[ "$(type -t "__terraform_ps1")" == "function" ]]; then
PS1+="${PURPLE}\$(__terraform_ps1)${RESET}"
PS1_PATTERN+="${PURPLE}\$(__terraform_ps1)${RESET}"
fi

PS1+=" \$(_ps1_previous_status)\n> "
PS1_PATTERN+=' $(_ps1_previous_status)'
PS1_BASH_TIMER="$(mktemp)"

export PS1
PS0='$(printf "%d" "${SECONDS}" > "${PS1_BASH_TIMER}")'

_update_ps1() {
local __END="${SECONDS}"
local __START

if [[ -e ${PS1_BASH_TIMER:-} ]]; then
__START=$(cat "${PS1_BASH_TIMER}")
printf "" >"${PS1_BASH_TIMER}"
fi

local DURATION="$((__END - ${__START:-__END}))"

PS1="${PS1_PATTERN}"
if [[ ${DURATION} -gt 0 ]]; then
PS1+="${DURATION}s"
fi

PS1+="\n> "
}

PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
4 changes: 4 additions & 0 deletions symlinks/bash_logout
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ if [[ $(pgrep -a bash | wc -l) -eq 2 ]]; then
fi
fi

if [[ -e ${PS1_BASH_TIMER:-} ]]; then
rm "${PS1_BASH_TIMER}"
fi

clear

0 comments on commit d102ed0

Please sign in to comment.