1. 9
  1.  

  2. 1

    Nice! Once I find time, will try switching over. Could you use something like this for calculating the width: https://github.com/knl/prezto/blob/master/modules/prompt/functions/prompt_knl_setup#L19?

    1. 2

      Could you use something like this for calculating the width: https://github.com/knl/prezto/blob/master/modules/prompt/functions/prompt_knl_setup#L19?

      If you are OK with occasional incorrect results, sure. This function passes 5/10 test cases from the article – not too bad. Another approximation people use is strlen from this question on stackoverflow. It passes 6/10 test cases. Keep in mind that if you use a function like this in your prompt and it gives incorrect results, your prompt will be very broken. For this reason no theme with flexible configuration has multi-line right prompt (except for Powerlevel10k).

      To be clear, the only novel part of my article is the solution to this long-standing problem. I believe my implementation of prompt-length hasn’t been known to the ZSH community.

      Once I find time, will try switching over.

      Just to be on the safe side: this article explains the technique of building prompts; the example prompt is not meant to be used. If you are feeling like trying a new prompt that uses this technique, take a look at Powerlevel10k. To see how it looks and feels before switching over, run this command to create a temporary ZSH instance with Powerlevel10k.

      (
        emulate -L zsh
        setopt err_return no_unset
        local tmp && tmp=$(mktemp -d "${TMPDIR:-/tmp}"/p10k.XXXXXXXXXX)
        {
          pushd $tmp
          git clone --depth 1 https://github.com/romkatv/powerlevel10k.git
          >>$tmp/.zshrc echo "source $tmp/powerlevel10k/config/p10k-lean.zsh"
          >>$tmp/.zshrc echo "source $tmp/powerlevel10k/powerlevel10k.zsh-theme"
          popd
          ZDOTDIR=$tmp zsh -d
        } always {
          cd / || true
          rm -rf $tmp
        }
      )
      

      Powerlevel10k is incredibly fast. cd to a git repo for best effect. It also has gorgeous no-configuration-required directory shortening: https://asciinema.org/a/258376.

      1. 1

        I’ve had to write code that takes into account escape sequences when calculating the length of a string. I ended up reading the standard for escape codes (ECMA-48) and used LPEG for the parsing. The ISO version (for ISO-8859-1 and the like) is a bit easier to understand than the UTF-8 version but both work the same [1]. There’s a bit more to escape codes than just \033[32m.

        [1] A bit of an LPEG primer: lpeg.P() matches the literal string, lpeg.R() matches a range of characters and lpeg.S() matches one of a set of characters. + means “or” (a + b means “match a OR b”), * means “and” (a * b means “match a AND b”) and ^0 means “match 0 or more of the given pattern. And being expressions, parentheses are used for grouping.