rwx/sh/shell.sh
2024-12-01 22:11:52 +01:00

116 lines
2.3 KiB
Bash

_rwx_shell_color() {
local code="${1}"
case "${RWX_SHELL}" in
"bash")
printf "\x01\e[0"
if [ -n "${code}" ]; then
printf "%s" ";${code}"
fi
printf "m\x02"
;;
*)
printf "\033["
if [ -n "${code}" ]; then
printf "%s" "${code}"
else
printf "0"
fi
printf "m"
;;
esac
}
RWX_COLOR_BROWN="$(_rwx_shell_color 33)"
RWX_COLOR_CYAN="$(_rwx_shell_color 36)"
RWX_COLOR_DEFAULT="$(_rwx_shell_color)"
RWX_COLOR_GREEN="$(_rwx_shell_color 31)"
RWX_COLOR_MAGENTA="$(_rwx_shell_color 35)"
RWX_COLOR_RED="$(_rwx_shell_color 32)"
rwx_shell_configure() {
[ -n "${ENV}" ] || ENV="${RWX_MAIN_PATH}"
export ENV
# prompt
PS1="\$(rwx_shell_prompt \${?})"
PS2="├ "
# specific
case "${RWX_SHELL}" in
"bash")
# completion
local root="/usr/share/bash-completion"
local file="bash_completion"
local path="${root}/${file}"
# shellcheck disable=SC1090
[ -f "${path}" ] && . "${path}"
root="${root}/completions"
if [ -d "${root}" ]; then
set \
"git" \
"tar"
for file in "${@}"; do
path="${root}/${file}"
# shellcheck disable=SC1090
[ -f "${path}" ] && . "${path}"
done
fi
# history
HISTCONTROL="ignorespace"
HISTSIZE=-1
HISTTIMEFORMAT="%Y%m%d %H%M%S "
;;
*) ;;
esac
}
rwx_shell_configure
rwx_shell_prompt() {
local date host id
local code="${1}"
date="$(date +%H:%M:%S)"
local git
host="$(hostname)"
id="$(id --user)"
local path="${PWD}"
local user="${USER}"
local view="└ "
# code
if [ "${code}" -ne 0 ]; then
view="${view}${RWX_COLOR_GREEN}"
else
view="${view}${RWX_COLOR_RED}"
fi
view="${view}${code}"
# date
view="${view}${RWX_COLOR_DEFAULT} @ "
view="${view}${RWX_COLOR_BROWN}${date}"
# git
if command -v "__git_ps1" >"/dev/null"; then
git="$(__git_ps1)"
if [ -n "${git}" ]; then
view="${view}${RWX_COLOR_DEFAULT}${RWX_COLOR_MAGENTA}${git}"
fi
fi
# new
view="${view}\\n"
# path
view="${view}${RWX_COLOR_CYAN}${path}"
# new
view="${view}\\n"
# frame
view="${view}${RWX_COLOR_DEFAULT}"
# user
if [ "${id}" -eq 0 ]; then
view="${view}${RWX_COLOR_GREEN}"
else
view="${view}${RWX_COLOR_RED}"
fi
view="${view}${user}"
# host
view="${view}${RWX_COLOR_DEFAULT} @ "
view="${view}${RWX_COLOR_BROWN}${host}"
# new
view="${view}\\n"
# prompt
view="${view}${RWX_COLOR_DEFAULT}${PS2}"
# print
printf "%b" "${view}"
}