|
1 | | -. "$BASH_IT/themes/powerline/powerline.base.bash" |
2 | | - |
3 | | -function __powerline_last_status_prompt { |
4 | | - [[ "$1" -ne 0 ]] && echo "$(set_color ${LAST_STATUS_THEME_PROMPT_COLOR} -) ${1} ${normal}" |
5 | | -} |
6 | | - |
7 | | -function __powerline_right_segment { |
8 | | - local OLD_IFS="${IFS}" |
9 | | - IFS="|" |
10 | | - local params=($1) |
11 | | - IFS="${OLD_IFS}" |
12 | | - local padding=0 |
| 1 | +# shellcheck shell=bash |
| 2 | +# shellcheck disable=SC2034 # Expected behavior for themes. |
| 3 | +# shellcheck source-path=SCRIPTDIR/../powerline |
| 4 | +source "${BASH_IT?}/themes/powerline/powerline.base.bash" |
| 5 | + |
| 6 | +function __powerline_right_segment() { |
| 7 | + local -a params |
| 8 | + IFS="|" read -ra params <<< "${1}" |
13 | 9 | local pad_before_segment=" " |
| 10 | + local padding=0 |
14 | 11 |
|
15 | 12 | if [[ "${SEGMENTS_AT_RIGHT}" -eq 0 ]]; then |
16 | | - if [[ "${POWERLINE_COMPACT_AFTER_LAST_SEGMENT}" -ne 0 ]]; then |
| 13 | + if [[ "${POWERLINE_COMPACT_AFTER_LAST_SEGMENT:-${POWERLINE_COMPACT:-0}}" -ne 0 ]]; then |
17 | 14 | pad_before_segment="" |
18 | 15 | fi |
19 | | - RIGHT_PROMPT+="$(set_color ${params[1]} -)${POWERLINE_RIGHT_END}${normal}" |
| 16 | + RIGHT_PROMPT+="$(set_color "${params[1]:-}" -)${POWERLINE_RIGHT_LAST_SEGMENT_END_CHAR:-}${normal?}" |
20 | 17 | ((padding += 1)) |
21 | 18 | else |
22 | | - if [[ "${POWERLINE_COMPACT_BEFORE_SEPARATOR}" -ne 0 ]]; then |
| 19 | + if [[ "${POWERLINE_COMPACT_BEFORE_SEPARATOR:-}" -ne 0 ]]; then |
23 | 20 | pad_before_segment="" |
24 | 21 | fi |
25 | 22 | # Since the previous segment wasn't the last segment, add padding, if needed |
26 | 23 | # |
27 | | - if [[ "${POWERLINE_COMPACT_AFTER_SEPARATOR}" -eq 0 ]]; then |
28 | | - RIGHT_PROMPT+="$(set_color - ${LAST_SEGMENT_COLOR}) ${normal}" |
| 24 | + if [[ "${POWERLINE_COMPACT_AFTER_SEPARATOR:-0}" -eq 0 ]]; then |
| 25 | + RIGHT_PROMPT+="$(set_color - "${LAST_SEGMENT_COLOR?}") ${normal}" |
29 | 26 | ((padding += 1)) |
30 | 27 | fi |
31 | | - if [[ "${LAST_SEGMENT_COLOR}" -eq "${params[1]}" ]]; then |
32 | | - RIGHT_PROMPT+="$(set_color - ${LAST_SEGMENT_COLOR})${POWERLINE_RIGHT_SEPARATOR_SOFT}${normal}" |
| 28 | + if [[ "${LAST_SEGMENT_COLOR}" -eq "${params[1]:-}" ]]; then |
| 29 | + RIGHT_PROMPT+="$(set_color - "${LAST_SEGMENT_COLOR?}")${POWERLINE_RIGHT_SEPARATOR_SOFT- }${normal?}" |
33 | 30 | else |
34 | | - RIGHT_PROMPT+="$(set_color ${params[1]} ${LAST_SEGMENT_COLOR})${POWERLINE_RIGHT_SEPARATOR}${normal}" |
| 31 | + RIGHT_PROMPT+="$(set_color "${params[1]:-}" "${LAST_SEGMENT_COLOR?}")${POWERLINE_RIGHT_SEPARATOR- }${normal?}" |
35 | 32 | fi |
36 | 33 | ((padding += 1)) |
37 | 34 | fi |
38 | 35 |
|
39 | | - RIGHT_PROMPT+="$(set_color "${POWERLINE_PROMPT_FOREGROUND_COLOR}" ${params[1]})${pad_before_segment}${params[0]}${normal}" |
| 36 | + RIGHT_PROMPT+="$(set_color - "${params[1]:-}")${pad_before_segment}${params[0]}${normal?}" |
40 | 37 |
|
41 | 38 | ((padding += ${#pad_before_segment})) |
42 | 39 | ((padding += ${#params[0]})) |
43 | 40 |
|
44 | 41 | ((RIGHT_PROMPT_LENGTH += padding)) |
45 | | - LAST_SEGMENT_COLOR="${params[1]}" |
| 42 | + LAST_SEGMENT_COLOR="${params[1]:-}" |
46 | 43 | ((SEGMENTS_AT_RIGHT += 1)) |
47 | 44 | } |
48 | 45 |
|
49 | | -function __powerline_right_first_segment_padding { |
50 | | - RIGHT_PROMPT+="$(set_color - ${LAST_SEGMENT_COLOR}) ${normal}" |
| 46 | +function __powerline_right_first_segment_padding() { |
| 47 | + RIGHT_PROMPT+="$(set_color - "${LAST_SEGMENT_COLOR?}") ${normal?}" |
51 | 48 | ((RIGHT_PROMPT_LENGTH += 1)) |
52 | 49 | } |
53 | 50 |
|
54 | | -function __powerline_prompt_command { |
| 51 | +function __powerline_last_status_prompt() { |
| 52 | + if [[ "${1?}" -ne 0 ]]; then |
| 53 | + printf '%b %s %b' "$(set_color "${LAST_STATUS_THEME_PROMPT_COLOR-"52"}" -)" "${1}" "${normal?}" |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +function __powerline_prompt_command() { |
55 | 58 | local last_status="$?" ## always the first |
56 | | - local move_cursor_rightmost='\033[500C' |
| 59 | + local beginning_of_line='\[\e[G\]' |
| 60 | + local move_cursor_rightmost='\e[500C' |
| 61 | + local info prompt_color segment prompt |
57 | 62 |
|
58 | | - LEFT_PROMPT="" |
59 | | - RIGHT_PROMPT="" |
60 | | - RIGHT_PROMPT_LENGTH=${POWERLINE_PADDING} |
61 | | - SEGMENTS_AT_LEFT=0 |
62 | | - SEGMENTS_AT_RIGHT=0 |
63 | | - LAST_SEGMENT_COLOR="" |
| 63 | + local LEFT_PROMPT="" |
| 64 | + local RIGHT_PROMPT="" |
| 65 | + local RIGHT_PROMPT_LENGTH=${POWERLINE_PADDING:-2} |
| 66 | + local SEGMENTS_AT_LEFT=0 |
| 67 | + local SEGMENTS_AT_RIGHT=0 |
| 68 | + local LAST_SEGMENT_COLOR="" |
64 | 69 |
|
65 | 70 | _save-and-reload-history "${HISTORY_AUTOSAVE:-0}" |
66 | 71 |
|
| 72 | + if [[ -n "${POWERLINE_PROMPT_DISTRO_LOGO:-}" ]]; then |
| 73 | + LEFT_PROMPT+="$(set_color "${PROMPT_DISTRO_LOGO_COLOR?}" "${PROMPT_DISTRO_LOGO_COLORBG?}")${PROMPT_DISTRO_LOGO?}$(set_color - -)" |
| 74 | + fi |
| 75 | + |
67 | 76 | ## left prompt ## |
68 | | - for segment in $POWERLINE_LEFT_PROMPT; do |
69 | | - local info="$(__powerline_${segment}_prompt)" |
70 | | - [[ -n "${info}" ]] && __powerline_left_segment "${info}" |
| 77 | + # shellcheck disable=SC2068 # intended behavior |
| 78 | + for segment in ${POWERLINE_PROMPT[@]-"user_info" "scm" "python_venv" "ruby" "node" "cwd"}; do |
| 79 | + info="$("__powerline_${segment}_prompt")" |
| 80 | + if [[ -n "${info}" ]]; then |
| 81 | + __powerline_left_segment "${info}" |
| 82 | + fi |
71 | 83 | done |
72 | 84 |
|
73 | | - if [[ -n "${LEFT_PROMPT}" ]] && [[ "${POWERLINE_COMPACT_AFTER_LAST_SEGMENT}" -eq 0 ]]; then |
| 85 | + if [[ -n "${LEFT_PROMPT:-}" && "${POWERLINE_COMPACT_AFTER_LAST_SEGMENT:-${POWERLINE_COMPACT:-0}}" -eq 0 ]]; then |
74 | 86 | __powerline_left_last_segment_padding |
75 | 87 | fi |
76 | 88 |
|
77 | | - [[ -n "${LEFT_PROMPT}" ]] && LEFT_PROMPT+="$(set_color ${LAST_SEGMENT_COLOR} -)${POWERLINE_LEFT_END}${normal}" |
| 89 | + # By default we try to match the prompt to the adjacent segment's background color, |
| 90 | + # but when part of the prompt exists within that segment, we instead match the foreground color. |
| 91 | + prompt_color="$(set_color "${LAST_SEGMENT_COLOR?}" -)" |
| 92 | + if [[ -n "${LEFT_PROMPT:-}" && -n "${POWERLINE_LEFT_LAST_SEGMENT_END_CHAR:-}" ]]; then |
| 93 | + LEFT_PROMPT+="$(set_color - "${LAST_SEGMENT_COLOR?}")${POWERLINE_LEFT_LAST_SEGMENT_END_CHAR}" |
| 94 | + prompt_color="${normal?}" |
| 95 | + fi |
78 | 96 |
|
79 | 97 | ## right prompt ## |
80 | | - if [[ -n "${POWERLINE_RIGHT_PROMPT}" ]]; then |
| 98 | + if [[ -n "${POWERLINE_RIGHT_PROMPT[*]:-}" ]]; then |
81 | 99 | # LEFT_PROMPT+="${move_cursor_rightmost}" |
82 | | - for segment in $POWERLINE_RIGHT_PROMPT; do |
83 | | - local info="$(__powerline_${segment}_prompt)" |
| 100 | + # shellcheck disable=SC2068 # intended behavior |
| 101 | + for segment in ${POWERLINE_RIGHT_PROMPT[@]}; do |
| 102 | + info="$("__powerline_${segment}_prompt")" |
84 | 103 | [[ -n "${info}" ]] && __powerline_right_segment "${info}" |
85 | 104 | done |
86 | 105 |
|
87 | | - if [[ -n "${RIGHT_PROMPT}" ]] && [[ "${POWERLINE_COMPACT_BEFORE_FIRST_SEGMENT}" -eq 0 ]]; then |
| 106 | + if [[ -n "${RIGHT_PROMPT:-}" && "${POWERLINE_COMPACT_BEFORE_FIRST_SEGMENT:-${POWERLINE_COMPACT:-0}}" -eq 0 ]]; then |
88 | 107 | __powerline_right_first_segment_padding |
89 | 108 | fi |
90 | 109 |
|
91 | | - RIGHT_PAD=$(printf "%.s " $(seq 1 $RIGHT_PROMPT_LENGTH)) |
| 110 | + RIGHT_PAD=$(printf "%.s " $(seq 1 "${RIGHT_PROMPT_LENGTH}")) |
92 | 111 | LEFT_PROMPT+="${RIGHT_PAD}${move_cursor_rightmost}" |
93 | 112 | LEFT_PROMPT+="\033[$((${#RIGHT_PAD} - 1))D" |
94 | 113 | fi |
95 | 114 |
|
96 | | - local prompt="${PROMPT_CHAR}" |
97 | | - if [[ "${POWERLINE_COMPACT_PROMPT}" -eq 0 ]]; then |
| 115 | + prompt="${prompt_color}${PROMPT_CHAR-${POWERLINE_PROMPT_CHAR-\\$}}${normal?}" |
| 116 | + if [[ "${POWERLINE_COMPACT_PROMPT:-${POWERLINE_COMPACT:-0}}" -eq 0 ]]; then |
98 | 117 | prompt+=" " |
99 | 118 | fi |
100 | 119 |
|
101 | | - PS1="${LEFT_PROMPT}${RIGHT_PROMPT}\n$(__powerline_last_status_prompt ${last_status})${prompt}" |
102 | | - |
103 | | - ## cleanup ## |
104 | | - unset LAST_SEGMENT_COLOR \ |
105 | | - LEFT_PROMPT RIGHT_PROMPT RIGHT_PROMPT_LENGTH \ |
106 | | - SEGMENTS_AT_LEFT SEGMENTS_AT_RIGHT |
| 120 | + PS1="${beginning_of_line}${normal?}${LEFT_PROMPT}${RIGHT_PROMPT}\n$(__powerline_last_status_prompt "${last_status}")${prompt}" |
107 | 121 | } |
0 commit comments