PS1 中的彩色路径

PS1 中的彩色路径

我正在尝试以颜色显示路径,但我遇到了问题。

我有一些不变的路径 - 在变量 MyPath 中。我想在 PS1 中创建彩色路径,但如果 pwd 显示不同的路径,那么我的路径(但包括 MyPath)那么我想以不同的颜色打印其余的路径(用不同颜色的斜杠)。

我写了一些代码,但我不知道是否可以将其应用到 PS1 上。

它应该是这样的:

[ [email protected]:/ -> /media/user/folder/ ]
# : cd /var/www/html
[ [email protected]:/ -> /var/www/html/ ] (blue slash and green dir names)
# : cd applications
[ [email protected]:/ -> /var/www/html/applications/ ] (blue slash and green dir names but last 2 slashes in green color and last dir "application" in red color)
# : cd tmp
[ [email protected]:/ -> /var/www/html/applications/tmp/ ] (blue slash and green dir names but last 3 slashes in green color and 2 last dirs "application" and "tmp" in red color)

我被困住了 - 我不知道该怎么做。

我的代码:

#!/bin/bash
MyPath="/var/www/html"
MyPathLength=$( echo ${MyPath} | wc -m)
CurrentPath="/var/www/html/functions/design"
slashColor="\[$(tput setaf 6)\]/\[$(tput sgr0)\]"
dirColor="\[$(tput setaf 2)\]"
path="";
FinalPath="";

for w in $(echo ${CurrentPath} | tr "/" " "); 
do 
  path="${path}/${w}";
  pathLength=$( echo ${path} | wc -m)

  if [ "${pathLength}" == "${MyPathLength}" ];
  then 
       FinalPath="${FinalPath}\[$(tput setaf 6)\]/\[$(tput sgr0)\]\[$(tput setaf 2)\]$w\[$(tput sgr0)\]\[$(tput setaf 6)\]/\[$(tput sgr0)\]";
  elif [ "${pathLength}" -lt "${MyPathLength}" ];
       then
            FinalPath="${FinalPath}\[$(tput setaf 6)\]/\[$(tput sgr0)\]\[$(tput setaf 2)\]$w\[$(tput sgr0)\]";
       elif [ "${pathLength}" -gt "${MyPathLength}" ];
            then
                 FinalPath="${FinalPath}\[$(tput setaf 1)\]$w\[$(tput sgr0)\]\[$(tput setaf 2)\]/\[$(tput sgr0)\]";
            fi;
done

echo "PS1=\"${FinalPath}\"" > /home/bashrc_split
cd "/";
(
 bash --rcfile /home/bashrc_split # I want to open new shell with new PS1
)

有什么帮助吗?

答案1

我会在 .bashrc 中放入类似于以下代码的内容,而不需要每次按 Enter 键时打开和写入其他文件

SEP=("/" "/")
SEP_COLOR=("\e[0;34m" "\e[0;32m")     #colors for: (FIXED - DEFAULT) SEPARATOR STRING
DIR_COLOR=("\e[0;32m" "\e[0;31m")     #colors for: (FIXED - DEFAULT) DIR NAMES
CLOSE_COLOR="\e[0m"

FIXED_DIR=" /var/www/html"
FIXED_DIR=$(realpath ${FIXED_DIR})
FIXED_DIR_ARRAY=()

DIR=${FIXED_DIR}
while [[ "$DIR" != "/" ]]; do
    B=$(basename  -z $DIR)
    DIR=$(dirname -z $DIR) 
    FIXED_DIR_ARRAY+=($B)
done

set_PS1 (){
    local DIR=$PWD
    local CUR_DIR_ARRAY=()

while : ; do
    local B=$(basename  -z $DIR)
    local DIR=$(dirname -z $DIR) 
    CUR_DIR_ARRAY+=($B)
    [[ "$DIR" == "/" ]] && break
done
local SELECTOR=0
local STR=""

local i=1 
while [[ "$i" -le "${#CUR_DIR_ARRAY[@]}" ]] ; do 
    if [ -n $SELECTOR ] &&
       [ $i -gt ${#FIXED_DIR_ARRAY[@]} ] ||
       [ "${CUR_DIR_ARRAY[-$i]}" != "${FIXED_DIR_ARRAY[-$i]}" ];
    then
        SELECTOR=1
    fi  
    local x=$(($SELECTOR%2));
    STR+="${SEP_COLOR[$x]}${SEP[$x]}"
    [[ "${CUR_DIR_ARRAY[-$i]}" != "${SEP[$x]}" ]] &&  STR+="${DIR_COLOR[$x]}${CUR_DIR_ARRAY[-$i]}"
    STR+="${CLOSE_COLOR}"
    ((i++))
done
    printf "${STR}"

}

PS1="[ \u@\h:/ -> \[\$(set_PS1)\] ] "

答案2

目前它工作得很好,但必须重写。当有人去 /var/www/html/next_dir/ 时,next_dir 是绿色的,var www 和 html 是红色的:)

子shell.sh:

cd "/var/www/html";
(
  bash --rcfile /home/username/bashrc_mount;
)

bashrc_挂载:

#!/bin/bash
function UpdatePath()
{
    MyPath="$1";
    MyPathLength=$( echo ${MyPath} | wc -m);
    CurrentPath="`pwd`";

    if [ $( echo ${CurrentPath} | grep "${MyPath}" | wc -l) == "0" ];
    then 
         $(cd ${MyPath}"/"); # this doesn't work in subshell - I don't know why... :(
    fi;

    path="";
    FinalPath="";

    for w in $(echo ${CurrentPath} | tr "/" " "); 
    do 
        path="${path}/${w}";
        pathLength=$( echo ${path} | wc -m);

     if [ "${pathLength}" == "${MyPathLength}" ];
     then 
          FinalPath="${FinalPath}$(tput setaf 3)/$(tput sgr0)$(tput setaf 1)$w$(tput sgr0)$(tput setaf 4)/$(tput sgr0)";
     elif [ "${pathLength}" -lt "${MyPathLength}" ];
          then
               FinalPath="${FinalPath}$(tput setaf 3)/$(tput sgr0)$(tput setaf 1)$w$(tput sgr0)";
          elif [ "${pathLength}" -gt "${MyPathLength}" ];
               then
                    FinalPath="${FinalPath}$(tput setaf 2)$w$(tput sgr0)$(tput setaf 4)/$(tput sgr0)";
               fi;
    done
    if [ "${CurrentPath}" == "/" ];
    then 
             FinalPath="$(tput setaf 3)/$(tput sgr0)";
    fi;

    echo "${FinalPath}";
}

DIR="$(pwd)";
PS1="$(tput setaf 4)$(tput bold)[ $(tput setaf 6)\u$(tput setaf 4)@$(tput setaf 1)${address}$(tput setaf 4):$(tput setaf 6)${mountFrom} $(tput setaf 4)]\n\[\$(UpdatePath \$(echo -en \$DIR) \${montTo} )\] \n$(tput setaf 6)\\$ $(tput setaf 7): $(tput sgr0)$(tput sgr0)";

现在我在更改目录时遇到问题

当 subshel​​l 在 /var/www/html 中启动并且有人会上升(例如 /var/www)时,它应该将他移动到 /var/www/html 。同样,当执行“cd /”时。目前它不起作用,但之后我将重写代码:)

相关内容