OSGeo / grass

GRASS GIS - free and open-source geospatial processing engine
https://grass.osgeo.org
Other
845 stars 307 forks source link

[Feat] Colorized prompt #942

Open neteler opened 4 years ago

neteler commented 4 years ago

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:

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

Would be cool to see that added somehow.

nilason commented 4 years ago

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 commented 4 years ago

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)'
wenzeslaus commented 4 years ago

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.

susi commented 2 years ago

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:

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 commented 2 years ago

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 commented 2 years ago

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?

petrasovaa commented 2 years ago

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

susi commented 2 years ago

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.

mneagul commented 2 years ago

This works the same on both bash and zsh ?

susi commented 2 years ago

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

nilason commented 2 years ago

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.