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="$(/usr/bin/tput setab 1)$(/usr/bin/tput setaf 7)"
color_error_off="$(/usr/bin/tput sgr0)" color_error_off="$(/usr/bin/tput sgr0)"
# set user color # Set user color
case `id -u` in case `id -u` in
0) color_user=$color_red ;; 0) color_user=$color_red ;;
*) color_user=$color_green ;; *) color_user=$color_green ;;
esac esac
fi fi
# Some kind of optimization - check if git installed only on config load # Some kind of optimization - check if git installed only on config load
PS1_GIT_BIN=$(which git 2>/dev/null) PS1_GIT_BIN=$(which git 2>/dev/null)
@ -48,24 +47,21 @@ function prompt_command {
# Parse git status and get git variables # Parse git status and get git variables
if [[ ! -z "$PS1_GIT_BIN" ]]; then if [[ ! -z "$PS1_GIT_BIN" ]]; then
# check we are in git repo # Check if we are in git repo
local CUR_DIR="$PWD" local CUR_DIR="$PWD"
while [[ ! -d "${CUR_DIR}/.git" ]] && [[ ! "${CUR_DIR}" == "/" ]] && [[ ! "${CUR_DIR}" == "~" ]] && [[ ! "${CUR_DIR}" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done while [[ ! -d "${CUR_DIR}/.git" ]] && [[ ! "${CUR_DIR}" == "/" ]] && [[ ! "${CUR_DIR}" == "~" ]] && [[ ! "${CUR_DIR}" == "" ]]; do CUR_DIR="${CUR_DIR%/*}"; done
if [[ -d "${CUR_DIR}/.git" ]]; then if [[ -d "${CUR_DIR}/.git" ]]; then
# 'git repo for dotfiles' fix: show git status only in home dir and other git repos # Get git branch
if [[ "${CUR_DIR}" != "${HOME}" ]] || [[ "${PWD}" == "${HOME}" ]]; then
# get git branch
GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)" GIT_BRANCH="$($PS1_GIT_BIN symbolic-ref HEAD 2>/dev/null)"
if [[ ! -z "$GIT_BRANCH" ]]; then if [[ ! -z "$GIT_BRANCH" ]]; then
GIT_BRANCH="${GIT_BRANCH#refs/heads/}" GIT_BRANCH="${GIT_BRANCH#refs/heads/}"
# get git status # Get git status
local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)" local GIT_STATUS="$($PS1_GIT_BIN status --porcelain 2>/dev/null)"
[[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1 [[ -n "$GIT_STATUS" ]] && GIT_DIRTY=1
fi fi
fi fi
fi fi
fi
# Build B&W prompt for git # Build B&W prompt for git
[[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #${GIT_BRANCH}" [[ ! -z "$GIT_BRANCH" ]] && PS1_GIT=" #${GIT_BRANCH}"
@ -74,12 +70,12 @@ function prompt_command {
local PS1_length=$((${#USER}+${#HOSTNAME}+${#PWDNAME}+${#PS1_GIT}+3)) local PS1_length=$((${#USER}+${#HOSTNAME}+${#PWDNAME}+${#PS1_GIT}+3))
local FILL= local FILL=
# Of length is greater, than terminal width # If length is greater, than terminal width
if [[ $PS1_length -gt $COLUMNS ]]; then if [[ $PS1_length -gt $COLUMNS ]]; then
# strip working directory name # Strip working directory name
PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}" PWDNAME="...${PWDNAME:$(($PS1_length-$COLUMNS+3))}"
else else
# else calculate fillsize # Else calculate fillsize
local fillsize=$(($COLUMNS-$PS1_length)) local fillsize=$(($COLUMNS-$PS1_length))
FILL="$color_gray" FILL="$color_gray"
while [[ $fillsize -gt 0 ]]; do FILL="${FILL}"; fillsize=$(($fillsize-1)); done while [[ $fillsize -gt 0 ]]; do FILL="${FILL}"; fillsize=$(($fillsize-1)); done
@ -97,22 +93,17 @@ function prompt_command {
fi fi
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→ " 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 # 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
echo -en "\033[6n" && read -sdR CURPOS echo -en "\033[6n" && read -sdR CURPOS
[[ "${CURPOS##*;}" -gt 1 ]] && echo "${color_error}${color_error_off}" [[ "${CURPOS##*;}" -gt 1 ]] && echo "${color_error}${color_error_off}"
# Set title # Terminal title
echo -ne "\033]0;${USER}@${HOSTNAME}:${PWDNAME}"; echo -ne "\007" 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) # Set prompt command (title update and color prompt)