Parte de la línea terminal desaparece al presionar la flecha hacia abajo

Parte de la línea terminal desaparece al presionar la flecha hacia abajo

Utilizo una PS1 personalizada para mostrar información más relevante en mi terminal, como si estoy en un directorio git y si está limpio o necesito realizar cambios. Sin embargo, a veces, cuando estoy pasando los comandos con flechas, parte de la línea del terminal desaparece:

@ ~/tests/testing [tests] > grunt
# up arrow, down arrow
@ ~/tests/testing [t

Básicamente, el archivo ests] >se corta y me queda solo el archivo [t.

¿Hay alguna razón particular por la cual parte de la línea se corta con esta configuración de PS1?

Aquí hay información adicional:

Mi TERM env var es xterm-256color. Aquí está mi .bash_profile:

red='\033[0;31m'
yellow='\033[0;32m'
orange='\033[0;33m'
blue='\033[0;34m'
pink='\033[0;35m'
NC='\033[0m'

function is_git {
  if git rev-parse --is-inside-work-tree 2>/dev/null; then
    return 1
  else
    return 0
  fi
}

function stuff {
  if [ $(is_git) ]; then
    git_dir="$(git rev-parse --git-dir 2>/dev/null)"

    if [ -z "$(ls -A ${git_dir}/refs/heads )" ]; then
      echo -en " [${orange}init${NC}]"
      return
    fi

    echo -n " ["
    if [ $(git status --porcelain 2>/dev/null| wc -l | tr -d ' ') -ne 0 ]; then
      echo -en "${red}"
    else
      echo -en "${blue}"
    fi
    echo -en "$(git rev-parse --abbrev-ref HEAD)${NC}]"
  fi
}

export PS1="@ \w\[\$(stuff)\]\[\$(tput sgr0)\] > "

Respuesta1

La sugerencia de @i_am_root de poner \[y \]dentro de la definición de redy similares es buena. Sin embargo, poreste, bash solo procesa \[y \]en PS1, no en el texto incluido en PS1por $(). Por lo tanto, use \001and \002(o \x01and \x02) dentro de redand similar en lugar de \[and \].

Nota: Poresta respuesta, solo los códigos de escape deben estar en \001y \002. El texto que será visible para el usuario debe estar fuera del \001y \002para que bash sepa que ocupa espacio en la pantalla y pueda tenerlo en cuenta al volver a dibujar.

Respuesta2

Los códigos de color de Bash, los caracteres de escape, las asignaciones y demás se vuelven confusos rápidamente.

Pruebe este ejemplo de código que reemplaza los echocomandos agregándolos a la PS1variable.

red='\[\033[0;31m\]'
yellow='\[\033[0;32m\]'
orange='\[\033[0;33m\]'
blue='\[\033[0;34m\]'
pink='\[\033[0;35m\]'
NC='\[\033[0m\]'

export PS1="@ \w"

function is_git {
  if git rev-parse --is-inside-work-tree 2>/dev/null; then
    return 1
  else
    return 0
  fi
}

function stuff {
  if [ $(is_git) ]; then
    git_dir="$(git rev-parse --git-dir 2>/dev/null)"

    if [ -z "$(ls -A ${git_dir}/refs/heads )" ]; then
      PS1="${PS1} [${orange}init${NC}]"
      return
    fi

    PS1="$PS1 ["
    if [ $(git status --porcelain 2>/dev/null| wc -l | tr -d ' ') -ne 0 ]; then
      PS1="${PS1}${red}"
    else
      PS1="${PS1}${blue}"
    fi
    PS1="${PS1}$(git rev-parse --abbrev-ref HEAD)${NC}]"
  fi
}

stuff
PS1="${PS1}$(tput sgr0) > "

información relacionada