I'm currently editing the agnoster theme from oh-my-zsh to my preferences, and was wondering if there is some way to color the names of directories in the status bar based off depth. Something like...


I was initially using the powerline package from the ubuntu repo, but found it to be slow so I switched to zsh themes. While using powerline I was kind of able to do what I wanted, but could only change the color of the current working directory. My configuration was something like...

"cwd": {"fg": 'color', "bg": 'color', "attrs": []}

"cwd:current_folder": {"fg": 'color', "bg": 'color', "attrs": []}

Any help in creating this rainbow monstrosity would be greatly appreciated. I'm on Ubuntu 16.04 using Terminator if that matters.


You can create a function which does it. It's not perfect, absolutely no portable but it seems to works. To use it in $PS1 you just have to change $fg_ and $bg_ (with %F/%B or $fg/$bg, I don't remember) and setup and precmd hook (add-zsh-hook precmd function_precmd) with a function you will create and that will modify a variable used in $PS1.

To customize the function, you just need to set PRE_ASSOC correctly.

function colorize_array () {
    local reset='\033[0m'

    for (( i = 1; i <= $# ; i++ )); do

        if (( $+PRE_ASSOC[$i] )); then
        elif (( $+PRE_ASSOC[$(( $i - $# - 1 ))] )) ;then
            pre=$PRE_ASSOC[$(( $i - $# - 1 ))]
        echo -n -e $pre$@[$i]$reset

typeset -A fg_ bg_ 
typeset -A PRE_ASSOC

fg_=([black]="\033[30m" [white]="\033[37m" [red]="\033[31m" [yellow]="\033[33m" [cyan]="\033[36m")
bg_=([black]="\033[40m" [white]="\033[47m" [red]="\033[41m" [yellow]="\033[43m" [cyan]="\033[46m")

PRE_ASSOC=([default]="$fg_[cyan]/" [1]="$fg_[red]"  [-1]="$fg_[yellow]/")

local pwd="${PWD/#$HOME/~}"

out=$(colorize_array $pwd_list)
echo $out

The code is far from being perfect, but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.