Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that the prompt-escaped string is stored in a variable <code>FOO</code>, this will count only user-visible characters: </p> <pre><code> FOO=$(git_prompt_info) local zero='%([BSUbfksu]|([FK]|){*})' FOOLENGTH=${#${(S%%)FOO//$~zero/}} </code></pre> <p>This comes from <a href="http://ruderich.org/simon/config/zshrc" rel="nofollow noreferrer">this <code>.zshrc</code></a>. </p> <p>This is a <em>rough</em> explanation of why it works, liberally quoting from <a href="http://linux.die.net/man/1/zshexpn" rel="nofollow noreferrer"><code>man zshexpn</code></a>, section <code>PARAMETER EXPANSION</code>. I'm not 100% sure of the details, so, if you're using this to develop your own equivalent, read the relevant <code>man zshall</code> sections. </p> <p>Working from the line <code>FOOLENGTH=${#${(S%%)FOO//$~zero/}}</code>, we've got a number of bits. Going from the inside out: </p> <ol> <li><p><code>$~zero</code>: The <code>~</code> ensures that <code>zero</code>, which we've defined as <code>'%([BSUbfksu]|([FB]|){*})'</code>, is treated as a pattern rather than as a plain string. </p></li> <li><p><code>${(S%%)FOO//$~zero/}</code>: This matches <code>${name//pattern/repl}</code>: </p> <blockquote> <p>Replace the longest possible match of pattern in the expansion of parameter name by string repl </p> </blockquote> <p>Note that we don't have a <code>repl</code>; we replace the longest possible match of <code>pattern</code> with nothing, thereby removing it.<br> <code>(S%%)FOO</code> conducts an expansion on <code>FOO</code> with several flags set. I don't quite follow it. </p></li> <li><p><code>${#${(S%%)FOO//$~zero/}}</code>: <code>${#spec}</code> will substitue the length in characters of the result of the substitution <code>spec</code>, if <code>spec</code> is a substitution. In our case, <code>spec</code> is the result of the substitution <code>${(S%%)FOO//$~zero/}</code>; so this basically returns the length of characters in the result of the regular expression <code>s/zero//</code> on <code>FOO</code>, where <code>zero</code> is the pattern above. </p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload