bashstarship

Starship prompt: conditional directory color based on path prefix


Using the starship prompt, how would I conditionally set the [directory] color based on path prefix?

I was able to achieve it in classic bash prompt, and would like to replicate it with starship.

It would work a bit like the read_only option of [directory] works (adding a lock symbol next to the [directory]).


Solution

  • I managed to do this with custom commands. It is not an ideal solution, but it works.

    [custom.directory1]
    format = "[directory1](bg:#33658A)[](fg:#33658A)"
    shell = 'bash'
    when = '[ "$PWD" == "/path/to/directory1" ]'
    
    
    [custom.directory2]
    format = "[directory2](bg:#4c873f)[](fg:#4c873f)"
    shell = 'bash'
    when = '[ "$PWD" == "/path/to/directory2" ]'