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

Enterキーを押すたびに別のファイルを開いて作成する代わりに、次のコードに似たコードを.bashrcに入れます。

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)\] ] "

おすすめ記事