tags:

views:

104

answers:

2

I found this in the adam2 zsh prompt, and I have no idea what it means. Apparently it counts the length of the "string", but how it does that is beyond me.

${#${(S%%)string//(\%([KF1]|)\{*\}|\%[Bbkf])}}
+1  A: 

I googled "zsh prompt regex", and found this. It says $# counts the length of the contents. That link also has more info than the zsh user's guide I found.

PanCrit
+3  A: 

Your pattern counts the length of the string after stripping out some ANSI-style character formatting.

Approximately:

  • ${# } - length

  • ${ } - nested

  • (S%%) - search substrings starting from the end

  • // - substitute globally (in this case delete, since there's no slash later)

  • ( ) - capture group

  • \% - literal percent sign

  • [KF1] - character list: K=begin background color, F=begin foreground color

  • [Bbkf] - character list: B=begin bold, b=end bold, k=end background, f=end foreground

And then I get lost.

Dennis Williamson
Do you have a source for documentation? Or is this all from memory? (Wow!)
PanCrit
It's all from mem^H^H^H the man page(s) and the info file and having played with the zsh prompt previously. I might have been able to get a litter farther if the OP had posted some possible values for the variable.
Dennis Williamson
It's one of the standard prompts shipped with zsh, `prompt adam2` to activate, perhaps after `autoload promptinit; promptinit` if you haven't already done so.Really, it's counting the length of the rendered prompt after ignoring the sequences which would change terminal attributes (eg, colour) without moving the cursor. It's to figure out whether or not the line has room for the right-hand-side part, the [email protected] that in (S%%) only the 'S' is for searching substrings. The '%%' is turning on full prompt expansion of the string, so that things like %~ map to the directory string.
Phil P