diff --git a/plugins/path_powerline.sh b/plugins/path_powerline.sh index 0198577..bd4430b 100644 --- a/plugins/path_powerline.sh +++ b/plugins/path_powerline.sh @@ -10,10 +10,45 @@ function wish_path_powerline_set_colors() { WISH_PATH_POWERLINE_FG=${WISH_PATH_POWERLINE_FG:-$WISH_DEFAULT_FG} WISH_PATH_POWERLINE_BG=${WISH_PATH_POWERLINE_BG:-$WISH_DEFAULT_BG} WISH_PATH_POWERLINE_NO_WRITE_SUFFIX=${WISH_PATH_POWERLINE_NO_WRITE_SUFFIX:- } + WISH_PATH_POWERLINE_MAX_PERC=${WISH_PATH_POWERLINE_MAX_PERC:-0} +} + +function wish_path_lensum() { + sum=0 + for i in $*; do + sum=$(($sum + ${#i})) + done + echo $sum +} + +function wish_path_powerline_shrink() { + local IFS='/' + local path=( $1 ) + local max=$2 + for ((i=0; i <$((${#path[@]} - 1)); i++)); do + path[$i]=${path[$i]:0:1} + if [[ $(wish_path_lensum ${path[@]}) -lt $max ]]; then + local short_path="" + for i in ${path[@]}; do + short_path+="$i/" + done + echo "${short_path%/}" + return + fi + done + local short_path="" + for i in ${path[@]}; do + short_path+="$i/" + done + echo "${short_path%/}" } function wish_path_powerline_main() { local path="${PWD/#$HOME/\~}" + if [[ $WISH_PATH_POWERLINE_MAX_PERC -ne 0 && ${#path} -ge $WISH_PATH_POWERLINE_MAX_LEN ]] ; then + local max=$(($WISH_PATH_POWERLINE_MAX_PERC * $(wish_remaining_chars) / 100)) + local path=$(wish_path_powerline_shrink "$path" "$max") + fi local path="${path//\//  }" if [[ -w $PWD ]]; then local path=" $path " diff --git a/themes/gradient.sh b/themes/gradient.sh index b6d9e67..14dea7b 100644 --- a/themes/gradient.sh +++ b/themes/gradient.sh @@ -5,7 +5,19 @@ local gradient=(226 118 37 66 60 237 233) local fg_gradient=(16 16 16 16 16 15 15) local j=0 for i in ${WISH_PLUGINS[@]}; do - [[ $i == "newline" ]] && j=0 && continue + [[ $i =~ newline$ ]] && j=0 && continue + eval WISH_${i^^}_BG=${gradient[$j]} + eval WISH_${i^^}_FG=${fg_gradient[$j]} + ((j++)) + if [[ $j -eq ${#gradient[@]} ]]; then + j=0 + fi +done + +j=0 +for ((idx=$((${#WISH_RIGHT_PLUGINS[@]} - 1)); idx >= 0; idx--)); do + i=${WISH_RIGHT_PLUGINS[$idx]} + [[ $i =~ newline$ ]] && j=0 && continue eval WISH_${i^^}_BG=${gradient[$j]} eval WISH_${i^^}_FG=${fg_gradient[$j]} ((j++)) diff --git a/themes/lowfi-neon.sh b/themes/lowfi-neon.sh index 5041ea0..a6aabd6 100644 --- a/themes/lowfi-neon.sh +++ b/themes/lowfi-neon.sh @@ -5,7 +5,19 @@ local i local gradient=(e7c547 c0e551 82e35a 62e177 6bdfb3 73d4dd 7aa6da) local j=0 for i in ${WISH_PLUGINS[@]}; do - [[ $i == "newline" ]] && j=0 && continue + [[ $i =~ newline$ ]] && j=0 && continue + eval WISH_${i^^}_BG=-1 + eval WISH_${i^^}_FG=${gradient[$j]} + ((j++)) + if [[ $j -eq ${#gradient[@]} ]]; then + j=0 + fi +done + +j=0 +for ((idx=$((${#WISH_RIGHT_PLUGINS[@]} - 1)); idx >= 0; idx--)); do + i=${WISH_RIGHT_PLUGINS[$idx]} + [[ $i =~ newline$ ]] && j=0 && continue eval WISH_${i^^}_BG=-1 eval WISH_${i^^}_FG=${gradient[$j]} ((j++)) diff --git a/themes/plain.sh b/themes/plain.sh index e89e2d1..ffef741 100644 --- a/themes/plain.sh +++ b/themes/plain.sh @@ -1,7 +1,7 @@ WISH_DEFAULT_BG=-1 WISH_DEFAULT_FG=-1 WISH_POWERLINE=0 -for i in ${WISH_PLUGINS[@]}; do +for i in ${WISH_PLUGINS[@]} ${WISH_RIGHT_PLUGINS[@]}; do eval WISH_${i^^}_BG=-1 eval WISH_${i^^}_FG=-1 done diff --git a/themes/rgb_gradient.sh b/themes/rgb_gradient.sh index cb54307..ffadde2 100644 --- a/themes/rgb_gradient.sh +++ b/themes/rgb_gradient.sh @@ -5,7 +5,19 @@ local gradient=(ffff5f 7ad767 66b097 5e7388 534d61 3a3338 121212) local fg_gradient=(000000 000000 000000 000000 000000 ffffff ffffff) local j=0 for i in ${WISH_PLUGINS[@]}; do - [[ $i == "newline" ]] && j=0 && continue + [[ $i =~ newline$ ]] && j=0 && continue + eval WISH_${i^^}_BG=${gradient[$j]} + eval WISH_${i^^}_FG=${fg_gradient[$j]} + ((j++)) + if [[ $j -eq ${#gradient[@]} ]]; then + j=0 + fi +done + +j=0 +for ((idx=$((${#WISH_RIGHT_PLUGINS[@]} - 1)); idx >= 0; idx--)); do + i=${WISH_RIGHT_PLUGINS[$idx]} + [[ $i =~ newline$ ]] && j=0 && continue eval WISH_${i^^}_BG=${gradient[$j]} eval WISH_${i^^}_FG=${fg_gradient[$j]} ((j++)) diff --git a/wish.sh b/wish.sh index e2c435c..8be88b3 100644 --- a/wish.sh +++ b/wish.sh @@ -1,10 +1,19 @@ #!/usr/bin/env bash +# INTERNAL USE ONLY! Do not use this in plugins. +function wish_print_right_prompt() { + local idx=0 + for i in ${WISH_RPL[@]}; do + echo "\e[$(($COLUMNS - $i + 1))G${WISH_RIGHT_PS1[$idx]}" + ((idx++)) + done +} + function wish_init() { # Source all plugins local plugin local path - for plugin in ${WISH_PLUGINS[@]}; do + for plugin in ${WISH_PLUGINS[@]} ${WISH_RIGHT_PLUGINS[@]}; do for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do source "$path/wish/plugins/$plugin.sh" &> /dev/null && break done @@ -30,7 +39,7 @@ function wish_init() { done # Call plugins to set colors - for plugin in ${WISH_PLUGINS[@]}; do + for plugin in ${WISH_PLUGINS[@]} ${WISH_RIGHT_PLUGINS[@]}; do eval wish_$(echo $plugin)_set_colors $prev done } @@ -59,7 +68,41 @@ function color_to_escape_code() { fi fi } +# INTERNAL USE ONLY! Do not use this in plugins. +# Usage: wish_append_left escape_codes text +function wish_append_left() { + local text="$2" + local colors="$1" + local prompt_text="${text@P}" + if [[ $text == "\n" ]]; then + ((WISH_LPLINE++)) + WISH_LPL=(${WISH_LPL[@]} 0) + WISH_LEFT_PS1="$WISH_LEFT_PS1$colors$text" + else + if [[ $((${WISH_LPL[$WISH_LPLINE]} + ${#prompt_text})) -lt $COLUMNS ]]; then + WISH_LEFT_PS1="$WISH_LEFT_PS1$colors$text" + WISH_LPL[$WISH_LPLINE]=$((${WISH_LPL[$WISH_LPLINE]} + ${#prompt_text})) + fi + fi +} +# INTERNAL USE ONLY! Do not use this in plugins. +# Usage: wish_append_right escape_codes text +function wish_append_right() { + local text="$2" + local colors="$1" + local prompt_text="${text@P}" + if [[ $text == "\n" ]]; then + ((WISH_RPLINE++)) + WISH_RIGHT_PS1=("${WISH_RIGHT_PS1[@]}" "") + WISH_RPL=(${WISH_RPL[@]} 0) + elif [[ $((${WISH_LPL[$WISH_RPLINE]} + ${WISH_RPL[$WISH_RPLINE]} + ${#prompt_text})) -lt $COLUMNS ]]; then + WISH_RIGHT_PS1[$WISH_RPLINE]="${WISH_RIGHT_PS1[$WISH_RPLINE]}$colors$text" + WISH_RPL[$WISH_RPLINE]=$((${WISH_RPL[$WISH_RPLINE]} + ${#prompt_text})) + fi +} + +# Public API # Usage: wish_append bg fg text # # Parameters: @@ -76,23 +119,60 @@ function wish_append() { local bg=$(color_to_escape_code 4 $bg_code) if [[ $fg_code == -1 ]]; then - PS1="$PS1$fg${bg}$text" + case $WISH_STATE in + 0) + wish_append_left "$fg$bg" "$text" + ;; + 1) + wish_append_right "$fg$bg" "$text" + ;; + esac else - PS1="$PS1$bg${fg}$text" + case $WISH_STATE in + 0) + wish_append_left "$bg$fg" "$text" + ;; + 1) + wish_append_right "$bg$fg" "$text" + ;; + esac fi } +# Public API +# Usage: wish_remaining_chars +# Parameters: None +# Return Value: Capture stdout to get the remaining characters available in the +# line. +wish_remaining_chars() { + if [[ $WISH_STATE -eq 0 ]]; then + echo "$(( $COLUMNS - ${WISH_LPL[$WISH_LPLINE]} ))" + else + echo "$(( $COLUMNS - ${WISH_LPL[$WISH_RPLINE]} - ${WISH_RPL[$WISH_RPLINE]} ))" + fi +} function wish_main() { local prev=$? + # Set local IFS to avoid being affected by shell's IFS + local IFS=$' \n' PS1="" + WISH_LEFT_PS1="" + WISH_RIGHT_PS1=("") + WISH_LPL=(0) + WISH_RPL=(0) + WISH_RPLINE=0 + WISH_LPLINE=0 local i + # Set newline if [[ $WISH_AUTONEWLINE != 0 ]]; then echo -ne "\033[6n" ; read -s -d ';'; read -s -d R WISH_CURSOR_POSITION if [[ $WISH_CURSOR_POSITION != "1" ]]; then PS1="\n" fi fi + # Generate left prompt. + WISH_STATE=0 # 0 = left prompt, 1 = right prompt for i in $(seq 0 $((${#WISH_PLUGINS[@]} - 1))); do wish_${WISH_PLUGINS[i]}_main $prev if [[ -v WISH_POWERLINE ]] && [[ $WISH_POWERLINE != 0 ]]; then @@ -112,11 +192,32 @@ function wish_main() { fi fi fi - - if [[ $i -eq $((${#WISH_PLUGINS[@]} - 1)) ]]; then - PS1="$PS1\[\033[0;5;0m\]" - fi done + # Generate Right prompt. + WISH_STATE=1 + for i in $(seq 0 $((${#WISH_RIGHT_PLUGINS[@]} - 1))); do + if [[ -v WISH_POWERLINE ]] && [[ $WISH_POWERLINE != 0 ]]; then + if wish_${WISH_RIGHT_PLUGINS[$i]}_end $prev; then + if [[ $i == 0 ]]; then + local plugin=${WISH_RIGHT_PLUGINS[$i]} + local fg_name="WISH_${plugin^^}_BG" + wish_append -1 ${!fg_name}  + elif wish_${WISH_RIGHT_PLUGINS[$(($i - 1))]}_start $prev; then + local plugin=${WISH_RIGHT_PLUGINS[$i]} + local prev_plugin=${WISH_RIGHT_PLUGINS[$(($i-1))]} + local fg_name="WISH_${plugin^^}_BG" + local bg_name="WISH_${prev_plugin^^}_BG" + wish_append ${!bg_name} ${!fg_name}  + fi + fi + fi + wish_${WISH_RIGHT_PLUGINS[$i]}_main $prev + done + # Save cursor position, print right prompt, restore cursor position, + # print left prompt, reset terminal + PS1=$PS1"\[\e7" + PS1="$PS1$(wish_print_right_prompt)" + PS1="$PS1\e8\]$WISH_LEFT_PS1\[\033[0;5;0m\]" } wish_init