From 408f8e8e4cc47e7afad87feab8df1e39c9b17798 Mon Sep 17 00:00:00 2001 From: Nikola Hristov Date: Wed, 18 Oct 2023 09:03:05 +0300 Subject: [PATCH] squash! --- completions/chezmoi.completion.sh | 2 +- lib/omb-completion.sh | 72 ++-- plugins/bashmarks/README.md | 24 +- plugins/bashmarks/bashmarks.plugin.sh | 250 ++++++------ plugins/chezmoi/README.md | 5 +- themes/edsonarios/edsonarios.theme.sh | 36 +- themes/powerbash10k/README.md | 69 ++-- themes/powerbash10k/powerbash10k.theme.sh | 474 +++++++++++----------- 8 files changed, 471 insertions(+), 461 deletions(-) diff --git a/completions/chezmoi.completion.sh b/completions/chezmoi.completion.sh index 2ad93dab0..65eb43baa 100644 --- a/completions/chezmoi.completion.sh +++ b/completions/chezmoi.completion.sh @@ -1,5 +1,5 @@ #! bash oh-my-bash.module if _omb_util_command_exists chezmoi; then - eval -- "$(chezmoi completion bash)" + eval -- "$(chezmoi completion bash)" fi diff --git a/lib/omb-completion.sh b/lib/omb-completion.sh index 5fd845d54..abf848a61 100644 --- a/lib/omb-completion.sh +++ b/lib/omb-completion.sh @@ -11,36 +11,36 @@ ## in COMP_WORDS. ## @arr[out] COMPREPLY This functions empties the array COMPREPLY. function _omb_completion_reassemble_breaks { - local exclude=$1 - local line=$COMP_LINE point=$COMP_POINT - local breaks=${COMP_WORDBREAKS//[\"\'$exclude]} + local exclude=$1 + local line=$COMP_LINE point=$COMP_POINT + local breaks=${COMP_WORDBREAKS//[\"\'$exclude]/} - COMPREPLY=() - cur=("${COMP_WORDS[COMP_CWORD]}" '') + COMPREPLY=() + cur=("${COMP_WORDS[COMP_CWORD]}" '') - local word rprefix= rword= - for word in "${COMP_WORDS[@]::COMP_CWORD+1}"; do - local space=${line%%"$word"*} - if [[ $space == "$line" ]]; then - # error: COMP_LINE does not contain enough words - return 1 - fi + local word rprefix= rword= + for word in "${COMP_WORDS[@]::COMP_CWORD+1}"; do + local space=${line%%"$word"*} + if [[ $space == "$line" ]]; then + # error: COMP_LINE does not contain enough words + return 1 + fi - word=${word::point - ${#space}} - if [[ $space || $rword == *["$breaks"] || $word == ["$breaks"]* ]]; then - rprefix= - rword=$word - else - rprefix=$rword - rword+=$word - fi + word=${word::point-${#space}} + if [[ $space || $rword == *["$breaks"] || $word == ["$breaks"]* ]]; then + rprefix= + rword=$word + else + rprefix=$rword + rword+=$word + fi - line=${line:${#space}+${#word}} - ((point -= ${#space} + ${#word})) - ((point >= 0)) || break - done + line=${line:${#space}+${#word}} + ((point -= ${#space} + ${#word})) + ((point >= 0)) || break + done - cur=("$rword" "$rprefix") + cur=("$rword" "$rprefix") } ## @fn _omb_completion_resolve_breaks @@ -48,15 +48,15 @@ function _omb_completion_reassemble_breaks { ## @var[out] cur[1] Prefix to remove set by _omb_completion_reassemble_breaks ## @arr[out] COMPREPLY This functions empties the array COMPREPLY. function _omb_completion_resolve_breaks { - if [[ ${cur[1]} ]]; then - local i - for i in "${!COMPREPLY[@]}"; do - if [[ ${COMPREPLY[i]} == "$cur_prefix"* ]]; then - COMPREPLY[i]=${COMPREPLY[i]#"$cur_prefix"} - else - unset -v 'COMPREPLY[i]' - fi - done - COMPREPLY=("${COMPREPLY[@]}") - fi + if [[ ${cur[1]} ]]; then + local i + for i in "${!COMPREPLY[@]}"; do + if [[ ${COMPREPLY[i]} == "$cur_prefix"* ]]; then + COMPREPLY[i]=${COMPREPLY[i]#"$cur_prefix"} + else + unset -v 'COMPREPLY[i]' + fi + done + COMPREPLY=("${COMPREPLY[@]}") + fi } diff --git a/plugins/bashmarks/README.md b/plugins/bashmarks/README.md index 1f2f0d892..7d43d942a 100644 --- a/plugins/bashmarks/README.md +++ b/plugins/bashmarks/README.md @@ -1,8 +1,8 @@ # Bashmarks plugin The Bashmarks plugin allows you to create and use bookmarks for directories on -your filesystems. This plugin is derived from the project -https://github.com/huing/bashmarks but contains improvements. This plugin is +your filesystems. This plugin is derived from the project +https://github.com/huing/bashmarks but contains improvements. This plugin is licensed under the 3-Clause BSD license. ## Quickstart @@ -14,7 +14,7 @@ $ bm -a mydir ``` The command above creates a bookmark for the current directory with the name -*mydir*. +_mydir_. You can navigate to the location of a bookmark using the _bm -g_ command: @@ -28,10 +28,10 @@ You can also supply just any bookmark name and the _-g_ option will be assumed: $ bm mydir ``` -Tab completion is available when you need to enter a bookmark name in a -command, such as when using *bm -g* +Tab completion is available when you need to enter a bookmark name in a command, +such as when using _bm -g_ -Easily list all bookmarks you have set up using the *bm -l* command: +Easily list all bookmarks you have set up using the _bm -l_ command: ```bash $ bm -l @@ -39,15 +39,15 @@ $ bm -l ## Configuration -- **`BASHMARKS_SDIR`**: This variable contains the path to the file that stores - the information of bookmarks of directory names. The default path is - `~/.sdirs`. The old interface `SDIRS` is now deprecated. +- **`BASHMARKS_SDIR`**: This variable contains the path to the file that + stores the information of bookmarks of directory names. The default path is + `~/.sdirs`. The old interface `SDIRS` is now deprecated. ## Configuration -- `BASHMARKS_SDIR`: This variable contains the path to the file that stores the - information of bookmarks of directory names. The default path is `~/.sdirs`. - The old interface `SDIRS` is now deprecated. +- `BASHMARKS_SDIR`: This variable contains the path to the file that stores + the information of bookmarks of directory names. The default path is + `~/.sdirs`. The old interface `SDIRS` is now deprecated. ## Commands diff --git a/plugins/bashmarks/bashmarks.plugin.sh b/plugins/bashmarks/bashmarks.plugin.sh index ee0e085d0..31ee7ac51 100644 --- a/plugins/bashmarks/bashmarks.plugin.sh +++ b/plugins/bashmarks/bashmarks.plugin.sh @@ -47,173 +47,173 @@ # Default configurations if [[ ! ${BASHMARKS_SDIRS-} ]]; then - BASHMARKS_SDIRS=${SDIRS:-$HOME/.sdirs} + BASHMARKS_SDIRS=${SDIRS:-$HOME/.sdirs} fi # Deprecated interfaces (2023-10-12) -_omb_deprecate_declare 20000 SDIRS BASHMARKS_SDIRS sync -_omb_deprecate_function 20000 _echo_usage _bashmarks_usage -_omb_deprecate_function 20000 _save_bookmark _bashmarks_save -_omb_deprecate_function 20000 _delete_bookmark _bashmarks_delete -_omb_deprecate_function 20000 _goto_bookmark _bashmarks_goto -_omb_deprecate_function 20000 _list_bookmark _bashmarks_list -_omb_deprecate_function 20000 _print_bookmark _bashmarks_print -_omb_deprecate_function 20000 _l _bashmarks_list_names -_omb_deprecate_function 20000 _bookmark_name_valid _bashmarks_is_valid_bookmark_name -_omb_deprecate_function 20000 _comp _bashmarks_comp_cmd_bm -_omb_deprecate_function 20000 _compzsh _bashmarks_compzsh_cmd_bm -_omb_deprecate_function 20000 _purge_line _bashmarks_purge_line +_omb_deprecate_declare 20000 SDIRS BASHMARKS_SDIRS sync +_omb_deprecate_function 20000 _echo_usage _bashmarks_usage +_omb_deprecate_function 20000 _save_bookmark _bashmarks_save +_omb_deprecate_function 20000 _delete_bookmark _bashmarks_delete +_omb_deprecate_function 20000 _goto_bookmark _bashmarks_goto +_omb_deprecate_function 20000 _list_bookmark _bashmarks_list +_omb_deprecate_function 20000 _print_bookmark _bashmarks_print +_omb_deprecate_function 20000 _l _bashmarks_list_names +_omb_deprecate_function 20000 _bookmark_name_valid _bashmarks_is_valid_bookmark_name +_omb_deprecate_function 20000 _comp _bashmarks_comp_cmd_bm +_omb_deprecate_function 20000 _compzsh _bashmarks_compzsh_cmd_bm +_omb_deprecate_function 20000 _purge_line _bashmarks_purge_line # setup file to store bookmarks if [[ ! -e $BASHMARKS_SDIRS ]]; then - touch "$BASHMARKS_SDIRS" + touch "$BASHMARKS_SDIRS" fi # main function function bm { - local option=$1 - case $option in - # save current directory to bookmarks [ bm -a BOOKMARK_NAME ] - -a) - _bashmarks_save "$2" - ;; - # delete bookmark [ bm -d BOOKMARK_NAME ] - -d) - _bashmarks_delete "$2" - ;; - # jump to bookmark [ bm -g BOOKMARK_NAME ] - -g) - _bashmarks_goto "$2" - ;; - # print bookmark [ bm -p BOOKMARK_NAME ] - -p) - _bashmarks_print "$2" - ;; - # show bookmark list [ bm -l ] - -l) - _bashmarks_list - ;; - # help [ bm -h ] - -h) - _bashmarks_usage - ;; - *) - if [[ $1 == -* ]]; then - # unrecognized option. echo error message and usage [ bm -X ] - echo "Unknown option '$1'" - _bashmarks_usage - kill -SIGINT $$ - exit 1 - elif [[ $1 == "" ]]; then - # no args supplied - echo usage [ bm ] - _bashmarks_usage - else - # non-option supplied as first arg. assume goto [ bm BOOKMARK_NAME ] - _bashmarks_goto "$1" - fi - ;; - esac + local option=$1 + case $option in + # save current directory to bookmarks [ bm -a BOOKMARK_NAME ] + -a) + _bashmarks_save "$2" + ;; + # delete bookmark [ bm -d BOOKMARK_NAME ] + -d) + _bashmarks_delete "$2" + ;; + # jump to bookmark [ bm -g BOOKMARK_NAME ] + -g) + _bashmarks_goto "$2" + ;; + # print bookmark [ bm -p BOOKMARK_NAME ] + -p) + _bashmarks_print "$2" + ;; + # show bookmark list [ bm -l ] + -l) + _bashmarks_list + ;; + # help [ bm -h ] + -h) + _bashmarks_usage + ;; + *) + if [[ $1 == -* ]]; then + # unrecognized option. echo error message and usage [ bm -X ] + echo "Unknown option '$1'" + _bashmarks_usage + kill -SIGINT $$ + exit 1 + elif [[ $1 == "" ]]; then + # no args supplied - echo usage [ bm ] + _bashmarks_usage + else + # non-option supplied as first arg. assume goto [ bm BOOKMARK_NAME ] + _bashmarks_goto "$1" + fi + ;; + esac } # print usage information function _bashmarks_usage { - echo 'USAGE:' - echo "bm -h - Prints this usage info" - echo 'bm -a - Saves the current directory as "bookmark_name"' - echo 'bm [-g] - Goes (cd) to the directory associated with "bookmark_name"' - echo 'bm -p - Prints the directory associated with "bookmark_name"' - echo 'bm -d - Deletes the bookmark' - echo 'bm -l - Lists all available bookmarks' + echo 'USAGE:' + echo "bm -h - Prints this usage info" + echo 'bm -a - Saves the current directory as "bookmark_name"' + echo 'bm [-g] - Goes (cd) to the directory associated with "bookmark_name"' + echo 'bm -p - Prints the directory associated with "bookmark_name"' + echo 'bm -d - Deletes the bookmark' + echo 'bm -l - Lists all available bookmarks' } # save current directory to bookmarks function _bashmarks_save { - if _bashmarks_is_valid_bookmark_name "$@"; then - _bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1=" - CURDIR=$(echo $PWD| sed "s#^$HOME#\$HOME#g") - echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_SDIRS" - fi + if _bashmarks_is_valid_bookmark_name "$@"; then + _bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1=" + CURDIR=$(echo $PWD | sed "s#^$HOME#\$HOME#g") + echo "export DIR_$1=\"$CURDIR\"" >> "$BASHMARKS_SDIRS" + fi } # delete bookmark function _bashmarks_delete { - if _bashmarks_is_valid_bookmark_name "$@"; then - _bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1=" - unset "DIR_$1" - fi + if _bashmarks_is_valid_bookmark_name "$@"; then + _bashmarks_purge_line "$BASHMARKS_SDIRS" "export DIR_$1=" + unset "DIR_$1" + fi } # jump to bookmark function _bashmarks_goto { - source "$BASHMARKS_SDIRS" - local target_varname=DIR_$1 - local target=${!target_varname-} - if [[ -d $target ]]; then - cd "$target" - elif [[ ! $target ]]; then - printf '%s\n' "${_omb_term_brown}WARNING: '${1}' bashmark does not exist${_omb_term_reset}" - else - printf '%s\n' "${_omb_term_brown}WARNING: '${target}' does not exist${_omb_term_reset}" - fi + source "$BASHMARKS_SDIRS" + local target_varname=DIR_$1 + local target=${!target_varname-} + if [[ -d $target ]]; then + cd "$target" + elif [[ ! $target ]]; then + printf '%s\n' "${_omb_term_brown}WARNING: '${1}' bashmark does not exist${_omb_term_reset}" + else + printf '%s\n' "${_omb_term_brown}WARNING: '${target}' does not exist${_omb_term_reset}" + fi } # list bookmarks with dirname function _bashmarks_list { - source "$BASHMARKS_SDIRS" - # if color output is not working for you, comment out the line below '\033[1;32m' == "red" - env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}' - # uncomment this line if color output is not working with the line above - # env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" + source "$BASHMARKS_SDIRS" + # if color output is not working for you, comment out the line below '\033[1;32m' == "red" + env | sort | awk '/^DIR_.+/{split(substr($0,5),parts,"="); printf("\033[0;33m%-20s\033[0m %s\n", parts[1], parts[2]);}' + # uncomment this line if color output is not working with the line above + # env | grep "^DIR_" | cut -c5- | sort |grep "^.*=" } # print bookmark function _bashmarks_print { - source "$BASHMARKS_SDIRS" - echo "$(eval $(echo echo $(echo \$DIR_$1)))" + source "$BASHMARKS_SDIRS" + echo "$(eval $(echo echo $(echo \$DIR_$1)))" } # list bookmarks without dirname function _bashmarks_list_names { - source "$BASHMARKS_SDIRS" - env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" + source "$BASHMARKS_SDIRS" + env | grep "^DIR_" | cut -c5- | sort | grep "^.*=" | cut -f1 -d "=" } # validate bookmark name # @var[out] exit_message function _bashmarks_is_valid_bookmark_name { - local exit_message="" - if [[ ! $1 ]]; then - exit_message="bookmark name required" - echo "$exit_message" >&2 - return 1 - elif [[ $1 == *[!A-Za-z0-9_]* ]]; then - exit_message="bookmark name is not valid" - echo "$exit_message" >&2 - return 1 - fi + local exit_message="" + if [[ ! $1 ]]; then + exit_message="bookmark name required" + echo "$exit_message" >&2 + return 1 + elif [[ $1 == *[!A-Za-z0-9_]* ]]; then + exit_message="bookmark name is not valid" + echo "$exit_message" >&2 + return 1 + fi } # completion command function _bashmarks_comp_cmd_bm { - COMPREPLY=() - if ((COMP_CWORD >= 2)) && [[ ${COMP_WORDS[1]} == -[gpd] ]]; then - local cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=($(compgen -W '$(_bashmarks_list_names)' -- "$cur")) - fi - return 0 + COMPREPLY=() + if ((COMP_CWORD >= 2)) && [[ ${COMP_WORDS[1]} == -[gpd] ]]; then + local cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=($(compgen -W '$(_bashmarks_list_names)' -- "$cur")) + fi + return 0 } # ZSH completion command function _bashmarks_compzsh_cmd_bm { - reply=($(_bashmarks_list_names)) + reply=($(_bashmarks_list_names)) } # safe delete line from sdirs function _bashmarks_purge_line { - if [[ -s $1 ]]; then - # safely create a temp file - t=$(mktemp -t bashmarks.XXXXXX) || exit 1 - trap "/bin/rm -f -- '$t'" EXIT + if [[ -s $1 ]]; then + # safely create a temp file + t=$(mktemp -t bashmarks.XXXXXX) || exit 1 + trap "/bin/rm -f -- '$t'" EXIT # purge line sed "/$2/d" "$1" >| "$t" @@ -227,20 +227,20 @@ function _bashmarks_purge_line { # bind completion command for g,p,d to _bashmarks_comp_cmd_bm if [[ ${ZSH_VERSION-} ]]; then - compctl -K _bashmarks_compzsh_cmd_bm bm -g - compctl -K _bashmarks_compzsh_cmd_bm bm -p - compctl -K _bashmarks_compzsh_cmd_bm bm -d - compctl -K _bashmarks_compzsh_cmd_bm g - compctl -K _bashmarks_compzsh_cmd_bm p - compctl -K _bashmarks_compzsh_cmd_bm d + compctl -K _bashmarks_compzsh_cmd_bm bm -g + compctl -K _bashmarks_compzsh_cmd_bm bm -p + compctl -K _bashmarks_compzsh_cmd_bm bm -d + compctl -K _bashmarks_compzsh_cmd_bm g + compctl -K _bashmarks_compzsh_cmd_bm p + compctl -K _bashmarks_compzsh_cmd_bm d else - shopt -s progcomp - complete -F _bashmarks_comp_cmd_bm bm - complete -F _bashmarks_comp_cmd_bm bm - complete -F _bashmarks_comp_cmd_bm bm - complete -F _bashmarks_comp_cmd_bm g - complete -F _bashmarks_comp_cmd_bm p - complete -F _bashmarks_comp_cmd_bm d + shopt -s progcomp + complete -F _bashmarks_comp_cmd_bm bm + complete -F _bashmarks_comp_cmd_bm bm + complete -F _bashmarks_comp_cmd_bm bm + complete -F _bashmarks_comp_cmd_bm g + complete -F _bashmarks_comp_cmd_bm p + complete -F _bashmarks_comp_cmd_bm d fi alias s='bm -a' # Save a bookmark [bookmark_name] diff --git a/plugins/chezmoi/README.md b/plugins/chezmoi/README.md index f86a1e511..865b83bed 100644 --- a/plugins/chezmoi/README.md +++ b/plugins/chezmoi/README.md @@ -1,11 +1,12 @@ # Chezmoi plugin -The chezmoi plugin defines a number of useful aliases for you. [Consult the complete list](./chezmoi.plugin.sh) +The chezmoi plugin defines a number of useful aliases for you. +[Consult the complete list](./chezmoi.plugin.sh) ## List of aliases | Alias | Command | Description | -|-------|------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------| +| ----- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | cz | 'chezmoi' | Manage your dotfiles across multiple diverse machines, securely | | cza | 'chezmoi add' | Add targets to the source state. | | czcd | 'chezmoi cd' | cd's into the chezmoi source directory | diff --git a/themes/edsonarios/edsonarios.theme.sh b/themes/edsonarios/edsonarios.theme.sh index b29bfc128..93ff2452b 100644 --- a/themes/edsonarios/edsonarios.theme.sh +++ b/themes/edsonarios/edsonarios.theme.sh @@ -12,24 +12,24 @@ OMB_THEME_EDSONARIOS_STATUS_BAD="${_omb_prompt_bold_brown}❯_${_omb_prompt_norm OMB_THEME_EDSONARIOS_STATUS_OK="${_omb_prompt_bold_green}❯_${_omb_prompt_normal} " function _omb_theme_PROMPT_COMMAND { - if (($? == 0)); then - local ret_status=${OMB_THEME_EDSONARIOS_STATUS_OK-} - else - local ret_status=${OMB_THEME_EDSONARIOS_STATUS_BAD-} - fi - - # If the current directory is the same as HOME, will just show "~/". If not, - # show the complete route unlike \w. - if [[ $PWD == "$HOME" ]]; then - local directory='\W/' - else - local directory="$PWD/" - fi - - local python_venv - _omb_prompt_get_python_venv - - PS1="\n⚡ \t $_omb_prompt_bold_teal${directory}$_omb_prompt_bold_purple$python_venv$_omb_prompt_bold_green$(__git_ps1 " (%s)") \n${ret_status}" + if (($? == 0)); then + local ret_status=${OMB_THEME_EDSONARIOS_STATUS_OK-} + else + local ret_status=${OMB_THEME_EDSONARIOS_STATUS_BAD-} + fi + + # If the current directory is the same as HOME, will just show "~/". If not, + # show the complete route unlike \w. + if [[ $PWD == "$HOME" ]]; then + local directory='\W/' + else + local directory="$PWD/" + fi + + local python_venv + _omb_prompt_get_python_venv + + PS1="\n⚡ \t $_omb_prompt_bold_teal${directory}$_omb_prompt_bold_purple$python_venv$_omb_prompt_bold_green$(__git_ps1 " (%s)") \n${ret_status}" } _omb_util_add_prompt_command _omb_theme_PROMPT_COMMAND diff --git a/themes/powerbash10k/README.md b/themes/powerbash10k/README.md index 6550c9990..727cd5c2a 100644 --- a/themes/powerbash10k/README.md +++ b/themes/powerbash10k/README.md @@ -1,31 +1,33 @@ # powerbash10k theme -Simple colorful terminal prompt theme (inspired by powerlevel10k zsh theme, based on Brainy theme). +Simple colorful terminal prompt theme (inspired by powerlevel10k zsh theme, +based on Brainy theme). ## Features ### Prompt Segments -- Username & Hostname -- Current Directory -- SCM Information -- Battery Charge -- Clock -- Command duration -- [Todo.txt](https://github.com/ginatrapani/todo.txt-cli) status -- Ruby Environment -- Python Environment -- Exit Code +- Username & Hostname +- Current Directory +- SCM Information +- Battery Charge +- Clock +- Command duration +- [Todo.txt](https://github.com/ginatrapani/todo.txt-cli) status +- Ruby Environment +- Python Environment +- Exit Code ### Others -- Indicator for cached `sudo` credential -- Indicator for ssh login -- `pb10k` command for showing/hiding various prompt segments on-the-fly +- Indicator for cached `sudo` credential +- Indicator for ssh login +- `pb10k` command for showing/hiding various prompt segments on-the-fly ## Configuration -Various prompt segments can be shown/hidden or modified according to your choice. There are two ways for doing that: +Various prompt segments can be shown/hidden or modified according to your +choice. There are two ways for doing that: 1. On-the-fly using `pb10k` command 2. Theme Environment Variables @@ -40,17 +42,21 @@ This theme provides a command for showing/hiding prompt segments. Tab-completion for this command is enabled by default. -Configuration specified by this command will only be applied to current and subsequent child shells. +Configuration specified by this command will only be applied to current and +subsequent child shells. ### Theme Environment Variables -This is used for permanent settings that apply to all terminal sessions. You have to define the value of specific theme variables in your `bashrc` (or equivalent) file. +This is used for permanent settings that apply to all terminal sessions. You +have to define the value of specific theme variables in your `bashrc` (or +equivalent) file. The name of the variables are listed below along with their default values. #### User Information -Indicator for cached `sudo` credential (see `sudo` manpage for more information): +Indicator for cached `sudo` credential (see `sudo` manpage for more +information): `THEME_SHOW_SUDO=true` @@ -107,19 +113,20 @@ Exit code of the last command: Currently available prompt segments are: -- battery -- char -- clock -- cmd_duration -- dir -- exitcode -- python -- ruby -- scm -- todo -- user_info - -Three environment variables can be defined to rearrange the segments order. The default values are: +- battery +- char +- clock +- cmd_duration +- dir +- exitcode +- python +- ruby +- scm +- todo +- user_info + +Three environment variables can be defined to rearrange the segments order. The +default values are: `__PB10K_TOP_LEFT="dir scm"` diff --git a/themes/powerbash10k/powerbash10k.theme.sh b/themes/powerbash10k/powerbash10k.theme.sh index 6c78d23c9..f733c7259 100644 --- a/themes/powerbash10k/powerbash10k.theme.sh +++ b/themes/powerbash10k/powerbash10k.theme.sh @@ -5,310 +5,312 @@ # Helpers function __pb10k_remove_empty_elements { - local origin_array new_array element trimmed - IFS="|" read -ra origin_array <<< "$1" - new_array=() - for element in "${origin_array[@]}"; do - trimmed="${element#"${element%%[![:space:]]*}"}" - trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" - if [ "$trimmed" != "" ]; then - new_array+=("$element") - fi - done - echo "${new_array[@]}" + local origin_array new_array element trimmed + IFS="|" read -ra origin_array <<< "$1" + new_array=() + for element in "${origin_array[@]}"; do + trimmed="${element#"${element%%[![:space:]]*}"}" + trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" + if [ "$trimmed" != "" ]; then + new_array+=("$element") + fi + done + echo "${new_array[@]}" } function __pb10k_format_duration { - local duration seconds minutes hours - duration=$1 - seconds=$((duration % 60)) - minutes=$((duration / 60 % 60)) - hours=$((duration / 3600)) - - if ((hours > 0)); then - echo "${hours}h ${minutes}m ${seconds}s" - elif ((minutes > 0)); then - echo "${minutes}m ${seconds}s" - else - echo "${seconds}s" - fi + local duration seconds minutes hours + duration=$1 + seconds=$((duration % 60)) + minutes=$((duration / 60 % 60)) + hours=$((duration / 3600)) + + if ((hours > 0)); then + echo "${hours}h ${minutes}m ${seconds}s" + elif ((minutes > 0)); then + echo "${minutes}m ${seconds}s" + else + echo "${seconds}s" + fi } # Last command duration function __pb10k_timer_start { - __pb10k_timer=${__pb10k_timer:-$SECONDS} + __pb10k_timer=${__pb10k_timer:-$SECONDS} } function __pb10k_timer_stop { - __pb10k_timer_show=$((SECONDS - __pb10k_timer)) - unset __pb10k_timer + __pb10k_timer_show=$((SECONDS - __pb10k_timer)) + unset __pb10k_timer } trap '__pb10k_timer_start' DEBUG # Parsers function __pb10k_top_left_parse { - local args - IFS="|" read -ra args <<< "$1" - if [ "${args[3]}" != "" ]; then - __TOP_LEFT+="${args[2]}${args[3]}" - fi - __TOP_LEFT+="${args[0]}${args[1]}" - if [ "${args[4]}" != "" ]; then - __TOP_LEFT+="${args[2]}${args[4]}" - fi - __TOP_LEFT+=" " + local args + IFS="|" read -ra args <<< "$1" + if [ "${args[3]}" != "" ]; then + __TOP_LEFT+="${args[2]}${args[3]}" + fi + __TOP_LEFT+="${args[0]}${args[1]}" + if [ "${args[4]}" != "" ]; then + __TOP_LEFT+="${args[2]}${args[4]}" + fi + __TOP_LEFT+=" " } function __pb10k_top_right_parse { - local args - IFS="|" read -ra args <<< "$1" - __TOP_RIGHT+=" " - if [ "${args[3]}" != "" ]; then - __TOP_RIGHT+="${args[2]}${args[3]}" - fi - __TOP_RIGHT+="${args[0]}${args[1]}" - if [ "${args[4]}" != "" ]; then - __TOP_RIGHT+="${args[2]}${args[4]}" - fi - __TOP_RIGHT_LEN=$(( __TOP_RIGHT_LEN + ${#args[1]} + ${#args[3]} + ${#args[4]} + 1 )) - (( __SEG_AT_RIGHT += 1 )) + local args + IFS="|" read -ra args <<< "$1" + __TOP_RIGHT+=" " + if [ "${args[3]}" != "" ]; then + __TOP_RIGHT+="${args[2]}${args[3]}" + fi + __TOP_RIGHT+="${args[0]}${args[1]}" + if [ "${args[4]}" != "" ]; then + __TOP_RIGHT+="${args[2]}${args[4]}" + fi + __TOP_RIGHT_LEN=$((__TOP_RIGHT_LEN + ${#args[1]} + ${#args[3]} + ${#args[4]} + 1)) + ((__SEG_AT_RIGHT += 1)) } function __pb10k_bottom_parse { - local args - IFS="|" read -ra args <<< "$1" - __BOTTOM+="${args[0]}${args[1]}" - [ ${#args[1]} -gt 0 ] && __BOTTOM+=" " + local args + IFS="|" read -ra args <<< "$1" + __BOTTOM+="${args[0]}${args[1]}" + [ ${#args[1]} -gt 0 ] && __BOTTOM+=" " } function __pb10k_top { - local seg segments info terminal_width filler_character cursor_adjust - local __TOP_LEFT="" - local __TOP_RIGHT="" - local __TOP_RIGHT_LEN=0 - local __SEG_AT_RIGHT=0 - - IFS=" " read -ra segments <<< "$__PB10K_TOP_LEFT" - for seg in "${segments[@]}"; do - info="$(__pb10k_prompt_"$seg")" - [ "$info" != "" ] && __pb10k_top_left_parse "$info" - done - - terminal_width=$(tput cols) - filler_character="·" - __TOP_LEFT+="$_omb_prompt_black" - __TOP_LEFT+="$(for ((i=0; i<"$terminal_width"; i++)); do printf "%s" "$filler_character"; done)" - __TOP_LEFT+="\033[${terminal_width}G\033[1K\033[1A" - - IFS=" " read -ra segments <<< "$__PB10K_TOP_RIGHT" - for seg in "${segments[@]}"; do - info="$(__pb10k_prompt_"$seg")" - [ "$info" != "" ] && __pb10k_top_right_parse "$info" - done - - [ "$__TOP_RIGHT_LEN" -gt 0 ] && __TOP_RIGHT_LEN=$(( __TOP_RIGHT_LEN - 1 )) - cursor_adjust="\033[${__TOP_RIGHT_LEN}D" - __TOP_LEFT+="$cursor_adjust" - - printf "%s%s" "$__TOP_LEFT" "$__TOP_RIGHT" + local seg segments info terminal_width filler_character cursor_adjust + local __TOP_LEFT="" + local __TOP_RIGHT="" + local __TOP_RIGHT_LEN=0 + local __SEG_AT_RIGHT=0 + + IFS=" " read -ra segments <<< "$__PB10K_TOP_LEFT" + for seg in "${segments[@]}"; do + info="$(__pb10k_prompt_"$seg")" + [ "$info" != "" ] && __pb10k_top_left_parse "$info" + done + + terminal_width=$(tput cols) + filler_character="·" + __TOP_LEFT+="$_omb_prompt_black" + __TOP_LEFT+="$(for ((i = 0; i < "$terminal_width"; i++)); do printf "%s" "$filler_character"; done)" + __TOP_LEFT+="\033[${terminal_width}G\033[1K\033[1A" + + IFS=" " read -ra segments <<< "$__PB10K_TOP_RIGHT" + for seg in "${segments[@]}"; do + info="$(__pb10k_prompt_"$seg")" + [ "$info" != "" ] && __pb10k_top_right_parse "$info" + done + + [ "$__TOP_RIGHT_LEN" -gt 0 ] && __TOP_RIGHT_LEN=$((__TOP_RIGHT_LEN - 1)) + cursor_adjust="\033[${__TOP_RIGHT_LEN}D" + __TOP_LEFT+="$cursor_adjust" + + printf "%s%s" "$__TOP_LEFT" "$__TOP_RIGHT" } function __pb10k_bottom { - local seg segments info - local __BOTTOM="" - IFS=" " read -ra segments <<< "$__PB10K_BOTTOM" - for seg in "${segments[@]}"; do - info="$(__pb10k_prompt_"$seg")" - [ "$info" != "" ] && __pb10k_bottom_parse "$info" - done - printf "\n%s" "$__BOTTOM" + local seg segments info + local __BOTTOM="" + IFS=" " read -ra segments <<< "$__PB10K_BOTTOM" + for seg in "${segments[@]}"; do + info="$(__pb10k_prompt_"$seg")" + [ "$info" != "" ] && __pb10k_bottom_parse "$info" + done + printf "\n%s" "$__BOTTOM" } # Segments function __pb10k_prompt_user_info { - local color box info - color=$_omb_prompt_bold_olive - if [ "$THEME_SHOW_SUDO" == "true" ]; then - if sudo -n id -u 2>&1 | grep -q 0; then - color=$_omb_prompt_bold_brown - fi - fi - box="" - info="$USER@$(hostname | cut -d'.' -f1)" - if [ "$SSH_CLIENT" != "" ]; then - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" - elif [ "$__PB10K_PROMPT_LOCAL_USER_INFO" == "true" ]; then - printf "%s|%s" "$color" "$info" - fi + local color box info + color=$_omb_prompt_bold_olive + if [ "$THEME_SHOW_SUDO" == "true" ]; then + if sudo -n id -u 2>&1 | grep -q 0; then + color=$_omb_prompt_bold_brown + fi + fi + box="" + info="$USER@$(hostname | cut -d'.' -f1)" + if [ "$SSH_CLIENT" != "" ]; then + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" + elif [ "$__PB10K_PROMPT_LOCAL_USER_INFO" == "true" ]; then + printf "%s|%s" "$color" "$info" + fi } function __pb10k_prompt_dir { - local color box info - color=$_omb_prompt_bold_navy - box="" - info=" \w" - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" + local color box info + color=$_omb_prompt_bold_navy + box="" + info=" \w" + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" } function __pb10k_prompt_scm { - local color box info - [ "$THEME_SHOW_SCM" != "true" ] && return - color=$_omb_prompt_bold_green - scm - box="" - info="$(if [ "$SCM" == "git" ]; then echo " "; fi)" - info+="$(if [ "$SCM" != "NONE" ]; then echo " $(scm_prompt_info)"; fi)" - [ "$info" == "" ] && return - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_green" "$box" + local color box info + [ "$THEME_SHOW_SCM" != "true" ] && return + color=$_omb_prompt_bold_green + scm + box="" + info="$(if [ "$SCM" == "git" ]; then echo " "; fi)" + info+="$(if [ "$SCM" != "NONE" ]; then echo " $(scm_prompt_info)"; fi)" + [ "$info" == "" ] && return + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_green" "$box" } function __pb10k_prompt_python { - local color box info response_array venvs - [ "$THEME_SHOW_PYTHON" != "true" ] && return - color=$_omb_prompt_bold_olive - box="" - read -ra response_array <<< "$(__pb10k_remove_empty_elements "$(python_version_prompt)")" - info="${response_array[-1]}" - if [ ${#response_array[@]} -gt 1 ]; then - # Print all elements except the last one, separated by commas - venvs=$(printf "%s," "${response_array[@]:0:${#response_array[@]}-1}") - # Remove the trailing comma - venvs="${venvs%,}" - info="${info} (${venvs})" - fi - info="${info/#py-/ }" - [ ${#response_array[@]} -lt 2 ] && return - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_navy" "$box" + local color box info response_array venvs + [ "$THEME_SHOW_PYTHON" != "true" ] && return + color=$_omb_prompt_bold_olive + box="" + read -ra response_array <<< "$(__pb10k_remove_empty_elements "$(python_version_prompt)")" + info="${response_array[-1]}" + if [ ${#response_array[@]} -gt 1 ]; then + # Print all elements except the last one, separated by commas + venvs=$(printf "%s," "${response_array[@]:0:${#response_array[@]}-1}") + # Remove the trailing comma + venvs="${venvs%,}" + info="${info} (${venvs})" + fi + info="${info/#py-/ }" + [ ${#response_array[@]} -lt 2 ] && return + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_navy" "$box" } function __pb10k_prompt_ruby { - local color box info - [ "$THEME_SHOW_RUBY" != "true" ] && return - color=$_omb_prompt_bold_white - box="[|]" - info="rb-$(_omb_prompt_print_ruby_env)" - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_brown" "$box" + local color box info + [ "$THEME_SHOW_RUBY" != "true" ] && return + color=$_omb_prompt_bold_white + box="[|]" + info="rb-$(_omb_prompt_print_ruby_env)" + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_brown" "$box" } function __pb10k_prompt_todo { - local color box info - [ "$THEME_SHOW_TODO" != "true" ] && return - _omb_util_binary_exists todo.sh || return - color=$_omb_prompt_bold_white - box="[|]" - info="t:$(todo.sh ls | command grep -E "TODO: [0-9]+ of ([0-9]+)" | awk '{ print $4 }' )" - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_green" "$box" + local color box info + [ "$THEME_SHOW_TODO" != "true" ] && return + _omb_util_binary_exists todo.sh || return + color=$_omb_prompt_bold_white + box="[|]" + info="t:$(todo.sh ls | command grep -E "TODO: [0-9]+ of ([0-9]+)" | awk '{ print $4 }')" + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_green" "$box" } function __pb10k_prompt_clock { - local color box info - [ "$THEME_SHOW_CLOCK" != "true" ] && return - color=$THEME_CLOCK_COLOR - box="" - info=" $(date +"$THEME_CLOCK_FORMAT")" - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_purple" "$box" + local color box info + [ "$THEME_SHOW_CLOCK" != "true" ] && return + color=$THEME_CLOCK_COLOR + box="" + info=" $(date +"$THEME_CLOCK_FORMAT")" + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_purple" "$box" } function __pb10k_prompt_battery { - local color box info - [ ! -e "$OSH/plugins/battery/battery.plugin.sh" ] || - [ "$THEME_SHOW_BATTERY" != "true" ] && return - info=$(battery_percentage) - color=$_omb_prompt_bold_green - if [ "$info" -lt 50 ]; then - color=$_omb_prompt_bold_olive - elif [ "$info" -lt 25 ]; then - color=$_omb_prompt_bold_brown - fi - box="[|]" - ac_adapter_connected && info+="+" - [ "$info" == "100+" ] && info="AC" - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" + local color box info + [ ! -e "$OSH/plugins/battery/battery.plugin.sh" ] \ + || [ "$THEME_SHOW_BATTERY" != "true" ] && return + info=$(battery_percentage) + color=$_omb_prompt_bold_green + if [ "$info" -lt 50 ]; then + color=$_omb_prompt_bold_olive + elif [ "$info" -lt 25 ]; then + color=$_omb_prompt_bold_brown + fi + box="[|]" + ac_adapter_connected && info+="+" + [ "$info" == "100+" ] && info="AC" + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" } function __pb10k_prompt_exitcode { - local color - [ "$THEME_SHOW_EXITCODE" != "true" ] && return - color=$_omb_prompt_bold_red - [ "$exitcode" -ne 0 ] && printf "%s|%s" "$color" "✘ ${exitcode}" + local color + [ "$THEME_SHOW_EXITCODE" != "true" ] && return + color=$_omb_prompt_bold_red + [ "$exitcode" -ne 0 ] && printf "%s|%s" "$color" "✘ ${exitcode}" } function __pb10k_prompt_char { - local color - color=$(if [ "$exitcode" -ne 0 ]; then - echo "$_omb_prompt_bold_red" - else - echo "$_omb_prompt_bold_green" - fi) - printf "%s|%s" "$color" "$__PB10K_PROMPT_CHAR_PS1" + local color + color=$(if [ "$exitcode" -ne 0 ]; then + echo "$_omb_prompt_bold_red" + else + echo "$_omb_prompt_bold_green" + fi) + printf "%s|%s" "$color" "$__PB10K_PROMPT_CHAR_PS1" } function __pb10k_prompt_cmd_duration { - local color box info - color=$_omb_prompt_bold_navy - box="" - info="$(if [ "$__pb10k_timer_show" -gt 0 ]; then - printf " %s" "$(__pb10k_format_duration "$__pb10k_timer_show")" - fi)" - [ "$info" == "" ] && return - printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" + local color box info + color=$_omb_prompt_bold_navy + box="" + info="$(if [ "$__pb10k_timer_show" -gt 0 ]; then + printf " %s" "$(__pb10k_format_duration "$__pb10k_timer_show")" + fi)" + [ "$info" == "" ] && return + printf "%s|%s|%s|%s" "$color" "$info" "$_omb_prompt_bold_white" "$box" } # cli function __pb10k_show { - local seg - seg="${1:-}" - shift - export THEME_SHOW_"$seg"=true + local seg + seg="${1:-}" + shift + export THEME_SHOW_"$seg"=true } function __pb10k_hide { - local seg - seg="${1:-}" - shift - export THEME_SHOW_"$seg"=false + local seg + seg="${1:-}" + shift + export THEME_SHOW_"$seg"=false } function __pb10k_completion { - local cur action actions segments - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - action="${COMP_WORDS[1]}" - actions="show hide" - segments="battery clock exitcode python ruby scm sudo todo" - case "$action" in - show) - COMPREPLY=( "$(compgen -W "$segments" -- "$cur")" ) - return 0 - ;; - hide) - COMPREPLY=( "$(compgen -W "$segments" -- "$cur")" ) - return 0 - ;; - esac - - COMPREPLY=( "$(compgen -W "$actions" -- "$cur")" ) - return 0 + local cur action actions segments + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + action="${COMP_WORDS[1]}" + actions="show hide" + segments="battery clock exitcode python ruby scm sudo todo" + case "$action" in + show) + COMPREPLY=("$(compgen -W "$segments" -- "$cur")") + return 0 + ;; + hide) + COMPREPLY=("$(compgen -W "$segments" -- "$cur")") + return 0 + ;; + esac + + COMPREPLY=("$(compgen -W "$actions" -- "$cur")") + return 0 } function pb10k { - local action segments seg - action="${1:-}" - shift - segments="${*:-}" - typeset func - case "$action" in - show) - func=__pb10k_show;; - hide) - func=__pb10k_hide;; - esac - IFS=" " read -ra segments <<< "$segments" - for seg in "${segments[@]}"; do - seg=$(printf "%s" "$seg" | tr '[:lower:]' '[:upper:]') - "$func" "$seg" - done + local action segments seg + action="${1:-}" + shift + segments="${*:-}" + typeset func + case "$action" in + show) + func=__pb10k_show + ;; + hide) + func=__pb10k_hide + ;; + esac + IFS=" " read -ra segments <<< "$segments" + for seg in "${segments[@]}"; do + seg=$(printf "%s" "$seg" | tr '[:lower:]' '[:upper:]') + "$func" "$seg" + done } complete -F __pb10k_completion pb10k @@ -349,19 +351,19 @@ __PB10K_PROMPT_LOCAL_USER_INFO=${__PB10K_PROMPT_LOCAL_USER_INFO:-"true"} # Prompt function __pb10k_ps1 { - printf "%s%s%s" "$(__pb10k_top)" "$(__pb10k_bottom)" "$_omb_prompt_normal" + printf "%s%s%s" "$(__pb10k_top)" "$(__pb10k_bottom)" "$_omb_prompt_normal" } function __pb10k_ps2 { - local color - color=$_omb_prompt_bold_white - printf "%s%s%s" "$color" "${__PB10K_PROMPT_CHAR_PS2} " "$_omb_prompt_normal" + local color + color=$_omb_prompt_bold_white + printf "%s%s%s" "$color" "${__PB10K_PROMPT_CHAR_PS2} " "$_omb_prompt_normal" } function _omb_theme_PROMPT_COMMAND { - local exitcode="$?" - PS1="$(__pb10k_ps1)" - PS2="$(__pb10k_ps2)" + local exitcode="$?" + PS1="$(__pb10k_ps1)" + PS2="$(__pb10k_ps2)" } _omb_util_add_prompt_command __pb10k_timer_stop