rwx/sh/shell.sh

117 lines
2.3 KiB
Bash
Raw Normal View History

2024-11-29 18:14:38 +00:00
_rwx_shell_color() {
2024-11-16 00:54:13 +00:00
local code="${1}"
2024-12-01 21:11:52 +00:00
case "${RWX_SHELL}" in
2024-11-16 01:19:41 +00:00
"bash")
2024-11-17 01:05:07 +00:00
printf "\x01\e[0"
2024-11-16 01:19:41 +00:00
if [ -n "${code}" ]; then
printf "%s" ";${code}"
fi
2024-11-17 01:05:07 +00:00
printf "m\x02"
2024-11-16 01:19:41 +00:00
;;
*)
2024-11-16 01:44:04 +00:00
printf "\033["
2024-11-16 01:19:41 +00:00
if [ -n "${code}" ]; then
printf "%s" "${code}"
else
printf "0"
fi
2024-11-17 01:05:07 +00:00
printf "m"
2024-11-16 01:19:41 +00:00
;;
esac
2024-11-16 00:54:13 +00:00
}
2024-11-29 18:14:38 +00:00
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)"
2024-11-16 00:54:13 +00:00
2024-11-29 18:14:38 +00:00
rwx_shell_configure() {
2024-11-29 21:28:09 +00:00
[ -n "${ENV}" ] || ENV="${RWX_MAIN_PATH}"
2024-11-25 16:32:44 +00:00
export ENV
# prompt
2024-11-29 18:14:38 +00:00
PS1="\$(rwx_shell_prompt \${?})"
2024-11-25 16:32:44 +00:00
PS2="├ "
# specific
2024-12-01 21:11:52 +00:00
case "${RWX_SHELL}" in
2024-11-16 02:21:30 +00:00
"bash")
# completion
2024-11-17 17:49:04 +00:00
local root="/usr/share/bash-completion"
2024-11-17 18:23:24 +00:00
local file="bash_completion"
local path="${root}/${file}"
2024-11-25 13:27:23 +00:00
# shellcheck disable=SC1090
2024-11-17 18:23:24 +00:00
[ -f "${path}" ] && . "${path}"
root="${root}/completions"
if [ -d "${root}" ]; then
set \
"git" \
"tar"
for file in "${@}"; do
path="${root}/${file}"
2024-11-25 13:27:23 +00:00
# shellcheck disable=SC1090
2024-11-17 18:23:24 +00:00
[ -f "${path}" ] && . "${path}"
done
fi
2024-11-16 02:21:30 +00:00
# history
HISTCONTROL="ignorespace"
HISTSIZE=-1
HISTTIMEFORMAT="%Y%m%d %H%M%S "
;;
2024-11-16 02:23:19 +00:00
*) ;;
2024-11-16 02:21:30 +00:00
esac
}
2024-11-29 18:14:38 +00:00
rwx_shell_configure
2024-11-15 16:06:29 +00:00
2024-11-29 18:14:38 +00:00
rwx_shell_prompt() {
2024-11-16 01:55:41 +00:00
local date host id
2024-11-13 12:14:58 +00:00
local code="${1}"
date="$(date +%H:%M:%S)"
2024-11-12 09:18:44 +00:00
local git
2024-11-13 12:14:58 +00:00
host="$(hostname)"
2024-11-16 00:41:42 +00:00
id="$(id --user)"
2024-11-12 09:18:44 +00:00
local path="${PWD}"
local user="${USER}"
local view="└ "
2024-11-16 01:27:43 +00:00
# code
2024-11-13 12:14:58 +00:00
if [ "${code}" -ne 0 ]; then
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_GREEN}"
2024-11-12 09:18:44 +00:00
else
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_RED}"
2024-11-12 09:18:44 +00:00
fi
2024-11-16 01:27:43 +00:00
view="${view}${code}"
# date
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_DEFAULT} @ "
view="${view}${RWX_COLOR_BROWN}${date}"
2024-11-16 01:27:43 +00:00
# git
2024-11-16 01:55:41 +00:00
if command -v "__git_ps1" >"/dev/null"; then
2024-11-12 09:18:44 +00:00
git="$(__git_ps1)"
2024-11-13 12:10:19 +00:00
if [ -n "${git}" ]; then
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_DEFAULT}${RWX_COLOR_MAGENTA}${git}"
2024-11-12 09:18:44 +00:00
fi
fi
2024-11-16 01:27:43 +00:00
# new
2024-11-17 00:31:06 +00:00
view="${view}\\n"
2024-11-16 01:27:43 +00:00
# path
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_CYAN}${path}"
2024-11-16 01:27:43 +00:00
# new
2024-11-17 00:31:06 +00:00
view="${view}\\n"
2024-11-16 01:27:43 +00:00
# frame
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_DEFAULT}"
2024-11-16 01:27:43 +00:00
# user
2024-11-16 00:41:42 +00:00
if [ "${id}" -eq 0 ]; then
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_GREEN}"
2024-11-12 09:18:44 +00:00
else
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_RED}"
2024-11-12 09:18:44 +00:00
fi
2024-11-16 01:27:43 +00:00
view="${view}${user}"
# host
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_DEFAULT} @ "
view="${view}${RWX_COLOR_BROWN}${host}"
2024-11-16 01:43:06 +00:00
# new
2024-11-17 00:31:06 +00:00
view="${view}\\n"
2024-11-16 01:43:06 +00:00
# prompt
2024-11-29 18:14:38 +00:00
view="${view}${RWX_COLOR_DEFAULT}${PS2}"
2024-11-16 01:43:06 +00:00
# print
2024-11-17 00:32:34 +00:00
printf "%b" "${view}"
2023-08-20 16:33:26 +00:00
}