Read Write eXecute framework rwx.rwx.work
at dev 209 lines 4.4 kB view raw
1# ╭───────╮ 2# │ shell │ 3# ╰───────╯ 4 5# currently running shell name 6RWX_SHELL="$(cat "/proc/${$}/comm")" 7 8_rwx_shell_color() { 9 local code="${1}" 10 case "${RWX_SHELL}" in 11 "bash") 12 printf "\x01\e[0" 13 if [ -n "${code}" ]; then 14 printf "%s" ";${code}" 15 fi 16 printf "m\x02" 17 ;; 18 *) 19 printf "\033[" 20 if [ -n "${code}" ]; then 21 printf "%s" "${code}" 22 else 23 printf "0" 24 fi 25 printf "m" 26 ;; 27 esac 28} 29 30# ╭───────┬───────────╮ 31# │ shell │ constants │ 32# ╰───────┴───────────╯ 33 34RWX_COLOR_BROWN="$(_rwx_shell_color 33)" 35RWX_COLOR_CYAN="$(_rwx_shell_color 36)" 36RWX_COLOR_DEFAULT="$(_rwx_shell_color)" 37RWX_COLOR_GREEN="$(_rwx_shell_color 31)" 38RWX_COLOR_MAGENTA="$(_rwx_shell_color 35)" 39RWX_COLOR_RED="$(_rwx_shell_color 32)" 40 41# ╭───────┬───────────╮ 42# │ shell │ variables │ 43# ╰───────┴───────────╯ 44 45# current shell interactive mode 46rwx_shell_interactive=1 47 48# ╭───────┬───────────╮ 49# │ shell │ functions │ 50# ╰───────┴───────────╯ 51 52# output help message 53rwx_shell_help() { 54 rwx_log_debug "rwx_…" 55} 56 57# return current shell interactive mode 58rwx_shell_interactive() { 59 return "${rwx_shell_interactive}" 60} 61 62rwx_shell_configure() { 63 [ -n "${ENV}" ] || ENV="${rwx_main_path}" 64 export ENV 65 # prompt 66 PS1="\$(rwx_shell_prompt \${?})" 67 PS2="├ " 68 # specific 69 case "${RWX_SHELL}" in 70 "bash") 71 # completion 72 local root="/usr/share/bash-completion" 73 local file="bash_completion" 74 local path="${root}/${file}" 75 # shellcheck disable=SC1090 76 [ -f "${path}" ] && . "${path}" 77 root="${root}/completions" 78 if [ -d "${root}" ]; then 79 set \ 80 "git" \ 81 "tar" 82 for file; do 83 path="${root}/${file}" 84 # shellcheck disable=SC1090 85 [ -f "${path}" ] && . "${path}" 86 done 87 fi 88 # history 89 HISTCONTROL="ignorespace" 90 HISTSIZE=-1 91 HISTTIMEFORMAT="%Y%m%d %H%M%S " 92 ;; 93 *) ;; 94 esac 95} 96 97rwx_shell_prompt() { 98 local date host id 99 local code="${1}" 100 date="$(date +%H:%M:%S)" 101 local git 102 host="$(hostname)" 103 id="$(id --user)" 104 local path="${PWD}" 105 local user="${USER}" 106 local view="╰ " 107 # code 108 if [ "${code}" -ne 0 ]; then 109 view="${view}${RWX_COLOR_GREEN}" 110 else 111 view="${view}${RWX_COLOR_RED}" 112 fi 113 view="${view}${code}" 114 # date 115 view="${view}${RWX_COLOR_DEFAULT} @ " 116 view="${view}${RWX_COLOR_BROWN}${date}" 117 # git 118 if command -v "__git_ps1" >"/dev/null"; then 119 git="$(__git_ps1)" 120 if [ -n "${git}" ]; then 121 view="${view}${RWX_COLOR_DEFAULT}${RWX_COLOR_MAGENTA}${git}" 122 fi 123 fi 124 # new 125 view="${view}\\n" 126 # path 127 view="${view}${RWX_COLOR_CYAN}${path}" 128 # new 129 view="${view}\\n" 130 # frame 131 view="${view}${RWX_COLOR_DEFAULT}" 132 # user 133 if [ "${id}" -eq 0 ]; then 134 view="${view}${RWX_COLOR_GREEN}" 135 else 136 view="${view}${RWX_COLOR_RED}" 137 fi 138 view="${view}${user}" 139 # host 140 view="${view}${RWX_COLOR_DEFAULT} @ " 141 view="${view}${RWX_COLOR_BROWN}${host}" 142 # new 143 view="${view}\\n" 144 # prompt 145 view="${view}${RWX_COLOR_DEFAULT}${PS2}" 146 # print 147 printf "%b" "${view}" 148} 149 150# ╭───────┬───────────╮ 151# │ shell │ shortcuts │ 152# ╰───────┴───────────╯ 153 154# shorten alias 155#= a 156rwx_shell_alias() { 157 alias \ 158 "${@}" 159} 160 161# swap directory (current ↔ previous) 162#= sd 163rwx_shell_swap_directory() { 164 cd \ 165 - || 166 return 167} 168 169# exit terminal 170#= x 171rwx_shell_exit() { 172 exit \ 173 "${@}" 174} 175 176# ╭───────┬──────╮ 177# │ shell │ main │ 178# ╰───────┴──────╯ 179 180rwx_shell_main() { 181 # set current shell interactive mode 182 case "${-}" in 183 *"i"*) rwx_shell_interactive=0 ;; 184 *) ;; 185 esac 186 # configure shell 187 rwx_shell_configure 188 # context / shell 189 if rwx_shell_interactive; then 190 # display help 191 rwx_shell_help 192 # context / command 193 else 194 local command 195 local function 196 # command name used to run 197 # (stripped from hyphen interactive flag) 198 command="$(basename "${0}" | sed "s|^-||")" 199 case "${command}" in 200 "bash" | "dash" | "sh") unset command ;; 201 *) ;; 202 esac 203 # find the matching function 204 function="$(rwx_code_action_target "function" "${command}")" 205 if [ -n "${function}" ]; then 206 "${function}" "${@}" 207 fi 208 fi 209}