0

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)

Edit

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}\$ "
KanseiKy
  • 23
  • 3
  • Typically, you set `PS1` itself in the code executed by `PROMPT_COMMAND`, not just `parse_git_branch`. – chepner Jun 24 '21 at 14:26
  • @chepner wdym? You mean, I set my `PS1` in `parse_git_branch`? – KanseiKy Jun 24 '21 at 14:27
  • `PS_BRANCH` may change everytime you display the prompt, but all the other variables (including `PS1`) only use the value of `PS_BRANCH` when they are first defined. – chepner Jun 24 '21 at 14:27
  • Something like `PROMPT_COMMAND=set_prompt`, where `set_prompt` is a function that uses `parse_git_branch` and other code to set the value of `PS1`. – chepner Jun 24 '21 at 14:29
  • I'll try that, thanks – KanseiKy Jun 24 '21 at 14:29
  • @chepner Didn't quite work, you can check my updated post for details – KanseiKy Jun 24 '21 at 14:37
  • 1
    No, `PS1=...` goes *inside* `set_prompt`; that's why it's called `set_prompt` and not `set_some_variables_related_to_the_prompt`. – chepner Jun 24 '21 at 14:38
  • 1
    `PS1` never needs to be exported, either. – chepner Jun 24 '21 at 14:38
  • No luck, I'm back to where I started – KanseiKy Jun 24 '21 at 14:41
  • What do you mean by "restart your shell"? `.bashrc` should be automatically resourced by any new interactive shell. – chepner Jun 24 '21 at 14:50
  • When my shell first run. Like exit my shell and enter it again – KanseiKy Jun 24 '21 at 14:51
  • A `.bashrc` file is always sourced and never executed. Remove the shebang line. – ceving Jun 24 '21 at 14:55
  • I think I should rephrased, every time I open my terminal and load my shell. The prompt become like [this](https://i.stack.imgur.com/R6bPL.png). But after I execute a single command from the CLI, (it don't matter if the command exits or not), it will load correctly (the way intended) – KanseiKy Jun 24 '21 at 15:04

1 Answers1

0

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.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • Firstly, where should I put the `sleep .2`? Second, if I set the `COLLUMNS` variable, what will happen when I resize? Thanks in advance – KanseiKy Jun 25 '21 at 08:02
  • You can put the `sleep` anywhere in `.bashrc`. When you resize the window, the variable `COLUMNS` will be adjusted as usual. – Armali Jun 25 '21 at 08:03