351 lines
12 KiB
Text
351 lines
12 KiB
Text
|
|
GITCRUX_ARROW_RIGHT=("" "")
|
|
GITCRUX_ARROW_LEFT=("" "")
|
|
|
|
# fg bg bold
|
|
GITCRUX_COLOR_WEAK="0 195 0"
|
|
GITCRUX_COLOR_NORMAL="0 39 1"
|
|
GITCRUX_COLOR_STRONG="0 220 1"
|
|
GITCRUX_COLOR_MSG="0 244 0"
|
|
|
|
GITCRUX_COL_WIDTH=10
|
|
GITCRUX_COL_DYNAMIC=1
|
|
|
|
# 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"
|
|
|
|
__lp_strip_escapes "$text"
|
|
raw_text="$ret"
|
|
|
|
if (( ${#raw_text} > width )); then
|
|
# gitcrux_centered="${text:0:width-1}…"
|
|
gitcrux_centered="$text"
|
|
return 2
|
|
elif (( ${#raw_text} == width )); then
|
|
gitcrux_centered="$text"
|
|
else
|
|
span=$((width - ${#raw_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
|
|
gitcrux_arrow=""
|
|
|
|
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_header() {
|
|
GITCRUX_VCS=""
|
|
|
|
local head_remote= head_repo= head_stage= head_branch=
|
|
local has_lines
|
|
|
|
# REMOTE
|
|
head_remote="remote" # TODO
|
|
|
|
# REPO
|
|
head_repo="${_GITCRUX_HEAD_REPO}"
|
|
|
|
local has_commit=
|
|
if _lp_vcs_commits_off_remote; then
|
|
if [[ "$lp_vcs_commit_behind" -ne "0" ]]; then
|
|
head_remote+="(${LP_COLOR_COMMITS_BEHIND}$lp_vcs_commit_behind${NO_COL})"
|
|
fi
|
|
if [[ "$lp_vcs_commit_ahead" -ne "0" ]]; then
|
|
head_repo+="(${LP_COLOR_COMMITS}$lp_vcs_commit_ahead${NO_COL})"
|
|
fi
|
|
fi
|
|
|
|
# STAGE
|
|
LP_TIME_ANALOG=1
|
|
_lp_analog_time
|
|
head_stage="$lp_analog_time"
|
|
# head_stage="STAGE"
|
|
|
|
local ret has_lines=
|
|
if _lp_vcs_uncommitted_files; then
|
|
_lp_vcs_unstaged_lines; ret=$?
|
|
# Only show unstaged changes if the VCS supports staging, otherwise
|
|
# show uncommitted changes
|
|
if (( ret == 0 )); then
|
|
has_lines="+$lp_vcs_unstaged_i_lines/-$lp_vcs_unstaged_d_lines"
|
|
elif (( ret == 1 )); then
|
|
has_lines="+0/-0"
|
|
else
|
|
_lp_vcs_uncommitted_lines
|
|
has_lines="+$lp_vcs_uncommitted_i_lines/-$lp_vcs_uncommitted_d_lines"
|
|
fi
|
|
fi
|
|
if [[ -n "$has_lines" ]]; then
|
|
head_stage="${LP_COLOR_DIFF}${has_lines}${NO_COL}"
|
|
fi
|
|
|
|
# BRANCH
|
|
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
|
|
|
|
if _lp_vcs_head_status; then
|
|
# NOTE: Replace the branch name.
|
|
head_branch="[${LP_COLOR_CHANGES}${lp_vcs_head_status}"
|
|
if [[ -n "${lp_vcs_head_details-}" ]]; then
|
|
head_branch+=":${lp_vcs_head_details}"
|
|
fi
|
|
head_branch+="${NO_COL}]"
|
|
fi
|
|
|
|
if _lp_vcs_untracked_files; then
|
|
head_branch+="(${LP_COLOR_CHANGES}${lp_vcs_untracked_files}${NO_COL})"
|
|
fi
|
|
|
|
# STASH
|
|
_GITCRUX_HEAD_STASH="stash"
|
|
head_stash="${_GITCRUX_HEAD_STASH}"
|
|
if _lp_vcs_stash_count; then
|
|
head_stash+="(${LP_COLOR_COMMITS}${lp_vcs_stash_count})"
|
|
fi
|
|
|
|
# COLUMNS
|
|
if [[ "$GITCRUX_COL_DYNAMIC" ]]; then
|
|
for col in "${head_remote}" "${head_repo}" "${head_stage}" "${head_branch}" "${head_stash}"; do
|
|
if (( ${#col} > GITCRUX_COL_WIDTH )); then
|
|
__lp_strip_escapes "${col}"
|
|
GITCRUX_COL_WIDTH="${#ret}"
|
|
fi
|
|
done
|
|
fi
|
|
# At least some spacing.
|
|
GITCRUX_COL_WIDTH=$((GITCRUX_COL_WIDTH+1))
|
|
|
|
gitcrux_VCS_header=""
|
|
line=""
|
|
for col in "${head_remote}" "${head_repo}" "${head_stage}" "${head_branch}" "${head_stash}"; do
|
|
_gitcrux_centered "│" $GITCRUX_COL_WIDTH
|
|
line+="$gitcrux_centered"
|
|
|
|
_gitcrux_centered "${col}" $GITCRUX_COL_WIDTH
|
|
gitcrux_VCS_header+="$gitcrux_centered"
|
|
done
|
|
}
|
|
|
|
_gitcrux_VCS() {
|
|
|
|
_gitcrux_VCS_header
|
|
|
|
GITCRUX_VCS+="${gitcrux_VCS_header}${n}"
|
|
# GITCRUX_VCS+="${line}${n}"
|
|
|
|
# Available states:
|
|
# - $lp_vcs_commit_behind
|
|
# - $lp_vcs_commit_ahead
|
|
# - $has_lines:
|
|
# - $lp_vcs_unstaged_i_lines
|
|
# - $lp_vcs_unstaged_d_lines
|
|
# OR (depending on VCS)
|
|
# - $lp_vcs_uncommitted_i_lines
|
|
# - $lp_vcs_uncommitted_d_lines
|
|
# - $lp_vcs_head_status
|
|
# - $lp_vcs_head_details
|
|
# - $lp_vcs_untracked_files
|
|
# - $lp_vcs_stash_count
|
|
|
|
if [[ "$lp_vcs_type" != "git" ]]; then
|
|
_gitcrux_arrow "$line" "${GITCRUX_ARROW_RIGHT[0]}" "${GITCRUX_ARROW_LEFT[1]}" "${GITCRUX_COLOR_MSG}" 1 5 "Unsupported VCS, cannot provide hints"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
else # VCS is git
|
|
if [[ "$lp_vcs_head_status" ]]; then
|
|
if [[ "$lp_vcs_head_status" == *"REBASE"* ]]; then
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
else # Unknown head status.
|
|
_gitcrux_arrow "$line" "${GITCRUX_ARROW_RIGHT[0]}" "${GITCRUX_ARROW_LEFT[1]}" "${GITCRUX_COLOR_MSG}" 1 5 "Unsupported head status, cannot provide hints"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
fi
|
|
else # No specific head status.
|
|
if [[ "$lp_vcs_commit_behind" ]]; then
|
|
if [[ "$has_lines" ]]; then
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 183 1" 4 5 "save"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 220 1" 1 4 "pull"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 183 1" 4 5 "pop"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
else # Do not have diff.
|
|
if [[ "$lp_vcs_stash_count" ]]; then
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 220 1" 1 4 "pull"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 183 1" 4 5 "pop"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
else # No stash.
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 220 1" 1 4 "pull"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
fi
|
|
fi
|
|
else # No commit behind.
|
|
if [[ "$has_lines" || "$lp_vcs_untracked_files" ]]; then
|
|
_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}"
|
|
else # Do not have diff.
|
|
if [[ "$lp_vcs_stash_count" ]]; then
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 183 1" 4 5 "pop"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
else # Do not have stash.
|
|
if [[ "$lp_vcs_commit_ahead" ]]; then
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 220 1" 1 2 "push"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
else # No commit ahead.
|
|
if [[ "$head_branch" == *"master"* || "$head_branch" == *"main"* ]]; then
|
|
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 45 1" 2 4 "branch"
|
|
GITCRUX_VCS+="${gitcrux_arrow}${n}"
|
|
fi # Main branch.
|
|
fi # Commits ahead.
|
|
fi # Stash.
|
|
fi # Has lines or untracked files.
|
|
fi # Commits behind.
|
|
fi # Head status.
|
|
fi # VCS is git.
|
|
}
|
|
|
|
# Print the whole chart of hints.
|
|
# gitcrux() {
|
|
# _gitcrux_VCS_header
|
|
# ret="${gitcrux_VCS_header}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 220 1" 1 4 "pull"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 45 1" 2 4 "branch"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 183 1" 4 5 "save"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 202 1" 2 3 "commit"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 183 1" 4 5 "pop"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 208 1" 2 4 "merge master"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 220 1" 1 2 "push"
|
|
# ret+="${gitcrux_arrow}${n}"
|
|
|
|
# printf '%s' "$ret"
|
|
# }
|
|
|
|
|
|
# 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+="${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
|
|
}
|
|
|
|
|