-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathheadline.zsh.theme
More file actions
682 lines (581 loc) · 21.2 KB
/
headline.zsh.theme
File metadata and controls
682 lines (581 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#!/bin/zsh
# Headline ZSH Prompt
# Copyright (c) 2025 Moarram under the MIT License
# To install, source this file from your ~/.zshrc
# Customization variables begin around line 60
# Formatting aliases
# (add more if you need)
reset=$'\e[0m'
bold=$'\e[1m'
faint=$'\e[2m'; no_faint_bold=$'\e[22m'
italic=$'\e[3m'; no_italic=$'\e[23m'
underline=$'\e[4m'; no_underline=$'\e[24m'
invert=$'\e[7m'; no_invert=$'\e[27m'
# ...
# Foreground color aliases
black=$'\e[30m'; light_black=$'\e[90m'
red=$'\e[31m'; light_red=$'\e[91m'
green=$'\e[32m'; light_green=$'\e[92m'
yellow=$'\e[33m'; light_yellow=$'\e[93m'
blue=$'\e[34m'; light_blue=$'\e[94m'
magenta=$'\e[35m'; light_magenta=$'\e[95m'
cyan=$'\e[36m'; light_cyan=$'\e[96m'
white=$'\e[37m'; light_white=$'\e[97m'
default_fg=$'\e[39m'
# Background color aliases
black_bg=$'\e[40m'; light_black_bg=$'\e[100m'
red_bg=$'\e[41m'; light_red_bg=$'\e[101m'
green_bg=$'\e[42m'; light_green_bg=$'\e[102m'
yellow_bg=$'\e[43m'; light_yellow_bg=$'\e[103m'
blue_bg=$'\e[44m'; light_blue_bg=$'\e[104m'
magenta_bg=$'\e[45m'; light_magenta_bg=$'\e[105m'
cyan_bg=$'\e[46m'; light_cyan_bg=$'\e[106m'
white_bg=$'\e[47m'; light_white_bg=$'\e[107m'
default_bg=$'\e[49m'
# Custom colors
# Ref: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
# orange_yellow=$'\e[38;5;214m' # example 8-bit color (n=214)
# orange_brown=$'\e[38;2;191;116;46m' # example rgb color (r=119, g=116, b=46)
# ...
# Terminal control aliases
cursor_up=$'\e[1F'
cursor_show=$'\e[?25h'
cursor_hide=$'\e[?25l'
cursor_to_top_left_corner=$'\e[H'
clear_entire_screen=$'\e[2J'
# ...
# Flags
[ ! -z "$SSH_TTY$SSH_CONNECTION$SSH_CLIENT" ] && IS_SSH='true'
# ------------------------------------------------------------------------------
# Customization
# Use the following variables to customize the theme.
# If you're setting them in ~/.zshrc, source the theme, THEN set the variables.
# To insert styles (ANSI SGR codes defined above) use syntax: "%{$style%}"
# Style applied to separator line, after other styles
HL_SEP_STYLE="%{$default_bg%}"
# Segments of the separator line
declare -A HL_SEP=(
_PRE ''
_LINE '_' # repeated char to create separator line, consider '▁'
_POST ''
)
# Style applied to all segments, before other styles
HL_BASE_STYLE=""
# Style of segment layout template
HL_LAYOUT_STYLE="%{$faint%}"
# Order of segments
declare -a HL_LAYOUT_ORDER=(
_PRE USER HOST VENV PATH _SPACER BRANCH STATUS _POST # ...
)
# Template for each segment's layout
declare -A HL_LAYOUT_TEMPLATE=(
_PRE "${IS_SSH+=> }" # shows "=> " if this is an ssh session
USER '...'
HOST ' @ ...'
VENV ' (...)'
PATH ': ...'
_SPACER ' | ' # special, only shows when compact, otherwise fill with space
BRANCH '...'
STATUS ' [...]'
_POST ''
# ...
)
# Template for first segment's layout (when prior segments removed)
declare -A HL_LAYOUT_FIRST=(
VENV '(...)'
PATH '...'
_SPACER ''
# ...
)
# The character used by _SPACER segment to fill space
HL_SPACE_CHAR=' '
# Template for each segment's content
declare -A HL_CONTENT_TEMPLATE=(
USER "%{$bold$red%}..." # consider ' ' or ' '
HOST "%{$bold$yellow%}..." # consider ' ' or ' '
VENV "%{$bold$green%}..." # consider ' ' or ' '
PATH "%{$bold$blue%}..." # consider ' ' or ' '
BRANCH "%{$bold$cyan%}..." # consider ' ' or ' '
STATUS "%{$bold$magenta%}..."
# ...
)
# Commands to produce each segment's content
declare -A HL_CONTENT_SOURCE=(
USER 'echo $USER'
HOST 'hostname -s'
VENV 'basename "$VIRTUAL_ENV"'
PATH 'print -rP "%~"'
BRANCH 'headline-git-branch'
STATUS 'headline-git-status'
# ...
)
# Show count of each status always, only when greater than one, or don't show
HL_GIT_COUNT_MODE='on' # on|auto|off
# Symbol to join each status
HL_GIT_SEP_SYMBOL=' | '
# Order of statuses
declare -a HL_GIT_STATUS_ORDER=(
STAGED CHANGED UNTRACKED BEHIND AHEAD DIVERGED STASHED CONFLICTS CLEAN
)
# Symbol for each status
declare -A HL_GIT_STATUS_SYMBOLS=(
STAGED '+'
CHANGED '!'
UNTRACKED '?'
BEHIND '↓'
AHEAD '↑'
DIVERGED '↕'
STASHED '*'
CONFLICTS '✘' # consider "%{$red%}✘"
CLEAN '' # consider '✓' or "%{$green%}✔"
)
# Minimum screen width to show segment
declare -A HL_COLS_REMOVAL=(
USER 50
HOST 70
VENV 30
# ...
)
# Order to truncate & remove segments
declare -a HL_TRUNC_ORDER=(
HOST USER VENV PATH BRANCH # ...
)
# Symbol to insert when truncating a segment
HL_TRUNC_SYMBOL='...' # consider '…'
# Minimum segment length for initial truncation
HL_TRUNC_INITIAL=16
# Minimum segment length before removal
HL_TRUNC_REMOVAL=2
# Prompt
HL_PROMPT='%(#.#.%(!.!.$)) ' # consider '%#'
# Right prompt
HL_RPROMPT=''
# Show the clock, or don't show
HL_CLOCK_MODE='on' # on|off
# Template for the clock
HL_CLOCK_TEMPLATE="%{$faint%}..."
# Command which outputs clock content
HL_CLOCK_SOURCE='date "+%+"' # consider 'date +%+' for full date
# Show non-zero exit code, include a guessed meaning too, or don't show
HL_ERR_MODE='detail' # on|detail|off
# Template for the exit code
HL_ERR_TEMPLATE="%{$faint$italic%}→ ..."
# Template for the optional detail
HL_ERR_DETAIL_TEMPLATE=' (...)'
# Print separator and information line with precmd hook or in PROMPT
HL_PRINT_MODE='precmd' # precmd|prompt
# Print the separator line always, when not following clear screen, or don't print
HL_SEP_MODE='auto' # on|auto|off
# Print the information line always, when it has changed, or don't print
HL_INFO_MODE='on' # on|auto|off
# Press <enter> with no commands to overwrite previous prompt
HL_OVERWRITE='on' # on|off
# The string to replace in templates
HL_TEMPLATE_TOKEN='...'
# ------------------------------------------------------------------------------
# Output variables
HL_OUTPUT_SEP='' # printed separator line
HL_OUTPUT_INFO='' # printed information line
# Local variables
_HL_SEP='' # computed separator line
_HL_INFO='' # computed information line
_HL_AT_TOP='true' # whether prompt is at top of the screen
_HL_CMD_NUM=0 # number of commands entered
_HL_CMD_NUM_PREV=-1 # previous number of commands entered, no command if same
# Zsh configuration
setopt PROMPT_SP # always start prompt on new line
setopt PROMPT_SUBST # enable substitutions
autoload -U add-zsh-hook
PROMPT_EOL_MARK='' # remove weird % symbol
ZLE_RPROMPT_INDENT=0 # remove extra space
# Calculate length of string, excluding formatting characters
headline-prompt-len() { # (str, num?)
# Ref: https://old.reddit.com/r/zsh/comments/cgbm24/multiline_prompt_the_missing_ingredient/
emulate -L zsh
local -i COLUMNS=${2:-COLUMNS}
local -i x y=${#1} m
if (( y )); then
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
x=y
(( y *= 2 ))
done
while (( y > x + 1 )); do
(( m = x + (y - x) / 2 ))
(( ${${(%):-$1%$m(l.x.y)}[-1]} = m ))
done
fi
echo $x
}
# Repeat character a number of times
headline-repeat-char() { # (char, num)
# Note: This replaces the "${(pl:$num::$char:)}" expansion
local result=''
for (( i = 0; i < $2; i++ )); do
result+=$1
done
echo $result
}
# Guess the exit code meaning
headline-exit-meaning() { # (num)
# Ref: https://tldp.org/LDP/abs/html/exitcodes.html
# Ref: https://man7.org/linux/man-pages/man7/signal.7.html
# Note: These meanings are not standardized
case $1 in
126) echo 'Command cannot execute';;
127) echo 'Command not found';;
129) echo 'Hangup';;
130) echo 'Interrupted';;
131) echo 'Quit';;
132) echo 'Illegal instruction';;
133) echo 'Trapped';;
134) echo 'Aborted';;
135) echo 'Bus error';;
136) echo 'Arithmetic error';;
137) echo 'Killed';;
138) echo 'User signal 1';;
139) echo 'Segmentation fault';;
140) echo 'User signal 2';;
141) echo 'Pipe error';;
142) echo 'Alarm';;
143) echo 'Terminated';;
*) ;;
esac
}
# Git command wrapper
headline-git() {
# TODO is this necessary?
GIT_OPTIONAL_LOCKS=0 command git "$@"
}
# Get git branch (or hash)
headline-git-branch() {
local ref
ref=$(headline-git symbolic-ref --quiet HEAD 2> /dev/null)
local err=$?
if [[ $err == 0 ]]; then
echo ${ref#refs/heads/} # remove "refs/heads/" to get branch
else # not on a branch
[[ $err == 128 ]] && return # not a git repo
ref=$(headline-git rev-parse --short HEAD 2> /dev/null) || return
echo ":${ref}" # hash prefixed to distingush from branch
fi
}
# Get the quantity of each git status
headline-git-status-counts() {
local -A counts=(
'STAGED' 0 # staged changes
'CHANGED' 0 # unstaged changes
'UNTRACKED' 0 # untracked files
'BEHIND' 0 # commits behind
'AHEAD' 0 # commits ahead
'DIVERGED' 0 # commits diverged
'STASHED' 0 # stashed files
'CONFLICTS' 0 # conflicted files
'CLEAN' 1 # clean branch 1=true 0=false
)
# Retrieve status
local raw lines
raw="$(headline-git status --porcelain -b 2> /dev/null)"
if [[ $? == 128 ]]; then
return 1 # catastrophic failure, abort
fi
lines=(${(@f)raw})
# Process tracking line
if [[ ${lines[1]} =~ '^## [^ ]+ \[(.*)\]' ]]; then
local items=("${(@s/,/)match}")
for item in $items; do
if [[ $item =~ '(behind|ahead|diverged) ([0-9]+)?' ]]; then
case $match[1] in
'behind') counts[BEHIND]=$match[2];;
'ahead') counts[AHEAD]=$match[2];;
'diverged') counts[DIVERGED]=$match[2];;
esac
fi
done
fi
# Process status lines
for line in $lines; do
if [[ $line =~ '^##|^!!' ]]; then
continue
elif [[ $line =~ '^U[ADU]|^[AD]U|^AA|^DD' ]]; then
counts[CONFLICTS]=$(( ${counts[CONFLICTS]} + 1 ))
elif [[ $line =~ '^\?\?' ]]; then
counts[UNTRACKED]=$(( ${counts[UNTRACKED]} + 1 ))
elif [[ $line =~ '^[MTADRC] ' ]]; then
counts[STAGED]=$(( ${counts[STAGED]} + 1 ))
elif [[ $line =~ '^[MTARC][MTD]' ]]; then
counts[STAGED]=$(( ${counts[STAGED]} + 1 ))
counts[CHANGED]=$(( ${counts[CHANGED]} + 1 ))
elif [[ $line =~ '^ [MTADRC]' ]]; then
counts[CHANGED]=$(( ${counts[CHANGED]} + 1 ))
fi
done
# Check for stashes
if $(headline-git rev-parse --verify refs/stash &> /dev/null); then
counts[STASHED]=$(headline-git rev-list --walk-reflogs --count refs/stash 2> /dev/null)
fi
# Update clean flag
for key val in ${(@kv)counts}; do
[[ $key == 'CLEAN' ]] && continue
(( $val > 0 )) && counts[CLEAN]=0
done
echo ${(@kv)counts} # key1 val1 key2 val2 ...
}
# Get git status
headline-git-status() {
local parts=( ${(ps:$HL_TEMPLATE_TOKEN:)HL_CONTENT_TEMPLATE[STATUS]} ) # split on template token
local style=${${parts[1]##*%\{}%%%\}*} # regex for "%{...%}"
local -A counts=( $(headline-git-status-counts) )
(( ${#counts} == 0 )) && return # not a git repo
local result=''
for key in $HL_GIT_STATUS_ORDER; do
if (( ${counts[$key]} > 0 )); then
if (( ${#HL_GIT_SEP_SYMBOL} != 0 && ${#result} != 0 )); then
result+="%{$reset%}$HL_BASE_STYLE$HL_LAYOUT_STYLE$HL_GIT_SEP_SYMBOL%{$reset%}$HL_BASE_STYLE%{$style%}"
fi
if [[ $key != 'CLEAN' && $HL_GIT_COUNT_MODE == 'on' || ( $HL_GIT_COUNT_MODE == 'auto' && ${counts[$key]} != 1 ) ]]; then
result+="${counts[$key]}${HL_GIT_STATUS_SYMBOLS[$key]}"
else
result+="${HL_GIT_STATUS_SYMBOLS[$key]}"
fi
fi
done
echo $result
}
# Transfer styles to another string
headline-transfer-styles() { # (str, str)
# Note: This assumes styles are wrapped with %{ and %}
local -a src=( ${(@s::)1} ) # source char array
local -a dest=( ${(@s::)2} ) # destination char array
local result=''
local prev=''
local is_style='false'
local index=0
for char in $src; do
if [[ $prev == '{' || $prev == '}' ]]; then
prev=$char
continue
elif [[ $prev == '%' && $char == '{' ]]; then
[[ $is_style != 'true' ]] && result+='%{'
is_style='true'
elif [[ $prev == '%' && $char == '}' ]]; then
[[ $is_style == 'true' ]] && result+='%}'
is_style='false'
elif [[ $is_style == 'true' ]]; then
result+=$prev
else
result+=${dest[$index]}
(( index += 1 ))
fi
prev=$char
done
result+=${dest[$index]}
echo $result
# TODO use regex... why does this suck so much? can't match multiple?
# if [[ $1 =~ '%{([^%]*)%}' ]]; then
# echo $MBEGIN $MEND $MATCH
# echo $mbegin $mend $match # expect arrays?
# fi
}
# Handle Ctrl+L press
zle -N headline-clear-screen
bindkey '^L' headline-clear-screen
headline-clear-screen() {
_HL_AT_TOP='true'
_HL_INFO='' # ensure info line will print
# Hide cursor and clear screen
print -nr "$cursor_hide$cursor_to_top_left_corner$clear_entire_screen"
# Update and print
for function in $precmd_functions; do
$function
done
zle .reset-prompt # re-print $PROMPT and $RPROMPT
# Show cursor
print -nr "$cursor_show"
}
# Before executing command
add-zsh-hook preexec headline-preexec
headline-preexec() {
(( _HL_CMD_NUM++ ))
# TODO better way of knowing the prompt is at the top of the terminal ?
if [[ $2 == 'clear' ]]; then
_HL_AT_TOP='true'
_HL_INFO='' # ensure info line will print
fi
}
# Before prompting
add-zsh-hook precmd headline-precmd
headline-precmd() {
local -i err=$?
local -i trunc_initial_length=$(( $HL_TRUNC_INITIAL + ${#HL_TRUNC_SYMBOL} ))
local -i trunc_removal_length=$(( $HL_TRUNC_REMOVAL + ${#HL_TRUNC_SYMBOL} ))
# Acquire contents
local -A contents
local -A content_lengths # length of each content (without style)
local -i content_length=0 # total length of content
for key val in "${(@kv)HL_CONTENT_SOURCE}"; do
content_lengths[$key]=0
(( $COLUMNS < ${HL_COLS_REMOVAL[$key]:-0} )) && continue # omit segment
contents[$key]=$(eval ${=val})
local -i length=$(headline-prompt-len ${contents[$key]:-''} 999)
(( content_length += $length )); content_lengths[$key]=$length
done
# Compute layout lengths
local -A layouts
local -A layout_lengths # length of each layout (without style)
local -i layout_length=0 # total length of layout
local -A first_layout_lengths # length of each first layout (without style)
for key val in "${(@kv)HL_LAYOUT_TEMPLATE}"; do
layout_lengths[$key]=0
local -i length=$(headline-prompt-len "$val$HL_CONTENT_TEMPLATE[$key]" 999)
local -i first_length=$(headline-prompt-len "$HL_LAYOUT_FIRST[$key]$HL_CONTENT_TEMPLATE[$key]" 999)
if [[ ${key[1]} != '_' ]]; then
(( content_lengths[$key] <= 0 )) && continue # skip omitted segment
(( length -= ${#HL_TEMPLATE_TOKEN} * 2 )) # subtract length of tokens
(( first_length -= ${#HL_TEMPLATE_TOKEN} * 2 )) # subtract length of tokens
fi
layouts[$key]=$val
(( layout_length += $length )); layout_lengths[$key]=$length
(( ${+HL_LAYOUT_FIRST[$key]} )) && first_layout_lengths[$key]=$first_length
done
# Compute target truncation length
local -i target_length=$content_length
for key in $HL_LAYOUT_ORDER; do
(( ! $HL_TRUNC_ORDER[(Ie)$key] )) && continue # no truncation specified
(( $trunc_initial_length >= $content_lengths[$key] )) && continue # already short enough
(( target_length -= $content_lengths[$key] - $trunc_initial_length ))
done
# Update first segment
for key in $HL_LAYOUT_ORDER; do
[[ $key == '_PRE' ]] && continue # skip special segment
(( content_lengths[$key] <= 0 && layout_lengths[$key] <= 0 )) && continue # skip omitted segment
if (( ${+HL_LAYOUT_FIRST[$key]} )); then
layouts[$key]=$HL_LAYOUT_FIRST[$key]
(( layout_length -= $layout_lengths[$key] - $first_layout_lengths[$key] ))
layout_lengths[$key]=$first_layout_lengths[$key]
fi
break
done
# Remove segments as needed
for key in $HL_TRUNC_ORDER; do
(( content_lengths[$key] <= 0 )) && continue # already removed
local -i remove=$(( $content_lengths[$key] < $trunc_initial_length ? $content_lengths[$key] : $trunc_initial_length ))
local -i offset=$(( $remove < $trunc_removal_length ? 0 : $remove - $trunc_removal_length ))
(( $target_length + $layout_length - $offset <= $COLUMNS )) && break # done removing
(( target_length -= $remove ))
contents[$key]=''; (( content_length -= $content_lengths[$key] )); content_lengths[$key]=0
layouts[$key]=''; (( layout_length -= $layout_lengths[$key] )); layout_lengths[$key]=0
# Update first segment
for key in $HL_LAYOUT_ORDER; do
[[ $key == '_PRE' ]] && continue # skip special segment
(( content_lengths[$key] <= 0 && layout_lengths[$key] <= 0 )) && continue # skip omitted segment
if (( ${+HL_LAYOUT_FIRST[$key]} )); then
layouts[$key]=$HL_LAYOUT_FIRST[$key]
(( layout_length -= $layout_lengths[$key] - $first_layout_lengths[$key] ))
layout_lengths[$key]=$first_layout_lengths[$key]
fi
break
done
done
# Truncate segments to initial length
for key in $HL_TRUNC_ORDER; do
(( content_lengths[$key] <= 0 )) && continue # segment removed
local -i excess=$(( $content_length + $layout_length - $COLUMNS ))
(( $excess <= 0 )) && break # done truncating
local -i removeable=$(( $content_lengths[$key] - $trunc_initial_length ))
(( $removeable <= 0 )) && continue # already short enough
local -i remove=$(( ( $excess < $removeable ? $excess : $removeable ) ))
(( content_length -= $remove ))
content_lengths[$key]=$(( $content_lengths[$key] - $remove ))
contents[$key]="$HL_TRUNC_SYMBOL${contents[$key]:$(( $remove + ${#HL_TRUNC_SYMBOL} ))}"
done
# Truncate segment to minimum length
for key in $HL_TRUNC_ORDER; do
(( content_lengths[$key] <= 0 )) && continue # segment removed
local -i excess=$(( $content_length + $layout_length - $COLUMNS ))
(( $excess <= 0 )) && break # done truncating
(( content_length -= $excess ))
content_lengths[$key]=$(( $content_lengths[$key] - $excess ))
contents[$key]="$HL_TRUNC_SYMBOL${contents[$key]:$(( excess + ${#HL_TRUNC_SYMBOL} ))}"
done
# Build spacer
local -i remainder=$(( $COLUMNS - $content_length - $layout_length ))
if (( $remainder > 0 )); then
contents[_SPACER]="$(headline-repeat-char "$HL_SPACE_CHAR" $(( $remainder + $layout_lengths[_SPACER] )) )"
fi
# Assemble segments
local information='' # the styled information line
for key in $HL_LAYOUT_ORDER; do
local segment=''; local segment_sep=''
if [[ ${key[1]} == '_' && ${#contents[$key]} == 0 ]]; then # special segment without content (ex: _PRE, _POST)
segment="${layouts[$key]}"; segment_sep=$segment
elif [[ ${key[1]} == '_' && ${#contents[$key]} != 0 ]]; then # special segment with generated content (ex: _SPACER)
segment="${contents[$key]}"; segment_sep=$segment
elif [[ ${key[1]} != '_' && ${#contents[$key]} != 0 ]]; then # normal segment with content
segment="${HL_CONTENT_TEMPLATE[$key]/$HL_TEMPLATE_TOKEN/$contents[$key]}"
segment="%{$reset%}$HL_BASE_STYLE$segment%{$reset%}$HL_BASE_STYLE$HL_LAYOUT_STYLE"
segment="${layouts[$key]/$HL_TEMPLATE_TOKEN/$segment}"
else # normal segment without content
continue
fi
information+="$HL_BASE_STYLE$HL_LAYOUT_STYLE$segment%{$reset%}"
done
# Assemble separator
local separator=$(headline-repeat-char "${HL_SEP[_LINE]}" $(( $COLUMNS - ${#HL_SEP[_PRE]} - ${#HL_SEP[_POST]} )) )
separator=$(headline-transfer-styles "$information" "${HL_SEP[_PRE]}$separator${HL_SEP[_POST]}")
separator="${separator//"%}"/"%}$HL_SEP_STYLE"}"
# Prepare cursor
local overwrite='false'
if [[ $HL_OVERWRITE == 'on' && $_HL_CMD_NUM == $_HL_CMD_NUM_PREV ]]; then
overwrite='true'
print -nr "$cursor_hide"
print -nr "$cursor_up" # to prompt line
(( ${#HL_OUTPUT_INFO} )) && print -nr "$cursor_up" # to info line
(( ${#HL_OUTPUT_SEP} )) && print -nr "$cursor_up" # to separator line
if [[ $HL_SEP_MODE == 'auto' && ! $HL_OUTPUT_SEP ]]; then
_HL_AT_TOP='true' # deduce that we were at top last time
fi
print -nr "$cursor_show"
fi
# Error line
if [[ $err != 0 && ($HL_ERR_MODE == 'on' || $HL_ERR_MODE == 'detail') && $overwrite != 'true' ]]; then
local message=$err
if [[ $HL_ERR_MODE == 'detail' ]]; then
local meaning=$(headline-exit-meaning $err)
(( ${#meaning} > 0 )) && message+="${HL_ERR_DETAIL_TEMPLATE/$HL_TEMPLATE_TOKEN/$meaning}%{$reset%}"
fi
print -rP "${HL_ERR_TEMPLATE/$HL_TEMPLATE_TOKEN/$message}%{$reset%}"
fi
# Separator line
if [[ $HL_SEP_MODE == 'on' || ($HL_SEP_MODE == 'auto' && $_HL_AT_TOP != 'true') ]]; then
HL_OUTPUT_SEP=$separator
[[ $HL_PRINT_MODE == 'precmd' ]] && print -rP "$HL_OUTPUT_SEP"
else
HL_OUTPUT_SEP=''
fi
_HL_SEP=$separator
# Information line
if [[ $HL_INFO_MODE == 'on' || ($HL_INFO_MODE == 'auto' && $information != $_HL_INFO) || $overwrite == 'true' ]]; then
HL_OUTPUT_INFO=$information
[[ $HL_PRINT_MODE == 'precmd' ]] && print -rP "$HL_OUTPUT_INFO"
else
HL_OUTPUT_INFO=''
fi
_HL_INFO=$information
# Prompt
if [[ $HL_PRINT_MODE == 'prompt' ]]; then
PROMPT='$('
(( ${#HL_OUTPUT_SEP} )) && PROMPT+='print -rP "$HL_OUTPUT_SEP"; '
(( ${#HL_OUTPUT_INFO} )) && PROMPT+='print -rP "$HL_OUTPUT_INFO"; '
PROMPT+='print -rP "$HL_PROMPT")'
else
PROMPT=$HL_PROMPT
fi
# Right prompt
if [[ $HL_CLOCK_MODE == 'on' ]]; then
RPROMPT='${HL_CLOCK_TEMPLATE/$HL_TEMPLATE_TOKEN/$(eval ${=HL_CLOCK_SOURCE})}%{$reset%}$HL_RPROMPT'
else
RPROMPT=$HL_RPROMPT
fi
_HL_CMD_NUM_PREV=$_HL_CMD_NUM
_HL_AT_TOP='false'
}