sh/shell/shell.sh
2024-11-16 02:27:43 +01:00

99 lines
1.6 KiB
Bash

SH="$(cat /proc/$$/comm)"
shell_color() {
local code="${1}"
case "${SH}" in
"bash")
printf "\e[0"
if [ -n "${code}" ]; then
printf "%s" ";${code}"
fi
;;
*)
printf "\033["
if [ -n "${code}" ]; then
printf "%s" "${code}"
else
printf "0"
fi
;;
esac
printf "m"
}
SH_DEFAULT="$(shell_color)"
SH_GREEN="$(shell_color 31)"
SH_RED="$(shell_color 32)"
SH_BROWN="$(shell_color 33)"
SH_MAGENTA="$(shell_color 35)"
SH_CYAN="$(shell_color 36)"
case "${SH}" in
"bash")
# completion
file="/usr/share/bash-completion/bash_completion"
if [ -f "${file}" ]; then
. "${file}"
fi
# history
HISTCONTROL="ignorespace"
HISTSIZE=-1
HISTTIMEFORMAT="%Y%m%d %H%M%S "
;;
*)
;;
esac
# prompt
ps1() {
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 [ "$(type -t __git_ps1)" = "function" ]; 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}"
# 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}${SH_DEFAULT}"
echo -e "${view}\n${PS2}"
}
PS1="\$(ps1 \${?})"
PS2="\
├ "