The zsh setup, provided by the grml team, enjoys a fair amount of popularity. One of the things we suck at (there are not a lot of those, but this definitely was one of them) was the way we were setting our prompt.

It used to be a conglomerate of string settings to "$PROMPT" with all the goodies you want to see in code you maintain: Inconceivable dependency upon random shell variables you never heard of, code and string duplication. It wasn't a pretty sight.

Needless to say, we didn't adhere to zsh's prompt system (or any other system for that matter) at all. There was a "$PROMPT" string, that was assembled at initialisation time, static precmd() and preexec() functions that did related stuff and no modularisation or customisability to speak of. If people wanted to do even little changes, our response was: "Look, wade through the zshrc file, and extract the precmd() function and all relevant lines that set $PROMPT and copy those to your `~/.zshrc.local' file and do your customisations there." It was truely rediculous.

But no more!

I spent the last two days to rewrite the whole way we went about dealing with prompts: It now conforms to zsh's prompt theme system; it is very much modularised; there is virtually no code duplication (except for maybe were a fallback prompt is defined, in case the prompt theme system isn't available); and about everything in the prompt can be customised via zsh's context-sensitive zstyle mechanism. It actually took a while until I had a mental model ready, of which I was convinced enough that it could handle all the aspects, that were important for maintaining a solid sense of backwards compatibility, while fixing all the things that were wrong with the old approach and adding all those features that we knew were nice to have. In the end, I came up with something that's largely based on the prompt I use in my own setup. Luckily however, we didn't need all of the complexity, so I was able to largely simplify the code. Simplified enough, that now grml's theming code is more readable than my own.

Anyway. Without further ado, here is the old prompt:

    old grml prompt

And here's the new one:

    new grml prompt

What!? Those are the same! ...Welcome to backward compatibility! :-)

The fun part about the new system is that the prompt is assembled from a set of items, that the user can decorate however he/she sees fit. So, say you don't like/need/want the version control part of the prompt, you can configure it away (the default list of items is available via the prompt's documentation "prompt -h grml"):

zstyle ':prompt:grml:left:setup' items rc change-root user at \
                                       host path percent

    new grml prompt without vcs_info
    [click thumbnail to enlarge]

It won't even run vcs_info anymore, because it doesn't need it.

Have the user-name green instead of blue? Sure, why not:

zstyle ':prompt:grml:left:items:user' pre '%F{green}'

    new grml prompt: green user name

Want to assemble it in a completely different way? Say with date and time instead of user@host and pathname and all that non-sense? Have it your way:

zstyle ':prompt:grml:left:setup' items date time percent

    new grml prompt: trying new things

`date' and `time'? Those aren't in the default item list. Why would you implement superfluous stuff? Well, I didn't. The new setup ships three themes. They all share the same code, so they behave the same. But they use different default item lists by default. The grml-large theme uses `time' and `date'; so that's where those are from.

What do the other themes look like? Well, let the prompt system tell you:

prompt -p grml-large grml-chroot

    new grml prompt: the other two themes

(I'm setting $GRML_CHROOT before, because the `grml-chroot' theme is only used (by default) if that variable is non-empty.)

This should cure any urge for customisation with our esteemed user base. If you need more than this, write your own prompt theme. It's not hard.

Note: This code is in its own feature branch at the time of writing (you can still see those "ft/prompt-rewrite" strings in vcs_info's data), but a merge into master of grml-etc-core is imminent.

Posted Tue 05 Mar 2013 16:35:56 CET Tags: