Skip to content

Commit

Permalink
added several demos
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Kucherenko committed Nov 24, 2023
1 parent bc13d6c commit 4b8ff41
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .scripts/_colors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 0 additions & 10 deletions .scripts/_commons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:-""}
Expand Down Expand Up @@ -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"
81 changes: 54 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -112,7 +118,7 @@ echo "Is --debug: $DEBUG"
parse:arguments "$@"
```

### Common(s) Functions
### Common(s) Functions And Inputs

```bash
source ".scripts/_commons.sh"
Expand All @@ -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 "[email protected]" -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
Expand All @@ -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
```
42 changes: 42 additions & 0 deletions demos/demo.colors.sh
Original file line number Diff line number Diff line change
@@ -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 ""
11 changes: 11 additions & 0 deletions demos/demo.readpswd.sh
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions demos/demo.selector.sh
Original file line number Diff line number Diff line change
@@ -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}"
Binary file added docs/bootstrap.direnv.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/profiler.version-up.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/terminal.colors.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ui.ask-for-password.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ui.selector.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4b8ff41

Please sign in to comment.