From d6555f369a067470451760f42e40ec7d7abd0acb Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 29 Jan 2022 23:16:40 -0800 Subject: [PATCH 1/3] lib/preview: refactor into a function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows future use like `bash-it preview`. Alsö, allows to use `$BASH_PREVIEW` to specify a particular theme to preview instead of just doing all of them. --- clean_files.txt | 1 + lib/preview.bash | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 8f9c173a..49e51abc 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -85,6 +85,7 @@ lib/colors.bash lib/helpers.bash lib/log.bash lib/preexec.bash +lib/preview.bash lib/search.bash lib/utilities.bash diff --git a/lib/preview.bash b/lib/preview.bash index 418839cd..60659df5 100644 --- a/lib/preview.bash +++ b/lib/preview.bash @@ -1,19 +1,30 @@ -if [[ "${BASH_PREVIEW:-}" ]]; -then - unset BASH_PREVIEW #Prevent infinite looping - echo " +# shellcheck shell=bash +# +# Displays the prompt from each _Bash It_ theme. - Previewing Bash-it Themes +function _bash-it-preview() { + local BASH_IT_THEME BASH_IT_LOG_LEVEL + local themes theme - " + printf '\n\n\t%s\n\n' "Previewing Bash-it Themes" - THEMES="$BASH_IT/themes/*/*.theme.bash" - for theme in $THEMES - do - BASH_IT_THEME=${theme%.theme.bash} - BASH_IT_THEME=${BASH_IT_THEME##*/} - echo " - $BASH_IT_THEME" - echo "" | bash --init-file "${BASH_IT}/bash_it.sh" -i - done + if [[ -n "${1:-}" && -s "${BASH_IT?}/themes/${1}/${1}.theme.bash" ]]; then + themes=("${1}") + else + themes=("${BASH_IT?}/themes"/*/*.theme.bash) + fi + + # shellcheck disable=SC2034 + for theme in "${themes[@]}"; do + BASH_IT_THEME="${theme%.theme.bash}" + BASH_IT_THEME="${BASH_IT_THEME##*/}" + BASH_IT_LOG_LEVEL=0 + + bash --init-file "${BASH_IT_BASHRC:-${BASH_IT?}/bash_it.sh}" -i <<< '_bash-it-flash-term "${#BASH_IT_THEME}" "${BASH_IT_THEME}"' + done +} + +if [[ -n "${BASH_PREVIEW:-}" ]]; then + _bash-it-preview "${BASH_PREVIEW}" "$@" + unset BASH_PREVIEW #Prevent infinite looping fi From a9a40a3cad024add8fbbace487e4005389674dff Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 25 Jan 2022 12:57:23 -0800 Subject: [PATCH 2/3] lib/helpers: add `preview` to `bash-it` spaghetti --- completion/available/bash-it.completion.bash | 4 ++-- docs/themes.rst | 2 +- lib/helpers.bash | 8 +++++++- test/completion/bash-it.completion.bats | 12 ++++++------ 4 files changed, 16 insertions(+), 10 deletions(-) mode change 100644 => 100755 completion/available/bash-it.completion.bash diff --git a/completion/available/bash-it.completion.bash b/completion/available/bash-it.completion.bash old mode 100644 new mode 100755 index 1f83d5c8..aa00a06e --- a/completion/available/bash-it.completion.bash +++ b/completion/available/bash-it.completion.bash @@ -13,7 +13,7 @@ function _bash-it() { prev="${COMP_WORDS[COMP_CWORD - 1]}" verb="${COMP_WORDS[1]}" file_type="${COMP_WORDS[2]:-}" - candidates=('disable' 'enable' 'help' 'migrate' 'reload' 'restart' 'profile' 'doctor' 'search' 'show' 'update' 'version') + candidates=('disable' 'enable' 'help' 'migrate' 'reload' 'restart' 'preview' 'profile' 'doctor' 'search' 'show' 'update' 'version') case "${verb}" in show) candidates=('aliases' 'completions' 'plugins') @@ -58,7 +58,7 @@ function _bash-it() { fi _compreply_candidates ;; - migrate | reload | restart | search | version) ;; + migrate | reload | restart | preview | search | version) ;; enable | disable) if [[ "${verb}" == "enable" ]]; then suffix="disabled" diff --git a/docs/themes.rst b/docs/themes.rst index 5b796389..8cfbeb23 100644 --- a/docs/themes.rst +++ b/docs/themes.rst @@ -22,7 +22,7 @@ Examples: # Disable theming export BASH_IT_THEME="" -You can easily preview the themes in your own shell using ``BASH_PREVIEW=true bash-it reload``. +You can easily preview the themes in your own shell using ``bash-it preview``. If you've created your own custom prompts, we'd love it if you shared them with everyone else! Just submit a Pull Request. You can see theme screenshots on `wiki/Themes `_. diff --git a/lib/helpers.bash b/lib/helpers.bash index 66cdec74..4728541a 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -98,7 +98,7 @@ alias reload_plugins="$(_make_reload_alias plugin plugins)" function bash-it() { about 'Bash-it help and maintenance' - param '1: verb [one of: help | show | enable | disable | migrate | update | search | version | reload | restart | doctor ] ' + param '1: verb [one of: help | show | enable | disable | migrate | update | search | preview | version | reload | restart | doctor ] ' param '2: component type [one of: alias(es) | completion(s) | plugin(s) ] or search term(s)' param '3: specific component [optional]' example '$ bash-it show plugins' @@ -108,6 +108,8 @@ function bash-it() { example '$ bash-it migrate' example '$ bash-it update' example '$ bash-it search [-|@]term1 [-|@]term2 ... [ -e/--enable ] [ -d/--disable ] [ -r/--refresh ] [ -c/--no-color ]' + example '$ bash-it preview' + example '$ bash-it preview essential' example '$ bash-it version' example '$ bash-it reload' example '$ bash-it restart' @@ -142,6 +144,10 @@ function bash-it() { _bash-it-search "$component" "$@" return ;; + preview) + _bash-it-preview "$component" "$@" + return + ;; update) func="_bash-it-update-$component" ;; diff --git a/test/completion/bash-it.completion.bats b/test/completion/bash-it.completion.bats index f2336185..087a926d 100755 --- a/test/completion/bash-it.completion.bats +++ b/test/completion/bash-it.completion.bats @@ -81,32 +81,32 @@ function __check_completion () { @test "completion bash-it: show options" { run __check_completion 'bash-it ' - assert_line -n 0 "disable enable help migrate reload restart profile doctor search show update version" + assert_line -n 0 "disable enable help migrate reload restart preview profile doctor search show update version" } @test "completion bash-it: bash-ti - show options" { run __check_completion 'bash-ti ' - assert_line -n 0 "disable enable help migrate reload restart profile doctor search show update version" + assert_line -n 0 "disable enable help migrate reload restart preview profile doctor search show update version" } @test "completion bash-it: shit - show options" { run __check_completion 'shit ' - assert_line -n 0 "disable enable help migrate reload restart profile doctor search show update version" + assert_line -n 0 "disable enable help migrate reload restart preview profile doctor search show update version" } @test "completion bash-it: bashit - show options" { run __check_completion 'bashit ' - assert_line -n 0 "disable enable help migrate reload restart profile doctor search show update version" + assert_line -n 0 "disable enable help migrate reload restart preview profile doctor search show update version" } @test "completion bash-it: batshit - show options" { run __check_completion 'batshit ' - assert_line -n 0 "disable enable help migrate reload restart profile doctor search show update version" + assert_line -n 0 "disable enable help migrate reload restart preview profile doctor search show update version" } @test "completion bash-it: bash_it - show options" { run __check_completion 'bash_it ' - assert_line -n 0 "disable enable help migrate reload restart profile doctor search show update version" + assert_line -n 0 "disable enable help migrate reload restart preview profile doctor search show update version" } @test "completion bash-it: profile - show options" { From 00e3955dd38bd7db49c20fc6cb8c48faabc016c5 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 25 Jan 2022 12:58:22 -0800 Subject: [PATCH 3/3] lib/preview: add full completion --- completion/available/bash-it.completion.bash | 6 ++++- lib/preview.bash | 24 ++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) mode change 100755 => 100644 completion/available/bash-it.completion.bash diff --git a/completion/available/bash-it.completion.bash b/completion/available/bash-it.completion.bash old mode 100755 new mode 100644 index aa00a06e..2259e37b --- a/completion/available/bash-it.completion.bash +++ b/completion/available/bash-it.completion.bash @@ -58,7 +58,11 @@ function _bash-it() { fi _compreply_candidates ;; - migrate | reload | restart | preview | search | version) ;; + migrate | reload | restart | search | version) ;; + preview) + _bash-it-preview # completes itself + return 0 + ;; enable | disable) if [[ "${verb}" == "enable" ]]; then suffix="disabled" diff --git a/lib/preview.bash b/lib/preview.bash index 60659df5..96fafae7 100644 --- a/lib/preview.bash +++ b/lib/preview.bash @@ -4,22 +4,26 @@ function _bash-it-preview() { local BASH_IT_THEME BASH_IT_LOG_LEVEL - local themes theme + local themes IFS=$'\n' cur - printf '\n\n\t%s\n\n' "Previewing Bash-it Themes" - - if [[ -n "${1:-}" && -s "${BASH_IT?}/themes/${1}/${1}.theme.bash" ]]; then - themes=("${1}") + if [[ $# -gt '0' ]]; then + themes=("$@") else themes=("${BASH_IT?}/themes"/*/*.theme.bash) + themes=("${themes[@]##*/}") + themes=("${themes[@]%.theme.bash}") fi - # shellcheck disable=SC2034 - for theme in "${themes[@]}"; do - BASH_IT_THEME="${theme%.theme.bash}" - BASH_IT_THEME="${BASH_IT_THEME##*/}" - BASH_IT_LOG_LEVEL=0 + if [[ ${COMP_CWORD:-} -gt '0' ]]; then + cur="${COMP_WORDS[COMP_CWORD]}" + read -d '' -ra COMPREPLY < <(compgen -W "all${IFS}${themes[*]}" -- "${cur}") + return + fi + printf '\n\n\t%s\n\n' "Previewing Bash-it Themes" + # shellcheck disable=SC2034 + for BASH_IT_THEME in "${themes[@]}"; do + BASH_IT_LOG_LEVEL=0 bash --init-file "${BASH_IT_BASHRC:-${BASH_IT?}/bash_it.sh}" -i <<< '_bash-it-flash-term "${#BASH_IT_THEME}" "${BASH_IT_THEME}"' done }