mirror of
				https://gitlab.com/ceda_ei/wish
				synced 2025-11-04 09:30:04 +01:00 
			
		
		
		
	[Feature] Add multiline support for right prompt
WISH_RPL (i.e. Right Prompt Lengths), WISH_RIGHT_PS1 are now arrays. WISH_RPLINE tracks the current line. wish_append_right is called by wish_append to add to WISH_RIGHT_PS1. If wish_append is called with \n as text, WISH_RPLINE is incremented. (Current limitation: \n can't be part of any other string passed to wish_append). Right prompt is now printed using wish_print_right_prompt which iterates over WISH_RPL and WISH_RIGHT_PS1. Right prompt size is also calculated in wish_append_right now.
This commit is contained in:
		
							
								
								
									
										42
									
								
								wish.sh
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								wish.sh
									
									
									
									
									
								
							@@ -1,5 +1,14 @@
 | 
				
			|||||||
#!/usr/bin/env bash
 | 
					#!/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() {
 | 
					function wish_init() {
 | 
				
			||||||
	# Source all plugins
 | 
						# Source all plugins
 | 
				
			||||||
	local plugin
 | 
						local plugin
 | 
				
			||||||
@@ -60,6 +69,21 @@ function color_to_escape_code() {
 | 
				
			|||||||
	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"
 | 
				
			||||||
 | 
						if [[ $text == "\n" ]]; then
 | 
				
			||||||
 | 
							((WISH_RPLINE++))
 | 
				
			||||||
 | 
							WISH_RIGHT_PS1=("${WISH_RIGHT_PS1[@]}" "")
 | 
				
			||||||
 | 
							WISH_RPL=(${WISH_RPL[@]} 0)
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							WISH_RIGHT_PS1[$WISH_RPLINE]="${WISH_RIGHT_PS1[$WISH_RPLINE]}$colors$text"
 | 
				
			||||||
 | 
							WISH_RPL[$WISH_RPLINE]=$((${WISH_RPL[$WISH_RPLINE]} + ${#text}))
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Usage: wish_append bg fg text
 | 
					# Usage: wish_append bg fg text
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# Parameters:
 | 
					# Parameters:
 | 
				
			||||||
@@ -75,16 +99,13 @@ function wish_append() {
 | 
				
			|||||||
	local fg=$(color_to_escape_code 3 $fg_code)
 | 
						local fg=$(color_to_escape_code 3 $fg_code)
 | 
				
			||||||
	local bg=$(color_to_escape_code 4 $bg_code)
 | 
						local bg=$(color_to_escape_code 4 $bg_code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if [[ $WISH_STATE == 1 ]]; then
 | 
					 | 
				
			||||||
		WISH_RPL=$((WISH_RPL + ${#text}))
 | 
					 | 
				
			||||||
	fi
 | 
					 | 
				
			||||||
	if [[ $fg_code == -1 ]]; then
 | 
						if [[ $fg_code == -1 ]]; then
 | 
				
			||||||
		case $WISH_STATE in
 | 
							case $WISH_STATE in
 | 
				
			||||||
			0)
 | 
								0)
 | 
				
			||||||
				WISH_LEFT_PS1="$WISH_LEFT_PS1$fg${bg}$text"
 | 
									WISH_LEFT_PS1="$WISH_LEFT_PS1$fg${bg}$text"
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			1)
 | 
								1)
 | 
				
			||||||
				WISH_RIGHT_PS1="$WISH_RIGHT_PS1$fg${bg}$text"
 | 
									wish_append_right "$fg$bg" "$text"
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
		esac
 | 
							esac
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
@@ -93,7 +114,7 @@ function wish_append() {
 | 
				
			|||||||
				WISH_LEFT_PS1="$WISH_LEFT_PS1$bg$fg$text"
 | 
									WISH_LEFT_PS1="$WISH_LEFT_PS1$bg$fg$text"
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
			1)
 | 
								1)
 | 
				
			||||||
				WISH_RIGHT_PS1="$WISH_RIGHT_PS1$bg$fg$text"
 | 
									wish_append_right "$bg$fg" "$text"
 | 
				
			||||||
				;;
 | 
									;;
 | 
				
			||||||
		esac
 | 
							esac
 | 
				
			||||||
	fi
 | 
						fi
 | 
				
			||||||
@@ -106,7 +127,9 @@ function wish_main() {
 | 
				
			|||||||
	local IFS=$' \n'
 | 
						local IFS=$' \n'
 | 
				
			||||||
	PS1=""
 | 
						PS1=""
 | 
				
			||||||
	WISH_LEFT_PS1=""
 | 
						WISH_LEFT_PS1=""
 | 
				
			||||||
	WISH_RIGHT_PS1=""
 | 
						WISH_RIGHT_PS1=("")
 | 
				
			||||||
 | 
						WISH_RPL=(0)
 | 
				
			||||||
 | 
						WISH_RPLINE=0
 | 
				
			||||||
	local i
 | 
						local i
 | 
				
			||||||
	# Set newline
 | 
						# Set newline
 | 
				
			||||||
	if [[ $WISH_AUTONEWLINE != 0 ]]; then
 | 
						if [[ $WISH_AUTONEWLINE != 0 ]]; then
 | 
				
			||||||
@@ -139,7 +162,6 @@ function wish_main() {
 | 
				
			|||||||
	done
 | 
						done
 | 
				
			||||||
	# Generate Right prompt.
 | 
						# Generate Right prompt.
 | 
				
			||||||
	WISH_STATE=1
 | 
						WISH_STATE=1
 | 
				
			||||||
	WISH_RPL=0
 | 
					 | 
				
			||||||
	for i in $(seq 0 $((${#WISH_RIGHT_PLUGINS[@]} - 1))); do
 | 
						for i in $(seq 0 $((${#WISH_RIGHT_PLUGINS[@]} - 1))); do
 | 
				
			||||||
		if [[ -v WISH_POWERLINE ]] && [[ $WISH_POWERLINE != 0 ]]; then
 | 
							if [[ -v WISH_POWERLINE ]] && [[ $WISH_POWERLINE != 0 ]]; then
 | 
				
			||||||
			if wish_${WISH_RIGHT_PLUGINS[$i]}_end $prev; then
 | 
								if wish_${WISH_RIGHT_PLUGINS[$i]}_end $prev; then
 | 
				
			||||||
@@ -158,7 +180,11 @@ function wish_main() {
 | 
				
			|||||||
		fi
 | 
							fi
 | 
				
			||||||
		wish_${WISH_RIGHT_PLUGINS[$i]}_main $prev
 | 
							wish_${WISH_RIGHT_PLUGINS[$i]}_main $prev
 | 
				
			||||||
	done
 | 
						done
 | 
				
			||||||
	PS1=$PS1"\[\e7\e[$(($COLUMNS - $WISH_RPL + 1))G$WISH_RIGHT_PS1\e8\]$WISH_LEFT_PS1\[\033[0;5;0m\]"
 | 
						# 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
 | 
					wish_init
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user