From 4492c386b62f8fe6f86c95f27dc98f8823ac3416 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 14 Apr 2023 00:01:58 +0300 Subject: [PATCH] dfm: rewrote the usage menu generation --- .editorconfig | 22 -------- local/bin/dfm | 132 +++++++++++++++++++++++++++------------------- scripts/shared.sh | 64 +++++++++++++++++++++- 3 files changed, 141 insertions(+), 77 deletions(-) diff --git a/.editorconfig b/.editorconfig index b2d9f71..0f17867 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,25 +7,3 @@ indent_size = 2 indent_style = space insert_final_newline = true trim_trailing_whitespace = true - -[*.lua] -insert_final_newline = true - -[*.yaml] -indent_size = 2 -insert_final_newline = true - -[*.yml] -indent_size = 2 -insert_final_newline = true - -[*.sh] -indent_size = 2 -indent_style = space -insert_final_newline = true -shell_variant = bash # --language-variant -binary_next_line = true -switch_case_indent = true # --case-indent -space_redirects = false -keep_padding = false -function_next_line = true # --func-next-line diff --git a/local/bin/dfm b/local/bin/dfm index ee8d798..8cb2b87 100755 --- a/local/bin/dfm +++ b/local/bin/dfm @@ -24,6 +24,22 @@ function section_install { USAGE_PREFIX="$SCRIPT install" + MENU=( + "all:Installs macos defaults, antigen, starship, brew, nvm, npm packages and others" + "antigen:Updates the antigen.zsh file" + "composer:Install composer" + "dotenv:Install dotenv-linter" + "gh:Install GitHub CLI Extensions" + "go:Install Go Packages" + "imagick:Install ImageMagick CLI" + "starship:Install starship.rs" + "macos:Setup nice macOS defaults" + "nvm:Install Node Version Manager (nvm)" + "npm:Install NPM Packages" + "ntfy:Install ntfy" + "z:Install z" + ) + case "$1" in all) $0 install macos @@ -42,71 +58,58 @@ function section_install ;; antigen) curl -L git.io/antigen > "$DOTFILES/local/bin/antigen.zsh" \ - && msg_done "🎉 New antigen installed!" + && msg_yay "New antigen installed!" ;; composer) bash "$DOTFILES/scripts/install-composer.sh" \ - && msg_done "🎉 composer installed!" + && msg_yay "composer installed!" ;; dotenv) curl -sSfL \ https://raw.githubusercontent.com/dotenv-linter/dotenv-linter/master/install.sh \ | sh -s -- -b "$XDG_BIN_HOME" \ - && msg_done "🎉 dotenv-linter installed!" + && msg_yay "dotenv-linter installed!" ;; gh) bash "$DOTFILES/scripts/install-gh-extensions.sh" \ - && msg_done "🎉 github cli extensions installed!" + && msg_yay "github cli extensions installed!" ;; go) bash "$DOTFILES/scripts/install-go-packages.sh" \ - && msg_done "🎉 go packages installed!" + && msg_yay "go packages installed!" ;; imagick) wget https://imagemagick.org/archive/binaries/magick > "$XDG_BIN_HOME/magick" \ - && msg_done "🎉 imagick installed!" + && msg_yay "imagick installed!" ;; starship) curl -sS https://starship.rs/install.sh | sh -s -- --bin-dir ~/.local/bin \ - && msg_done "🎉 starship installed!" + && msg_yay "starship installed!" ;; macos) bash "$DOTFILES/scripts/set-macos-defaults.sh" \ - && msg_done "🎉 Brewfile defined apps has been installed!" + && msg_yay "Brewfile defined apps has been installed!" ;; nvm) curl -o- "https://raw.githubusercontent.com/nvm-sh/nvm/$VERSION_NVM/install.sh" | bash \ && nvm install --lts --latest-npm --default \ && git checkout "$HOME/.zshrc" \ - && msg_done "🎉 nvm installed!" + && msg_yay "nvm installed!" ;; npm) bash "$DOTFILES/scripts/install-npm-packages.sh" \ - && msg_done "🎉 NPM Packages have been installed!" + && msg_yay "NPM Packages have been installed!" ;; ntfy) bash "$DOTFILES/scripts/install-ntfy.sh" \ - && msg_done "🎉 ntfy installed!" + && msg_yay "ntfy installed!" ;; z) bash "$DOTFILES/scripts/install-z.sh" \ - && msg_done "🎉 z has been installed!" + && msg_yay "z has been installed!" ;; *) - menu_section "$USAGE_PREFIX" "all | antigen | composer | dotenv | gh | go | imagick | starship | macos | nvm | npm" - menu_item "all" "Installs macos defaults, antigen, starship, brew, nvm, npm packages and others" - menu_item "antigen" "Updates the antigen.zsh file" - menu_item "composer" "Install composer" - menu_item "dotenv" "Install dotenv-linter" - menu_item "gh" "Install GitHub CLI Extensions" - menu_item "go" "Install Go Packages" - menu_item "imagick" "Install ImageMagick CLI" - menu_item "starship" "Install starship.rs" - menu_item "macos" "Setup nice macOS defaults" - menu_item "nvm" "Install Node Version Manager (nvm)" - menu_item "npm" "Install NPM Packages" - menu_item "ntfy" "Install ntfy" - menu_item "z" "Install z" + menu_usage "$USAGE_PREFIX" "${MENU[@]}" ;; esac } @@ -115,34 +118,37 @@ function section_brew { USAGE_PREFIX="$SCRIPT brew" + MENU=( + "install:Installs items defined in Brewfile" + "update:Updates and upgrades brew packages" + "updatebundle:Updates Brewfile with descriptions" + "autoupdate:Setups brew auto-update and runs it immediately" + ) + if ! command -v brew &> /dev/null; then menu_section "$USAGE_PREFIX" "brew not available on this system" else case "$1" in install) - brew bundle install --file="$BREWFILE" && msg_done "🎉 Done!" + brew bundle install --file="$BREWFILE" && msg_yay "Done!" ;; update) brew update && brew outdated && brew upgrade && brew cleanup - msg_done "🎉 Done!" + msg_yay "Done!" ;; updatebundle) # Updates .dotfiles/Brewfile with descriptions brew bundle dump \ --force \ --file="$BREWFILE" \ - --describe && msg_done "🎉 Done!" + --describe && msg_yay "Done!" ;; autoupdate) brew autoupdate delete brew autoupdate start 43200 --upgrade --cleanup --immediate ;; *) - menu_section "$USAGE_PREFIX" "install | update | updatebundle | autoupdate" - menu_item "install" "Installs items defined in Brewfile" - menu_item "update" "Updates and upgrades brew packages" - menu_item "updatebundle" "Updates Brewfile with descriptions" - menu_item "autoupdate" "Setups brew auto-update and runs it immediately" + menu_usage "$USAGE_PREFIX" "${MENU[@]}" ;; esac fi @@ -152,6 +158,14 @@ function section_dotfiles { USAGE_PREFIX="$SCRIPT dotfiles" + MENU=( + "fmt:Run all formatters" + "yamlfmt:Run yamlfmt to all dotfiles, which are in our control" + "shfmt:Run shfmt to all dotfiles" + "reset_all:Reset everything, runs all configured reset actions" + "reset_nvim:Resets nvim. Deletes caches, removes nvim folders and relinks nvim folders" + ) + case "$1" in fmt) msg_ok "Running all formatters" @@ -171,8 +185,8 @@ function section_dotfiles ~/.config/astronvim \ ~/.config/nvim msg_ok "Deleted old nvim files" - ln -s ~/.dotfiles/config/astronvim ~/.config/astronvim - ln -s ~/.dotfiles/config/nvim ~/.config/nvim + ln -s $DOTFILES/config/astronvim ~/.config/astronvim + ln -s $DOTFILES/config/nvim ~/.config/nvim msg_ok "Linked nvim and astronvim" hash npm 2> /dev/null && $0 install npm msg_ok "Installed packages" @@ -197,15 +211,10 @@ function section_dotfiles --func-next-line --list --write \ --indent 2 --case-indent --space-redirects \ --binary-next-line {} \; - msg_done "🎉 dotfiles have been shfmt formatted!" + msg_yay "dotfiles have been shfmt formatted!" ;; *) - menu_section "$USAGE_PREFIX" "fmt | reset_all | reset_nvim | yamlfmt | shfmt" - menu_item "fmt" "Run all formatters" - menu_item "reset_all" "Reset everything, runs all configured reset actions" - menu_item "reset_nvim" "Resets nvim. Deletes caches, removes nvim folders and relinks nvim folders" - menu_item "yamlfmt" "Run yamlfmt to all dotfiles, which are in our control" - menu_item "shfmt" "Run shfmt to all dotfiles" + menu_usage "$USAGE_PREFIX" "${MENU[@]}" ;; esac } @@ -216,6 +225,11 @@ function section_check X_HOSTNAME=$(hostname) X_ARCH=$(uname) + MENU=( + "arch :Empty returns current. Exit code 0 when match, 1 when not." + "host :Empty returns current. Exit code 0 when match, 1 when not." + ) + case "$1" in a | arch) [[ "$2" = "" ]] && echo "$X_ARCH" && exit 0 @@ -226,9 +240,7 @@ function section_check [[ $X_HOSTNAME = "$2" ]] && exit 0 || exit 1 ;; *) - menu_section "$USAGE_PREFIX" "arch | host" - menu_item "arch " "Empty returns current. Exit code 0 when match, 1 when not." - menu_item "host " "Empty returns current. Exit code 0 when match, 1 when not." + menu_usage "$USAGE_PREFIX" "${MENU[@]}" ;; esac } @@ -238,30 +250,44 @@ function section_tests { USAGE_PREFIX="$SCRIPT tests" + MENU=( + "msg:List all log functions from shared.sh" + ) + case "$1" in msg) msg "msg" msg_done "msg_done" - msg_prompt "msg_prompt" - msg_prompt_done "msg_prompt_done" + msg_done_suffix "msg_done_suffix" + msg_err "msg_err" msg_nested "msg_nested" msg_nested_done "msg_nested_done" - msg_run "msg_run" "second_param" msg_ok "msg_ok" + msg_prompt "msg_prompt" + msg_prompt_done "msg_prompt_done" + msg_run "msg_run" "second_param" + msg_run_done "msg_run_done" "second_param" msg_warn "msg_warn" - msg_err "msg_err" + msg_yay "msg_yay" + msg_yay_done "msg_yay_done" ;; *) - menu_section "$USAGE_PREFIX" "msg" - menu_item "msg" "List all log functions from shared.sh" + menu_usage "$USAGE_PREFIX" "${MENU[@]}" ;; esac } function usage { + SECTIONS=( + "install" + "reset" + "brew" + "check" + "dotfiles" + ) echo "" - menu_section "Usage: $SCRIPT" "install | reset | brew | check | dotfiles" + menu_usage_header "Usage: $SCRIPT" "${SECTIONS[@]}" echo $" All commands have their own subcommands." echo "" section_install diff --git a/scripts/shared.sh b/scripts/shared.sh index 86ee883..43eb148 100755 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -32,6 +32,11 @@ function __log_marker_warn() echo -e "${CLR_YELLOW}⁕${CLR_RESET}" } +function __log_marker_question() +{ + echo -e "${CLR_YELLOW}?${CLR_RESET}" +} + function __log_marker_err() { echo -e "${CLR_RED}⛌${CLR_RESET}" @@ -48,19 +53,33 @@ function msg() echo -e "$(__log_marker) $1" } +function msg_yay() +{ + echo -e "🎉 $1" +} + +function msg_yay_done() +{ + echo -e "🎉 $1 ...$(__log_marker_ok)" +} + function msg_done() { echo -e "$(__log_marker) $1 ...$(__log_marker_ok)" } +function msg_done_suffix() { + echo -e "$(__log_marker) ...$(__log_marker_ok)" +} + function msg_prompt() { - echo -e "$(__log_marker) $1" + echo -e "$(__log_marker_question) $1" } function msg_prompt_done() { - echo -e "$(__log_marker) $1 ...$(__log_marker_ok)" + echo -e "$(__log_marker_question) $1 ...$(__log_marker_ok)" } function msg_nested() @@ -78,6 +97,11 @@ function msg_run() echo -e "${CLR_GREEN}➜ $1${CLR_RESET} $2" } +function msg_run_done() +{ + echo -e "${CLR_GREEN}➜ $1${CLR_RESET} $2 ...$(__log_marker_ok)" +} + function msg_ok() { echo -e "$(__log_marker_ok) $1" @@ -111,3 +135,39 @@ function fn_exists() declare -f -F "$1" > /dev/null return $? } + +# Takes a bash array ("cow:moo", "dinosaur:roar") and loops +# through the keys to build menu section listing. +function menu_usage_header() +{ + MENU_CMD="$1" + shift + MENU_ARRAY=("$@") + + KEYS="" + for item in "${MENU_ARRAY[@]}"; do + CMD=$(echo "${item}"|awk -F ":" '{print $1}') + KEYS+="${CMD} | " + done + + # "???" removes 3 last characters, being " | " from the end + menu_section "$MENU_CMD" "${KEYS%???}" +} + +# Takes the usage command "$0 dotfiles" and a +# bash array ("cow:moo" "dinosaur:roar") and loops +# through in building a menu for dfm command usage listing. +function menu_usage() +{ + MENU_CMD="$1" + shift + MENU_ARRAY=("$@") + + menu_usage_header "$MENU_CMD" "${MENU_ARRAY[@]}" + + for item in "${MENU_ARRAY[@]}" ; do + CMD=$(echo "${item}" | awk -F ":" '{print $1}') + DESC=$(echo "${item}" | awk -F ":" '{print $2}') + menu_item "$CMD" "$DESC" + done +}