Skip to content

fix(themes/powerline-multiline): measure right segments at their rendered width - #2422

Merged
seefood merged 1 commit into
Bash-it:masterfrom
archonik:fix/powerline-multiline-clock-width
Nov 22, 2026
Merged

seefood merged 1 commit into
Bash-it:masterfrom
archonik:fix/powerline-multiline-clock-width

Conversation

@archonik

@archonik archonik commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

powerline-multiline right-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:

segment returns counted rendered
clock \D{%H:%M:%S} 12 8
user_info \u 2 length of the username
hostname \h 2 length of the short hostname
wd \W 2 length of the directory name

With 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_segment and leaves the segments alone. It measures the text the segment renders to:

  • bash 4.4+: ${text@P}, with no extra processes;
  • bash 3.2–4.3: __powerline_expand_prompt_escapes expands \u, \h, \w, \W and \D{...}. \D uses printf '%(...)T' on 4.2+ and date before 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, username petrb, output rendered with pyte):

config master this PR
default theme, POWERLINE_RIGHT_PROMPT="in_vim clock user_info" (POWERLINE_PADDING=2) 3 free columns 2 free columns (= the padding)
POWERLINE_RIGHT_PROMPT="clock duration ", POWERLINE_PADDING=1 5 free columns 1 free column (= the padding)

Not covered:

  • The fallback doesn't expand \!, \# (no segment in the right prompt by default), or apply PROMPT_DIRTRIM to \w. ${text@P} on 4.4+ handles all of them.
  • Wide characters still count as one column, e.g. the battery segment's ⚡ (U+26A1, 2 columns in the terminal). With the battery segment shown, the default prompt ends 1 column closer to the edge than POWERLINE_PADDING.

How Has This Been Tested?

  • New test/themes/powerline-multiline.base.bats:
    • The default clock + user_info segments must give the same RIGHT_PROMPT_LENGTH as their rendered text. This fails on master (19 vs 18) and on this PR's first version (15 vs 18), and passes now.
    • The bash < 4.4 fallback must match ${text@P} for \u@\h \w \W \D{%Y} in $HOME, a subdirectory, and /.
    • Mutating any of the new lines makes one of the two tests fail.
  • The fallback under bash 3.2.57, 4.1.17 and 4.3.48 (docker.io/library/bash images): the theme loads, the escaped and rendered lengths match, and \D goes through date on 3.2/4.1 and printf %()T on 4.3.
  • Full suite: test/run gives 313 tests, 0 failures, 3 skipped (Linux, bash 5.3).
  • pre-commit run --from-ref 72ea9e2 --to-ref HEAD passes (shellcheck, shfmt, the .bash checks).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • If my change requires a change to the documentation, I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • If I have added a new file, I also added it to clean_files.txt and formatted it using lint_clean_files.sh. (test/ is already listed as a directory.)
  • I have added tests to cover my changes, and all the new and existing tests pass.

@seefood

seefood commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

I tried it both ways and I swear I can't see the diff... can you add a screenshot before and after?

@archonik

Copy link
Copy Markdown
Contributor Author

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)
before

After (this PR, b473d5a)
after

How the screenshots were made

  • Fresh clones of master and of this branch, started with bash --noprofile --rcfile . The rc sets only:
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"
  • XTerm 410 with the Hack 3.003 font, 100x4.
  • The raw PS1 shows the same difference: master pads with 16 spaces and moves the cursor back with \e[15D, this PR uses 12 spaces and \e[11D.

My daily setup, where I noticed it

  • NixOS 26.05 (Yarara), kernel 6.18, GNU bash 5.3.9.
  • bash-it is pinned with fetchFromGitHub in home-manager, so it sits read-only in the Nix store with no components enabled. I only use it for the prompt theme. programs.bash.bashrcExtra sets BASH_IT, BASH_IT_CUSTOM and the theme variables above, and then sources bash_it.sh last.
  • My left prompt also has a custom nix_shell segment, which is defined in my bashrc. I left it out of the screenshots so they only use stock bash-it.
  • I've been working around this with a local override of __powerline_clock_prompt after bash_it.sh is sourced.

@seefood

seefood commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

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 characters

In themes/powerline-multiline/powerline-multiline.theme.bash, the default right prompt is:

POWERLINE_RIGHT_PROMPT="in_vim clock battery user_info" # with POWERLINE_PADDING=2

__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
underestimate:

• On master: POWERLINE_PADDING=2 + 4 − (${#USER} − 2) leaves 8 - ${#USER} free columns on the right (fits up to 8-char usernames).
• With this PR: it leaves 4 - ${#USER} free columns, so any username with 5+ characters (including seefood or archonik) overflows the right edge and wraps onto the next line.

(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.bash

themes/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
process fork on every prompt for all powerline users (and changes THEME_CLOCK_FORMAT="" behavior).

Suggested approach

Instead 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)?

  1. On Bash 4.4+ (BASH_VERSINFO): Expand the segment text with ${text@P} before taking ${#...}. That natively resolves \D{...}, \u, \h, \w, \W, !, #, etc. with zero forks and fixes all segments at once.
  2. On Bash 3.2–4.3: Fall back to expanding the known powerline prompt escapes for width measurement only (e.g., \u → ${USER}, \h → ${HOSTNAME%%.}, \W → ${PWD##/}, \w → ${PWD/#$HOME/~}, and \D{...} via printf '%(...)T' -1 on 4.2+ or date on 3.2–4.
    1).
  3. Add a bats test for __powerline_right_segment covering clock + user_info (the default right prompt) so we verify the actual RIGHT_PROMPT_LENGTH calculation.

Let me know if you'd like to update the PR along those lines!

@seefood

seefood commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

(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...)

@archonik

Copy link
Copy Markdown
Contributor Author

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.
@archonik
archonik force-pushed the fix/powerline-multiline-clock-width branch from b473d5a to ae3c7fc Compare November 21, 2026 19:25
@archonik archonik changed the title fix(themes/powerline): render the clock segment at its real width fix(themes/powerline-multiline): measure right segments at their rendered width Nov 21, 2026
@archonik

Copy link
Copy Markdown
Contributor Author

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 8 - ${#USER}.

I've reworked the PR along your lines (force-pushed, one commit):

  • powerline.base.bash is back to master, so there's no date call in the clock segment.
  • __powerline_right_segment now measures the rendered text: ${text@P} on bash 4.4+, and on older bash a fallback, __powerline_expand_prompt_escapes, for \u, \h, \w, \W and \D{...} (printf '%(...)T' on 4.2+, date before that).
  • New test/themes/powerline-multiline.base.bats: the default clock + user_info right prompt has to measure the same as its rendered text (this fails on master and on the first version), and the fallback has to match ${text@P}.

I ran the fallback under bash 3.2, 4.1 and 4.3 in the official bash images. ${text@P} in the branch older bash never takes doesn't break parsing there.

Two things this doesn't cover, both listed in the description: the fallback leaves \!, \# and PROMPT_DIRTRIM alone, and wide characters still count as one column. The battery segment's ⚡ is 2 columns wide, so with the battery shown the default prompt ends one column closer to the edge than the padding.

@seefood seefood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@seefood
seefood merged commit 0191fca into Bash-it:master Nov 22, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants