From baae0305b6ded4b7e709ace3755ee0c424239f3b Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Sat, 17 Nov 2018 17:45:44 -0800 Subject: [PATCH 01/16] Speed up bash-it Search & support exact matches This commit improves Bash-It search functionality in a couple of ways: * bash-it search (with no arguments) will print detailed help. * bash-it search now accepts terms prefixed with '@' sign, indicating an exact match. * bash-it search now performs smarter caching of the component listings/status New search syntax is as follows: bash-it search [-|@]term1 [-|@]term2 [ --enable | --disable | --help ] --- lib/helpers.bash | 8 +- lib/search.bash | 453 ++++++++++++++++++++++++++++++++---------- test/lib/helpers.bats | 1 + test/lib/search.bats | 92 ++++++--- 4 files changed, 419 insertions(+), 135 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index 565173ed..cbcdd42d 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -70,7 +70,7 @@ bash-it () example '$ bash-it disable alias hg [tmux]...' example '$ bash-it migrate' example '$ bash-it update' - example '$ bash-it search ruby [[-]rake]... [--enable | --disable]' + example '$ bash-it search [-|@]term1 [-|@]term2 ... [--enable | --disable | --help | --refresh | --no-color ]' example '$ bash-it version' example '$ bash-it reload' typeset verb=${1:-} @@ -393,6 +393,9 @@ _disable-thing () fi fi + local flag="DEFER_CACHE_CLEANUP_FOR_${file_type}" + [[ -z ${!flag} ]] && _bash_it_search_cache_clean "${file_type}" + if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then exec ${0/-/} fi @@ -488,6 +491,9 @@ _enable-thing () ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" fi + local flag="DEFER_CACHE_CLEANUP_FOR_${file_type}" + [[ -z ${!flag} ]] && _bash_it_search_cache_clean "${file_type}" + if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then exec ${0/-/} fi diff --git a/lib/search.bash b/lib/search.bash index f643ad12..a54971f5 100644 --- a/lib/search.bash +++ b/lib/search.bash @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # # Search by Konstantin Gredeskoul «github.com/kigster» #——————————————————————————————————————————————————————————————————————————————— @@ -5,46 +6,172 @@ # whose name or description matches one of the search terms provided as arguments. # # Usage: -# ❯ bash-it search term1 [[-]term2] ... [[-]termN] [ --enable | --disable ] +# ❯ bash-it search [-|@]term1 [-|@]term2 ... \ +# [ --enable | -e ] \ +# [ --disable | -d ] \ +# [ --refresh | -r ] +# [ --help | -h ] # -# Exmplanation: # Single dash, as in "-chruby", indicates a negative search term. # Double dash indicates a command that is to be applied to the search result. -# At the moment only --enable and --disable are supported. +# At the moment only --help, --enable and --disable are supported. +# An '@' sign indicates an exact (not partial) match. # # Examples: # ❯ bash-it search ruby rbenv rvm gem rake -# aliases : bundler -# plugins : chruby chruby-auto rbenv ruby rvm -# completions : gem rake - +# aliases: bundler +# plugins: chruby chruby-auto ruby rbenv rvm ruby +# completions: rvm gem rake +# # ❯ bash-it search ruby rbenv rvm gem rake -chruby -# aliases : bundler -# plugins : rbenv ruby rvm -# completions : gem rake +# aliases: bundler +# plugins: ruby rbenv rvm ruby +# completions: rvm gem rake # # Examples of enabling or disabling results of the search: # # ❯ bash-it search ruby -# aliases => bundler -# plugins => chruby chruby-auto ruby +# aliases: bundler +# plugins: chruby chruby-auto ruby # # ❯ bash-it search ruby -chruby --enable -# aliases => ✓bundler -# plugins => ✓ruby +# aliases: bundler +# plugins: ruby # +# Examples of using exact match: + +# ❯ bash-it search @git @ruby +# aliases: git +# plugins: git ruby +# completions: git # + _bash-it-search() { _about 'searches for given terms amongst bash-it plugins, aliases and completions' _param '1: term1' _param '2: [ term2 ]...' - _example '$ _bash-it-search ruby rvm rake bundler' + _example '$ _bash-it-search @git ruby -rvm rake bundler' - declare -a _components=(aliases plugins completions) + local component + export BASH_IT_SEARCH_USE_COLOR=true + export BASH_IT_GREP=${BASH_IT_GREP:-$(which egrep)} + declare -a BASH_IT_COMPONENTS=(aliases plugins completions) - for _component in "${_components[@]}" ; do - _bash-it-search-component "${_component}" "$@" + if [[ -z "$*" ]] ; then + _bash-it-search-help + return 0 + fi + + local -a args=() + for word in $@; do + if [[ ${word} == "--help" || ${word} == "-h" ]]; then + _bash-it-search-help + return 0 + elif [[ ${word} == "--refresh" || ${word} == "-r" ]]; then + _bash_it_search_cache_clean + elif [[ ${word} == "--no-color" ]]; then + export BASH_IT_SEARCH_USE_COLOR=false + else + args=(${args[@]} ${word}) + fi done + + for component in "${BASH_IT_COMPONENTS[@]}" ; do + _bash-it-search-component "${component}" "${args[@]}" + done + + return 0 +} + +_bash-it-search-help() { + printf "${echo_normal} +${echo_underline_yellow}USAGE${echo_normal} + + bash-it search [-|@]term1 [-|@]term2 ... \\ + [ --enable | --disable | --help | --refresh | --no-color ] + +${echo_underline_yellow}DESCRIPTION${echo_normal} + + Use ${echo_bold_green}search${echo_normal} bash-it command to search for a list of terms or term negations + across all components: aliases, completions and plugins. Components that are + enabled are shown in green (or with a check box if --no-color option is used). + + In addition to simply finding the right component, you can use the results + of the search to enable or disable all components that the search returns. + + When search is used to enable/disable components it becomes clear that + you must be able to perform not just a partial match, but an exact match, + as well as be able to exclude some components. + + * To exclude a component (or all components matching a substring) use + a search term with minus as a prefix, eg '-flow' + + * To perform an exact match, use character '@' in front of the term, + eg. '@git' would only match aliases, plugins and completions named 'git'. + +${echo_underline_yellow}FLAGS${echo_normal} + --enable ${echo_purple}Enable all matching componenents.${echo_normal} + --disable ${echo_purple}Disable all matching componenents.${echo_normal} + --help ${echo_purple}Print this help.${echo_normal} + --refresh ${echo_purple}Force a refresh of the search cache.${echo_normal} + --no-color ${echo_purple}Disable color output and use monochrome text.${echo_normal} + +${echo_underline_yellow}EXAMPLES${echo_normal} + + For example, ${echo_bold_green}bash-it search git${echo_normal} would match any alias, completion + or plugin that has the word 'git' in either the module name or + it's description. You should see something like this when you run this + command: + + ${echo_bold_green}❯ bash-it search git${echo_bold_blue} + ${echo_bold_yellow}aliases: ${echo_bold_green}git ${echo_normal}gitsvn + ${echo_bold_yellow}plugins: ${echo_normal}autojump fasd ${echo_bold_green}git ${echo_normal}git-subrepo jgitflow jump + ${echo_bold_yellow}completions: ${echo_bold_green}git ${echo_normal}git_flow git_flow_avh${echo_normal} + + You can exclude some terms by prefixing a term with a minus, eg: + + ${echo_bold_green}❯ bash-it search git -flow -svn${echo_bold_blue} + ${echo_bold_yellow}aliases: ${echo_normal}git + ${echo_bold_yellow}plugins: ${echo_normal}autojump fasd git git-subrepo jump + ${echo_bold_yellow}completions: ${echo_normal}git${echo_normal} + + Finally, if you prefix a term with '@' symbol, that indicates an exact + match. Note, that we also pass the '--enable' flag, which would ensure + that all matches are automatically enabled. The example is below: + + ${echo_bold_green}❯ bash-it search @git --enable${echo_bold_blue} + ${echo_bold_yellow}aliases: ${echo_normal}git + ${echo_bold_yellow}plugins: ${echo_normal}git + ${echo_bold_yellow}completions: ${echo_normal}git${echo_normal} + +${echo_underline_yellow}SUMMARY${echo_normal} + + Take advantage of the search functionality to discover what Bash-It can do + for you. Try searching for partial term matches, mix and match with the + negative terms, or specify an exact matches of any number of terms. Once + you created the search command that returns ONLY the modules you need, + simply append '--enable' or '--disable' at the end to activate/deactivate + each module. + +" +} + +_bash-it-cache-file() { + local component="${1}" + local file="/tmp/bash_it/${component}.status" + mkdir -p $(dirname ${file}) + printf ${file} +} + +_bash_it_search_cache_clean() { + local component="$1" + if [[ -z ${component} ]] ; then + for component in "${BASH_IT_COMPONENTS[@]}" ; do + _bash_it_search_cache_clean "${component}" + done + else + rm -f $(_bash-it-cache-file ${component}) + fi } #——————————————————————————————————————————————————————————————————————————————— @@ -58,137 +185,261 @@ _bash-it-array-contains-element () { echo -n $r } +_bash-it-array-dedup() { + echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' ' +} + +_bash-it-grep() { + if [[ -z "${BASH_IT_GREP}" ]] ; then + export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" + fi + printf "%s " "${BASH_IT_GREP}" +} + +_bash-it-is-partial-match() { + local component="$1" + local term="$2" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -i -q -- "${term}" +} + +_bash-it-component-term-matches-negation() { + local match="$1"; shift + local negative + for negative in "$@"; do + [[ "${match}" =~ "${negative}" ]] && return 0 + done + + return 1 +} + +_bash-it-component-help() { + local component="$1" + local file=$(_bash-it-cache-file ${component}) + if [[ ! -s "${file}" || -z $(find "${file}" -mmin -2) ]] ; then + rm -f "${file}" 2>/dev/null + local func="_bash-it-${component}" + ${func} | $(_bash-it-grep) -E ' \[' | cat > ${file} + fi + cat "${file}" +} + +_bash-it-component-list() { + local component="$1" + _bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' ' +} + +_bash-it-component-list-matching() { + local component="$1"; shift + local term="$1" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq +} + +_bash-it-component-list-enabled() { + local component="$1" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' ' +} + +_bash-it-component-list-disabled() { + local component="$1" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' ' +} + +_bash-it-component-item-is-enabled() { + local component="$1" + local item="$2" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s" +} + +_bash-it-component-item-is-disabled() { + local component="$1" + local item="$2" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s" +} + + _bash-it-search-component() { - _about 'searches for given terms amongst a given component' - _param '1: component type, one of: [ aliases | plugins | completions ]' - _param '2: term1 ' - _param '3: [-]term2 [-]term3 ...' - _example '$ _bash-it-search-component aliases rake bundler -chruby' - - _component=$1 - - local func=_bash-it-${_component} - local help=$($func) + local component="$1" shift + _about 'searches for given terms amongst a given component' + _param '1: component type, one of: [ aliases | plugins | completions ]' + _param '2: term1 term2 @term3' + _param '3: [-]term4 [-]term5 ...' + _example '$ _bash-it-search-component aliases @git rake bundler -chruby' + # if one of the search terms is --enable or --disable, we will apply - # this action to the matches further down. - local action action_func component_singular - declare -a _search_commands=(enable disable) - for _search_command in "${_search_commands[@]}"; do - if [[ $(_bash-it-array-contains-element "--${_search_command}" "$@") == "true" ]]; then - action=$_search_command - component_singular=${_component} + # this action to the matches further ` down. + local component_singular action action_func + local -a search_commands=(enable disable) + for search_command in "${search_commands[@]}"; do + if [[ $(_bash-it-array-contains-element "--${search_command}" "$@") == "true" ]]; then + component_singular=${component} component_singular=${component_singular/es/} # aliases -> alias component_singular=${component_singular/ns/n} # plugins -> plugin + + action="${search_command}" action_func="_${action}-${component_singular}" break fi done - local _grep=$((which --skip-alias grep 2> /dev/null || which grep) | tail -n 1) + local -a terms=($@) # passed on the command line - declare -a terms=($@) # passed on the command line - declare -a matches=() # results that we found - declare -a negative_terms=() # terms that began with a dash + unset exact_terms + unset partial_terms + unset negative_terms + + local -a exact_terms=() # terms that should be included only if they match exactly + local -a partial_terms=() # terms that should be included if they match partially + local -a negative_terms=() # negated partial terms that should be excluded + + unset component_list + local -a component_list=( $(_bash-it-component-list "${component}") ) + local term for term in "${terms[@]}"; do - # -- can only be used for the actions: enable/disable - [[ "${term:0:2}" == "--" ]] && continue - [[ "${term:0:1}" == "-" ]] && negative_terms=(${negative_terms[@]} ${term:1}) && continue - - # print asterisk next to each result that is already enabled by the user - local term_match=($(echo "${help}"| ${_grep} -i -- ${term} | ${_grep} -E '\[( |x)\]' | cut -b -30 | sed 's/ *\[ \]//g;s/ *\[x\]/*/g;' )) - [[ "${#term_match[@]}" -gt 0 ]] && { - matches=(${matches[@]} ${term_match[@]}) # append to the list of results - } + local search_term="${term:1}" + if [[ "${term:0:2}" == "--" ]] ; then + continue + elif [[ "${term:0:1}" == "-" ]] ; then + negative_terms=(${negative_terms[@]} "${search_term}") + elif [[ "${term:0:1}" == "@" ]] ; then + if [[ $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}") == "true" ]]; then + exact_terms=(${exact_terms[@]} "${search_term}") + fi + else + partial_terms=(${partial_terms[@]} $(_bash-it-component-list-matching "${component}" "${term}") ) + fi done - # now check if we found any negative terms, and subtract them - [[ ${#negative_terms} -gt 0 ]] && { - declare -a filtered_matches=() - for match in "${matches[@]}"; do - local negations=0 - for nt in "${negative_terms[@]}"; do - [[ "${match}" =~ "${nt}" ]] && negations=$(($negations+1)) - done - [[ $negations -eq 0 ]] && filtered_matches=(${filtered_matches[@]} ${match}) - done - matches=(${filtered_matches[@]}) - } + local -a total_matches=( $(_bash-it-array-dedup ${exact_terms[@]} ${partial_terms[@]}) ) - _bash-it-search-result $action $action_func - - unset matches filtered_matches terms + unset matches + declare -a matches=() + for match in ${total_matches[@]}; do + local include_match=true + if [[ ${#negative_terms[@]} -gt 0 ]]; then + ( _bash-it-component-term-matches-negation "${match}" "${negative_terms[@]}" ) && include_match=false + fi + ( ${include_match} ) && matches=(${matches[@]} "${match}") + done + _bash-it-search-result "${component}" "${action}" "${action_func}" "${matches[@]}" + unset matches final_matches terms } _bash-it-search-result() { - local action=$1; shift - local action_func=$1; shift + local component="$1"; shift + local action="$1"; shift + local action_func="$1"; shift + local -a matches=($@) + local color_component color_enable color_disable color_off - [[ -z "$NO_COLOR" ]] && { + color_sep=':' + + ( ${BASH_IT_SEARCH_USE_COLOR} ) && { color_component='\e[1;34m' color_enable='\e[1;32m' + suffix_enable='' + suffix_disable='' color_disable='\e[0;0m' color_off='\e[0;0m' - color_sep=':' } - [[ -n "$NO_COLOR" ]] && { + ( ${BASH_IT_SEARCH_USE_COLOR} ) || { color_component='' - color_sep=' => ' - color_enable='✓' + suffix_enable=' ✓ ︎' + suffix_disable=' ' + color_enable='' color_disable='' color_off='' } - if [[ "${#matches[*]}" -gt 0 ]] ; then - printf "${color_component}%13s${color_sep} ${color_off}" "${_component}" + local match + local modified=0 - sorted_matches=($(echo "${matches[*]}" | tr ' ' '\n' | sort | uniq)) + local flag="DEFER_CACHE_CLEANUP_FOR_${component}" + eval "export ${flag}=true" - for match in "${sorted_matches[@]}"; do - local match_color compatible_action - if [[ $match =~ "*" ]]; then - match_color=$color_enable + if [[ "${#matches[@]}" -gt 0 ]] ; then + printf "${color_component}%13s${color_sep} ${color_off}" "${component}" + + for match in "${matches[@]}"; do + local enabled=0 + ( _bash-it-component-item-is-enabled "${component}" "${match}" ) && enabled=1 + + local match_color compatible_action suffix opposite_suffix + + (( ${enabled} )) && { + match_color=${color_enable} + suffix=${suffix_enable} + opposite_suffix=${suffix_disable} compatible_action="disable" - else - match_color=$color_disable - compatible_action="enable" - fi + } - match_value=${match/\*/} # remove asterisk - len=${#match_value} - if [[ -n $NO_COLOR ]]; then - local m="${match_color}${match_value}" + (( ${enabled} )) || { + match_color=${color_disable} + suffix=${suffix_disable} + opposite_suffix=${suffix_enable} + compatible_action="enable" + } + + local len + if ( ${BASH_IT_SEARCH_USE_COLOR} ); then + local m="${match_color}${match}${suffix}" + len=${#m} + else + local m="${match}${suffix}" len=${#m} fi - printf " ${match_color}${match_value}" # print current state - + printf " ${match_color}${match}${suffix}" # print current state if [[ "${action}" == "${compatible_action}" ]]; then - # oh, i see – we need to either disable enabled, or enable disabled - # component. Let's start with the most important part: redrawing - # the search result backwards. Because style. - - printf "\033[${len}D" - for a in {0..30}; do - [[ $a -gt $len ]] && break - printf "%.*s" $a " " - sleep 0.07 # who knew you could sleep for fraction of the cost :) - done - printf "\033[${len}D" - result=$(${action_func} ${match_value}) + if [[ ${action} == "enable" && ${BASH_IT_SEARCH_USE_COLOR} == false ]]; then + _bash-it-flash-term ${len} "${match}${suffix}" + else + _bash-it-erase-term ${len} + fi + modified=1 + result=$(${action_func} ${match}) local temp="color_${compatible_action}" match_color=${!temp} - printf "${match_color}${match_value}" + _bash-it-rewind ${len} + printf "${match_color}${match}${opposite_suffix}" fi printf "${color_off}" done + [[ ${modified} -gt 0 ]] && _bash_it_search_cache_clean ${component} printf "\n" fi - +} + +_bash-it-rewind() { + local len="$1" + printf "\033[${len}D" +} + +_bash-it-flash-term() { + local len="$1" + local match="$2" + local delay=0.1 + local color + + for color in ${text_black} ${echo_bold_blue} ${bold_yellow} ${bold_red} ${echo_bold_green} ; do + sleep ${delay} + _bash-it-rewind "${len}" + printf "${color}${match}" + done +} + +_bash-it-erase-term() { + local len="$1" + _bash-it-rewind ${len} + for a in {0..30}; do + [[ ${a} -gt ${len} ]] && break + printf "%.*s" $a " " + sleep 0.05 + done } diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index 7720b930..563bc380 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -2,6 +2,7 @@ load ../test_helper load ../../lib/composure +load ../../lib/search load ../../plugins/available/base.plugin cite _about _param _example _group _author _version diff --git a/test/lib/search.bats b/test/lib/search.bats index 1043c1a4..29e861d7 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -1,16 +1,20 @@ #!/usr/bin/env bats load ../test_helper - load ../../lib/composure +load ../../lib/helpers +load ../../lib/search load ../../plugins/available/base.plugin +load ../../aliases/available/git.aliases +load ../../plugins/available/ruby.plugin +load ../../plugins/available/rails.plugin +load ../../completion/available/bundler.completion +load ../../completion/available/gem.completion +load ../../completion/available/rake.completion cite _about _param _example _group _author _version load ../../lib/helpers -load ../../lib/search - -NO_COLOR=true function local_setup { mkdir -p "$BASH_IT" @@ -23,46 +27,68 @@ function local_setup { rm -rf "$BASH_IT"/aliases/enabled rm -rf "$BASH_IT"/completion/enabled rm -rf "$BASH_IT"/plugins/enabled + + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled + + export OLD_PATH="$PATH" + export PATH="/usr/bin:/bin:/usr/sbin" +} + +function local_teardown { + export PATH="$OLD_PATH" + unset OLD_PATH } @test "search: plugin base" { + export BASH_IT_SEARCH_USE_COLOR=false run _bash-it-search-component 'plugins' 'base' - [[ "${lines[0]}" =~ 'plugins' && "${lines[0]}" =~ 'base' ]] + assert_line -n 0 ' plugins: base ' +} + +@test "search: git" { + run _bash-it-search 'git' --no-color + assert_line -n 0 ' aliases: git gitsvn ' + assert_line -n 1 ' plugins: autojump fasd git git-subrepo jgitflow jump ' + assert_line -n 2 ' completions: git git_flow git_flow_avh ' } @test "search: ruby gem bundle rake rails" { - # first disable them all, so that the output does not appear with a checkbox - # and we can compare the result - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' - # Now perform the search - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' - # And verify - assert [ "${lines[0]/✓/}" == ' aliases => bundler rails' ] - assert [ "${lines[1]/✓/}" == ' plugins => chruby chruby-auto rails ruby' ] - assert [ "${lines[2]/✓/}" == ' completions => bundler gem rake' ] + run _bash-it-search rails ruby gem bundler rake --no-color + + assert_line -n 0 ' aliases: bundler rails ' + assert_line -n 1 ' plugins: chruby chruby-auto rails ruby ' + assert_line -n 2 ' completions: bundler gem rake ' } -@test "search: ruby gem bundle -chruby rake rails" { - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' - run _bash-it-search 'ruby' 'gem' 'bundle' '-chruby' 'rake' 'rails' - assert [ "${lines[0]/✓/}" == ' aliases => bundler rails' ] - assert [ "${lines[1]/✓/}" == ' plugins => rails ruby' ] - assert [ "${lines[2]/✓/}" == ' completions => bundler gem rake' ] +@test "search: rails ruby gem bundler rake -chruby" { + run _bash-it-search rails ruby gem bundler rake -chruby --no-color + + assert_line -n 0 ' aliases: bundler rails ' + assert_line -n 1 ' plugins: rails ruby ' + assert_line -n 2 ' completions: bundler gem rake ' } -@test "search: (rails enabled) ruby gem bundle rake rails" { - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' '--disable' - run _enable-alias 'rails' - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' 'rails' - assert_line -n 0 ' aliases => bundler ✓rails' - assert_line -n 1 ' plugins => chruby chruby-auto rails ruby' - assert_line -n 2 ' completions => bundler gem rake' +@test "search: @git" { + run _bash-it-search '@git' --no-color + assert_line -n 0 ' aliases: git ' + assert_line -n 1 ' plugins: git ' + assert_line -n 2 ' completions: git ' } -@test "search: (all enabled) ruby gem bundle rake rails" { - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' '--enable' - run _bash-it-search 'ruby' 'gem' 'bundle' 'rake' '-chruby' 'rails' - assert_line -n 0 ' aliases => ✓bundler ✓rails' - assert_line -n 1 ' plugins => ✓rails ✓ruby' - assert_line -n 2 ' completions => ✓bundler ✓gem ✓rake' +@test "search: @git --enable / --disable" { + set -e + run _bash-it-search '@git' --enable --no-color + run _bash-it-search '@git' --no-color + + [[ "${lines[0]}" =~ '✓' ]] + + run _bash-it-search '@git' --disable --no-color + run _bash-it-search '@git' --no-color + + assert_line -n 0 ' aliases: git ' + assert_line -n 0 ' aliases: git ' + assert_line -n 2 ' completions: git ' } From 09c2be9d6bec0d0d6b917203e3c222970ea8ebe2 Mon Sep 17 00:00:00 2001 From: Konstantin Gredeskoul Date: Fri, 23 Nov 2018 16:14:12 -0800 Subject: [PATCH 02/16] =?UTF-8?q?Refactor=20(extract=20utilities=20+=20add?= =?UTF-8?q?=20tests)=20&=20more=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Extracting common utilities into utilities.bash * Adding new tests for utilities * Relocating the cache file to be under $BASH_IT * Removing cache cleanup deferral code for now * Wiping the cache in local_setup in tests. --- .gitignore | 1 + lib/helpers.bash | 9 ++- lib/search.bash | 142 ++++++++------------------------------- lib/utilities.bash | 144 ++++++++++++++++++++++++++++++++++++++++ test/lib/helpers.bats | 1 + test/lib/search.bats | 2 + test/lib/utilities.bats | 108 ++++++++++++++++++++++++++++++ 7 files changed, 289 insertions(+), 118 deletions(-) mode change 100644 => 100755 lib/search.bash create mode 100644 lib/utilities.bash create mode 100644 test/lib/utilities.bats diff --git a/.gitignore b/.gitignore index 2e6fb7c0..6fad6279 100755 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ bats *.sublime-workspace *.sublime-project enabled/* +tmp/ diff --git a/lib/helpers.bash b/lib/helpers.bash index cbcdd42d..bec603f1 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -70,7 +70,7 @@ bash-it () example '$ bash-it disable alias hg [tmux]...' example '$ bash-it migrate' example '$ bash-it update' - example '$ bash-it search [-|@]term1 [-|@]term2 ... [--enable | --disable | --help | --refresh | --no-color ]' + example '$ bash-it search [-|@]term1 [-|@]term2 ... [ -e/--enable ] [ -d/--disable ] [ -r/--refresh ] [ -c/--no-color ]' example '$ bash-it version' example '$ bash-it reload' typeset verb=${1:-} @@ -78,6 +78,7 @@ bash-it () typeset component=${1:-} shift typeset func + case $verb in show) func=_bash-it-$component;; @@ -393,8 +394,7 @@ _disable-thing () fi fi - local flag="DEFER_CACHE_CLEANUP_FOR_${file_type}" - [[ -z ${!flag} ]] && _bash_it_search_cache_clean "${file_type}" + _bash-it-clean-component-cache "${file_type}" if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then exec ${0/-/} @@ -491,8 +491,7 @@ _enable-thing () ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" fi - local flag="DEFER_CACHE_CLEANUP_FOR_${file_type}" - [[ -z ${!flag} ]] && _bash_it_search_cache_clean "${file_type}" + _bash-it-clean-component-cache "${file_type}" if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then exec ${0/-/} diff --git a/lib/search.bash b/lib/search.bash old mode 100644 new mode 100755 index a54971f5..1c487a3e --- a/lib/search.bash +++ b/lib/search.bash @@ -7,10 +7,11 @@ # # Usage: # ❯ bash-it search [-|@]term1 [-|@]term2 ... \ -# [ --enable | -e ] \ -# [ --disable | -d ] \ -# [ --refresh | -r ] -# [ --help | -h ] +# [ --enable | -e ] \ +# [ --disable | -d ] \ +# [ --no-color | -c ] \ +# [ --refresh | -r ] \ +# [ --help | -h ] # # Single dash, as in "-chruby", indicates a negative search term. # Double dash indicates a command that is to be applied to the search result. @@ -52,6 +53,8 @@ _bash-it-search() { _param '2: [ term2 ]...' _example '$ _bash-it-search @git ruby -rvm rake bundler' + [[ -z "$(type _bash-it-array-contains-element 2>/dev/null)" ]] && source "${BASH_IT}/lib/utilities.bash" + local component export BASH_IT_SEARCH_USE_COLOR=true export BASH_IT_GREP=${BASH_IT_GREP:-$(which egrep)} @@ -68,17 +71,19 @@ _bash-it-search() { _bash-it-search-help return 0 elif [[ ${word} == "--refresh" || ${word} == "-r" ]]; then - _bash_it_search_cache_clean - elif [[ ${word} == "--no-color" ]]; then + _bash-it-clean-component-cache + elif [[ ${word} == "--no-color" || ${word} == '-c' ]]; then export BASH_IT_SEARCH_USE_COLOR=false else args=(${args[@]} ${word}) fi done - for component in "${BASH_IT_COMPONENTS[@]}" ; do - _bash-it-search-component "${component}" "${args[@]}" - done + if [[ ${#args} -gt 0 ]]; then + for component in "${BASH_IT_COMPONENTS[@]}" ; do + _bash-it-search-component "${component}" "${args[@]}" + done + fi return 0 } @@ -88,7 +93,11 @@ _bash-it-search-help() { ${echo_underline_yellow}USAGE${echo_normal} bash-it search [-|@]term1 [-|@]term2 ... \\ - [ --enable | --disable | --help | --refresh | --no-color ] + [ --enable | -e ] \\ + [ --disable | -d ] \\ + [ --no-color | -c ] \\ + [ --refresh | -r ] \\ + [ --help | -h ] ${echo_underline_yellow}DESCRIPTION${echo_normal} @@ -110,11 +119,11 @@ ${echo_underline_yellow}DESCRIPTION${echo_normal} eg. '@git' would only match aliases, plugins and completions named 'git'. ${echo_underline_yellow}FLAGS${echo_normal} - --enable ${echo_purple}Enable all matching componenents.${echo_normal} - --disable ${echo_purple}Disable all matching componenents.${echo_normal} - --help ${echo_purple}Print this help.${echo_normal} - --refresh ${echo_purple}Force a refresh of the search cache.${echo_normal} - --no-color ${echo_purple}Disable color output and use monochrome text.${echo_normal} + --enable | -e ${echo_purple}Enable all matching componenents.${echo_normal} + --disable | -d ${echo_purple}Disable all matching componenents.${echo_normal} + --help | -h ${echo_purple}Print this help.${echo_normal} + --refresh | -r ${echo_purple}Force a refresh of the search cache.${echo_normal} + --no-color | -c ${echo_purple}Disable color output and use monochrome text.${echo_normal} ${echo_underline_yellow}EXAMPLES${echo_normal} @@ -156,46 +165,6 @@ ${echo_underline_yellow}SUMMARY${echo_normal} " } -_bash-it-cache-file() { - local component="${1}" - local file="/tmp/bash_it/${component}.status" - mkdir -p $(dirname ${file}) - printf ${file} -} - -_bash_it_search_cache_clean() { - local component="$1" - if [[ -z ${component} ]] ; then - for component in "${BASH_IT_COMPONENTS[@]}" ; do - _bash_it_search_cache_clean "${component}" - done - else - rm -f $(_bash-it-cache-file ${component}) - fi -} - -#——————————————————————————————————————————————————————————————————————————————— -# array=("something to search for" "a string" "test2000") -# _bash-it-array-contains-element "a string" "${array[@]}" -# ( prints "true" or "false" ) -_bash-it-array-contains-element () { - local e - local r=false - for e in "${@:2}"; do [[ "$e" == "$1" ]] && r=true; done - echo -n $r -} - -_bash-it-array-dedup() { - echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' ' -} - -_bash-it-grep() { - if [[ -z "${BASH_IT_GREP}" ]] ; then - export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" - fi - printf "%s " "${BASH_IT_GREP}" -} - _bash-it-is-partial-match() { local component="$1" local term="$2" @@ -212,51 +181,6 @@ _bash-it-component-term-matches-negation() { return 1 } -_bash-it-component-help() { - local component="$1" - local file=$(_bash-it-cache-file ${component}) - if [[ ! -s "${file}" || -z $(find "${file}" -mmin -2) ]] ; then - rm -f "${file}" 2>/dev/null - local func="_bash-it-${component}" - ${func} | $(_bash-it-grep) -E ' \[' | cat > ${file} - fi - cat "${file}" -} - -_bash-it-component-list() { - local component="$1" - _bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' ' -} - -_bash-it-component-list-matching() { - local component="$1"; shift - local term="$1" - _bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq -} - -_bash-it-component-list-enabled() { - local component="$1" - _bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' ' -} - -_bash-it-component-list-disabled() { - local component="$1" - _bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' ' -} - -_bash-it-component-item-is-enabled() { - local component="$1" - local item="$2" - _bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s" -} - -_bash-it-component-item-is-disabled() { - local component="$1" - local item="$2" - _bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s" -} - - _bash-it-search-component() { local component="$1" shift @@ -272,7 +196,7 @@ _bash-it-search-component() { local component_singular action action_func local -a search_commands=(enable disable) for search_command in "${search_commands[@]}"; do - if [[ $(_bash-it-array-contains-element "--${search_command}" "$@") == "true" ]]; then + if $(_bash-it-array-contains-element "--${search_command}" "$@"); then component_singular=${component} component_singular=${component_singular/es/} # aliases -> alias component_singular=${component_singular/ns/n} # plugins -> plugin @@ -304,7 +228,7 @@ _bash-it-search-component() { elif [[ "${term:0:1}" == "-" ]] ; then negative_terms=(${negative_terms[@]} "${search_term}") elif [[ "${term:0:1}" == "@" ]] ; then - if [[ $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}") == "true" ]]; then + if $(_bash-it-array-contains-element "${search_term}" "${component_list[@]}"); then exact_terms=(${exact_terms[@]} "${search_term}") fi else @@ -358,9 +282,6 @@ _bash-it-search-result() { local match local modified=0 - local flag="DEFER_CACHE_CLEANUP_FOR_${component}" - eval "export ${flag}=true" - if [[ "${#matches[@]}" -gt 0 ]] ; then printf "${color_component}%13s${color_sep} ${color_off}" "${component}" @@ -384,14 +305,9 @@ _bash-it-search-result() { compatible_action="enable" } + local m="${match}${suffix}" local len - if ( ${BASH_IT_SEARCH_USE_COLOR} ); then - local m="${match_color}${match}${suffix}" - len=${#m} - else - local m="${match}${suffix}" - len=${#m} - fi + len=${#m} printf " ${match_color}${match}${suffix}" # print current state if [[ "${action}" == "${compatible_action}" ]]; then @@ -411,7 +327,7 @@ _bash-it-search-result() { printf "${color_off}" done - [[ ${modified} -gt 0 ]] && _bash_it_search_cache_clean ${component} + [[ ${modified} -gt 0 ]] && _bash-it-clean-component-cache ${component} printf "\n" fi } diff --git a/lib/utilities.bash b/lib/utilities.bash new file mode 100644 index 00000000..93e96606 --- /dev/null +++ b/lib/utilities.bash @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +# +# A collection of reusable functions. + +########################################################################### +# Component-specific functions (component is either an alias, a plugin, or a +# completion). +########################################################################### + +_bash-it-component-help() { + local component=$(_bash-it-pluralize-component "${1}") + local file=$(_bash-it-component-cache-file ${component}) + if [[ ! -s "${file}" || -z $(find "${file}" -mmin -300) ]] ; then + rm -f "${file}" 2>/dev/null + local func="_bash-it-${component}" + ${func} | $(_bash-it-grep) -E ' \[' | cat > ${file} + fi + cat "${file}" +} + +_bash-it-component-cache-file() { + local component=$(_bash-it-pluralize-component "${1}") + local file="${BASH_IT}/tmp/cache/${component}" + [[ -f ${file} ]] || mkdir -p $(dirname ${file}) + printf "${file}" +} + +_bash-it-pluralize-component() { + local component="${1}" + local len=$(( ${#component} - 1 )) + # pluralize component name for consistency + [[ ${component:${len}:1} != 's' ]] && component="${component}s" + [[ ${component} == "alias" ]] && component="aliases" + printf ${component} +} + +_bash-it-clean-component-cache() { + local component="$1" + local cache + local -a BASH_IT_COMPONENTS=(aliases plugins completions) + if [[ -z ${component} ]] ; then + for component in "${BASH_IT_COMPONENTS[@]}" ; do + _bash-it-clean-component-cache "${component}" + done + else + cache="$(_bash-it-component-cache-file ${component})" + [[ -f "${cache}" ]] && rm -f "${cache}" + fi +} + +########################################################################### +# Generic utilies +########################################################################### + +# This function searches an array for an exact match against the term passed +# as the first argument to the function. This function exits as soon as +# a match is found. +# +# Returns: +# 0 when a match is found, otherwise 1. +# +# Examples: +# $ declare -a fruits=(apple orange pear mandarin) +# +# $ _bash-it-array-contains-element apple "@{fruits[@]}" && echo 'contains apple' +# contains apple +# +# $ if $(_bash-it-array-contains-element pear "${fruits[@]}"); then +# echo "contains pear!" +# fi +# contains pear! +# +# +_bash-it-array-contains-element() { + local e + for e in "${@:2}"; do + [[ "$e" == "$1" ]] && return 0 + done + return 1 +} + +# Dedupe a simple array of words without spaces. +_bash-it-array-dedup() { + echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' ' +} + +# Outputs a full path of the gre found on the filesystem +_bash-it-grep() { + if [[ -z "${BASH_IT_GREP}" ]] ; then + export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')" + fi + printf "%s " "${BASH_IT_GREP}" +} + + +# Returns an array of items within each compoenent. +_bash-it-component-list() { + local component="$1" + _bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' ' +} + +_bash-it-component-list-matching() { + local component="$1"; shift + local term="$1" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq +} + +_bash-it-component-list-enabled() { + local component="$1" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' ' +} + +_bash-it-component-list-disabled() { + local component="$1" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' ' +} + +# Checks if a given item is enabled for a particular component/file-type. +# Uses the component cache if available. +# +# Returns: +# 0 if an item of the component is enabled, 1 otherwise. +# +# Examples: +# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled" +_bash-it-component-item-is-enabled() { + local component="$1" + local item="$2" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s" +} + +# Checks if a given item is disabled for a particular component/file-type. +# Uses the component cache if available. +# +# Returns: +# 0 if an item of the component is enabled, 1 otherwise. +# +# Examples: +# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled" +_bash-it-component-item-is-disabled() { + local component="$1" + local item="$2" + _bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s" +} diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index 563bc380..6c4f706a 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -2,6 +2,7 @@ load ../test_helper load ../../lib/composure +load ../../lib/utilities load ../../lib/search load ../../plugins/available/base.plugin diff --git a/test/lib/search.bats b/test/lib/search.bats index 29e861d7..56cda073 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -3,6 +3,7 @@ load ../test_helper load ../../lib/composure load ../../lib/helpers +load ../../lib/utilities load ../../lib/search load ../../plugins/available/base.plugin load ../../aliases/available/git.aliases @@ -27,6 +28,7 @@ function local_setup { rm -rf "$BASH_IT"/aliases/enabled rm -rf "$BASH_IT"/completion/enabled rm -rf "$BASH_IT"/plugins/enabled + rm -rf "$BASH_IT"/tmp/cache mkdir -p "$BASH_IT"/enabled mkdir -p "$BASH_IT"/aliases/enabled diff --git a/test/lib/utilities.bats b/test/lib/utilities.bats new file mode 100644 index 00000000..be21fa40 --- /dev/null +++ b/test/lib/utilities.bats @@ -0,0 +1,108 @@ +#!/usr/bin/env bats + +load ../test_helper +load ../../lib/composure +load ../../lib/helpers +load ../../lib/utilities +load ../../lib/search + +cite _about _param _example _group _author _version + +function local_setup { + mkdir -p "$BASH_IT" + lib_directory="$(cd "$(dirname "$0")" && pwd)" + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git $lib_directory/../../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled + rm -rf "$BASH_IT"/tmp/cache + + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled +} + +function has_match() { + $(_bash-it-array-contains-element ${@}) && echo "has" "$1" +} + +function item_enabled() { + $(_bash-it-component-item-is-enabled ${@}) && echo "$1" "$2" "is enabled" +} + +function item_disabled() { + $(_bash-it-component-item-is-disabled ${@}) && echo "$1" "$2" "is disabled" +} + +@test "_bash-it-component-item-is-enabled() - for a disabled item" { + run item_enabled aliases svn + assert_line -n 0 '' +} + +@test "_bash-it-component-item-is-enabled() - for an enabled/disabled item" { + run bash-it enable alias svn + assert_line -n 0 'svn enabled with priority 150.' + + run item_enabled alias svn + assert_line -n 0 'alias svn is enabled' + + run bash-it disable alias svn + assert_line -n 0 'svn disabled.' + + run item_enabled alias svn + assert_line -n 0 '' +} + +@test "_bash-it-component-item-is-disabled() - for a disabled item" { + run item_disabled alias svn + assert_line -n 0 'alias svn is disabled' +} + +@test "_bash-it-component-item-is-disabled() - for an enabled/disabled item" { + run bash-it enable alias svn + assert_line -n 0 'svn enabled with priority 150.' + + run item_disabled alias svn + assert_line -n 0 '' + + run bash-it disable alias svn + assert_line -n 0 'svn disabled.' + + run item_disabled alias svn + assert_line -n 0 'alias svn is disabled' +} + +@test "_bash-it-array-contains-element() - when match is found, and is the first" { + declare -a fruits=(apple pear orange mandarin) + run has_match apple "${fruits[@]}" + assert_line -n 0 'has apple' +} + +@test "_bash-it-array-contains-element() - when match is found, and is the last" { + declare -a fruits=(apple pear orange mandarin) + run has_match mandarin "${fruits[@]}" + assert_line -n 0 'has mandarin' +} + +@test "_bash-it-array-contains-element() - when match is found, and is in the middle" { + declare -a fruits=(apple pear orange mandarin) + run has_match pear "${fruits[@]}" + assert_line -n 0 'has pear' +} + +@test "_bash-it-array-contains-element() - when match is found, and it has spaces" { + declare -a fruits=(apple pear orange mandarin "yellow watermelon") + run has_match "yellow watermelon" "${fruits[@]}" + assert_line -n 0 'has yellow watermelon' +} + +@test "_bash-it-array-contains-element() - when match is not found" { + declare -a fruits=(apple pear orange mandarin) + run has_match xyz "${fruits[@]}" + assert_line -n 0 '' +} From 2bc990269ab503268f8e8cff9781b96d0e0bf077 Mon Sep 17 00:00:00 2001 From: Li Zou Date: Sat, 22 Dec 2018 15:25:08 -0800 Subject: [PATCH 03/16] Add alias for listing untracked files --- aliases/available/git.aliases.bash | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aliases/available/git.aliases.bash b/aliases/available/git.aliases.bash index 169fe7b8..75b7997a 100644 --- a/aliases/available/git.aliases.bash +++ b/aliases/available/git.aliases.bash @@ -77,6 +77,8 @@ alias gcsam="git commit -S -am" alias gstd="git stash drop" alias gstl="git stash list" alias gh='cd "$(git rev-parse --show-toplevel)"' +# Show untracked files +alias gu='git ls-files . --exclude-standard --others' case $OSTYPE in darwin*) From 7b41736d1747939cb9978d3537744f578cab0651 Mon Sep 17 00:00:00 2001 From: Dan Beste Date: Sun, 22 Jul 2018 16:44:06 -0500 Subject: [PATCH 04/16] New theme: essential A simple, console-compatible Bash-it theme. Features: * Reactive coloring for: * Privileged and standard users. * Local and remote hosts. * Exit status for prompt strings 1 and 2. * Console-compatible! * No unicode "tofu" in console sessions. * Matching alignment for prompt strings 1 and 2. Preview: $PS1| $TIME $USER@$HOST:$PWD $PS1| + function foo() { $PS2| | bar $PS2| | baz $PS2| | } --- themes/essential/essential.theme.bash | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 themes/essential/essential.theme.bash diff --git a/themes/essential/essential.theme.bash b/themes/essential/essential.theme.bash new file mode 100644 index 00000000..c0b1a38e --- /dev/null +++ b/themes/essential/essential.theme.bash @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +# https://github.com/koalaman/shellcheck/wiki/Sc2154 +# shellcheck disable=SC2154 + +function _user-prompt() { + local -r user='\\u' + + if [[ "${EUID}" -eq 0 ]]; then + # Privileged users: + local -r user_color="${bold_red}" + else + # Standard users: + local -r user_color="${bold_green}" + fi + + # Print the current user's name (colored according to their current EUID): + echo -e "${user_color}${user}${normal}" +} + +function _host-prompt() { + local -r host='\\h' + + # Check whether or not $SSH_TTY is set: + if [[ -z "${SSH_TTY}" ]]; then + # For local hosts, set the host's prompt color to blue: + local -r host_color="${bold_blue}" + else + # For remote hosts, set the host's prompt color to red: + local -r host_color="${bold_red}" + fi + + # Print the current hostname (colored according to $SSH_TTY's status): + echo -e "${host_color}${host}${normal}" +} + +function _user-at-host-prompt() { + # Concatenate the user and host prompts into: user@host: + echo -e "$(_user-prompt)${bold_white}@$(_host-prompt)" +} + +function _exit-status-prompt() { + local -r prompt_string="${1}" + local -r exit_status="${2}" + + # Check the exit status of the last command captured by $exit_status: + if [[ "${exit_status}" -eq 0 ]]; then + # For commands that return an exit status of zero, set the exit status's + # notifier to green: + local -r exit_status_color="${bold_green}" + else + # For commands that return a non-zero exit status, set the exit status's + # notifier to red: + local -r exit_status_color="${bold_red}" + fi + + echo -ne "${exit_status_color}" + if [[ "${prompt_string}" -eq 1 ]]; then + # $PS1: + echo -e " +${normal} " + elif [[ "${prompt_string}" -eq 2 ]]; then + # $PS2: + echo -e " |${normal} " + else + # Default: + echo -e " ?${normal} " + fi +} + +function _ps1() { + local -r time='\\t' + + echo -ne "${bold_white}${time} " + echo -ne "$(_user-at-host-prompt)" + echo -e "${bold_white}:${normal}${PWD}" + echo -e "$(_exit-status-prompt 1 "${exit_status}")" +} + +function _ps2() { + echo -e "$(_exit-status-prompt 2 "${exit_status}")" +} + +function prompt_command() { + # Capture the exit status of the last command: + local -r exit_status="${?}" + + # Build the $PS1 prompt: + PS1="$(_ps1)" + + # Build the $PS2 prompt: + PS2="$(_ps2)" +} + +safe_append_prompt_command prompt_command + +# vim: sw=2 ts=2 et: From 0fe631ea7cda5b2c1e5ad73e2a83a0770a57e1bf Mon Sep 17 00:00:00 2001 From: Claudia Date: Wed, 2 Jan 2019 17:45:43 +0100 Subject: [PATCH 05/16] Make `bash-it reload` preserve working directory In e5b6869 (part of #1283), a regression was introduced, which caused `bash-it reload` to return with the working directory set to `.bash_it` and the original working directory discarded. The root cause is the function wrapper of `bash-it reload`, which has always relied on the working directory not to change during execution of the wrapped code. This assumption no longer holds with the changes introduced in #1283. This commit fixes the regression by using `pushd`/`popd` in `bash-it reload`. --- lib/helpers.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers.bash b/lib/helpers.bash index e632b80b..4d11e657 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -256,7 +256,7 @@ _bash-it-reload() { _about 'reloads a profile file' _group 'lib' - cd "${BASH_IT}" || return + pushd "${BASH_IT}" &> /dev/null || return case $OSTYPE in darwin*) @@ -267,7 +267,7 @@ _bash-it-reload() { ;; esac - cd - &> /dev/null || return + popd &> /dev/null || return } _bash-it-describe () From fe8f5e42d09abd78051df82829227b37996135e7 Mon Sep 17 00:00:00 2001 From: tbhaxor Date: Mon, 7 Jan 2019 01:10:21 +0530 Subject: [PATCH 06/16] added wpscan completions --- completion/available/wpscan.completion.bash | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 completion/available/wpscan.completion.bash diff --git a/completion/available/wpscan.completion.bash b/completion/available/wpscan.completion.bash new file mode 100644 index 00000000..ec517aec --- /dev/null +++ b/completion/available/wpscan.completion.bash @@ -0,0 +1,16 @@ +#!/usr/bin/bash + +if command -v wpscan > /dev/null; then + __wpscan_completion() { + local OPTS=("--help --hh --version --ignore-main-redirect --verbose --output --format --detection-mode --scope --headers --user-agent --vhost --random-user-agent --user-agents-list --http-auth --max-threads --throttle --request-timeout --connect-timeout --disable-tlc-checks --proxy --proxy-auth --cookie-string --cookie-jar --cache-ttl --clear-cache --server --cache-dir --update --no-update --wp-content-dir --wp-plugins-dir --wp-version-detection --main-theme-detection --enumrate --exclude-content-based --plugins-list --plugins-detection --plugins-version-all --plugins-version-detection --themes-list --themes-detection --themes-version-all --themes-version-detection --timthumbs-list --timthumbs-detection --config-backups-list --config-backups-detection --db-exports-list --db-exports-detection --medias-detection --users-list --users-detection --passwords --usernames --multicall-max-passwords --password-attack --stealthy") + COMPREPLY=() + for _opt_ in ${OPTS[@]}; do + if [[ "$_opt_" == "$2"* ]]; then + COMPREPLY+=("$_opt_") + fi + done + } + + complete -F __wpscan_completion wpscan +fi + From 508f82eb95d8aa3d04f3bf722af244cf97ae0379 Mon Sep 17 00:00:00 2001 From: tbhaxor Date: Mon, 7 Jan 2019 01:18:37 +0530 Subject: [PATCH 07/16] added vuejs completions --- completion/available/vuejs.completion.bash | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 completion/available/vuejs.completion.bash diff --git a/completion/available/vuejs.completion.bash b/completion/available/vuejs.completion.bash new file mode 100644 index 00000000..2959f29b --- /dev/null +++ b/completion/available/vuejs.completion.bash @@ -0,0 +1,15 @@ +#!/usr/bin/bash + +if command -v vue > /dev/null; then + __vuejs_completion() { + local OPTS=("--version --help create add invoke inspect serve build ui init config upgrade info") + COMPREPLY=() + for _opt_ in ${OPTS[@]}; do + if [[ "$_opt_" == "$2"* ]]; then + COMPREPLY+=("$_opt_") + fi + done + } + + complete -F __vuejs_completion vue +fi From a636f602c84985528e4b02e326adb32e26645a9b Mon Sep 17 00:00:00 2001 From: tbhaxor Date: Mon, 7 Jan 2019 01:23:00 +0530 Subject: [PATCH 08/16] added main option --- completion/available/wpscan.completion.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completion/available/wpscan.completion.bash b/completion/available/wpscan.completion.bash index ec517aec..9f21a0d8 100644 --- a/completion/available/wpscan.completion.bash +++ b/completion/available/wpscan.completion.bash @@ -2,7 +2,7 @@ if command -v wpscan > /dev/null; then __wpscan_completion() { - local OPTS=("--help --hh --version --ignore-main-redirect --verbose --output --format --detection-mode --scope --headers --user-agent --vhost --random-user-agent --user-agents-list --http-auth --max-threads --throttle --request-timeout --connect-timeout --disable-tlc-checks --proxy --proxy-auth --cookie-string --cookie-jar --cache-ttl --clear-cache --server --cache-dir --update --no-update --wp-content-dir --wp-plugins-dir --wp-version-detection --main-theme-detection --enumrate --exclude-content-based --plugins-list --plugins-detection --plugins-version-all --plugins-version-detection --themes-list --themes-detection --themes-version-all --themes-version-detection --timthumbs-list --timthumbs-detection --config-backups-list --config-backups-detection --db-exports-list --db-exports-detection --medias-detection --users-list --users-detection --passwords --usernames --multicall-max-passwords --password-attack --stealthy") + local OPTS=("--help --hh --version --url --ignore-main-redirect --verbose --output --format --detection-mode --scope --headers --user-agent --vhost --random-user-agent --user-agents-list --http-auth --max-threads --throttle --request-timeout --connect-timeout --disable-tlc-checks --proxy --proxy-auth --cookie-string --cookie-jar --cache-ttl --clear-cache --server --cache-dir --update --no-update --wp-content-dir --wp-plugins-dir --wp-version-detection --main-theme-detection --enumrate --exclude-content-based --plugins-list --plugins-detection --plugins-version-all --plugins-version-detection --themes-list --themes-detection --themes-version-all --themes-version-detection --timthumbs-list --timthumbs-detection --config-backups-list --config-backups-detection --db-exports-list --db-exports-detection --medias-detection --users-list --users-detection --passwords --usernames --multicall-max-passwords --password-attack --stealthy") COMPREPLY=() for _opt_ in ${OPTS[@]}; do if [[ "$_opt_" == "$2"* ]]; then From ec088d4271de855e0c173f9df5b3623c38f3b26b Mon Sep 17 00:00:00 2001 From: LanikSJ Date: Tue, 8 Jan 2019 14:01:54 -0800 Subject: [PATCH 09/16] Remove Deprecated Brew Command --- aliases/available/homebrew.aliases.bash | 1 - 1 file changed, 1 deletion(-) diff --git a/aliases/available/homebrew.aliases.bash b/aliases/available/homebrew.aliases.bash index 239d884b..15907518 100644 --- a/aliases/available/homebrew.aliases.bash +++ b/aliases/available/homebrew.aliases.bash @@ -4,7 +4,6 @@ cite 'about-alias' about-alias 'homebrew abbreviations' alias bup='brew update && brew upgrade' -alias bupc='brew update && brew upgrade --cleanup' alias bout='brew outdated' alias bin='brew install' alias brm='brew uninstall' From 56945e3bdfe2b8d00db7ec86dfbbc53388790874 Mon Sep 17 00:00:00 2001 From: Kyle Welsby Date: Wed, 16 Jan 2019 17:15:23 +0000 Subject: [PATCH 10/16] Fixes #1309 _bash-it-clean-component-cache: command not found Silence errors when installing ``` Would you like to enable the ag aliases? [y/N] y ~/.bash_it/lib/helpers.bash: line 488: _bash-it-clean-component-cache: command not found ag enabled with priority 150. ``` --- install.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/install.sh b/install.sh index c0848cdb..8250c3df 100755 --- a/install.sh +++ b/install.sh @@ -155,6 +155,7 @@ fi # Load dependencies for enabling components source "$BASH_IT/lib/composure.bash" +source "$BASH_IT/lib/utilities.bash" cite _about _param _example _group _author _version source "$BASH_IT/lib/helpers.bash" From fedb72b34f148e9b3dd2c3aa3164c4d58feed2ef Mon Sep 17 00:00:00 2001 From: Kyle Welsby Date: Wed, 16 Jan 2019 17:35:44 +0000 Subject: [PATCH 11/16] Fixes exit 1 issue when tmp cache doesn't exist Found if this `${cache}` file doesn't exist, the single line conditional command would return false causing a `exit 1`. This little change makes the conditional verbose and not cause a false return failing the function. --- lib/utilities.bash | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utilities.bash b/lib/utilities.bash index 93e96606..d7cf6f3f 100644 --- a/lib/utilities.bash +++ b/lib/utilities.bash @@ -44,7 +44,9 @@ _bash-it-clean-component-cache() { done else cache="$(_bash-it-component-cache-file ${component})" - [[ -f "${cache}" ]] && rm -f "${cache}" + if [[ -f "${cache}" ]] ; then + rm -f "${cache}" + fi fi } From ca58587217661357663967667ff8b2a57df0671a Mon Sep 17 00:00:00 2001 From: Kyle Welsby Date: Wed, 16 Jan 2019 18:16:08 +0000 Subject: [PATCH 12/16] Fixes #1203 convert alias to aliases A ungraceful patch but fixes this one case where the command is singular `alias` while the file are plural with `aliases. --- lib/helpers.bash | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/helpers.bash b/lib/helpers.bash index 7e9ff2ab..3a47cd4a 100644 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -452,6 +452,9 @@ _enable-thing () for f in "${BASH_IT}/$subdirectory/available/"*.bash do to_enable=$(basename $f .$file_type.bash) + if [ "$file_type" = "alias" ]; then + to_enable=$(basename $f ".aliases.bash") + fi _enable-thing $subdirectory $file_type $to_enable $load_priority done else From af1bf11748c16fa6bdff83cf1f20ba76caa62181 Mon Sep 17 00:00:00 2001 From: Kaiming Guo Date: Tue, 22 Jan 2019 14:38:58 +0800 Subject: [PATCH 13/16] add MSYS2 aliases Ignore NTUSER.DAT files in Git/MSYS2 Bash. --- aliases/available/msys2.aliases.bash | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 aliases/available/msys2.aliases.bash diff --git a/aliases/available/msys2.aliases.bash b/aliases/available/msys2.aliases.bash new file mode 100644 index 00000000..83b14a13 --- /dev/null +++ b/aliases/available/msys2.aliases.bash @@ -0,0 +1,16 @@ +#!/bin/bash + +cite 'about-alias' +about-alias 'MSYS2 aliases' + +LS_COMMON="-hG" +LS_COMMON="$LS_COMMON --color=auto" +LS_COMMON="$LS_COMMON -I NTUSER.DAT\* -I ntuser.dat\*" + +test -n "$LS_COMMON" && + +# alias +alias ls="command ls $LS_COMMON" +alias ll="ls -l" +alias la="ls -a" +alias lal="ll -a" From 212eb6636eb659b5390a987675c0604d11f60a6d Mon Sep 17 00:00:00 2001 From: Kai-Ming Guo Date: Tue, 22 Jan 2019 17:19:35 +0800 Subject: [PATCH 14/16] complete condition command `test` command to check `$LS_COMMON` argument exists. and merged the next line. --- aliases/available/msys2.aliases.bash | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/aliases/available/msys2.aliases.bash b/aliases/available/msys2.aliases.bash index 83b14a13..a309a5b2 100644 --- a/aliases/available/msys2.aliases.bash +++ b/aliases/available/msys2.aliases.bash @@ -7,10 +7,9 @@ LS_COMMON="-hG" LS_COMMON="$LS_COMMON --color=auto" LS_COMMON="$LS_COMMON -I NTUSER.DAT\* -I ntuser.dat\*" -test -n "$LS_COMMON" && - # alias -alias ls="command ls $LS_COMMON" +# setup the main ls alias if we've established common args +test -n "$LS_COMMON" && alias ls="command ls $LS_COMMON" alias ll="ls -l" alias la="ls -a" alias lal="ll -a" From 34487076ba506437c97ecf796c4560dfda810d37 Mon Sep 17 00:00:00 2001 From: NiilShon <51centimos@gmail.com> Date: Wed, 23 Jan 2019 00:29:11 +0100 Subject: [PATCH 15/16] Update general.aliases.bash --- aliases/available/general.aliases.bash | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aliases/available/general.aliases.bash b/aliases/available/general.aliases.bash index c3455525..542b76cd 100644 --- a/aliases/available/general.aliases.bash +++ b/aliases/available/general.aliases.bash @@ -99,8 +99,9 @@ alias bshenc="bash-it enable completion" # Shorten extract alias xt="extract" -# sudo vim +# sudo editors alias svim="sudo vim" +alias snano="sudo nano" # Display whatever file is regular file or folder catt() { From cb74bd63b9158072a7b59c223ee284918586a02b Mon Sep 17 00:00:00 2001 From: jrab66 Date: Fri, 25 Jan 2019 15:00:21 -0600 Subject: [PATCH 16/16] squash screenshot --- themes/metal/metal.theme.bash | 81 +++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 themes/metal/metal.theme.bash diff --git a/themes/metal/metal.theme.bash b/themes/metal/metal.theme.bash new file mode 100644 index 00000000..e0eddd26 --- /dev/null +++ b/themes/metal/metal.theme.bash @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +# Emoji-based theme to display source control management and +# virtual environment info beside the ordinary bash prompt. + +# Theme inspired by: +# - Naming your Terminal tabs in OSX Lion - http://thelucid.com/2012/01/04/naming-your-terminal-tabs-in-osx-lion/ +# - Bash_it sexy theme + +# inspired by previous bash_it theme : cupcake + +# Demo: +# ┌ⓔ virtualenv 🐲🤘user @ 💻 host in 🗂️ directory on 🌵 branch {1} ↑1 ↓1 +1 •1 ⌀1 ✗ +# └❯ cd .bash-it/themes/cupcake + +# virtualenv prompts +VIRTUALENV_CHAR="ⓔ " +VIRTUALENV_THEME_PROMPT_PREFIX="" +VIRTUALENV_THEME_PROMPT_SUFFIX="" + +# SCM prompts +SCM_NONE_CHAR="" +SCM_GIT_CHAR="[±] " +SCM_GIT_BEHIND_CHAR="${red}↓${normal}" +SCM_GIT_AHEAD_CHAR="${bold_green}↑${normal}" +SCM_GIT_UNTRACKED_CHAR="⌀" +SCM_GIT_UNSTAGED_CHAR="${bold_yellow}•${normal}" +SCM_GIT_STAGED_CHAR="${bold_green}+${normal}" + +SCM_THEME_PROMPT_DIRTY="" +SCM_THEME_PROMPT_CLEAN="" +SCM_THEME_PROMPT_PREFIX="" +SCM_THEME_PROMPT_SUFFIX="" + +# Git status prompts +GIT_THEME_PROMPT_DIRTY=" ${red}✗${normal}" +GIT_THEME_PROMPT_CLEAN=" ${bold_green}✓${normal}" +GIT_THEME_PROMPT_PREFIX="" +GIT_THEME_PROMPT_SUFFIX="" + +# ICONS ======================================================================= + +icon_start="┌" +icon_user="🤘-🐧" +icon_host="@ 💻 " +icon_directory=" - 🧱 " +icon_branch="🌵" +icon_end="└🤘-> " + +# extra spaces ensure legiblity in prompt + +# FUNCTIONS =================================================================== + +# Display virtual environment info +function virtualenv_prompt { + if [[ -n "$VIRTUAL_ENV" ]]; then + virtualenv=`basename "$VIRTUAL_ENV"` + echo -e "$VIRTUALENV_CHAR$virtualenv " + fi +} + +# Rename tab +function tabname { + printf "\e]1;$1\a" +} + +# Rename window +function winname { + printf "\e]2;$1\a" +} + +# PROMPT OUTPUT =============================================================== + +# Displays the current prompt +function prompt_command() { + PS1="\n${icon_start}$(virtualenv_prompt)${icon_user}${bold_green}\u${normal}${icon_host}${bold_cyan}\h${normal}${icon_directory}${bold_purple}\W${normal}\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" on ${icon_branch} \")${white}$(scm_prompt_info)${normal}\n${icon_end}" + PS2="${icon_end}" +} + +# Runs prompt (this bypasses bash_it $PROMPT setting) +safe_append_prompt_command prompt_command