caminho colorido no PS1

0

Estou tentando exibir o caminho em cores, mas tenho problemas com isso.

Eu tenho um caminho constante - na variável MyPath. Eu quero criar caminho colorized no PS1, mas se pwd mostrando caminho diferente, então o meu (mas incluindo MyPath), então o resto eu quero imprimir em cores diferentes (com barra em cores diferentes).

Eu escrevi algum código, mas não sei como aplicar isso ao PS1.

Deve ser algo assim:

[ [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)

E eu fiquei preso - não sei como fazer isso.

Meu código:

#!/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
)

Alguma ajuda?

    
por TMSeth 12.08.2016 / 22:16

2 respostas

1

Eu colocaria algo semelhante ao seguinte código no .bashrc, sem exigir que outros arquivos sejam abertos e gravados sempre que você pressionar a tecla 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)\] ] "
    
por 13.08.2016 / 19:52
0

Por enquanto, funciona bem, mas deve ser reescrito. Quando alguém vai para / var / www / html / next_dir / then next_dir é verde e var www e html são vermelhos:)

subshell.sh:

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

bashrc_mount:

#!/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)";

Agora eu tenho problema com a mudança de diretório

Quando o subshell é iniciado em / var / www / html e alguém vai subir (como / var / www) então ele deve movê-lo para / var / www / html. o mesmo quando vai fazer "cd /". Por enquanto não funciona, mas depois disso vou reescrever o código:)

    
por 13.08.2016 / 20:33