简体   繁体   English

命令提示符目录样式

[英]Command Prompt Directory Styling

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. 目前,我正在使用Holmans Dotfiles ,我想进一步对其进行自定义,以创建一个更具可读性和清晰度的提示。 What I would like is described below using image, plz note that these are photoshopped in order to look as i want them to ;). 我想要的是在下面使用图像描述的,请注意,这些都是photoshop的,以便看起来像我想要的那样;)。 This is also an issue on github, with inline images! 这也是github上的问题,带有内联图像!

Let's say you have this file structure as in this image: 假设您具有如下图所示的文件结构:

在此处输入图片说明

At the moment, when I am in lets say map3 my prompt only shows: 此刻,当我进入map3时,我的提示仅显示:

图片

I want to extend this but with alternative styling. 我想扩展此样式,但要使用其他样式。 At the moment the current map (map3) i am in is highlighted with cyan. 目前,我所在的当前地图(map3)用青色突出显示。 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: 请看下图:

图片

from what i know, is that %3 gives the last 3 dir. 据我所知,%3给出了最后3个目录。 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. 我有另一个想法,但是对上述问题次要的重要性是基于dir是git存储库yes或no来有一个相对提示。 (so the dirtree is always visible up to the rootmap of the git repo) (因此,直到git repo的根图,始终可以看到dirty)

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: 也就是说map0是git仓库的根,而我在map3中,那么我希望我的提示像这样:

图片

when i am in map5 like this: 当我像这样在map5中时:

图片

optionally it would be nice to be able to style the rootgit map like this for example: 可选地,能够像这样对rootgit映射进行样式设置会很好:

图片

at the moment my prompt is the same as in holmans dotfiles 目前,我的提示与holmans dotfiles中的相同

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 ): 这将显示git根目录的路径( magenta中的git root,除非您位于git root中,在这种情况下,它将仅以red显示当前目录):

修复了一些问题。

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?). 这将显示magenta的git repo的根目录,除非您位于根目录中,在这种情况下,它就像您所在的其他所有目录一样都是红色的。始终为git root着色(即使它是当前目录)很好(当前可能令人困惑?)。

  2. I show the path relative to the root of the git repo, if it exists. 如果存在,我将显示相对于git repo根目录的路径。 Another option may be to display the full path, coloring the root of the git repo, like the example below: 另一个选项可能是显示完整路径,为git repo的根着色,如下例所示:

     ~/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 ). 子模块着色:注意在屏幕截图中,路径根已重置为最深的git repo(因此,在子模块中,我们看不到config-files/oh-my-zsh ,而只有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. 它还没有最后的联系(git root和PWD之间的路径),但是其他所有内容都存在。 It may be useful if you're trying to modify this and want a better understanding. 如果您要修改此设置并希望更好地理解它,可能会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM