clean code

This commit is contained in:
Johann Dreo 2023-07-09 21:54:11 +02:00
commit f9cecd0864

View file

@ -2,8 +2,14 @@
GITCRUX_ARROW_RIGHT=("" "") GITCRUX_ARROW_RIGHT=("" "")
GITCRUX_ARROW_LEFT=("" "") GITCRUX_ARROW_LEFT=("" "")
# bg fg # fg bg bold
GITCRUX_COLOR_REMOTE=(220 0) 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. # Center the given $1 text with spaces to fit a string of length $2.
_gitcrux_centered() { _gitcrux_centered() {
@ -40,6 +46,7 @@ _gitcrux_centered() {
_gitcrux_arrow() { _gitcrux_arrow() {
local in start end color from to text color_body color_ends local in start end color from to text color_body color_ends
gitcrux_arrow=""
in="$1" in="$1"
start="$2" start="$2"
@ -69,14 +76,14 @@ _gitcrux_arrow() {
gitcrux_arrow="${in:0:from}${color_ends}${start}${color_body}${gitcrux_centered}${color_ends}${end}${NO_COL}${in:to:${#in}}" gitcrux_arrow="${in:0:from}${color_ends}${start}${color_body}${gitcrux_centered}${color_ends}${end}${NO_COL}${in:to:${#in}}"
} }
_gitcrux_VCS() { _gitcrux_VCS_header() {
GITCRUX_COL_WIDTH=0
GITCRUX_VCS="" GITCRUX_VCS=""
local head_remote head_repo head_stage head_branch local head_remote= head_repo= head_stage= head_branch=
local has_lines
# REMOTE # REMOTE
head_remote="origin" # TODO head_remote="remote" # TODO
# REPO # REPO
head_repo="${_GITCRUX_HEAD_REPO}" head_repo="${_GITCRUX_HEAD_REPO}"
@ -94,8 +101,8 @@ _gitcrux_VCS() {
# STAGE # STAGE
LP_TIME_ANALOG=1 LP_TIME_ANALOG=1
_lp_analog_time _lp_analog_time
# head_stage="$lp_analog_time" head_stage="$lp_analog_time"
head_stage="STAGE" # head_stage="STAGE"
local ret has_lines= local ret has_lines=
if _lp_vcs_uncommitted_files; then if _lp_vcs_uncommitted_files; then
@ -141,7 +148,7 @@ _gitcrux_VCS() {
fi fi
if _lp_vcs_untracked_files; then if _lp_vcs_untracked_files; then
head_branch+="(${LP_COLOR_CHANGES}${lp_vcs_untracked_files}${NO_COL})" head_branch+="(${LP_COLOR_CHANGES}${lp_vcs_untracked_files}${NO_COL})"
fi fi
# STASH # STASH
@ -151,9 +158,8 @@ _gitcrux_VCS() {
head_stash+="(${LP_COLOR_COMMITS}${lp_vcs_stash_count})" head_stash+="(${LP_COLOR_COMMITS}${lp_vcs_stash_count})"
fi fi
# COLUMNS
# ARROWS if [[ "$GITCRUX_COL_DYNAMIC" ]]; then
if [[ "$GITCRUX_COL_WIDTH" -eq "0" ]]; then
for col in "${head_remote}" "${head_repo}" "${head_stage}" "${head_branch}" "${head_stash}"; do for col in "${head_remote}" "${head_repo}" "${head_stage}" "${head_branch}" "${head_stash}"; do
if (( ${#col} > GITCRUX_COL_WIDTH )); then if (( ${#col} > GITCRUX_COL_WIDTH )); then
__lp_strip_escapes "${col}" __lp_strip_escapes "${col}"
@ -164,18 +170,22 @@ _gitcrux_VCS() {
# At least some spacing. # At least some spacing.
GITCRUX_COL_WIDTH=$((GITCRUX_COL_WIDTH+1)) GITCRUX_COL_WIDTH=$((GITCRUX_COL_WIDTH+1))
header="" gitcrux_VCS_header=""
line="" line=""
for col in "${head_remote}" "${head_repo}" "${head_stage}" "${head_branch}" "${head_stash}"; do for col in "${head_remote}" "${head_repo}" "${head_stage}" "${head_branch}" "${head_stash}"; do
_gitcrux_centered "│" $GITCRUX_COL_WIDTH _gitcrux_centered "│" $GITCRUX_COL_WIDTH
line+="$gitcrux_centered" line+="$gitcrux_centered"
_gitcrux_centered "${col}" $GITCRUX_COL_WIDTH _gitcrux_centered "${col}" $GITCRUX_COL_WIDTH
header+="$gitcrux_centered" gitcrux_VCS_header+="$gitcrux_centered"
done done
}
_gitcrux_VCS() {
GITCRUX_VCS+="${header}${n}" _gitcrux_VCS_header
GITCRUX_VCS+="${gitcrux_VCS_header}${n}"
# GITCRUX_VCS+="${line}${n}" # GITCRUX_VCS+="${line}${n}"
# Available states: # Available states:
@ -193,14 +203,16 @@ _gitcrux_VCS() {
# - $lp_vcs_stash_count # - $lp_vcs_stash_count
if [[ "$lp_vcs_type" != "git" ]]; then if [[ "$lp_vcs_type" != "git" ]]; then
GITCRUX_VCS+="[Unsupported VCS, cannot provide hints.]${n}" _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 else # VCS is git
if [[ "$lp_vcs_head_status" ]]; then if [[ "$lp_vcs_head_status" ]]; then
if [[ "$lp_vcs_head_status" == *"REBASE"* ]]; then if [[ "$lp_vcs_head_status" == *"REBASE"* ]]; then
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add" _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add"
GITCRUX_VCS+="${gitcrux_arrow}${n}" GITCRUX_VCS+="${gitcrux_arrow}${n}"
else # Unknown head status. else # Unknown head status.
GITCRUX_VCS+="[Unsupported head status, cannot provide hints.]${n}" _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 fi
else # No specific head status. else # No specific head status.
if [[ "$lp_vcs_commit_behind" ]]; then if [[ "$lp_vcs_commit_behind" ]]; then
@ -226,7 +238,7 @@ _gitcrux_VCS() {
fi fi
fi fi
else # No commit behind. else # No commit behind.
if [[ "$has_lines" ]]; then if [[ "$has_lines" || "$lp_vcs_untracked_files" ]]; then
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add" _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 46 1" 3 4 "add"
GITCRUX_VCS+="${gitcrux_arrow}${n}" GITCRUX_VCS+="${gitcrux_arrow}${n}"
@ -241,41 +253,50 @@ _gitcrux_VCS() {
_gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 220 1" 1 2 "push" _gitcrux_arrow "$line" ${GITCRUX_ARROW_LEFT[@]} "0 220 1" 1 2 "push"
GITCRUX_VCS+="${gitcrux_arrow}${n}" GITCRUX_VCS+="${gitcrux_arrow}${n}"
else # No commit ahead. else # No commit ahead.
_gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 45 1" 2 4 "branch" if [[ "$head_branch" == *"master"* || "$head_branch" == *"main"* ]]; then
GITCRUX_VCS+="${gitcrux_arrow}${n}" _gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 45 1" 2 4 "branch"
fi GITCRUX_VCS+="${gitcrux_arrow}${n}"
fi fi # Main branch.
fi fi # Commits ahead.
fi fi # Stash.
fi fi # Has lines or untracked files.
fi fi # Commits behind.
fi # Head status.
# _gitcrux_arrow "$line" ${GITCRUX_ARROW_RIGHT[@]} "0 220 1" 1 4 "pull" fi # VCS is git.
# 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}"
} }
# 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, # This function is called when the prompt is activated,
# either at the very beginning of the shell session, # either at the very beginning of the shell session,