0

I have a particular need for adjusting the command prompt. At the moment i am using Holmans Dotfiles and I want to further customize it in order to create a prompt that's more readable and clear. What I would like is described below using image, plz note that these are photoshopped in order to look as i want them to ;). This is also an issue on github, with inline images!

Let's say you have this file structure as in this image:

enter image description here

At the moment, when I am in lets say map3 my prompt only shows:

image

I want to extend this but with alternative styling. At the moment the current map (map3) i am in is highlighted with cyan. I want to be able to see it's parents but those not being highlighted in the same color. Plz look at the image below:

image

from what i know, is that %3 gives the last 3 dir. However I don't know how to style each dir individually.

--------------------------- the other optional idea ----------------------------------------

The other idea I had, but which is of inferior importance to the problem described above is to have a relative prompt based on if a dir is a git repository yes or no. (so the dirtree is always visible up to the rootmap of the git repo)

that is say that map0 is the root of the git repository and i am in map3, then I would like my prompt to be like this:

image

when i am in map5 like this:

image

optionally it would be nice to be able to style the rootgit map like this for example:

image

at the moment my prompt is the same as in holmans dotfiles

Community
  • 1
  • 1
dimitrieh
  • 401
  • 3
  • 17
  • Updated your post with proper image embedding. Check the upper menu when editing, you have privileges to embed images. – fedorqui Apr 22 '13 at 12:36

1 Answers1

3

Multi-color path in prompt

 directory_name() {
    PROMPT_PATH=""

    CURRENT=`dirname ${PWD}`
    if [[ $CURRENT = / ]]; then
        PROMPT_PATH=""
    elif [[ $PWD = $HOME ]]; then
        PROMPT_PATH=""
    else
        if [[ -d $(git rev-parse --show-toplevel 2>/dev/null) ]]; then
            # We're in a git repo.
            BASE=$(basename $(git rev-parse --show-toplevel))
            if [[ $PWD = $(git rev-parse --show-toplevel) ]]; then
                # We're in the root.
                PROMPT_PATH=""
            else
                # We're not in the root. Display the git repo root.
                GIT_ROOT="%{$fg_bold[magenta]%}${BASE}%{$reset_color%}"

                PATH_TO_CURRENT="${PWD#$(git rev-parse --show-toplevel)}"
                PATH_TO_CURRENT="${PATH_TO_CURRENT%/*}"

                PROMPT_PATH="${GIT_ROOT}${PATH_TO_CURRENT}/"
            fi
        else
            PROMPT_PATH=$(print -P %3~)
            PROMPT_PATH="${PROMPT_PATH%/*}/"
        fi
    fi

    echo "%{$fg_bold[cyan]%}${PROMPT_PATH}%{$reset_color%}%{$fg[red]%}%1~%{$reset_color%}"
}

This'll show the path to the git root (git root in magenta, unless you're in the git root, in which case it'll just show the current directory in red):

Fixed some issues.

Possible Improvements:

  1. This shows the root directory of the git repo in magenta, unless you're in the root, in which case it's red, like every other directory you're in. Always-coloring a git root (even when it's the current directory) might be nice (currently it may be confusing?).

  2. I show the path relative to the root of the git repo, if it exists. Another option may be to display the full path, coloring the root of the git repo, like the example below:

    ~/repositories/config-files/zshrc.d
    ^-------------^
       White
                   ^-----------^
                     Magenta
                                ^------^
                                  Red
    
  3. Submodule coloring: Note in the screenshot that the path root gets reset to the deepest git repo (so when in a submodule, we don't see config-files/oh-my-zsh, but only oh-my-zsh). I'm not sure how to detect submodules, but it could be a further improvement.

Further Details:

There's a reasonably in-depth look [it's my notes] of how I did all this here. It doesn't yet have the final touch (path between git root and PWD), but everything else is there. It may be useful if you're trying to modify this and want a better understanding.

dimitrieh
  • 401
  • 3
  • 17
simont
  • 68,704
  • 18
  • 117
  • 136
  • thanks for the help! So how is it inefficient? and can i be combined with the git idea? – dimitrieh Apr 27 '13 at 11:09
  • @user2307317 Combining it with the `git` idea almost certainly possible, but I'd have to spend a little while thinking about it. The given answer is inefficient because I get the `PWD` using both `$PWD` and `%3~`. Ideally, I'd only use one method, but I'm not certain how to 'optimise' this. If you require a longer answer which goes into detail how I made the functions, and how they work, I can add that. I'll add in the `git` stuff when I figure it out (I'd like to use it myself), but that won't be for a few days. – simont Apr 27 '13 at 11:40
  • very nice and thanks so far :) I'll await your answer en check back every so few days or so! ... maybe an added difficulty would be how the function would handle sub repo's ( however now that i mention i don't know how my prompts see them asof right now (same prompt as holman) ) – dimitrieh Apr 29 '13 at 10:41
  • wow, nice! mmh almost working version( what does not work yet then? ). As for the functionality: i like the idea of making it the "root" when in the git repo regardless of how deep you are inside even if only one level. I think the color preference is up to oneself ( i like mine blue :) ). I am going to try it out now and see if works nicely – dimitrieh Apr 30 '13 at 11:15
  • ah i see it doesn't yet show the entire relative path inside a git repo! yup that's something that needs to be taken care of :) apart from that i think it works pretty nice ! – dimitrieh Apr 30 '13 at 11:36
  • @user2307317 Updated again. Now shows the path between `git root` and `PWD`. – simont May 01 '13 at 09:01
  • thanks mate! this is great! now figuring it out how to adjust it to my own liking cause you got rid of current_dir_path ;) will take a look on your website for sure – dimitrieh May 02 '13 at 08:33
  • wow, i though i was going crazy! but the echo in on the almost last rule has a lot of indentions after it so the text goes out of sight! now it makes sense, where the red is comming from! – dimitrieh May 02 '13 at 08:54
  • @user2307317 :) Hopefully it's not *too* difficult to follow. I think it's a reasonably nice prompt: useful without being obnoxious. I haven't yet worked it in to my dotfiles - it'll probably get tweaked when I do. I'll add back here if I find a neater approach. – simont May 02 '13 at 09:05
  • i adjusted it now to my liking :) when i found out the missing stuff behind echo it was childsplay! it works perfectly! going to send it to holmans dotfiles as pull request i think :) credits to you! – dimitrieh May 02 '13 at 09:23