grass icon indicating copy to clipboard operation
grass copied to clipboard

[Feat] Colorized prompt

Open neteler opened this issue 5 years ago • 11 comments
trafficstars

Is your feature request related to a problem? Please describe.

As a followup to #923 I'd suggest to use colors for the prompt.

Current implementation (bash):

image

Proposed change (bash):

PS1='\[\e[0;1;38;5;28m\]GRASS\[\e[m\] \[\e[0m\]$_GRASS_DB_PLACE:\[\e[0;96m\]\W\[\e[m\] \[\e[0m\]>\[\e0'

(for testing, simply add to $HOME/.grass.bashrc)

image

I have generated this PS1 string easily with:

  • https://scriptim.github.io/bash-prompt-generator/ (for bash)

There are similar ones for other shells, e.g.

  • https://zsh-prompt-generator.site/ (for zsh)

Would be cool to see that added somehow.

neteler avatar Aug 27 '20 20:08 neteler

I have been experimenting with this, but I reached the conclusion that it is very difficult to find colour combination(s) that would work for every terminal setup. See for example just two examples:

Screenshot2 Screenshot1

This could only work if it is possible to set the colours and/or opt out of colouring at all.

nilason avatar Aug 27 '20 21:08 nilason

It is also possible to set this on individual level. This is what I was experimenting with for zsh, before recent changes to prompt were made (so won't work with copy and paste), but for the general idea...

in ~/.grass.zshrc :

# enable colors
setopt PROMPT_SUBST
autoload colors
colors


PS1_orig=$PS1
z_grass_version() {
     g=`echo $PS1_orig | cut -d'(' -f 1`
     echo "$g"
}

z_location() {
    l=`echo $PS1_orig | cut -d'(' -f 2 | cut -d')' -f 1`
    echo "%{$fg[cyan]%}$l%{$reset_color%}"
}

# current time with milliseconds
z_current_time() {
   echo "%*"
}

z_return_status() {
   echo "%(?..❗️)"
}

PS1='$(z_grass_version)(%B$(z_location)):%1~ > '
RPROMPT='$(grass_prompt) $(z_current_time)$(z_return_status)'

nilason avatar Aug 28 '20 14:08 nilason

Many command line tools use colors by default now (e.g., Git, Clang, GCC or have color aliases set at least on Ubuntu), so I think should be doable. Are these doing something special in terms of recognizing the available colors, color support, or default colors used in the terminal? What about things like bold? Are there terminals not supporting colors? Putty? Even xterm supports colors.

The example from Markus worked for me well enough in dark and light terminal as well as with green on black.

wenzeslaus avatar Sep 11 '20 01:09 wenzeslaus

I could take this on. I started poking at the code and drafted something like this:

Screenshot_2022-08-14_16-40-52

I'm thinking of introducing a few flags:

  • --monochrome: Disables all colors
  • --mapset-color: Sets the prompt color of the mapset
  • --location-color: Sets the prompt color of the location
  • --path-color: Sets the prompt color of the path

What do you think of this plan?

Also I noticed that a lot of the shells end up using different kinds of prompts, do we want to somewhat unify them?

susi avatar Aug 14 '22 15:08 susi

A small update. I decided that I'm going to go for a 2-line prompt that will look the same in most shells, rather than utilizing right-prompts, leaving that for less critical information such as time etc. Here is what it looks like in zsh (and bash)

Screenshot from 2022-08-15 20-52-42

Personally I like the 2-line prompt more, and by linking them together with the the box drawing characters it gives the the appearance of belonging together. Thoughts? Complaints? PR coming soon (once I tackle tcsh (and possibly ksh).

I don't have a windows nor a mac, so will need some help with testing on those platforms, but it should work as it's using ANSI escape sequences which should just work...

susi avatar Aug 15 '22 19:08 susi

Screenshot_2022-08-28_16-04-43

Thoughts on the prompt... :thinking: Should we go with separate location & mapset or a combined project centric one?

susi avatar Aug 28 '22 14:08 susi

I am unsure 2-line prompt is better, I am pretty happy with the current state on bash.

petrasovaa avatar Aug 28 '22 15:08 petrasovaa

For me at least, having 2 path-like things makes it too easy to mix them up (but then again I'm dyslexic which makes these things harder). Having the prompt take up half the line of the whole terminal is also something I'm trying to avoid, so that you have more space for the actual command to fit on one line.

susi avatar Aug 28 '22 15:08 susi

This works the same on both bash and zsh ?

mneagul avatar Aug 28 '22 21:08 mneagul

Yes! Actually I got it to fully work in bash, zsh and tcsh :grin:

susi avatar Aug 28 '22 21:08 susi

Regarding colourising the prompt, I don't have a very strong opinion, as long as there is a way to opt out. I don't have the possibility at the moment to test out #2527 for Mac Terminal. However, I find it unfortunate to mix in changes for the prompt layout in the same PR. I definitely prefer the current 1-line zsh prompt with location and maps (or alternatively project) on the right hand, as is readily possible for zsh. I'm not too worried if it doesn't look the same for all shells. Two-line prompts add too much noise, especially when reading and/or copying the terminal history.

nilason avatar Aug 29 '22 13:08 nilason