Repository navigation
fix(themes/powerline-multiline): measure right segments at their rendered width - #2422
Conversation
|
I tried it both ways and I swear I can't see the diff... can you add a screenshot before and after? |
|
Sure, here are both screenshots. The = row is printed across the full terminal width (100 columns), so it marks the right edge. On master the clock stops 5 columns short of it. With this PR it stops 1 column short, and that remaining column is POWERLINE_PADDING=1. The difference is only 4 columns, so it's easy to miss without a ruler. Before (master, 72ea9e2) After (this PR, b473d5a) How the screenshots were made
export BASH_IT_THEME=powerline-multiline
export POWERLINE_LEFT_PROMPT="python_venv user_info cwd scm "
export POWERLINE_RIGHT_PROMPT="clock duration "
export POWERLINE_PADDING=1
export BASH_IT_COMMAND_DURATION=true
export LANG=C.UTF-8
source "$BASH_IT/bash_it.sh"
My daily setup, where I noticed it
|
|
I took a closer look at why this shifts the prompt and found a blocking regression with the default powerline-multiline configuration: 1. Default POWERLINE_RIGHT_PROMPT wraps for usernames ≥ 5 charactersIn themes/powerline-multiline/powerline-multiline.theme.bash, the default right prompt is: __powerline_user_info_prompt returns \u, which __powerline_right_segment counts as 2 characters (${#params[0]}) instead of ${#USER}. On master, the +4 overestimate from \D{%H:%M:%S} (12 chars vs. 8 rendered) was accidentally offsetting that \u • On master: POWERLINE_PADDING=2 + 4 − (${#USER} − 2) leaves 8 - ${#USER} free columns on the right (fits up to 8-char usernames). (This is also why the single-line powerline theme shows no difference, and why short usernames just shift 4 columns closer to the edge.) 2. Avoid reintroducing a date fork in powerline.base.bashthemes/powerline/powerline.base.bash is shared across 5 themes (powerline, powerline-plain, powerline-naked, redline, and powerline-multiline), and only powerline-multiline needs to measure segment widths. Calling $(date +...) adds an external Suggested approachInstead of changing __powerline_clock_prompt in powerline.base.bash, could we fix the width measurement at the source in __powerline_right_segment (themes/powerline-multiline/powerline-multiline.base.bash)?
Let me know if you'd like to update the PR along those lines! |
|
(as a side note, since I switched to oh-my-posh none of those weird edge cases ever pop up. I mean, usernames over 5 chars breaking the prompt theme? it's ridiculous...) |
|
I did not notice that at all!. I was working with this override, i will rework the PR. |
…ered width
powerline-multiline right-aligns its right prompt by adding up
${#params[0]} of each right segment, but several segments return prompt
escapes rather than text: the clock is `\D{%H:%M:%S}` (counted as 12,
rendered as 8), user_info is `\u` (counted as 2, rendered as the
username), and hostname and wd are `\h` and `\W`. The default right
prompt (clock + user_info) only fit because the clock's overcount
happened to offset the username's undercount.
Measure the text a segment renders to instead: `${text@P}` on bash 4.4+,
and on older bash a fallback that expands \u, \h, \w, \W and \D{...}
(printf %(...)T on 4.2+, date before that). powerline.base.bash, which
the single-line powerline themes share, is unchanged.
b473d5a to
ae3c7fc
Compare
|
Thanks, you're right. I reproduced it: with the default theme and my 5-character username, the first version left the right prompt 1 column too long, and its last cell wrapped onto the next line. Master leaves 3 columns free, exactly your I've reworked the PR along your lines (force-pushed, one commit):
I ran the fallback under bash 3.2, 4.1 and 4.3 in the official Two things this doesn't cover, both listed in the description: the fallback leaves |


Description
powerline-multilineright-aligns its right prompt by adding up${#params[0]}of each right segment (__powerline_right_segment). Several segments return prompt escapes rather than text, so the count is off:clock\D{%H:%M:%S}user_info\uhostname\hwd\WWith the default right prompt (
in_vim clock battery user_info) the clock's +4 happened to offset the username's undercount. That's why simply making the clock exact, as this PR's first version did, wrapped the default prompt for usernames of 5+ characters.This version fixes the measurement in
__powerline_right_segmentand leaves the segments alone. It measures the text the segment renders to:${text@P}, with no extra processes;__powerline_expand_prompt_escapesexpands\u,\h,\w,\Wand\D{...}.\Dusesprintf '%(...)T'on 4.2+ anddatebefore that.themes/powerline/powerline.base.bash, which the single-line powerline themes share, is unchanged.Motivation and Context
Rendered in a pty at 100 columns (
LANG=C.UTF-8, usernamepetrb, output rendered with pyte):POWERLINE_RIGHT_PROMPT="in_vim clock user_info"(POWERLINE_PADDING=2)POWERLINE_RIGHT_PROMPT="clock duration ",POWERLINE_PADDING=1Not covered:
\!,\#(no segment in the right prompt by default), or applyPROMPT_DIRTRIMto\w.${text@P}on 4.4+ handles all of them.⚡(U+26A1, 2 columns in the terminal). With the battery segment shown, the default prompt ends 1 column closer to the edge thanPOWERLINE_PADDING.How Has This Been Tested?
test/themes/powerline-multiline.base.bats:clock+user_infosegments must give the sameRIGHT_PROMPT_LENGTHas their rendered text. This fails on master (19 vs 18) and on this PR's first version (15 vs 18), and passes now.${text@P}for\u@\h \w \W \D{%Y}in$HOME, a subdirectory, and/.docker.io/library/bashimages): the theme loads, the escaped and rendered lengths match, and\Dgoes throughdateon 3.2/4.1 andprintf %()Ton 4.3.test/rungives 313 tests, 0 failures, 3 skipped (Linux, bash 5.3).pre-commit run --from-ref 72ea9e2 --to-ref HEADpasses (shellcheck, shfmt, the.bashchecks).Types of changes
Checklist:
clean_files.txtand formatted it usinglint_clean_files.sh. (test/is already listed as a directory.)