From c9fd0aca3ee98479fc66e7d2f19604da7de9e620 Mon Sep 17 00:00:00 2001 From: pvincent Date: Tue, 5 Mar 2024 22:30:01 +0400 Subject: [PATCH] improved readability --- .vscode/extensions.json | 1 + .vscode/settings.json | 3 + bash.bashrc | 147 +++++++++++++++++++++------------------- inputrc | 5 +- install.sh | 68 +++++++++++-------- tools/elapsed | 8 +++ tools/semver_git_tag | 2 +- tools/timing | 13 ---- 8 files changed, 131 insertions(+), 116 deletions(-) create mode 100644 .vscode/settings.json create mode 100755 tools/elapsed delete mode 100755 tools/timing diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 600bc2c..97ce921 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -2,5 +2,6 @@ "recommendations": [ "mads-hartmann.bash-ide-vscode", "rogalmic.bash-debug", + "mkhl.shfmt", ], } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..23fd35f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": true +} \ No newline at end of file diff --git a/bash.bashrc b/bash.bashrc index b213b58..fa2b1ec 100644 --- a/bash.bashrc +++ b/bash.bashrc @@ -1,65 +1,16 @@ -# ~/.bashrc: executed by bash(1) for non-login shells. +#!/bin/bash -# If not running interactively, don't do anything -case $- in -*i*) ;; -*) return ;; -esac - -# don't put duplicate lines or lines starting with space in the history. -HISTCONTROL=ignoreboth - -# append to the history file, don't overwrite it -shopt -s histappend - -# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) -HISTSIZE=10000 -HISTFILESIZE=20000 - -# check the window size after each command and, if necessary, -# update the values of LINES and COLUMNS. -shopt -s checkwinsize - -# make less more friendly for non-text input files, see lesspipe(1) -[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" - -# set a fancy prompt (non-color, unless we know we "want" color) -case "$TERM" in -xterm-color | *-256color) - color_prompt=yes - export COLOR_OPTIONS=' --color=auto' - ;; -esac - -export LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.sql.gz=01;35:*.mariadb.gz=01;35:*.postgres.gz=01;35:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:" -export LESS="r" - -# enable programmable completion features (you don't need to enable -# this, if it's already enabled in /etc/bash.bashrc and /etc/profile -# sources /etc/bash.bashrc). -if ! shopt -oq posix; then - if [ -f /usr/share/bash-completion/bash_completion ]; then - . /usr/share/bash-completion/bash_completion - elif [ -f /etc/bash_completion ]; then - . /etc/bash_completion - fi -fi - -# prevent CTRL+S to freeze in vim, CTRL+Q to unfreeze! -stty -ixon - -__prompt_command() { +function __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\]' + 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" @@ -68,7 +19,8 @@ __prompt_command() { if [[ -n ${container_hostname:-} ]]; then PS1+="${Gra}[LXC:${R}" - local host=$(builtin echo $container_hostname | tr ' ' ':') + local host + host=$(builtin echo $container_hostname | tr ' ' ':') case ${host:0:4} in 'beta') PS1+="${Yel}" @@ -81,8 +33,8 @@ __prompt_command() { PS1+="${host}${Gra}] " fi - if [ $(id -u) -eq 0 ]; then - PS1+="${Yel}\u${R}" + if [[ "$(id -u)" -eq 0 ]]; then + PS1+="$Yel\u$R" PROMPT='$' else PS1+="$Gre\u$R" @@ -107,18 +59,18 @@ __prompt_command() { 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) + local branch tag_release dirty + 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) + 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 @@ -147,21 +99,22 @@ __prompt_command() { } -__vte_osc7() { +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=$(builtin echo $container_hostname | tr ' ' ':') + 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}")" } -__vte_urlencode() ( +function __vte_urlencode { # This is important to make sure string manipulation is handled # byte-by-byte. LC_ALL=C @@ -175,7 +128,59 @@ __vte_urlencode() ( str="${str#?}" fi done -) +} + +###------ +### MAIN +###------ + +# If not running interactively, don't do anything +case $- in +*i*) ;; +*) return ;; +esac + +# don't put duplicate lines or lines starting with space in the history. +HISTCONTROL=ignoreboth + +# append to the history file, don't overwrite it +shopt -s histappend + +# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) +HISTSIZE=10000 +HISTFILESIZE=20000 + +# check the window size after each command and, if necessary, +# update the values of LINES and COLUMNS. +shopt -s checkwinsize + +# make less more friendly for non-text input files, see lesspipe(1) +[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" + +# set a fancy prompt (non-color, unless we know we "want" color) +case "$TERM" in +xterm-color | *-256color) + color_prompt=yes + export COLOR_OPTIONS=' --color=auto' + ;; +esac + +export LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.sql.gz=01;35:*.mariadb.gz=01;35:*.postgres.gz=01;35:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:" +export LESS="r" + +# enable programmable completion features (you don't need to enable +# this, if it's already enabled in /etc/bash.bashrc and /etc/profile +# sources /etc/bash.bashrc). +if ! shopt -oq posix; then + if [ -f /usr/share/bash-completion/bash_completion ]; then + . /usr/share/bash-completion/bash_completion + elif [ -f /etc/bash_completion ]; then + . /etc/bash_completion + fi +fi + +# prevent CTRL+S to freeze in vim, CTRL+Q to unfreeze! +stty -ixon PROMPT_COMMAND='__prompt_command' # Func to gen PS1 after CMDs @@ -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. # See /usr/share/doc/bash-doc/examples in the bash-doc package. -if [ -f ~/.bash_aliases ]; then - . ~/.bash_aliases +if [ -f "$HOME/.bash_aliases" ]; then + . "$HOME/.bash_aliases" fi # Add path to any tools folder in /opt/miaou-* diff --git a/inputrc b/inputrc index 60433a8..0cea7e3 100644 --- a/inputrc +++ b/inputrc @@ -1,3 +1,5 @@ +# GNU Readline main configuration file + ## completion case-insentivive and ambiguous set completion-ignore-case on set show-all-if-ambiguous on @@ -33,5 +35,4 @@ set completion-query-items 20 "\C-H": backward-kill-word # SHIFT + DELETE for whole line suppressing - "\e[3;2~": kill-whole-line - +"\e[3;2~": kill-whole-line diff --git a/install.sh b/install.sh index 486afd6..2f12d16 100755 --- a/install.sh +++ b/install.sh @@ -1,39 +1,49 @@ #!/bin/bash +## CONSTANTS -function install() { - [ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 1 +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" - 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 +## FUNCTIONS - else - apt-get update - apt-get install -y "${REQUIRED_PKGS[@]}" - ./init.sh - fi +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 } ## MAIN -CURDIR=$PWD -REQUIRED_PKGS=(file git bc) -install +root_required + +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 diff --git a/tools/elapsed b/tools/elapsed new file mode 100755 index 0000000..d939471 --- /dev/null +++ b/tools/elapsed @@ -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 diff --git a/tools/semver_git_tag b/tools/semver_git_tag index b9b6538..ce46d1c 100755 --- a/tools/semver_git_tag +++ b/tools/semver_git_tag @@ -5,7 +5,7 @@ function usage() { exit 1 } -function setCommand() { +function setCommand { #arg1 input, #arg2 return value declare -n ret=$2 diff --git a/tools/timing b/tools/timing deleted file mode 100755 index 32ba5ac..0000000 --- a/tools/timing +++ /dev/null @@ -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}"