lp-explain/explain.theme
2024-12-16 09:48:06 +01:00

219 lines
6.2 KiB
Text

_lp_explain_theme_activate() {
# Disable tyographic marks without meaning.
LP_MARK_DEV_OPEN=
LP_MARK_DEV_CLOSE=
LP_MARK_DEV_MID=
LP_MARK_MODULES_OPEN=
LP_MARK_MODULES_SEP=
LP_MARK_MODULES_CLOSE=
LP_MARK_ENV_VARS_OPEN=
LP_MARK_ENV_VARS_SEP=
LP_MARK_ENV_VARS_CLOSE=
# A prompt starting on the third line looks weird,
# so we duplicate the mark and put it on a new line.
LP_MARK_PREFIX=${LP_MARK_PREFIX:-$'\n'}
# Light gray as a default.
lp_terminal_format 239 -1
LP_COLOR_EXPLAIN=${LP_COLOR_EXPLAIN:-"$lp_terminal_format"}
_lp_default_theme_activate
}
_lp_explain_theme_directory() {
_lp_default_theme_directory
}
_lp_explain_theme_prompt() {
_lp_default_theme_prompt_data
_lp_explain_theme_prompt_template
}
# Add a boxed section of the prompt, along with the corresponding variable name.
# Interpret "name" as an LP_* variable, and show lowercase names.
# E.G. `_box PWD` will show the content of $LP_PWD under the name "pwd".
_box() { # name [color]
local name="${1-}"
name_len=${#name}
# printf "$name\n"
if _lp_create_link "https://liquidprompt.readthedocs.io/en/stable/theme/default.html#LP_${name}" "${name,,}" ; then
name_link="$lp_link"
else
name_link="${name,,}"
fi
# Get LP_ variable from name.
local varname="LP_${name}"
local var=${!varname} # Get indirection.
# Trim spaces from content,
# since they are useless here
# (added by the default theme,
# that uses them as separators).
shopt -s extglob
local what="${var##+([[:space:]])}"
what="${what%%+([[:space:]])}"
# Get length from text without color/link escapes.
__lp_strip_escapes "$what"
local raw_data="$ret"
local raw_data_len=${#raw_data}
# If no color, use global var.
local color="${2:-$LP_COLOR_EXPLAIN}"
if [[ -n "$what" ]]; then
local i
box_top+="┬"
box_mid+="${color}│${NO_COL}$what"
box_bot+="┴"
__lp_strip_escapes "$box_mid"
local raw_mid="$ret"
# Fill in missing spaces, using box_mid as reference.
__lp_strip_escapes "$box_hyp"
local raw_hyp="$ret"
for (( i=${#raw_hyp}; i < ${#raw_mid}-raw_data_len; i++ )) ; do
box_hyp+=" "
done
# printf "${#box_sup} -> ${#raw_mid}\n"
__lp_strip_escapes "$box_sup"
local raw_sup="$ret"
for (( i=${#raw_sup}; i < ${#raw_mid}-raw_data_len; i++ )) ; do
box_sup+=" "
done
__lp_strip_escapes "$box_top"
local raw_top="$ret"
# Add names.
if (( name_len > raw_data_len )); then
# printf "On sup/hyp line\n"
local top_len=${#raw_top}
local cursor="${raw_sup:top_len:1}"
# printf "$raw_top\n"
# printf "${top_len}«${cursor}»\n"
if [[ "$cursor" != " " && "$cursor" != "" ]] ; then
# printf "On hyp line\n"
box_hyp+="┌$name_link"
box_top+="┴"
else
# printf "On sup line\n"
box_sup+="┌$name_link"
box_top+="┴"
fi
else
# printf "On top line\n"
box_top+="$name_link"
fi
# Fill in missing spaces, using box_mid as reference.
__lp_strip_escapes "$box_top"
local raw_top="$ret"
for (( i=${#raw_top}; i < ${#raw_mid}; i++ )) ; do
box_top+="─"
done
__lp_strip_escapes "$box_bot"
local raw_bot="$ret"
for (( i=${#raw_bot}; i < ${#raw_mid}; i++ )) ; do
box_bot+="─"
done
fi
# __lp_strip_escapes "$box_hyp"
# printf "$ret\n"
# __lp_strip_escapes "$box_sup"
# printf "$ret\n"
# __lp_strip_escapes "$box_top"
# printf "$ret\n"
# __lp_strip_escapes "$box_mid"
# printf "$ret\n"
# __lp_strip_escapes "$box_bot"
# printf "$ret\n"
}
_lp_explain_theme_prompt_template() {
if [[ -f "${LP_PS1_FILE-}" ]]; then
# shellcheck source=liquid.ps1
source "$LP_PS1_FILE"
fi
if [[ -z "${LP_PS1-}" ]]; then
# Initial global variables.
box_hyp=""
box_sup=""
box_top=""
box_mid=""
box_bot=""
# Add title escape time, battery, load, temperature, RAM, disk, wifi, jobs.
_box PS1_PREFIX
_box TIME
_box BATT
_box LOAD
_box TEMP
_box RAM
_box DISK
_box WIFI
_box JOBS
# Add multiplexer brackets, user, host, permissions colon, working directory, dirstack, proxy, watched environment variables and nested shell level.
_box BRACKET_OPEN
_box USER
_box HOST
_box PERM
_box PWD
_box DIRSTACK
_box BRACKET_CLOSE
_box PROXY
_box ENVVARS
_box SHLVL
# Add the list of development environments/config/etc.
_box DEV_ENV
# Add VCS infos
# If root, the info has not been collected unless LP_ENABLE_VCS_ROOT
# is set.
_box VCS
# Add last runtime, return code & meaning, prompt mark and user-defined postfix.
_box RUNTIME
_box ERR
_box ERR_MEANING
# prefix/mark/suffix will be shown twice.
_box MARK_PREFIX
_box MARK
_box PS1_POSTFIX
# End last segment with an arrow shape.
box_hyp+=" "
box_sup+=" "
box_top+="🮢"
box_mid+="${LP_COLOR_EXPLAIN}🮥${NO_COL}"
box_bot+="🮠"
# Show hyp/sup lines only if needed.
PS1="${LP_COLOR_EXPLAIN}"
if [[ -n ${box_hyp// } ]] ; then # If not just spaces.
PS1+="${box_hyp}\n"
fi
if [[ -n ${box_sup// } ]] ; then
PS1+="${box_sup}\n"
fi
# Assemble, and add mark+prompt on a new line.
PS1+="${box_top}${NO_COL}\n${box_mid}\n${LP_COLOR_EXPLAIN}${box_bot}${NO_COL}${LP_MARK_PREFIX}${LP_MARK}${LP_PS1_POSTFIX}"
# Get the core sections without prompt escapes and make them into a title.
_lp_formatted_title "${LP_PS1_PREFIX}${LP_BRACKET_OPEN}${LP_USER}${LP_HOST}${LP_MARK_PERM}${lp_path-}${LP_BRACKET_CLOSE}${LP_MARK_PREFIX}${LP_MARK}${LP_PS1_POSTFIX}"
else
PS1=$LP_PS1
fi
}