sh/shell/shell.sh
2024-11-17 19:23:24 +01:00

152 lines
2.5 KiB
Bash

PS1="\$(shell_prompt \${?})"
PS2="\
├ "
SH="$(cat /proc/$$/comm)"
shell_color() {
local code="${1}"
case "${SH}" 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_BROWN="$(shell_color 33)"
SH_CYAN="$(shell_color 36)"
SH_DEFAULT="$(shell_color)"
SH_GREEN="$(shell_color 31)"
SH_MAGENTA="$(shell_color 35)"
SH_RED="$(shell_color 32)"
shell_configure() {
case "${SH}" 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 "
;;
*) ;;
esac
}
shell_configure
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_GREEN}"
else
view="${view}${SH_RED}"
fi
view="${view}${code}"
# date
view="${view}${SH_DEFAULT} @ "
view="${view}${SH_BROWN}${date}"
# git
if command -v "__git_ps1" >"/dev/null"; then
git="$(__git_ps1)"
if [ -n "${git}" ]; then
view="${view}${SH_DEFAULT}${SH_MAGENTA}${git}"
fi
fi
# new
view="${view}\\n"
# path
view="${view}${SH_CYAN}${path}"
# new
view="${view}\\n"
# frame
view="${view}${SH_DEFAULT}"
# user
if [ "${id}" -eq 0 ]; then
view="${view}${SH_GREEN}"
else
view="${view}${SH_RED}"
fi
view="${view}${user}"
# host
view="${view}${SH_DEFAULT} @ "
view="${view}${SH_BROWN}${host}"
# new
view="${view}\\n"
# prompt
view="${view}${SH_DEFAULT}${PS2}"
# print
printf "%b" "${view}"
}
shell_setup() {
# shell
echo "export ENV=\"${ENV}\"" >"/etc/profile.d/shell.sh"
# bash
local file="/etc/bash.bashrc"
rm --force --recursive "${file}"
ln --symbolic "${ENV}" "${file}"
}
# shorten alias
a() {
alias \
"${@}"
}
# swap directory (current ↔ previous)
sd() {
cd \
- ||
return
}
# exit terminal
x() {
exit \
"${@}"
}
[ "${SH}" = "bash" ] || return
# shellcheck disable=SC3033
..() {
cd ..
}
# shellcheck disable=SC3033
...() {
cd ../..
}