This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Ps1.sh

133 lines
2.5 KiB
Bash
Raw Normal View History

2023-12-07 01:44:42 +03:00
export PROMPT_COMMAND=(__prompt_command "${PROMPT_COMMAND[@]}")
2023-08-08 16:24:15 +03:00
2023-12-07 04:02:47 +03:00
# Custom terminal prompt format.
2023-12-07 01:44:42 +03:00
function __prompt_command() {
2023-12-05 21:50:45 +03:00
local last_status="${?}"
local is_error=false
local is_root=false
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
if [[ $last_status != 0 && $last_status != 130 ]]; then
is_error=true
fi
if [[ "$UID" -eq 0 ]]; then
is_root=true
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# add newline
PS1="\n"
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# set error red
if $is_error; then
PS1+="\[${color_bred}\]"
PS1+="["
else
PS1+="\[${color_default}\]"
PS1+="["
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# add time
PS1+="\[${color_white}\]"
PS1+="$(date +%H:%M) "
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# set root red
if $is_root; then
PS1+="\[${color_red}\]"
else
PS1+="\[${color_cyan}\]"
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# add user, host and working dir
PS1+="\u@\h "
PS1+="\[${color_blue}\]"
PS1+="\w"
# PS1+="\${PWD}"
2023-11-27 21:18:55 +03:00
2023-12-05 21:50:45 +03:00
# Add git branch if available.
if ! _git_is_dotfiles; then
local git_branch="$(_git_current_branch)"
if [[ "${git_branch}" != "" ]]; then
PS1+=" ${color_bblue}@${git_branch}"
fi
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# set error red
if $is_error; then
PS1+="\[${color_bred}\]"
PS1+="] "
else
PS1+="\[${color_default}\]"
PS1+="] "
fi
2023-08-08 16:24:15 +03:00
2023-12-05 21:50:45 +03:00
# If error, show code.
if ${is_error}; then
PS1+="${color_red}("
PS1+="${last_status}"
local error_type="$(_ps1error ${last_status})"
[[ "${error_type}" != "" ]] && PS1+=" ${error_type}"
PS1+=")${color_default} "
fi
2023-11-20 18:39:04 +03:00
2023-12-05 21:50:45 +03:00
# command on new line
PS1+="\n"
PS1+="\[${color_default}\]"
2023-08-08 16:24:15 +03:00
2024-01-04 18:47:03 +03:00
# Show remote connections.
if [ -n "${SSH_TTY}" ]; then
2024-01-04 19:33:13 +03:00
PS1+=">"
2024-01-04 18:47:03 +03:00
fi
2023-12-05 21:50:45 +03:00
# set user tag
if $is_root; then
PS1+="# "
else
PS1+="$ "
fi
2023-08-08 16:24:15 +03:00
}
2023-11-20 18:39:04 +03:00
2023-12-07 04:02:47 +03:00
# Convert error code into short description.
# Usage: _ps1error <CODE>
2023-12-07 01:44:42 +03:00
function _ps1error() {
2023-12-05 21:50:45 +03:00
local type
case ${1} in
1) type="GENERAL" ;;
2) type="MISUSE" ;;
126) type="CANTEXEC" ;;
127) type="CMDNF" ;;
129) type="SIGHUP" ;;
130) type="SIGINT" ;;
131) type="SIGQUIT" ;;
132) type="SIGILL" ;;
133) type="SIGTRAP" ;;
134) type="SIGABRT" ;;
135) type="SIGBUS" ;;
136) type="SIGFPE" ;;
137) type="SIGKILL" ;;
138) type="SIGUSR1" ;;
139) type="SIGSEGV" ;;
140) type="SIGUSR2" ;;
141) type="SIGPIPE" ;;
142) type="SIGALRM" ;;
143) type="SIGTERM" ;;
144) type="" ;;
145) type="SIGCHLD" ;;
146) type="SIGCONT" ;;
147) type="SIGSTOP" ;;
148) type="SIGTSTP" ;;
149) type="SIGTTIN" ;;
150) type="SIGTTOU" ;;
151) type="SIGURG" ;;
152) type="SIGXCPU" ;;
153) type="SIGXFSZ" ;;
154) type="SIGVTALRM" ;;
155) type="SIGPROF" ;;
156) type="SIGWINCH" ;;
157) type="SIGIO" ;;
158) type="SIGPWR" ;;
159) type="SIGSYS" ;;
2023-12-05 23:56:04 +03:00
*) type="" ;;
2023-12-05 21:50:45 +03:00
esac
2023-11-20 18:39:04 +03:00
2023-12-05 21:50:45 +03:00
echo -n "${type}"
2023-11-20 18:39:04 +03:00
}