improved readability

This commit is contained in:
pvincent
2024-03-05 22:30:01 +04:00
parent 44f50c773b
commit c9fd0aca3e
8 changed files with 194 additions and 179 deletions
+1
View File
@@ -2,5 +2,6 @@
"recommendations": [ "recommendations": [
"mads-hartmann.bash-ide-vscode", "mads-hartmann.bash-ide-vscode",
"rogalmic.bash-debug", "rogalmic.bash-debug",
"mkhl.shfmt",
], ],
} }
+3
View File
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
+137 -132
View File
@@ -1,4 +1,138 @@
# ~/.bashrc: executed by bash(1) for non-login shells. #!/bin/bash
function __prompt_command {
local EXIT="$?" # This needs to be first
local R='\[\e[0m\]'
local Red='\[\e[31m\]'
local Gre='\[\e[32m\]'
local Yel='\[\e[33m\]'
local Blu='\[\e[34m\]'
local Cya='\[\e[36m\]'
local Mag='\[\e[95m\]'
local Gra='\[\e[90m\]'
# set xterm title
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
PS1=''
if [[ -n ${container_hostname:-} ]]; then
PS1+="${Gra}[LXC:${R}"
local host
host=$(builtin echo $container_hostname | tr ' ' ':')
case ${host:0:4} in
'beta')
PS1+="${Yel}"
;;
'prod')
PS1+="${Red}"
;;
*) ;;
esac
PS1+="${host}${Gra}] "
fi
if [[ "$(id -u)" -eq 0 ]]; then
PS1+="$Yel\u$R"
PROMPT='$'
else
PS1+="$Gre\u$R"
PROMPT='#'
fi
PS1+="${Gra}@"
case ${HOSTNAME:0:4} in
'beta')
PS1+="${Yel}"
;;
'prod')
PS1+="${Red}"
;;
*)
PS1+="${R}"
;;
esac
PS1+="\h"
local pwd='~'
[ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
PS1+=" ${Cya}${pwd}$(__vte_osc7)${R}"
if hash git 2>&-; then
# git command exists
if git rev-parse --git-dir >/dev/null 2>&1; then
# current dir is version controlled
local branch tag_release dirty
branch=$(git branch 2>/dev/null | grep -e ^* | cut -d ' ' -f2)
PS1+=" ${R}[${Mag}${branch}${R}|"
tag_release=$(git describe --tags 2>/dev/null | cut -d'-' -f1)
dirty=$(git status -s | wc -l)
if [[ $dirty -ne 0 ]]; then
PS1+="⚡|${tag_release}${Yel}…$dirty"
else
local ahead=$(git rev-list --branches --not --remotes | wc -l)
if [[ $ahead -ne 0 ]]; then
PS1+="⚡|${tag_release}${Cya}↑${ahead}"
else
if [[ -z $tag_release ]]; then
PS1+="${Blu}✔"
else
PS1+="${Gre}✔${R}|${Blu}${tag_release}"
fi
fi
fi
PS1+="${R}]"
fi
fi
if [ $EXIT != 0 ]; then
PS1+="$Mag" # Add red if exit code non 0
else
PS1+="$Gra"
fi
PS1+=" $PROMPT ${R}"
}
function __vte_osc7 {
# HINT: special character vte to get TILIX show proper hostname and pwd
# Use \[...\] around the parts of PS1 that have length 0.
# https://unix.stackexchange.com/questions/28827/why-is-my-bash-prompt-getting-bugged-when-i-browse-the-history#28828
VTE_INFO="${HOSTNAME:-}"
if [[ -n ${container_hostname:-} ]]; then
local host
host=$(builtin echo $container_hostname | tr ' ' ':')
VTE_INFO="$host:$HOSTNAME"
fi
printf "\[\033]7;file://%s%s\a\]" "${VTE_INFO}" "$(__vte_urlencode "${PWD}")"
}
function __vte_urlencode {
# This is important to make sure string manipulation is handled
# byte-by-byte.
LC_ALL=C
str="$1"
while [ -n "$str" ]; do
safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
printf "%s" "$safe"
str="${str#"$safe"}"
if [ -n "$str" ]; then
printf "%%%02X" "'$str"
str="${str#?}"
fi
done
}
###------
### MAIN
###------
# If not running interactively, don't do anything # If not running interactively, don't do anything
case $- in case $- in
@@ -48,135 +182,6 @@ fi
# prevent CTRL+S to freeze in vim, CTRL+Q to unfreeze! # prevent CTRL+S to freeze in vim, CTRL+Q to unfreeze!
stty -ixon stty -ixon
__prompt_command() {
local EXIT="$?" # This needs to be first
local R='\[\e[0m\]'
local Red='\[\e[1;31m\]'
local Gre='\[\e[1;32m\]'
local Yel='\[\e[1;33m\]'
local Ora='\[\e[0;33m\]'
local Blu='\[\e[1;34m\]'
local Cya='\[\e[1;36m\]'
local Mag='\[\e[1;95m\]'
local Gra='\[\e[1;30m\]'
# set xterm title
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
PS1=''
if [[ -n ${container_hostname:-} ]]; then
PS1+="${Gra}[LXC:${R}"
local host=$(builtin echo $container_hostname | tr ' ' ':')
case ${host:0:4} in
'beta')
PS1+="${Yel}"
;;
'prod')
PS1+="${Red}"
;;
*) ;;
esac
PS1+="${host}${Gra}] "
fi
if [ $(id -u) -eq 0 ]; then
PS1+="${Yel}\u${R}"
PROMPT='$'
else
PS1+="$Gre\u$R"
PROMPT='#'
fi
PS1+="${Gra}@"
case ${HOSTNAME:0:4} in
'beta')
PS1+="${Yel}"
;;
'prod')
PS1+="${Red}"
;;
*)
PS1+="${R}"
;;
esac
PS1+="\h"
local pwd='~'
[ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
PS1+=" ${Cya}${pwd}$(__vte_osc7)${R}"
#PS1+=" ${Cya}\w${R}"
if hash git 2>&-; then
# git command exists
if git rev-parse --git-dir >/dev/null 2>&1; then
# current dir is version controlled
local branch=$(git branch 2>/dev/null | grep -e ^* | cut -d ' ' -f2)
PS1+=" ${R}[${Mag}${branch}${R}|"
local tag_release=$(git describe --tags 2>/dev/null | cut -d'-' -f1)
local dirty=$(git status -s | wc -l)
if [[ $dirty -ne 0 ]]; then
PS1+="⚡|${tag_release}${Yel}…$dirty"
else
local ahead=$(git rev-list --branches --not --remotes | wc -l)
if [[ $ahead -ne 0 ]]; then
PS1+="⚡|${tag_release}${Cya}↑${ahead}"
else
if [[ -z $tag_release ]]; then
PS1+="${Blu}✔"
else
PS1+="${Gre}✔${R}|${Blu}${tag_release}"
fi
fi
fi
PS1+="${R}]"
fi
fi
if [ $EXIT != 0 ]; then
PS1+="$Mag" # Add red if exit code non 0
else
PS1+="$Gra"
fi
PS1+=" $PROMPT ${R}"
}
__vte_osc7() {
# HINT: special character vte to get TILIX show proper hostname and pwd
# Use \[...\] around the parts of PS1 that have length 0.
# https://unix.stackexchange.com/questions/28827/why-is-my-bash-prompt-getting-bugged-when-i-browse-the-history#28828
VTE_INFO="${HOSTNAME:-}"
if [[ -n ${container_hostname:-} ]]; then
local host=$(builtin echo $container_hostname | tr ' ' ':')
VTE_INFO="$host:$HOSTNAME"
fi
printf "\[\033]7;file://%s%s\a\]" "${VTE_INFO}" "$(__vte_urlencode "${PWD}")"
}
__vte_urlencode() (
# This is important to make sure string manipulation is handled
# byte-by-byte.
LC_ALL=C
str="$1"
while [ -n "$str" ]; do
safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
printf "%s" "$safe"
str="${str#"$safe"}"
if [ -n "$str" ]; then
printf "%%%02X" "'$str"
str="${str#?}"
fi
done
)
PROMPT_COMMAND='__prompt_command' # Func to gen PS1 after CMDs PROMPT_COMMAND='__prompt_command' # Func to gen PS1 after CMDs
MIAOU_BASH_DIR=/opt/miaou-bash MIAOU_BASH_DIR=/opt/miaou-bash
@@ -211,8 +216,8 @@ alias ssu4="ss -ntel4p | perl -pne 'if(/uid:(\\d+)/){@a=getpwuid(\$1);s/uid:(\\d
# ~/.bash_aliases, instead of adding them here directly. # ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package. # See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then if [ -f "$HOME/.bash_aliases" ]; then
. ~/.bash_aliases . "$HOME/.bash_aliases"
fi fi
# Add path to any tools folder in /opt/miaou-* # Add path to any tools folder in /opt/miaou-*
+3 -2
View File
@@ -1,3 +1,5 @@
# GNU Readline main configuration file
## completion case-insentivive and ambiguous ## completion case-insentivive and ambiguous
set completion-ignore-case on set completion-ignore-case on
set show-all-if-ambiguous on set show-all-if-ambiguous on
@@ -33,5 +35,4 @@ set completion-query-items 20
"\C-H": backward-kill-word "\C-H": backward-kill-word
# SHIFT + DELETE for whole line suppressing # SHIFT + DELETE for whole line suppressing
"\e[3;2~": kill-whole-line "\e[3;2~": kill-whole-line
+41 -31
View File
@@ -1,39 +1,49 @@
#!/bin/bash #!/bin/bash
## CONSTANTS
function install() { readonly CURDIR=$PWD
readonly REQUIRED_PKGS=(file git bc)
readonly MAIN_TAR_GZ_URL="https://git.artcode.re/miaou/miaou-bash/archive/main.tar.gz"
## FUNCTIONS
function push_directory {
pushd "$1" || echo "unable to push directory <$1>" && exit 1
}
function pop_directory {
popd || echo "unable to pop from previous directory" && exit 1
}
function root_required {
[ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 1 [ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 1
if [[ ! $CURDIR == '/opt/miaou-bash' ]]; then
# download and fullfill /opt/miaou-bash, then run it from folder
if [[ -L /opt/miaou-bash ]]; then
cd /opt/miaou-bash && ./install.sh
exit 0
else
rm -rf /opt/miaou-bash
TEMP=$(mktemp -d)
cd "$TEMP" || return
echo "$TEMP"
wget https://git.artcode.re/miaou/miaou-bash/archive/main.tar.gz
tar -xzf main.tar.gz
mv miaou-bash /opt/
cd /opt/miaou-bash || return
./install.sh "$PARAM1"
rm -rf "$TEMP"
exit 0
fi
else
apt-get update
apt-get install -y "${REQUIRED_PKGS[@]}"
./init.sh
fi
} }
## MAIN ## MAIN
CURDIR=$PWD root_required
REQUIRED_PKGS=(file git bc)
install if [[ $CURDIR != '/opt/miaou-bash' ]]; then
# download and fullfill /opt/miaou-bash, then run it from folder
if [[ -L /opt/miaou-bash ]]; then
/opt/miaou-bash/install.sh
else
rm -rf /opt/miaou-bash
push_directory "$(mktemp -d)"
wget $MAIN_TAR_GZ_URL
tar -xzf main.tar.gz
mv miaou-bash /opt/
pop_directory
rm -rf "$TEMP"
push_directory /opt/miaou-bash && ./install.sh
fi
else
apt-get install -y "${REQUIRED_PKGS[@]}"
./init.sh
fi
Executable
+8
View File
@@ -0,0 +1,8 @@
#!/bin/bash
START=$(date +%s.%N)
eval -- "$*"
END=$(date +%s.%N)
DIFF=$(echo "scale=6; (${END} - ${START}) / 1" | bc)
printf -- "-----------------\nelapsed seconds = %f\n" "${DIFF}" >&2
+1 -1
View File
@@ -5,7 +5,7 @@ function usage() {
exit 1 exit 1
} }
function setCommand() { function setCommand {
#arg1 input, #arg2 return value #arg1 input, #arg2 return value
declare -n ret=$2 declare -n ret=$2
-13
View File
@@ -1,13 +0,0 @@
#!/bin/bash
echo >&2 "time elapsed in seconds..."
START=$(date +%s.%N)
# do something #######################
$@
#######################################
END=$(date +%s.%N)
DIFF=$(echo "scale=2; (${END} - ${START}) / 1" | bc)
echo "${DIFF}"