feat: prompt.sh

This commit is contained in:
Jamie Albert
2025-11-08 06:38:37 +00:00
parent bd32baf09f
commit e3b9e6d581
2 changed files with 69 additions and 55 deletions

View File

@@ -61,7 +61,7 @@ listen_on unix:~/.config/kitty/.kitty_socket
strip_trailing_spaces smart strip_trailing_spaces smart
# --- Keybinds # --- Keybinds
kitty_mod ctrl+shift kitty_mod ctrl
map kitty_mod+c copy_to_clipboardround map kitty_mod+c copy_to_clipboardround
map kitty_mod+v paste_from_clipboard map kitty_mod+v paste_from_clipboard
map kitty_mod+enter launch --cwd=current --type os-window map kitty_mod+enter launch --cwd=current --type os-window

View File

@@ -49,21 +49,24 @@ _screen_symbol="scr:"
# --- # ---
# * Colours # * Colours
# --- # ---
_prompt_success_color="142" _prompt_success_colour="142"
_prompt_fail_color="203" _prompt_fail_colour="203"
_prompt_user_color="109" _prompt_user_colour="109"
_prompt_sudo_color="198" _prompt_sudo_colour="208"
_prompt_info_color="172" _prompt_at_colour="249"
_prompt_input_color="254" _prompt_hostname_colour="132"
_git_module_color="66" _prompt_info_colour="172"
_pyenv_module_color="66" _prompt_input_colour="254"
_readonly_module_color="196" _prompt_dir_colour="249"
_screen_module_color="33" _git_module_colour="66"
_pyenv_module_colour="66"
_readonly_module_colour="196"
_screen_module_colour="33"
# --- # ---
# * Miscellaneous # * Miscellaneous
# --- # ---
_prompt_hostname="ssh" _prompt_hostname="all"
_prompt_separator=" " _prompt_separator=" "
_prompt_wrapper="[]" _prompt_wrapper="[]"
@@ -105,9 +108,9 @@ declare -x HISTFILESIZE=10000
declare -x HISTSIZE=${HISTFILESIZE} declare -x HISTSIZE=${HISTFILESIZE}
# --- # ---
# * Color function # * colour function
# --- # ---
_prompt_color() { _prompt_colour() {
[[ ${1} != "-" ]] && echo -ne "\[\033[38;5;${1}m\]" || echo -ne "\[\033[0m\]" [[ ${1} != "-" ]] && echo -ne "\[\033[38;5;${1}m\]" || echo -ne "\[\033[0m\]"
} }
@@ -115,14 +118,9 @@ _prompt_color() {
# * Generate prompt segments # * Generate prompt segments
# --- # ---
_prompt_generate() { _prompt_generate() {
declare _separator="" declare _separator="" _text="${1%%|*}" _colour="${1##*|}"
[[ ${_prompt_seg} -gt 1 ]] && _separator="${_prompt_separator}" [[ ${_prompt_seg} -gt 1 ]] && _separator="${_prompt_separator}"
_prompt_information+="${_separator}$(_prompt_colour "${_colour}")${_text}\[\e[0m\]"
# Parse parameters without IFS manipulation
declare _text="${1%%|*}"
declare _color="${1##*|}"
_prompt_information+="${_separator}$(_prompt_color "${_color}")${_text}\[\e[0m\]"
((_prompt_seg++)) ((_prompt_seg++))
} }
@@ -130,21 +128,27 @@ _prompt_generate() {
# * Screen # * Screen
# --- # ---
_screen_pre() { _screen_pre() {
[[ ${TERM} == "screen"* ]] && _prompt_generate "${_screen_symbol}|${_screen_module_color}" [[ ${TERM} == "screen"* ]] && _prompt_generate "${_screen_symbol}|${_screen_module_colour}"
} }
# --- # ---
# * Git # * Git
# --- # ---
_git_post() { _git_post() {
[[ -z "$_git_dir" ]] && _git_dir="$(git rev-parse --git-dir 2>/dev/null)" [[ -z "$_git_dir" ]] && _git_dir="$(git rev-parse --git-dir 2>/dev/null)"
[[ -n "$_git_dir" ]] || return [[ -n "$_git_dir" ]] || return
declare _unsafe_ref local _unsafe_ref
_unsafe_ref=$(git symbolic-ref -q HEAD 2>/dev/null) || return _unsafe_ref=$(git symbolic-ref -q HEAD 2>/dev/null) || return
declare _clean_ref="${_unsafe_ref##refs/heads/}" local _clean_ref="${_unsafe_ref##refs/heads/}"
_clean_ref="${_clean_ref//[^a-zA-Z0-9\/]/-}" _clean_ref="${_clean_ref//[^a-zA-Z0-9\/]/-}"
[[ -n ${_clean_ref} ]] && _prompt_generate "${_prompt_wrapper:0:1}${_git_symbol} ${_clean_ref}${_prompt_wrapper:1:1}|${_git_module_color}"
# Add status indicators
local _status=""
[[ -n "$(git status --porcelain 2>/dev/null)" ]] && _status="*"
[[ -n "$(git ls-files --others --exclude-standard 2>/dev/null)" ]] && _status="${_status}+"
[[ -n ${_clean_ref} ]] && _prompt_generate "${_git_symbol} ${_clean_ref}${_status}|${_git_module_colour}"
} }
# --- # ---
@@ -158,7 +162,7 @@ _pyenv_post() {
_cached_pyenv_version="${_cached_pyenv_version#Python }" _cached_pyenv_version="${_cached_pyenv_version#Python }"
_pyenv_cache_key="$VIRTUAL_ENV" _pyenv_cache_key="$VIRTUAL_ENV"
} }
[[ -n ${_cached_pyenv_version} ]] && _prompt_generate "${_prompt_wrapper:0:1}${_pyenv_symbol} ${_cached_pyenv_version}${_prompt_wrapper:1:1}|${_pyenv_module_color}" [[ -n ${_cached_pyenv_version} ]] && _prompt_generate "${_prompt_wrapper:0:1}${_pyenv_symbol} ${_cached_pyenv_version}${_prompt_wrapper:1:1}|${_pyenv_module_colour}"
} }
# --- # ---
@@ -170,8 +174,7 @@ _readonly_post() {
[[ ! -w "$(pwd)" ]] && _cached_readonly="readonly" [[ ! -w "$(pwd)" ]] && _cached_readonly="readonly"
_readonly_cache_key="$PWD" _readonly_cache_key="$PWD"
} }
[[ -n "$_cached_readonly" ]] && _prompt_generate "${_prompt_wrapper:0:1}${_readonly_symbol}${_prompt_wrapper:1:1}|${_readonly_module_colour}"
[[ -n "$_cached_readonly" ]] && _prompt_generate "${_prompt_wrapper:0:1}${_readonly_symbol}${_prompt_wrapper:1:1}|${_readonly_module_color}"
} }
# --- # ---
@@ -194,32 +197,43 @@ _prompt_build() {
# END # END
# --- # ---
# * User and hostname # * User and hostname
# --- # ---
declare _user_color="${_prompt_user_color}" declare _user_colour="${_prompt_user_colour}"
sudo -n -v 2>/dev/null && _user_color="${_prompt_sudo_color}" sudo -nv 2>/dev/null && _user_colour="${_prompt_sudo_colour}"
_gen_uh="${_cached_uh}" _gen_uh="${_cached_uh}"
_uh_cache_key="${_prompt_hostname}:${SSH_CLIENT:-}" # Change the cache key to include sudo status
[[ "$_uh_cache_key" != "$_last_uh_key" ]] && { _uh_cache_key="${_prompt_hostname}:${SSH_CLIENT:-}:$(sudo -n -v 2>/dev/null && echo 'sudo' || echo 'nosudo')"
_gen_uh="${USER}"
[[ ${_prompt_hostname} == "all" ]] || [[ ${_prompt_hostname} == "ssh" && -n ${SSH_CLIENT} ]] && _gen_uh="${USER}@${HOSTNAME}"
_cached_uh="$_gen_uh"
_last_uh_key="$_uh_cache_key"
}
_prompt_generate "${_gen_uh}|${_user_color}"
# --- [[ "$_uh_cache_key" != "$_last_uh_key" ]] && {
# * Current directory _gen_uh="$(_prompt_colour "${_user_colour}")${USER}"
# ---
declare _current_dir="${PWD}" # Add hostname if needed
_current_dir="${_current_dir/#$HOME/\~}" if [[ ${_prompt_hostname} == "all" ]] || [[ ${_prompt_hostname} == "ssh" && -n ${SSH_CLIENT} ]]; then
_prompt_generate "${_prompt_wrapper:0:1}${_current_dir}${_prompt_wrapper:1:1}|${_prompt_info_color}" _gen_uh+="$(_prompt_colour "${_prompt_at_colour}")@$(_prompt_colour "${_prompt_hostname_colour}")${HOSTNAME}"
fi
_gen_uh+="\[\033[0m\]" # Reset color
_cached_uh="$_gen_uh"
_last_uh_key="$_uh_cache_key"
}
_prompt_generate "${_gen_uh}|${_prompt_info_colour}"
# ---
# * Current directory
# ---
declare _current_dir="${PWD}"
_current_dir="${_current_dir/#$HOME/\~}"
# Build the segment with individual colours for each part
_prompt_generate "$(_prompt_colour "${_prompt_info_colour}")${_prompt_wrapper:0:1}$(_prompt_colour "${_prompt_dir_colour}")${_current_dir}$(_prompt_colour "${_prompt_info_colour}")${_prompt_wrapper:1:1}\[\033[0m\]|${_prompt_info_colour}"
# --- # ---
# * Error status # * Error status
# --- # ---
[[ ${_last_status} -ne 0 ]] && _prompt_generate "${_prompt_wrapper:0:1}${_last_status}${_prompt_wrapper:1:1}|${_prompt_fail_color}" [[ ${_last_status} -ne 0 ]] && _prompt_generate "${_prompt_wrapper:0:1}${_last_status}${_prompt_wrapper:1:1}|${_prompt_fail_colour}"
# --- # ---
# ! Insert *post* functions here # ! Insert *post* functions here
@@ -234,10 +248,10 @@ _prompt_build() {
# --- # ---
# *Final prompt symbol # *Final prompt symbol
# --- # ---
_prompt_status_color=${_prompt_success_color} _prompt_status_colour=${_prompt_success_colour}
[[ ${_last_status} -ne 0 ]]&& _prompt_status_color=${_prompt_fail_color} [[ ${_last_status} -ne 0 ]]&& _prompt_status_colour=${_prompt_fail_colour}
_prompt_information+="$(_prompt_color "${_prompt_status_color}")\n${_prompt_symbol}\[\e[0m\]" _prompt_information+="$(_prompt_colour "${_prompt_status_colour}")\n${_prompt_symbol}\[\e[0m\]"
PS1="${_prompt_information} " PS1="${_prompt_information} "
unset _prompt_information _prompt_seg unset _prompt_information _prompt_seg