Live data from Hacker News

Bash PS1 Generator

bashrcgenerator.com

141–146 of 146 posts

Re: Bash PS1 Generator

#141
I wrote my PS1 in Python once, but on review it turned out much better to have it completely in shell:

    set_PS1()
    {
        local RESET=$(tput sgr0 )
        local BOLD=$(tput bold )
        local RED=$(tput setaf 1 )
        local GREEN=$(tput setaf 2 )
        local YELLOW=$(tput setaf 3 )
        local BLUE=$(tput setaf 4 )
        local MAGENTABG=$(tput setab 5 )
        local CYAN=$(tput setaf 6 )

        local WHOAMI='\u'
        local WHERE='\w'
        local HOSTNAME='\h'
        local DATE='\D{%Y-%m-%d %H:%M:%S}'
        local LAST_RET='${?#0}'

        local LINE_1="$YELLOW$DATE $GREEN$WHOAMI$RED@$CYAN$HOSTNAME $BLUE$BOLD$WHERE$RESET"
        local LINE_2="\\[$MAGENTABG\\]$LAST_RET\\[$RESET\\]"'\$ '

        PS1="$LINE_1\n$LINE_2"

        unset -f set_PS1
    }

    set_PS1

sauce: https://codereview.stackexchange.com/q/174019

Re: Bash PS1 Generator

#142
post #110

Seeing '>' as an option makes it somewhat relevant to mention here: I'd recommend not ending your prompt with a '>' character (as you'd see in DOS/Windows a lot, which might make it seem like a nice option). If you accidentally copy and paste your prompt with a command after it, (which is easy to do with a couple mouse clicks, or if you forget what's in your clipboard after copying some lines from your terminal,) the…

Maybe >(Unicode 0xFF1/>) would be a good substitute? Side by side: >>

Japanese has a few brackets that might be useful but less easy to confuse.

Here’s a few the IME on my phone suggested (the middle ones are the same as yours as far as I can tell):

〉> > ≫ 》

Re: Bash PS1 Generator

#143
Another useful thing to have in one's PS1 is an optional newline if the last output didn't print one itself. Turns out this is non-trivial to get working perfectly (https://stackoverflow.com/questions/19943482/configure-shell..., https://work.lisk.in/2020/11/20/even-faster-bash-startup.htm...). The version that I've been using for a while without problems is this one:

  function __col1_ps1 {
    [[ $MC_SID ]] && return

    local termios cur_y
    # ask the terminal for any pending (line buffered) input
    termios=$(stty --save) && stty -icanon && stty "$termios"
    # if there's pending input, assume it's been echoed and we're not in first column
    # otherwise ask the terminal for current column and read it from input
    if read -t 0 || { IFS='[;' read -s -r -d'R' -p$'\033[6n' _ _ cur_y && [[ $cur_y != 1 ]]; }; then
      echo $'\001\033[41m↵\033[m\002\n\001\r\002'
    fi
  }

  PS1='$(__col1_ps1)'
My full prompt is here: https://github.com/liskin/dotfiles/blob/9785740f7f97d7da1f84...

Re: Bash PS1 Generator

#144

Earlier quoted context omitted.

In case someone wonders, this does retain the repeated "+" to indicate call depth: "The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection." -- this is an important feature when working on large-ish (1-10+k LoC) scripts, which end up having very deep call stacks and a flat trace output would be completely inscrutable.

If you have 10k LoC Bash scripts, then you’ve got bigger problems!

Overall it's more like 150k LoC or something like that (split into the classic three layer architecture: "scripts", "library" and "more scripts on top"). It does work though. Much better than you'd expect from hearing "tens of thousands of lines of shell".

Re: Bash PS1 Generator

#145
post #124
post #14

Notes: * As an idea, this is excellent. It's intuitive to use and provides full instructions on how to implement * Putting an element in any other position than the end of the prompt is fiddly. * I'd love for the option to to be able to colour elements. * More advanced or exotic element types such as Git Branch would be excellent (Not sure if Bash supports this, but I've seen it in some shells)

There's one prompt in ZSH, the adam2 prompt[1], that I completely fell in love with due to the automatic horizontal rule since it makes tracing histories so much easier when dealing with long-scrolling outputs. I've never been able to find a similar feature in another prompt/shell, nor have I had the inclination to try and replicate it, but I'd be much more inclined to switch shells/try new prompts if that were avail…

The tide prompt for fish has an option to add a horizontal rule and spaces between each command

Re: Bash PS1 Generator

#146

Earlier quoted context omitted.

This. The one time a month when I can't remember which git branch I'm on I'll just run 'git branch'. If you're feeling frisky, you could even alias that to 'gb'.

One time a month? Currently I have 85 work related repositories checked out, putting the current git branch and current Kubernetes context in the prompt is helping to avoid mistakes. I also have a newline in my prompt which makes it easier to copy commands and output into Jira.

I don't have 85 repo but 5 worktree were enough to make me do some errors, so I print the 'environment' configured and the current path but that wasn't enough so now I 'invert' the colors if the current directory doesn't match the environement variables --> no more errors
Post reply on HN