This is my .bashrc
file, I'll explain my problem in a bit.
#!/usr/bin/env bash
PS_LINE=`printf -- ' %.0s' {1..200}`
function parse_git_branch {
PS_BRANCH=''
PS_FILL=${PS_LINE:0:$COLUMNS}
if [ -d .svn ] ; then
PS_BRANCH="(svn r$(svn info|awk '/Revision/{print $2}'))"
return
elif [ -f _FOSSIL_ -o -f .fslckout ] ; then
PS_BRANCH="(fossil $(fossil status|awk '/tags/{print $2}')) "
return
elif [ -d .hg ] ; then
PS_BRANCH="(hg $(hg branch))"
return
fi
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
PS_BRANCH="(git ${ref#refs/heads/}) "
}
PROMPT_COMMAND=parse_git_branch
PS_INFO="$GREEN$USER$RESET@$GREEN$HOSTNAME$RESET:$BLUE\w"
PS_VCS="$YELLOW\$PS_BRANCH"
PS_TIME="\[\033[\$((COLUMNS-10))G\] $RED[\t]"
export PS1="\${PS_FILL}\[\033[0G\]${PS_INFO} ${PS_GIT}${PS_TIME}\n${RESET}\$ "
Ok. So what I want is there's an empty line between my current directory and time (like this). As you can see, that works fine but when I restart my shell (not resource
my .bashrc
), it looks like this (there's no whitespace between them)
Based on @chepner answer, I got something like this
Updated code:
function parse_git_branch {
PS_BRANCH=''
PS_FILL=${PS_LINE:0:$COLUMNS}
if [ -d .svn ] ; then
PS_BRANCH="(svn r$(svn info|awk '/Revision/{print $2}'))"
return
elif [ -f _FOSSIL_ -o -f .fslckout ] ; then
PS_BRANCH="(fossil $(fossil status|awk '/tags/{print $2}')) "
return
elif [ -d .hg ] ; then
PS_BRANCH="(hg $(hg branch))"
return
fi
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
PS_BRANCH="(git ${ref#refs/heads/}) "
}
function set_prompt {
PS_INFO="$GREEN$USER$RESET@$GREEN$HOSTNAME$RESET:$BLUE\w"
PS_VCS="$YELLOW\$PS_BRANCH"
PS_TIME="\[\033[\$((COLUMNS-10))G\] $RED[\t]"
}
PROMPT_COMMAND=set_prompt
export PS1="\${PS_FILL}\[\033[0G\]${PS_INFO} ${PS_GIT}${PS_TIME}\n${RESET}\$ "
Apparently the actual number of columns in the terminal window is assigned to the COLUMNS
variable only after the first prompt is issued - I could reproduce this. (It might depend on the time when a window resize signal is handled.) A not quite nice, but effective remedy is to put a delay in .bashrc
- for me, sleep .2
worked. Another way is, if the number of columns of the opening terminal is known in advance, to set COLUMNS
to that.