refactor: updated most logic

This commit is contained in:
Jamie Albert
2025-11-08 04:16:31 +00:00
parent 7ee60a6e80
commit bd32baf09f

View File

@@ -9,7 +9,16 @@
# Copyright (C) 2025, Jamie Albert
# ---
# Shell options configuration
declare -g _cached_uh=""
declare -g _uh_cache_key=""
declare -g _cached_pyenv_version=""
declare -g _pyenv_cache_key=""
declare -g _cached_readonly=""
declare -g _readonly_cache_key=""
# ---
# Shell options configuration
# ---
shopt -s histappend
shopt -s cmdhist
shopt -s histreedit
@@ -28,57 +37,65 @@ if [[ $- =~ "i" ]]; then
bind "set mark-symlinked-directories on"
fi
# Configuration
_prompt_input_symbol=""
_prompt_nerd_symbol="󰥭"
# ---
# * Symbols
# ---
_prompt_symbol=""
_git_symbol="git:"
_pyenv_symbol="pyenv:"
_readonly_symbol="ro:"
_screen_symbol="scr:"
# ---
# * Colours
# ---
_prompt_success_color="142"
_prompt_fail_color="203"
_prompt_user_color="109"
_prompt_sudo_color="198"
_prompt_info_color="172"
_prompt_input_color="254"
_git_module_color="66"
_pyenv_module_color="66"
_readonly_module_color="196"
_screen_module_color="33"
# ---
# * Miscellaneous
# ---
_prompt_hostname="ssh"
_prompt_separator=" "
_prompt_wrapper="[]"
_prompt_success_color="106"
_prompt_fail_color="203"
_prompt_user_color="109"
_prompt_sudo_color="72"
_prompt_info_color="172"
_prompt_input_color="254"
# Git module configuration
_git_nerd_symbol="󰘦"
_git_module_symbol="git:"
_git_module_color="38"
# ---
# // Nerd Symbols
# ---
# _git_symbol="󰘦"
# _pyenv_symbol="󰌠"
# _readonly_symbol="󰍁"
# _screen_symbol="󰐅"
# _prompt_symbol="󰥭"
# Pyenv module configuration
_pyenv_nerd_symbol="󰌠"
_pyenv_module_symbol="pyenv:"
_pyenv_module_color="66"
# Readonly module configuration
_readonly_nerd_symbol="󰍁"
_readonly_module_symbol="ro:"
_readonly_module_color="196"
# Screen module configuration
_screen_nerd_symbol="󰐅"
_screen_module_symbol="scr:"
_screen_module_color="33"
# shellcheck disable=1091
#. /usr/local/share/dao/config/dao.conf
# --- Aliases
# ---
# * Aliases
# ---
alias count='find . -type f | wc -l'
alias cdd='cd /home/jamie/dao'
alias dnf='sudo dnf'
alias hist='history|grep'
alias hist='history|grep'
alias ll="ls -laFh"
alias la='ls -A'
alias li='la | grep -i'
alias lh='ll -d .*'
alias mkdir='mkdir -p'
alias nano='nano -W'
alias qq='exit'
alias sc='sudo systemctl'
alias xclipc='xclip -sel c'
# Environment settings
# ---
# * Environment settings
# ---
declare -g HISTTIMEFORMAT='%F %T '
declare -g PROMPT_DIRTRIM="2"
[[ -z "$LC_CTYPE" && -z "$LC_ALL" ]] && declare -g LC_CTYPE="${LANG%%:*}"
@@ -87,124 +104,157 @@ declare -x HISTCONTROL=ignorespace
declare -x HISTFILESIZE=10000
declare -x HISTSIZE=${HISTFILESIZE}
# Color function for prompt
# ---
# * Color function
# ---
_prompt_color() {
[[ ${1} != "-" ]] && local _fg="38;5;${1}"
echo -ne "\[\033[${_fg}m\]"
[[ ${1} != "-" ]] && echo -ne "\[\033[38;5;${1}m\]" || echo -ne "\[\033[0m\]"
}
# Generate prompt segments
# ---
# * Generate prompt segments
# ---
_prompt_generate() {
local OLD_IFS="${IFS}"
IFS="|"
read -ra _params <<<"$1"
IFS="${OLD_IFS}"
local _separator=""
declare _separator=""
[[ ${_prompt_seg} -gt 1 ]] && _separator="${_prompt_separator}"
_prompt_information+="${_separator}$(_prompt_color "${_params[1]}")${_params[0]}\[\e[0m\]"
((_prompt_seg += 1))
# Parse parameters without IFS manipulation
declare _text="${1%%|*}"
declare _color="${1##*|}"
_prompt_information+="${_separator}$(_prompt_color "${_color}")${_text}\[\e[0m\]"
((_prompt_seg++))
}
# Screen module pre function
# ---
# * Screen
# ---
_screen_pre() {
if [[ ${TERM} == "screen"* ]]; then
_prompt_generate "${_screen_nerd_symbol}|${_screen_module_color}"
fi
[[ ${TERM} == "screen"* ]] && _prompt_generate "${_screen_symbol}|${_screen_module_color}"
}
# Git module post function
# ---
# * Git
# ---
_git_post() {
if git status --porcelain &>/dev/null; then
local _unsafe_ref
_unsafe_ref=$(command git symbolic-ref -q HEAD 2>/dev/null)
local _stripped_ref="${_unsafe_ref##refs/heads/}"
local _clean_ref="${_stripped_ref//[^a-zA-Z0-9\/]/-}"
if [[ -n ${_clean_ref} ]]; then
_prompt_generate "${_prompt_wrapper:0:1}${_git_nerd_symbol} ${_clean_ref}${_prompt_wrapper:1:1}|${_git_module_color}"
fi
fi
[[ -z "$_git_dir" ]] && _git_dir="$(git rev-parse --git-dir 2>/dev/null)"
[[ -n "$_git_dir" ]] || return
declare _unsafe_ref
_unsafe_ref=$(git symbolic-ref -q HEAD 2>/dev/null) || return
declare _clean_ref="${_unsafe_ref##refs/heads/}"
_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}"
}
# Pyenv module post function
# ---
# * Pyenv
# ---
_pyenv_post() {
if [[ -n ${VIRTUAL_ENV} ]]; then
local _pyenv_version
_pyenv_version="$(python --version | cut -d " " -f2)"
[[ -z ${_pyenv_version} ]] && _pyenv_version=$(python3 --version | cut -d " " -f2)
if [[ -n ${_pyenv_version} ]]; then
_prompt_generate "${_prompt_wrapper:0:1}${_pyenv_nerd_symbol} ${_pyenv_version}${_prompt_wrapper:1:1}|${_pyenv_module_color}"
fi
fi
[[ -n ${VIRTUAL_ENV} ]] || return
[[ "$VIRTUAL_ENV" != "$_pyenv_cache_key" ]] && {
_cached_pyenv_version="${PYTHON_VERSION}"
[[ -z ${_cached_pyenv_version} ]] && _cached_pyenv_version="$(python3 --version 2>/dev/null)"
_cached_pyenv_version="${_cached_pyenv_version#Python }"
_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}"
}
# Readonly module post function
# ---
# * Readonly
# ---
_readonly_post() {
if [[ ! -w "$(pwd)" ]]; then
_prompt_generate "${_prompt_wrapper:0:1}${_readonly_nerd_symbol}${_prompt_wrapper:1:1}|${_readonly_module_color}"
fi
[[ "$PWD" != "$_readonly_cache_key" ]] && {
_cached_readonly=""
[[ ! -w "$(pwd)" ]] && _cached_readonly="readonly"
_readonly_cache_key="$PWD"
}
[[ -n "$_cached_readonly" ]] && _prompt_generate "${_prompt_wrapper:0:1}${_readonly_symbol}${_prompt_wrapper:1:1}|${_readonly_module_color}"
}
# Build the main prompt
# ---
# * Build the main prompt
# ---
_prompt_build() {
local _last_status="$?"
local _gen_uh
local -gi _prompt_seg=1
declare _last_status="$?" _gen_uh _cache_key="${PWD}"
declare -gi _prompt_seg=1
# Run module pre functions
if command -v _screen_pre >/dev/null; then
_screen_pre
fi
[[ "$_cache_key" != "$_last_cache_key" ]] && {
unset _git_dir _cached_pyenv_version
_last_cache_key="$_cache_key"
}
# User and hostname
local _user_color="${_prompt_user_color}"
sudo -n uptime 2>&1 | grep -q "load" && _user_color="${_prompt_sudo_color}"
# ---
# ! Insert *pre* functions here
# ---
# START
_screen_pre
# END
_gen_uh="${USER}"
{ [[ ${_prompt_hostname} == "all" ]]; } || { [[ ${_prompt_hostname} == "ssh" && -n ${SSH_CLIENT} ]]; } && _gen_uh="${_gen_uh}@${HOSTNAME}"
# ---
# * User and hostname
# ---
declare _user_color="${_prompt_user_color}"
sudo -n -v 2>/dev/null && _user_color="${_prompt_sudo_color}"
_gen_uh="${_cached_uh}"
_uh_cache_key="${_prompt_hostname}:${SSH_CLIENT:-}"
[[ "$_uh_cache_key" != "$_last_uh_key" ]] && {
_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}"
# Current directory
_prompt_generate "${_prompt_wrapper:0:1}$(pwd | sed "s|^${HOME}|~|")${_prompt_wrapper:1:1}|${_prompt_info_color}"
# ---
# * Current directory
# ---
declare _current_dir="${PWD}"
_current_dir="${_current_dir/#$HOME/\~}"
_prompt_generate "${_prompt_wrapper:0:1}${_current_dir}${_prompt_wrapper:1:1}|${_prompt_info_color}"
# Error status
# ---
# * Error status
# ---
[[ ${_last_status} -ne 0 ]] && _prompt_generate "${_prompt_wrapper:0:1}${_last_status}${_prompt_wrapper:1:1}|${_prompt_fail_color}"
# Run module post functions
if command -v _git_post >/dev/null; then
_git_post
fi
if command -v _pyenv_post >/dev/null; then
_pyenv_post
fi
if command -v _readonly_post >/dev/null; then
_readonly_post
fi
# ---
# ! Insert *post* functions here
# ---
# START
_git_post
_pyenv_post
_readonly_post
# Final prompt symbol
if [[ ${_last_status} -ne 0 ]]; then
_prompt_status_color=${_prompt_fail_color}
else
_prompt_status_color=${_prompt_success_color}
fi
# END
_prompt_information+="$(_prompt_color "${_prompt_status_color}")\n${_prompt_nerd_symbol}\[\e[0m\]"
# ---
# *Final prompt symbol
# ---
_prompt_status_color=${_prompt_success_color}
[[ ${_last_status} -ne 0 ]]&& _prompt_status_color=${_prompt_fail_color}
_prompt_information+="$(_prompt_color "${_prompt_status_color}")\n${_prompt_symbol}\[\e[0m\]"
PS1="${_prompt_information} "
unset _prompt_information _prompt_seg
}
# Initialize prompt
# ---
# * Initialize prompt
# ---
_prompt_init() {
# Add to PROMPT_COMMAND
if [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="_prompt_build"
else
PROMPT_COMMAND="_prompt_build;${PROMPT_COMMAND}"
fi
case "$PROMPT_COMMAND" in
*"_prompt_build"*) ;;
"") PROMPT_COMMAND="_prompt_build" ;;
*) PROMPT_COMMAND="_prompt_build;${PROMPT_COMMAND}" ;;
esac
}
# Initialize when sourced
if [[ ${BASH_SOURCE[0]} != "${0}" ]]; then
_prompt_init
fi
# ---
# * Initialize when sourced
# ---
[[ ${BASH_SOURCE[0]} != "${0}" ]] && _prompt_init