2019-02-15 15:02:32 +01:00
|
|
|
#!/usr/bin/env bash
|
2021-09-22 09:55:12 +02:00
|
|
|
# shellcheck disable=SC1090
|
2019-02-15 15:02:32 +01:00
|
|
|
|
2019-12-24 15:22:15 +01:00
|
|
|
# INTERNAL USE ONLY! Do not use this in plugins.
|
|
|
|
function wish_print_right_prompt() {
|
|
|
|
local idx=0
|
2021-09-22 09:37:31 +02:00
|
|
|
for i in "${WISH_RPL[@]}"; do
|
|
|
|
echo "\e[$((COLUMNS - i + 1))G${WISH_RIGHT_PS1[idx]}"
|
2019-12-24 15:22:15 +01:00
|
|
|
((idx++))
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-02-15 15:27:48 +01:00
|
|
|
function wish_init() {
|
2019-12-31 07:25:23 +01:00
|
|
|
# Find default config file if WISH_CONFIG_FILE is unset
|
|
|
|
if [[ ! -v WISH_CONFIG_FILE ]]; then
|
|
|
|
for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do
|
|
|
|
if [[ -f "$path/wish/config.gie" ]]; then
|
|
|
|
WISH_CONFIG_FILE="$path/wish/config.gie"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
# Source config files
|
|
|
|
for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do
|
|
|
|
if [[ -f "$path/wish/wish.py" ]]; then
|
2021-09-24 16:41:30 +02:00
|
|
|
source <("$path/wish/wish.py" "${WISH_CONFIG_FILE[@]}")
|
2019-12-31 07:25:23 +01:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2021-09-22 09:55:12 +02:00
|
|
|
|
|
|
|
# Set defaults for core if not found
|
|
|
|
: "${WISH_POWERLINE_LEFT:=}"
|
|
|
|
: "${WISH_POWERLINE_RIGHT=}"
|
2019-02-15 15:27:48 +01:00
|
|
|
# Source all plugins
|
2019-12-31 09:02:02 +01:00
|
|
|
# If WISH_CONFIG_FILE is not set, then assume that the user hasn't updated
|
|
|
|
# to a config file yet. Set WISH_PLUGINS_SOURCE=WISH_PLUGINS.
|
|
|
|
if [[ ! -v WISH_CONFIG_FILE ]]; then
|
|
|
|
WISH_PLUGINS_SOURCE=("${WISH_PLUGINS[@]}" "${WISH_RIGHT_PLUGINS[@]}")
|
|
|
|
fi
|
2019-09-06 15:51:53 +02:00
|
|
|
local plugin
|
|
|
|
local path
|
2021-09-22 09:37:31 +02:00
|
|
|
for plugin in "${WISH_PLUGINS_SOURCE[@]}"; do
|
|
|
|
if ! for path in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do
|
2019-09-06 15:51:53 +02:00
|
|
|
source "$path/wish/plugins/$plugin.sh" &> /dev/null && break
|
2021-09-22 09:37:31 +02:00
|
|
|
done; then
|
|
|
|
echo "Plugin $plugin not found." >&2
|
|
|
|
fi
|
2019-02-15 15:02:32 +01:00
|
|
|
done
|
|
|
|
|
2019-02-15 15:27:48 +01:00
|
|
|
# Source theme
|
|
|
|
WISH_THEME=${WISH_THEME:-plain}
|
2019-09-06 15:51:53 +02:00
|
|
|
while :; do
|
2021-09-24 16:41:30 +02:00
|
|
|
if for theme in "$XDG_CONFIG_HOME" "/usr/share" "$HOME/.config"; do
|
2019-09-06 15:51:53 +02:00
|
|
|
source "$theme/wish/themes/$WISH_THEME.sh" &> /dev/null && break
|
2021-09-24 16:41:30 +02:00
|
|
|
done; then
|
2019-09-06 15:51:53 +02:00
|
|
|
break
|
|
|
|
else
|
|
|
|
echo "Theme $WISH_THEME not found. Using theme plain." >&2
|
|
|
|
if [[ $WISH_THEME == "plain" ]]; then
|
|
|
|
break
|
|
|
|
else
|
|
|
|
WISH_THEME=plain
|
|
|
|
fi
|
|
|
|
fi
|
2019-02-15 15:27:48 +01:00
|
|
|
done
|
|
|
|
|
2019-02-16 07:59:33 +01:00
|
|
|
# Call plugins to set colors
|
2021-09-22 09:37:31 +02:00
|
|
|
for plugin in "${WISH_PLUGINS[@]}" "${WISH_RIGHT_PLUGINS[@]}"; do
|
2021-09-24 16:41:30 +02:00
|
|
|
"wish_${plugin}_set_colors" "$prev"
|
2019-02-16 07:59:33 +01:00
|
|
|
done
|
2019-02-15 15:27:48 +01:00
|
|
|
}
|
2019-02-15 15:02:32 +01:00
|
|
|
|
2019-09-06 17:05:29 +02:00
|
|
|
# Usage: color_to_escape_code [3|4] color
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# - [3|4]: Use 3 if escape code is for foreground, 4 for background
|
|
|
|
# - color: -1 to reset, 0-255 for terminal color codes. 6 digit hexadecimal
|
|
|
|
# value for true color.
|
|
|
|
#
|
|
|
|
# Return value: Prints escape code that sets the fg/bg as requested.
|
|
|
|
function color_to_escape_code() {
|
|
|
|
local choice=$1
|
|
|
|
local color=$2
|
|
|
|
if [[ $color == -1 ]]; then
|
|
|
|
echo "\[\033[0;5;0m\]"
|
2019-02-16 09:00:42 +01:00
|
|
|
else
|
2023-12-07 13:49:31 +01:00
|
|
|
if [[ ${#color} -eq 6 ]]; then
|
2019-09-06 17:05:29 +02:00
|
|
|
local r=$(( 16#${color:0:2} ))
|
|
|
|
local g=$(( 16#${color:2:2} ))
|
|
|
|
local b=$(( 16#${color:4:2} ))
|
|
|
|
echo "\[\033[${choice}8;2;$r;$g;${b}m\]"
|
2019-02-16 13:05:58 +01:00
|
|
|
else
|
2023-12-07 13:49:31 +01:00
|
|
|
echo "\[\033[${choice}8;5;${color}m\]"
|
2019-02-16 13:05:58 +01:00
|
|
|
fi
|
2019-02-16 09:00:42 +01:00
|
|
|
fi
|
2019-09-06 17:05:29 +02:00
|
|
|
}
|
2019-12-24 15:51:42 +01:00
|
|
|
# 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"
|
2019-12-25 15:52:49 +01:00
|
|
|
local prompt_text="${text@P}"
|
2019-12-24 15:51:42 +01:00
|
|
|
if [[ $text == "\n" ]]; then
|
|
|
|
((WISH_LPLINE++))
|
2021-09-24 16:41:30 +02:00
|
|
|
WISH_LPL=("${WISH_LPL[@]}" 0)
|
2019-12-24 16:19:52 +01:00
|
|
|
WISH_LEFT_PS1="$WISH_LEFT_PS1$colors$text"
|
2019-12-24 15:51:42 +01:00
|
|
|
else
|
2019-12-25 15:52:49 +01:00
|
|
|
if [[ $((${WISH_LPL[$WISH_LPLINE]} + ${#prompt_text})) -lt $COLUMNS ]]; then
|
2019-12-24 16:19:52 +01:00
|
|
|
WISH_LEFT_PS1="$WISH_LEFT_PS1$colors$text"
|
2019-12-25 15:52:49 +01:00
|
|
|
WISH_LPL[$WISH_LPLINE]=$((${WISH_LPL[$WISH_LPLINE]} + ${#prompt_text}))
|
2019-12-24 16:19:52 +01:00
|
|
|
fi
|
2019-12-24 15:51:42 +01:00
|
|
|
fi
|
|
|
|
}
|
2019-09-06 17:05:29 +02:00
|
|
|
|
2019-12-24 15:22:15 +01:00
|
|
|
# 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"
|
2019-12-25 15:52:49 +01:00
|
|
|
local prompt_text="${text@P}"
|
2019-12-24 15:22:15 +01:00
|
|
|
if [[ $text == "\n" ]]; then
|
|
|
|
((WISH_RPLINE++))
|
|
|
|
WISH_RIGHT_PS1=("${WISH_RIGHT_PS1[@]}" "")
|
2021-09-24 16:41:30 +02:00
|
|
|
WISH_RPL=("${WISH_RPL[@]}" 0)
|
2019-12-25 15:52:49 +01:00
|
|
|
elif [[ $((${WISH_LPL[$WISH_RPLINE]} + ${WISH_RPL[$WISH_RPLINE]} + ${#prompt_text})) -lt $COLUMNS ]]; then
|
2019-12-24 15:22:15 +01:00
|
|
|
WISH_RIGHT_PS1[$WISH_RPLINE]="${WISH_RIGHT_PS1[$WISH_RPLINE]}$colors$text"
|
2019-12-25 15:52:49 +01:00
|
|
|
WISH_RPL[$WISH_RPLINE]=$((${WISH_RPL[$WISH_RPLINE]} + ${#prompt_text}))
|
2019-12-24 15:22:15 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:16:52 +01:00
|
|
|
# Public API
|
2019-09-06 17:05:29 +02:00
|
|
|
# Usage: wish_append bg fg text
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# - fg, bg: -1 to reset, 0-255 for terminal color codes. 6 digit hexadecimal
|
|
|
|
# value for true color.
|
|
|
|
# - text: Text of the plugin
|
|
|
|
#
|
|
|
|
# Return value: None
|
|
|
|
function wish_append() {
|
|
|
|
local bg_code=$1
|
|
|
|
local fg_code=$2
|
|
|
|
local text=$3
|
2021-09-24 16:41:30 +02:00
|
|
|
local fg
|
|
|
|
local bg
|
|
|
|
fg=$(color_to_escape_code 3 "$fg_code")
|
|
|
|
bg=$(color_to_escape_code 4 "$bg_code")
|
2019-02-16 13:43:11 +01:00
|
|
|
|
|
|
|
if [[ $fg_code == -1 ]]; then
|
2019-12-15 17:16:52 +01:00
|
|
|
case $WISH_STATE in
|
|
|
|
0)
|
2019-12-24 15:51:42 +01:00
|
|
|
wish_append_left "$fg$bg" "$text"
|
2019-12-15 17:16:52 +01:00
|
|
|
;;
|
|
|
|
1)
|
2019-12-24 15:22:15 +01:00
|
|
|
wish_append_right "$fg$bg" "$text"
|
2019-12-15 17:16:52 +01:00
|
|
|
;;
|
|
|
|
esac
|
2019-02-16 09:00:42 +01:00
|
|
|
else
|
2019-12-15 17:16:52 +01:00
|
|
|
case $WISH_STATE in
|
|
|
|
0)
|
2019-12-24 15:51:42 +01:00
|
|
|
wish_append_left "$bg$fg" "$text"
|
2019-12-15 17:16:52 +01:00
|
|
|
;;
|
|
|
|
1)
|
2019-12-24 15:22:15 +01:00
|
|
|
wish_append_right "$bg$fg" "$text"
|
2019-12-15 17:16:52 +01:00
|
|
|
;;
|
|
|
|
esac
|
2019-02-16 09:00:42 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:16:52 +01:00
|
|
|
# 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
|
2021-09-24 16:41:30 +02:00
|
|
|
echo "$(( COLUMNS - ${WISH_LPL[$WISH_LPLINE]} ))"
|
2019-12-24 18:16:52 +01:00
|
|
|
else
|
2021-09-24 16:41:30 +02:00
|
|
|
echo "$(( COLUMNS - ${WISH_LPL[$WISH_RPLINE]} - ${WISH_RPL[$WISH_RPLINE]} ))"
|
2019-12-24 18:16:52 +01:00
|
|
|
fi
|
|
|
|
}
|
2019-02-15 19:46:17 +01:00
|
|
|
|
|
|
|
function wish_main() {
|
|
|
|
local prev=$?
|
2019-12-15 17:16:52 +01:00
|
|
|
# Set local IFS to avoid being affected by shell's IFS
|
|
|
|
local IFS=$' \n'
|
2019-02-15 19:46:17 +01:00
|
|
|
PS1=""
|
2019-12-15 17:16:52 +01:00
|
|
|
WISH_LEFT_PS1=""
|
2019-12-24 15:22:15 +01:00
|
|
|
WISH_RIGHT_PS1=("")
|
2019-12-24 15:51:42 +01:00
|
|
|
WISH_LPL=(0)
|
2019-12-24 15:22:15 +01:00
|
|
|
WISH_RPL=(0)
|
|
|
|
WISH_RPLINE=0
|
2019-12-24 15:51:42 +01:00
|
|
|
WISH_LPLINE=0
|
2019-02-15 19:46:17 +01:00
|
|
|
local i
|
2019-12-15 17:16:52 +01:00
|
|
|
# Set newline
|
2021-09-22 09:37:31 +02:00
|
|
|
if [[ $WISH_AUTO_NEWLINE != 0 ]]; then
|
2021-09-24 16:41:30 +02:00
|
|
|
echo -ne "\033[6n"
|
|
|
|
read -rs -d ';'
|
|
|
|
# shellcheck disable=SC2034
|
|
|
|
read -rs -d R WISH_CURSOR_POSITION
|
2019-06-10 13:18:40 +02:00
|
|
|
if [[ $WISH_CURSOR_POSITION != "1" ]]; then
|
|
|
|
PS1="\n"
|
|
|
|
fi
|
|
|
|
fi
|
2019-12-15 17:16:52 +01:00
|
|
|
# Generate left prompt.
|
|
|
|
WISH_STATE=0 # 0 = left prompt, 1 = right prompt
|
2019-02-15 19:46:17 +01:00
|
|
|
for i in $(seq 0 $((${#WISH_PLUGINS[@]} - 1))); do
|
2021-09-24 16:41:30 +02:00
|
|
|
"wish_${WISH_PLUGINS[i]}_main" "$prev"
|
2019-02-16 06:34:56 +01:00
|
|
|
if [[ -v WISH_POWERLINE ]] && [[ $WISH_POWERLINE != 0 ]]; then
|
2021-09-24 16:41:30 +02:00
|
|
|
if "wish_${WISH_PLUGINS[$i]}_end" $prev; then
|
2019-02-16 06:34:56 +01:00
|
|
|
if [[ $i -lt $((${#WISH_PLUGINS[@]} - 1)) ]]; then
|
2021-09-24 16:41:30 +02:00
|
|
|
if "wish_${WISH_PLUGINS[$((i + 1))]}_start" "$prev"; then
|
2019-02-16 06:34:56 +01:00
|
|
|
local plugin=${WISH_PLUGINS[$i]}
|
2021-09-24 16:41:30 +02:00
|
|
|
local next_plugin=${WISH_PLUGINS[$((i + 1))]}
|
2019-09-06 18:54:35 +02:00
|
|
|
local fg_name="WISH_${plugin^^}_BG"
|
|
|
|
local bg_name="WISH_${next_plugin^^}_BG"
|
2021-09-24 16:41:30 +02:00
|
|
|
wish_append "${!bg_name}" "${!fg_name}" "${WISH_POWERLINE_LEFT}"
|
2019-02-16 06:34:56 +01:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
local plugin=${WISH_PLUGINS[$i]}
|
2019-09-06 18:54:35 +02:00
|
|
|
local fg_name="WISH_${plugin^^}_BG"
|
2021-09-24 16:41:30 +02:00
|
|
|
wish_append -1 "${!fg_name}" "${WISH_POWERLINE_LEFT}"
|
2019-02-16 06:34:56 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2019-12-15 17:16:52 +01:00
|
|
|
done
|
|
|
|
# Generate Right prompt.
|
|
|
|
WISH_STATE=1
|
|
|
|
for i in $(seq 0 $((${#WISH_RIGHT_PLUGINS[@]} - 1))); do
|
2019-12-22 14:15:07 +01:00
|
|
|
if [[ -v WISH_POWERLINE ]] && [[ $WISH_POWERLINE != 0 ]]; then
|
2021-09-24 16:41:30 +02:00
|
|
|
if "wish_${WISH_RIGHT_PLUGINS[$i]}_end" "$prev"; then
|
2019-12-22 14:15:07 +01:00
|
|
|
if [[ $i == 0 ]]; then
|
|
|
|
local plugin=${WISH_RIGHT_PLUGINS[$i]}
|
|
|
|
local fg_name="WISH_${plugin^^}_BG"
|
2021-09-24 16:41:30 +02:00
|
|
|
wish_append -1 "${!fg_name}" "${WISH_POWERLINE_RIGHT}"
|
|
|
|
elif "wish_${WISH_RIGHT_PLUGINS[$((i - 1))]}_start" $prev; then
|
2019-12-22 14:15:07 +01:00
|
|
|
local plugin=${WISH_RIGHT_PLUGINS[$i]}
|
2021-09-24 16:41:30 +02:00
|
|
|
local prev_plugin=${WISH_RIGHT_PLUGINS[$((i - 1))]}
|
2019-12-22 14:15:07 +01:00
|
|
|
local fg_name="WISH_${plugin^^}_BG"
|
|
|
|
local bg_name="WISH_${prev_plugin^^}_BG"
|
2021-09-24 16:41:30 +02:00
|
|
|
wish_append "${!bg_name}" "${!fg_name}" "${WISH_POWERLINE_RIGHT}"
|
2019-12-22 14:15:07 +01:00
|
|
|
fi
|
2019-12-15 17:16:52 +01:00
|
|
|
fi
|
2019-02-16 06:34:56 +01:00
|
|
|
fi
|
2021-09-24 16:41:30 +02:00
|
|
|
"wish_${WISH_RIGHT_PLUGINS[$i]}_main" "$prev"
|
2019-02-15 19:46:17 +01:00
|
|
|
done
|
2019-12-24 15:22:15 +01:00
|
|
|
# Save cursor position, print right prompt, restore cursor position,
|
|
|
|
# print left prompt, reset terminal
|
2021-09-24 16:41:30 +02:00
|
|
|
# shellcheck disable=SC2025
|
2019-12-24 15:22:15 +01:00
|
|
|
PS1=$PS1"\[\e7"
|
|
|
|
PS1="$PS1$(wish_print_right_prompt)"
|
2021-09-24 16:41:30 +02:00
|
|
|
# shellcheck disable=SC2025
|
2019-12-24 15:22:15 +01:00
|
|
|
PS1="$PS1\e8\]$WISH_LEFT_PS1\[\033[0;5;0m\]"
|
2019-02-15 19:46:17 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 15:27:48 +01:00
|
|
|
wish_init
|
2019-02-15 19:46:17 +01:00
|
|
|
PROMPT_COMMAND="wish_main; $PROMPT_COMMAND"
|