diff --git a/.scripts/_colors.sh b/.scripts/_colors.sh index fd9981d..343af3b 100755 --- a/.scripts/_colors.sh +++ b/.scripts/_colors.sh @@ -30,3 +30,5 @@ export cl_lpurple=$(tput setaf 13) export cl_lcyan=$(tput setaf 14) export cl_lwhite=$(tput setaf 15) export cl_black=$(tput setaf 16) + +export cl_selected=$(tput setab 241 && tput setaf 15) diff --git a/.scripts/_commons.sh b/.scripts/_commons.sh index 157de18..1b02ffb 100755 --- a/.scripts/_commons.sh +++ b/.scripts/_commons.sh @@ -364,7 +364,6 @@ function input:selector() { local distance=$((max_col - x_pos - ${#hint} - 4)) local filler=$(printf ' %.0s' $(seq 1 $distance)) local eraser=$(printf ' %.0s' $(seq 1 $((max_col - x_pos - 1)))) - local cl_selected="$(tput setab 0 && tput setaf 7)" function selections() { local highlight=${1:-""} @@ -473,12 +472,3 @@ alias validate_yn_input=validate:input:yn alias env_variable_or_secret_file=env:variable:or:secret:file alias optional_env_variable_or_secret_file=env:variable:or:secret:file:optional alias isHelp=args:isHelp - -# Usage: -# declare -A -g connections && connections=(["d"]="direct" ["s"]="proxy-to-staging" ["p"]="proxy-to-local") -# echo -n "Select connection type: " && tput civis # hide cursor -# selected=$(input:selector "connections") && echo "${cl_blue}$selected${cl_reset}" - -# Usage: -# echo -n "Enter password: " -# password=$(input:readpwd) && echo "" && echo "Password: $password" diff --git a/README.md b/README.md index c19e55c..1c7ac83 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,14 @@ - [TDD - Test Driven Development, run tests on file change](#tdd---test-driven-development-run-tests-on-file-change) - [Usage](#usage) - [Colors](#colors) + - [Script Dependencies](#script-dependencies) - [Logger](#logger) - - [Dependencies](#dependencies) - [Arguments Parsing](#arguments-parsing) - - [Common(s) Functions](#commons-functions) - - [Deploy / GitHub Pages](#deploy--github-pages) + - [Common(s) Functions And Inputs](#commons-functions-and-inputs) + - [UI: Selector](#ui-selector) + - [UI: Ask for Password](#ui-ask-for-password) - [Profile BASH script execution](#profile-bash-script-execution) + - [Colors support in my terminal](#colors-support-in-my-terminal) ## Roadmap @@ -21,10 +23,12 @@ - [ ] Slack notifications helper scripts - [ ] Telemetry module (report metrics to CI or DataDog) - [ ] Globals module (declarative way of defining script dependencies to global environment variables) -- [ ] Loggs monitoring documentation (different streams/files/tty for different information: info, debug, telemetry, dependencies) +- [ ] Logs monitoring documentation (different streams/files/tty for different information: info, debug, telemetry, dependencies) ## Local Dev Environment - Requirements +![Bootstrap](docs/bootstrap.direnv.gif) + - DirEnv - https://github.com/direnv/direnv - ShellFormat - https://github.com/mvdan/sh - ShellCheck - https://github.com/koalaman/shellcheck @@ -61,6 +65,22 @@ source ".scripts/_colors.sh" echo -e "${cl_red}Hello World${cl_reset}" ``` +### Script Dependencies + +![Bootstrap](docs/bootstrap.direnv.gif) + +```bash +source ".scripts/_dependencies.sh" + +dependency bash "5.*.*" "brew install bash" +dependency direnv "2.*.*" "curl -sfL https://direnv.net/install.sh | bash" +dependency shellspec "0.28.*" "brew install shellspec" +optional kcov "42" "brew install kcov" +dependency shellcheck "0.9.*" "curl -sS https://webi.sh/shellcheck | sh" +dependency shfmt "3.*.*" "curl -sS https://webi.sh/shfmt | sh" +dependency watchman "2023.07.*.*" "brew install watchman" +``` + ### Logger ```bash @@ -78,20 +98,6 @@ export DEBUG=*,-common # enable logger output for all tags except common config:logger:Common "$@" # re-configure logger enable/disable for common tag ``` -### Dependencies - -```bash -source ".scripts/_dependencies.sh" - -dependency bash "5.*.*" "brew install bash" -dependency direnv "2.*.*" "curl -sfL https://direnv.net/install.sh | bash" -dependency shellspec "0.28.*" "brew install shellspec" -optional kcov "42" "brew install kcov" -dependency shellcheck "0.9.*" "curl -sS https://webi.sh/shellcheck | sh" -dependency shfmt "3.*.*" "curl -sS https://webi.sh/shfmt | sh" -dependency watchman "2023.07.*.*" "brew install watchman" -``` - ### Arguments Parsing ```bash @@ -112,7 +118,7 @@ echo "Is --debug: $DEBUG" parse:arguments "$@" ``` -### Common(s) Functions +### Common(s) Functions And Inputs ```bash source ".scripts/_commons.sh" @@ -128,21 +134,35 @@ echo "Extracted: ${new_value}" # validate/confirm input parameter by user input # string # Yes/No +``` + +### UI: Selector +![Selector](docs/ui.selector.gif) -# track execution time +```bash +# Select value from short list of choices +declare -A -g connections && connections=(["d"]="production" ["s"]="cors-proxy:staging" ["p"]="cors-proxy:local") +echo -n "Select connection type: " && tput civis # hide cursor +selected=$(input:selector "connections") && echo "${cl_blue}${selected}${cl_reset}" ``` -## Deploy / GitHub Pages +### UI: Ask for Password + +![Ask for Password](docs/ui.ask-for-password.gif) ```bash -# generate ssh key for gh-pages publishing -# https://github.com/marketplace/actions/github-pages-action#%EF%B8%8F-create-ssh-deploy-key -ssh-keygen -t rsa -b 4096 -C "kucherenko.alex@gmail.com" -f gh-pages -N "" +source ".scripts/_commons.sh" + +# Usage: +echo -n "Enter password: " +password=$(input:readpwd) && echo "" && echo "Password: $password" ``` ## Profile BASH script execution +![Profiler](docs/profiler.version-up.gif) + ```bash # print timestamp for each line of executed script PS4='+ $(gdate "+%s.%N ($LINENO) ")' bash -x bin/version-up.sh @@ -157,7 +177,14 @@ PS4='+ $(echo -n "$EPOCHREALTIME [$LINENO]: ")' bash -x bin/version-up.sh 2>trac bin/profiler/profile.sh bin/version-up.sh ``` -src: https://itecnote.com/tecnote/r-performance-profiling-tools-for-shell-scripts/ +- ref1: https://itecnote.com/tecnote/r-performance-profiling-tools-for-shell-scripts/ +- ref2: https://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/ -- https://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/ -- +## Colors support in my terminal + +![Terminal Colors](docs/terminal.colors.gif) + +```bash +# print all colors for easier selection +demos/demo.colors.sh +``` diff --git a/demos/demo.colors.sh b/demos/demo.colors.sh new file mode 100755 index 0000000..c2a968a --- /dev/null +++ b/demos/demo.colors.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +# ESC[38:5:⟨n⟩m Select foreground color where n is a number from the table below +# ESC[48:5:⟨n⟩m Select background color +# "\033[38;5;%dm" "\033[48;5;%dm" "\033[0m" + +function report:colors() { + local contrast=0 reset="" nl="" + + reset=$(printf "\033[0m") + + for ((i = 0; i < 256; i++)); do + local mod8=$(((i + 1) % 8)) + local mod6=$(((i - 15) % 6)) + local c1=$((i > 231 && i < 244)) + local c2=$((i < 17 && i % 8 < 2)) + local c3=$((i > 16 && i < 232)) + local c4=$(((i - 16) % 6 < (i < 100 ? 3 : 2))) + local c5=$(((i - 16) % 36 < 15)) + + # Use conditions to set contrast + contrast=16 && nl="" + if [[ $c1 -eq 1 || $c2 -eq 1 ]] || [[ $c3 -eq 1 && $c4 -eq 1 && $c5 -eq 1 ]]; then contrast=7; fi + + if [ $i -lt 16 ] || [ $i -gt 231 ]; then + [ $mod8 -eq 0 ] && nl=$'\n' + else + [ $mod6 -eq 0 ] && nl=$'\n' + fi + + printf " \033[48;5;%dm\033[38;5;%dm C %03d %s%s" $i $contrast $i "$reset" "$nl" + done +} + +report:colors + +echo "" +echo "Hints:" +echo " - use command 'tput setab [0-255]' to change background color" +echo " - use command 'tput setaf [0-255]' to change foreground color" +echo " - use command 'tput op' to reset colors" +echo "" diff --git a/demos/demo.readpswd.sh b/demos/demo.readpswd.sh new file mode 100755 index 0000000..10d2818 --- /dev/null +++ b/demos/demo.readpswd.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2034 + +# include other scripts: _colors, _logger, _commons, _dependencies, _arguments +scripts_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../.scripts" +# shellcheck disable=SC1090 source=../.scripts/_commons.sh +source "$scripts_dir/_commons.sh" + +# Usage: +echo -n "Enter password: " +password=$(input:readpwd) && echo "" && echo "Password: $password" diff --git a/demos/demo.selector.sh b/demos/demo.selector.sh new file mode 100755 index 0000000..6f13f7b --- /dev/null +++ b/demos/demo.selector.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2034 + +# include other scripts: _colors, _logger, _commons, _dependencies, _arguments +scripts_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../.scripts" +# shellcheck disable=SC1090 source=../.scripts/_commons.sh +source "$scripts_dir/_commons.sh" + +# Usage: +declare -A -g connections && connections=(["d"]="production" ["s"]="cors-proxy-staging" ["p"]="cors-proxy-local") +echo -n "Select connection type: " && tput civis # hide cursor +selected=$(input:selector "connections") && echo "${cl_blue}$selected${cl_reset}" diff --git a/docs/bootstrap.direnv.gif b/docs/bootstrap.direnv.gif new file mode 100644 index 0000000..edaea17 Binary files /dev/null and b/docs/bootstrap.direnv.gif differ diff --git a/docs/profiler.version-up.gif b/docs/profiler.version-up.gif new file mode 100644 index 0000000..8c6b31a Binary files /dev/null and b/docs/profiler.version-up.gif differ diff --git a/docs/terminal.colors.gif b/docs/terminal.colors.gif new file mode 100644 index 0000000..57aee14 Binary files /dev/null and b/docs/terminal.colors.gif differ diff --git a/docs/ui.ask-for-password.gif b/docs/ui.ask-for-password.gif new file mode 100644 index 0000000..1c18cc6 Binary files /dev/null and b/docs/ui.ask-for-password.gif differ diff --git a/docs/ui.selector.gif b/docs/ui.selector.gif new file mode 100644 index 0000000..6860bea Binary files /dev/null and b/docs/ui.selector.gif differ