rwx/sh/shell.sh
2024-11-20 14:39:01 +01:00

120 lines
2.3 KiB
Bash

_sh_shell_color() {
local code="${1}"
case "${SH_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
}
SH_COLOR_BROWN="$(_sh_shell_color 33)"
SH_COLOR_CYAN="$(_sh_shell_color 36)"
SH_COLOR_DEFAULT="$(_sh_shell_color)"
SH_COLOR_GREEN="$(_sh_shell_color 31)"
SH_COLOR_MAGENTA="$(_sh_shell_color 35)"
SH_COLOR_RED="$(_sh_shell_color 32)"
sh_shell_configure() {
case "${SH_SHELL}" in
"bash")
# completion
local root="/usr/share/bash-completion"
local file="bash_completion"
local path="${root}/${file}"
[ -f "${path}" ] && . "${path}"
root="${root}/completions"
if [ -d "${root}" ]; then
set \
"git" \
"tar"
for file in "${@}"; do
path="${root}/${file}"
[ -f "${path}" ] && . "${path}"
done
fi
# history
HISTCONTROL="ignorespace"
HISTSIZE=-1
HISTTIMEFORMAT="%Y%m%d %H%M%S "
# prompt
PS1="\$(sh_shell_prompt \${?})"
PS2="├ "
;;
*) ;;
esac
}
sh_shell_configure
sh_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}${SH_COLOR_GREEN}"
else
view="${view}${SH_COLOR_RED}"
fi
view="${view}${code}"
# date
view="${view}${SH_COLOR_DEFAULT} @ "
view="${view}${SH_COLOR_BROWN}${date}"
# git
if command -v "__git_ps1" >"/dev/null"; then
git="$(__git_ps1)"
if [ -n "${git}" ]; then
view="${view}${SH_COLOR_DEFAULT}${SH_COLOR_MAGENTA}${git}"
fi
fi
# new
view="${view}\\n"
# path
view="${view}${SH_COLOR_CYAN}${path}"
# new
view="${view}\\n"
# frame
view="${view}${SH_COLOR_DEFAULT}"
# user
if [ "${id}" -eq 0 ]; then
view="${view}${SH_COLOR_GREEN}"
else
view="${view}${SH_COLOR_RED}"
fi
view="${view}${user}"
# host
view="${view}${SH_COLOR_DEFAULT} @ "
view="${view}${SH_COLOR_BROWN}${host}"
# new
view="${view}\\n"
# prompt
view="${view}${SH_COLOR_DEFAULT}${PS2}"
# print
printf "%b" "${view}"
}
sh_shell_setup() {
# shell
echo "export ENV=\"${ENV}\"" >"/etc/profile.d/sh.sh"
# bash
local file="/etc/bash.bashrc"
rm --force --recursive "${file}"
ln --symbolic "${ENV}" "${file}"
}