Files
dao_hades/cradle/usr/local/bin/prompt.sh
Jamie Albert e5d26bd32b Lazy commit
2025-11-06 02:30:35 +00:00

211 lines
5.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# ---
# @file_name: prompt.sh
# @version: 1.0.1
# @description: a prompt
# @author: Jamie Albert (empty_produce)
# @author_contact: <mailto:empty.produce@flatmail.me>
# @license: GNU Affero General Public License v3.0 (Included in LICENSE)
# Copyright (C) 2025, Jamie Albert
# ---
# Shell options configuration
shopt -s histappend
shopt -s cmdhist
shopt -s histreedit
shopt -s histverify
shopt -s interactive_comments
shopt -s checkwinsize
shopt -s globstar
shopt -s nocaseglob
shopt -s dirspell
shopt -s cdspell
shopt -u autocd
if [[ $- =~ "i" ]]; then
bind "set completion-ignore-case on"
bind "set show-all-if-ambiguous on"
bind "set mark-symlinked-directories on"
fi
# Configuration
_prompt_input_symbol=""
_prompt_nerd_symbol="󰥭"
_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"
# 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
alias count='find . -type f | wc -l'
alias cdd='cd /home/jamie/dao'
alias dnf='sudo dnf'
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 sc='sudo systemctl'
alias xclipc='xclip -sel c'
# Environment settings
declare -g HISTTIMEFORMAT='%F %T '
declare -g PROMPT_DIRTRIM="2"
[[ -z "$LC_CTYPE" && -z "$LC_ALL" ]] && declare -g LC_CTYPE="${LANG%%:*}"
[[ -z "$HISTFILE" ]] && declare -g HISTFILE="$HOME/.bash_history"
declare -x HISTCONTROL=ignorespace
declare -x HISTFILESIZE=10000
declare -x HISTSIZE=${HISTFILESIZE}
# Color function for prompt
_prompt_color() {
[[ ${1} != "-" ]] && local _fg="38;5;${1}"
echo -ne "\[\033[${_fg}m\]"
}
# Generate prompt segments
_prompt_generate() {
local OLD_IFS="${IFS}"
IFS="|"
read -ra _params <<<"$1"
IFS="${OLD_IFS}"
local _separator=""
[[ ${_prompt_seg} -gt 1 ]] && _separator="${_prompt_separator}"
_prompt_information+="${_separator}$(_prompt_color "${_params[1]}")${_params[0]}\[\e[0m\]"
((_prompt_seg += 1))
}
# Screen module pre function
_screen_pre() {
if [[ ${TERM} == "screen"* ]]; then
_prompt_generate "${_screen_nerd_symbol}|${_screen_module_color}"
fi
}
# Git module post function
_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
}
# Pyenv module post function
_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
}
# Readonly module post function
_readonly_post() {
if [[ ! -w "$(pwd)" ]]; then
_prompt_generate "${_prompt_wrapper:0:1}${_readonly_nerd_symbol}${_prompt_wrapper:1:1}|${_readonly_module_color}"
fi
}
# Build the main prompt
_prompt_build() {
local _last_status="$?"
local _gen_uh
local -gi _prompt_seg=1
# Run module pre functions
if command -v _screen_pre >/dev/null; then
_screen_pre
fi
# User and hostname
local _user_color="${_prompt_user_color}"
sudo -n uptime 2>&1 | grep -q "load" && _user_color="${_prompt_sudo_color}"
_gen_uh="${USER}"
{ [[ ${_prompt_hostname} == "all" ]]; } || { [[ ${_prompt_hostname} == "ssh" && -n ${SSH_CLIENT} ]]; } && _gen_uh="${_gen_uh}@${HOSTNAME}"
_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}"
# 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
# Final prompt symbol
if [[ ${_last_status} -ne 0 ]]; then
_prompt_status_color=${_prompt_fail_color}
else
_prompt_status_color=${_prompt_success_color}
fi
_prompt_information+="$(_prompt_color "${_prompt_status_color}")\n${_prompt_nerd_symbol}\[\e[0m\]"
PS1="${_prompt_information} "
unset _prompt_information _prompt_seg
}
# 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
}
# Initialize when sourced
if [[ ${BASH_SOURCE[0]} != "${0}" ]]; then
_prompt_init
fi