lp-gitcrux/gitcrux.theme
2023-07-07 21:22:21 +02:00

192 lines
5.6 KiB
Text

GITCRUX_ARROW_RIGHT=("" "")
GITCRUX_ARROW_LEFT=("" "")
# bg fg
GITCRUX_COLOR_REMOTE=(220 0)
# Center the given $1 text with spaces to fit a string of length $2.
_gitcrux_centered() {
local text width padl padr span diff i
text="$1"
width="$2"
if (( ${#text} > width )); then
gitcrux_centered="${text:0:width-1}…"
elif (( ${#text} == width )); then
gitcrux_centered="$text"
else
span=$((width - ${#text}))
diff=$(( span/2 ))
padl=""
padr=""
for (( i=0; i < diff; i++ )) ; do
padl+=" "
padr+=" "
done
if (( span % 2 > 0 )); then
padr+=" "
fi
gitcrux_centered="${padl}${text}${padr}"
fi
}
_gitcrux_arrow() {
local in start end color from to text color_body color_ends
in="$1"
start="$2"
end="$3"
color="$4"
from=$(($5 * GITCRUX_COL_WIDTH - GITCRUX_COL_WIDTH/2))
to=$(($6 * GITCRUX_COL_WIDTH - GITCRUX_COL_WIDTH/2 - 1))
text="$7"
local lp_terminal_format
lp_terminal_format ${color}
color_body="$lp_terminal_format"
lp_terminal_format -3 -1
color_ends="$lp_terminal_format"
span=$((to-from-${#start}-${#end}))
if (( from > ${#in} || to > ${#in} )); then
return 2
else
if (( ${#text} > span )); then
return 3
fi
fi
_gitcrux_centered "$text" $span
gitcrux_arrow="${in:0:from}${color_ends}${start}${color_body}${gitcrux_centered}${color_ends}${end}${NO_COL}${in:to:${#in}}"
}
_gitcrux_VCS() {
GITCRUX_COL_WIDTH=11
GITCRUX_VCS="${n}"
local head_remote head_stage head_branch
head_remote="origin" # TODO
LP_TIME_ANALOG=1
_lp_analog_time
head_stage="$lp_analog_time"
if _lp_vcs_branch; then
head_branch="$lp_vcs_branch"
if _lp_vcs_bookmark; then
head_branch+=": $lp_vcs_bookmark"
fi
elif _lp_vcs_bookmark; then
head_branch="$lp_vcs_bookmark"
elif _lp_vcs_tag; then
head_branch="tag: $lp_vcs_tag"
else
_lp_vcs_commit_id
head_branch="${lp_vcs_commit_id:0:7}"
fi
_GITCRUX_HEAD_STASH="stash"
header=""
line=""
for col in "${head_remote}" "${_GITCRUX_HEAD_REPO}" "${head_stage}" "${head_branch}" "${_GITCRUX_HEAD_STASH}"; do
_gitcrux_centered "│" $GITCRUX_COL_WIDTH
line+="$gitcrux_centered"
if ((${#col}+1 > GITCRUX_COL_WIDTH)); then
header+="${col:0:GITCRUX_COL_WIDTH-1}…"
elif ((${#col}+1 == GITCRUX_COL_WIDTH)); then
header+="${col}"
else
_gitcrux_centered "${col}" $GITCRUX_COL_WIDTH
header+="$gitcrux_centered"
fi
done
GITCRUX_VCS+="${header}${n}"
GITCRUX_VCS+="${line}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 220 1" 1 4 "pull"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 45 1" 2 4 "branch"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 183 1" 4 5 "save"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 202 1" 2 3 "commit"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 183 1" 4 5 "pop"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 208 1" 2 4 "merge master"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 220 1" 1 2 "push"
GITCRUX_VCS+="${gitcrux_arrow}${n}"
}
# This function is called when the prompt is activated,
# either at the very beginning of the shell session,
# either when the user call `lp_theme`.
_lp_gitcrux_theme_activate() {
_lp_default_theme_activate
}
# This function is called everytime the user change their directory.
# It should set up any data that does not change when the user stays in the same directory.
_lp_gitcrux_theme_directory() {
_lp_default_theme_directory
_GITCRUX_HEAD_REPO="${PWD##*/}"
}
# This function is called every time the prompt is displayed,
# that is, between commands.
_lp_gitcrux_theme_prompt() {
_lp_default_theme_prompt_data
local n=$'\n'
if [[ -z "${LP_PS1-}" ]]; then
# Add user-defined prefix, battery, load, temperature, wifi and jobs.
PS1="${LP_PS1_PREFIX}${LP_BATT}${LP_LOAD}${LP_TEMP}${LP_WIFI}${LP_JOBS}"
# Add multiplexer brackets, user, host, permissions colon, working directory, dirstack, proxy, watched environment variables and nested shell level.
PS1+="${LP_BRACKET_OPEN}${LP_USER}${LP_HOST}${LP_PERM}${LP_PWD}${LP_DIRSTACK}${LP_BRACKET_CLOSE}${LP_PROXY}${LP_ENVVARS}${LP_SHLVL}"
# Add the list of development environments/config/etc.
PS1+="${LP_DEV_ENV}"
# Add last runtime, return code & meaning.
PS1+="${LP_RUNTIME}${LP_ERR}${LP_ERR_MEANING}"
# Add VCS infos
# If root, the info has not been collected unless LP_ENABLE_VCS_ROOT
# is set.
if _lp_find_vcs; then
_gitcrux_VCS
PS1+="${n}${GITCRUX_VCS}"
fi
# prompt mark and user-defined postfix
PS1+="${n}${LP_MARK_PREFIX}${LP_COLOR_MARK}${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
}