Bash: cleaner terminal titles + some file cleaning.

This commit is contained in:
Artem Sapegin 2012-11-01 15:28:32 +04:00
parent 02574be937
commit b32b420963

View File

@ -22,14 +22,13 @@ if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
color_error="$(/usr/bin/tput setab 1)$(/usr/bin/tput setaf 7)"
color_error_off="$(/usr/bin/tput sgr0)"
# set user color
# Set user color
case `id -u` in
0) color_user=$color_red ;;
*) color_user=$color_green ;;
esac
fi
# Some kind of optimization - check if git installed only on config load
PS1_GIT_BIN=$(which git 2>/dev/null)
@ -48,21 +47,18 @@ function prompt_command {
# Parse git status and get git variables
if [[ ! -z "$PS1_GIT_BIN" ]]; then
# check we are in git repo
# Check if we are in git repo
local CUR_DIR="$PWD"
while [[ ! -d "${CUR_DIR}/.git" ]] && [[ ! "${CUR_DIR}" == "/" ]] && [[ ! "${CUR_DIR}" == "~" ]] && [[ ! "${CUR_DIR}" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done
if [[ -d "${CUR_DIR}/.git" ]]; then
# 'git repo for dotfiles' fix: show git status only in home dir and other git repos
if [[ "${CUR_DIR}" != "${HOME}" ]] || [[ "${PWD}" == "${HOME}" ]]; then
# get git branch
GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)"
if [[ ! -z "$GIT_BRANCH" ]]; then
GIT_BRANCH="${GIT_BRANCH#refs/heads/}"
# Get git branch
GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)"
if [[ ! -z "$GIT_BRANCH" ]]; then
GIT_BRANCH="${GIT_BRANCH#refs/heads/}"
# get git status
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
fi
# Get git status
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
fi
fi
fi
@ -74,12 +70,12 @@ function prompt_command {
local PS1_length=$((${#USER}+${#HOSTNAME}+${#PWDNAME}+${#PS1_GIT}+3))
local FILL=
# Of length is greater, than terminal width
# If length is greater, than terminal width
if [[ $PS1_length -gt $COLUMNS ]]; then
# strip working directory name
# Strip working directory name
PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}"
else
# else calculate fillsize
# Else calculate fillsize
local fillsize=$(($COLUMNS-$PS1_length))
FILL="$color_gray"
while [[ $fillsize -gt 0 ]]; do FILL="${FILL}"; fillsize=$(($fillsize-1)); done
@ -97,22 +93,17 @@ function prompt_command {
fi
fi
# set new color prompt
# Set new color prompt
PS1="${color_user}${USER}${color_off}@${color_yellow}${HOSTNAME}${color_off}:${color_white}${PWDNAME}${color_off}${PS1_GIT} ${FILL}\n→ "
# get cursor position and add new line if we're not in first column
# cool'n'dirty trick (http://stackoverflow.com/a/2575525/1164595)
# XXX FIXME: this hack broke ssh =(
# exec < /dev/tty
# local OLDSTTY=$(stty -g)
# stty raw -echo min 0
# echo -en "\033[6n" > /dev/tty && read -sdR CURPOS
# stty $OLDSTTY
# Get cursor position and add new line if we're not in first column
echo -en "\033[6n" && read -sdR CURPOS
[[ "${CURPOS##*;}" -gt 1 ]] && echo "${color_error}${color_error_off}"
# Set title
echo -ne "\033]0;${USER}@${HOSTNAME}:${PWDNAME}"; echo -ne "\007"
# Terminal title
TITLE=`basename ${PWDNAME}`
[ $SHLVL -gt 1 ] && TITLE="${TITLE}${HOSTNAME}"
echo -ne "\033]0;${TITLE}"; echo -ne "\007"
}
# Set prompt command (title update and color prompt)