Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #239601 > unrolled thread

Why ``color_prompt`` is only set for ``xterm``?

Started byIL Ka <kazakevichilya@gmail.com>
First post2021-09-03 02:20 +0200
Last post2021-09-07 20:40 +0200
Articles 15 — 10 participants

Back to article view | Back to linux.debian.user


Contents

  Why ``color_prompt`` is only set for ``xterm``? IL Ka <kazakevichilya@gmail.com> - 2021-09-03 02:20 +0200
    Re: Why ``color_prompt`` is only set for ``xterm``? David Wright <deblis@lionunicorn.co.uk> - 2021-09-03 02:40 +0200
      Re: Why ``color_prompt`` is only set for ``xterm``? Nicholas Geovanis <nickgeovanis@gmail.com> - 2021-09-03 03:10 +0200
      Re: Why ``color_prompt`` is only set for ``xterm``? piorunz <piorunz@gmx.com> - 2021-09-03 03:50 +0200
        Re: Why ``color_prompt`` is only set for ``xterm``? Mark Neyhart <Mark.Neyhart@akleg.gov> - 2021-09-03 22:00 +0200
          Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples) Intense Red <intnsred@golgotha.net> - 2021-09-05 07:30 +0200
            Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples) Cindy Sue Causey <butterflybytes@gmail.com> - 2021-09-05 14:20 +0200
            Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt  examples) Greg Wooledge <greg@wooledge.org> - 2021-09-05 15:10 +0200
              Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples) Cindy Sue Causey <butterflybytes@gmail.com> - 2021-09-05 15:40 +0200
                Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt  examples) Greg Wooledge <greg@wooledge.org> - 2021-09-05 15:50 +0200
        Re: Why ``color_prompt`` is only set for ``xterm``? David Wright <deblis@lionunicorn.co.uk> - 2021-09-05 06:30 +0200
          Re: Why ``color_prompt`` is only set for ``xterm``? Greg Wooledge <greg@wooledge.org> - 2021-09-05 15:10 +0200
            Re: Why ``color_prompt`` is only set for ``xterm``? David Wright <deblis@lionunicorn.co.uk> - 2021-09-07 17:10 +0200
      Re: Why ``color_prompt`` is only set for ``xterm``? Nate Bargmann <n0nb@n0nb.us> - 2021-09-05 18:50 +0200
      Re: Why ``color_prompt`` is only set for ``xterm``? Andrei POPESCU <andreimpopescu@gmail.com> - 2021-09-07 20:40 +0200

#239601 — Why ``color_prompt`` is only set for ``xterm``?

FromIL Ka <kazakevichilya@gmail.com>
Date2021-09-03 02:20 +0200
SubjectWhy ``color_prompt`` is only set for ``xterm``?
Message-ID<CT562-5UL-1@gated-at.bofh.it>

[Multipart message — attachments visible in raw view] — view raw

Hi

.bashrc on bullseye contains following lines

```
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac
```

So we only have colors in the terminal emulator, but not in virtual
linux console.

This could be fixed by one more hardcode
``
    xterm-color|*-256color|linux) color_prompt=yes;;
``

But more robust way is something like:
```
if [ -x /usr/bin/tput ] && [ `/usr/bin/tput colors` -gt 1 ]; then
        color_prompt=yes
fi
```

I do not know how much people care about console these days, but why have
hardcode if we can check the number of colors?

Ilya.

[toc] | [next] | [standalone]


#239603

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2021-09-03 02:40 +0200
Message-ID<CT5pn-61K-3@gated-at.bofh.it>
In reply to#239601
On Fri 03 Sep 2021 at 03:15:13 (+0300), IL Ka wrote:

> .bashrc on bullseye contains following lines
> 
> ```
> # set a fancy prompt (non-color, unless we know we "want" color)
> case "$TERM" in
>     xterm-color|*-256color) color_prompt=yes;;
> esac
> ```
> 
> So we only have colors in the terminal emulator, but not in virtual
> linux console.
> 
> This could be fixed by one more hardcode
> ``
>     xterm-color|*-256color|linux) color_prompt=yes;;
> ``
> 
> But more robust way is something like:
> ```
> if [ -x /usr/bin/tput ] && [ `/usr/bin/tput colors` -gt 1 ]; then
>         color_prompt=yes
> fi
> ```

I'm not on bullseye yet, but buster's /etc/skel/.bashrc contains:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

Have you tried removing the last # ?

(I use my own customisations for distinct colours on each host,
and inverse colours for root's prompt.)

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#239605

FromNicholas Geovanis <nickgeovanis@gmail.com>
Date2021-09-03 03:10 +0200
Message-ID<CT5Sq-6s4-7@gated-at.bofh.it>
In reply to#239603

[Multipart message — attachments visible in raw view] — view raw

On Thu, Sep 2, 2021, 7:34 PM David Wright <deblis@lionunicorn.co.uk> wrote

>
> (I use my own customisations for distinct colours on each host,
> and inverse colours for root's prompt.)
>

Good idea :-)
For some reason putting "root:" there doesn't save me 100% of the time :-)

Cheers,
> David.
>
>

[toc] | [prev] | [next] | [standalone]


#239606

Frompiorunz <piorunz@gmx.com>
Date2021-09-03 03:50 +0200
Message-ID<CT6v8-6FH-3@gated-at.bofh.it>
In reply to#239603
On 03/09/2021 01:34, David Wright wrote:
> (I use my own customisations for distinct colours on each host,
> and inverse colours for root's prompt.)

Can you please share your root prompt invocation? Thanks!

-- 

With kindest regards, piorunz.

⢀⣴⠾⠻⢶⣦⠀
⣾⠁⢠⠒⠀⣿⡁ Debian - The universal operating system
⢿⡄⠘⠷⠚⠋⠀ https://www.debian.org
⠈⠳⣄⠀⠀⠀⠀

[toc] | [prev] | [next] | [standalone]


#239652

FromMark Neyhart <Mark.Neyhart@akleg.gov>
Date2021-09-03 22:00 +0200
Message-ID<CTnvY-1kV-7@gated-at.bofh.it>
In reply to#239606
On 9/2/21 5:45 PM, piorunz wrote:
> On 03/09/2021 01:34, David Wright wrote:
>> (I use my own customisations for distinct colours on each host,
>> and inverse colours for root's prompt.)
> 
> Can you please share your root prompt invocation? Thanks!
> 

In /root/.bashrc I use this to give a red prompt including host and
full path followed by a new line.  This leaves more room for a long
command when the full path is long.

export PS1='\[\e[1;31m\]\h:\w\$\[\e[0m\]\n'

[toc] | [prev] | [next] | [standalone]


#239700 — Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)

FromIntense Red <intnsred@golgotha.net>
Date2021-09-05 07:30 +0200
SubjectRe: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)
Message-ID<CTST8-53d-5@gated-at.bofh.it>
In reply to#239652
> In /root/.bashrc I use this to give a red prompt including host and
> full path followed by a new line.

   I take this idea a bit further, setting a longer prompt and setting 
workstation hosts for specific colors for user logins, and then doing a red 
prompt for servers.

   Part of my ~/.bashrc:

# Set the hostname to a specific color
HostName=`hostname -s`
if [ $HostName = "capncrunch" ]; then
   HostColor="\[\033[1;36m\]" # Bright Cyan
 elif [ $HostName = "piglet" ]; then
   HostColor="\[\033[1;35m\]" # Bright Purple
 elif [ $HostName = "wiseguy" ]; then
   HostColor="\[\033[1;32m\]" # Bright Green
 elif [ $HostName = "munchkin" ]; then
   HostColor="\[\033[1;34m\]" # Bright Blue
 elif [ $HostName = "bingo" ]; then
   HostColor="\[\033[1;33m\]" # Bright Yellow
# Now set red colors for servers:
 elif [ $HostName = "toto" ]; then
   HostColor="\[\033[1;31m\]" # Bright Red
 elif [ $HostName = "dorothy" ]; then
   HostColor="\[\033[1;31m\]" # Bright Red
# set any other machine to Bright White
 else
   HostColor="\[\033[1;37m\]" # Bright White
fi

# Check to see if the script is being run by root and set
# appropriate prompts:
if [ "$(id -u)" != "0" ]; then
   PromptSymbol="$"
   UserNameColor="\[\033[1;32m\]"
else
# let's make the root user's prompt a blinking red prompt
   PromptSymbol="\[\033[1;31m\]\[\033[5m\]#\[\033[0m\]"
   UserNameColor="\[\033[1;31m\]"
fi

# And everyone wants a 3-line prompt that gives a little more info than the
# default bash prompt, right?!
#
# E-mail messaging might screw this up a little, but try this as your PS1
# prompt:
PS1="┌─[$UserNameColor\u\033[0m@$HostColor$HostName\033[0m][\033[1;33m\]
\w\033[0m]\n│ [\033[1;36m\]\t   \d\033[0m]\n└─$PromptSymbol "
#
# That PS1 above, of course, should be all on one line.

# We'll unset the temporary variables we set above just to clean things up:
unset HostName HostColor PromptSymbol UserNameColor 

# I like to tweak the Bash colors a bit for "ls" listings. This might give
# someone some ideas:
LS_COLORS='no=00:fi=00:di=01;33:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.rar=01;31:*.arj=01;31:*.taz=01;31:
*.lzh=01;31:*.zip=01;31:*.bz2=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.deb=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:
*.tif=01;35:*.mpg=01;37:*.mp4=01;37:*.avi=01;37:*.mkv=01;37:*.gl=01;37:*.dl=01;37:'
# That LS_COLORS line should, of course, all be on one long line.

# Then we need to export them so the shell picks things up.
export PS1 LS_COLORS

   That's worth playing with to change some of the default settings if one 
wants to play with the colors of a terminal (I use KDE's "konsole" rather than 
xterm, but it's the same idea.

-- 
"I remember when I was a boy and I heard repeated time and time again the 
phrase, 'My country, right or wrong, my country!' How absolutely absurd is 
such an idea. How absolutely absurd to teach this idea to the youth of the 
country." -- Mark Twain, 1907.

[toc] | [prev] | [next] | [standalone]


#239708 — Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)

FromCindy Sue Causey <butterflybytes@gmail.com>
Date2021-09-05 14:20 +0200
SubjectRe: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)
Message-ID<CTZhT-HX-1@gated-at.bofh.it>
In reply to#239700
On 9/5/21, Intense Red <intnsred@golgotha.net> wrote:
>> In /root/.bashrc I use this to give a red prompt including host and
>> full path followed by a new line.
>
>    I take this idea a bit further, setting a longer prompt and setting
> workstation hosts for specific colors for user logins, and then doing a red
>
> prompt for servers.
>
>    Part of my ~/.bashrc:
>
> # Set the hostname to a specific color
> HostName=`hostname -s`
> if [ $HostName = "capncrunch" ]; then
>    HostColor="\[\033[1;36m\]" # Bright Cyan
>
< lots of snip for brevity >
>
> # Then we need to export them so the shell picks things up.
> export PS1 LS_COLORS
>
>    That's worth playing with to change some of the default settings if one
> wants to play with the colors of a terminal (I use KDE's "konsole" rather
> than
> xterm, but it's the same idea.


That's cool. I did play with it last night as my normal user. I'm
going to leave it for now as a reminder to personalize it more. I've
seen very similar in some operating system somewhere but never stopped
to pursue how it was accomplished. Maybe it was even for a different
terminal (emulator) from Debian's repositories.

Its first tweak needs to address that it renders the tab "Title" as
"Untitled window". I've tried a few times over the years to alter that
via "Edit > Preferences > General (tab)" in xfce4-terminal, but it
never stuck. It would always reverse back each time a terminal window
was fully closed. I could see putting something under (dot)bashrc
along with this other as being more geeky fun.

PS I just plugged it in under root after going back above and reading
from the original snippets. I'm really liking that blinking reminder
as to where one is. That might be just what I've always wanted to
cognitively distinguish between chroot and root terminal tabs. That's
been a worry quite often over time. Thank you!

Cindy :)
-- 
Cindy-Sue Causey
Talking Rock, Pickens County, Georgia, USA
* runs with birdseed *

[toc] | [prev] | [next] | [standalone]


#239712 — Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)

FromGreg Wooledge <greg@wooledge.org>
Date2021-09-05 15:10 +0200
SubjectRe: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)
Message-ID<CU04i-1ex-13@gated-at.bofh.it>
In reply to#239700
On Sun, Sep 05, 2021 at 12:28:37AM -0500, Intense Red wrote:
> > In /root/.bashrc I use this to give a red prompt including host and
> > full path followed by a new line.
> 
>    I take this idea a bit further, setting a longer prompt and setting 
> workstation hosts for specific colors for user logins, and then doing a red 
> prompt for servers.
> 
>    Part of my ~/.bashrc:
> 
> # Set the hostname to a specific color
> HostName=`hostname -s`
> if [ $HostName = "capncrunch" ]; then
>    HostColor="\[\033[1;36m\]" # Bright Cyan
>  elif [ $HostName = "piglet" ]; then
>    HostColor="\[\033[1;35m\]" # Bright Purple
>  elif [ $HostName = "wiseguy" ]; then

Not a big fan of case statements?

HostName=${HOSTNAME%%.*}
case $HostName in
  capncrunch) HostColor="\[\033[1;36m\]";; # Bright Cyan
  piglet)     HostColor="\[\033[1;35m\]";; # Bright Purple
  ...
esac

Also, for the record, your quotes are in the wrong place in your [
commands.  You need quotes around "$HostName" to avoid globbing and
word splitting.  You don't need quotes around simple strings like
capncrunch and piglet, unless one of them contains whitespace or
punctuation that's significant to the shell.

The missing quotes around "$HostName" have never mattered because so
far all of your hostnames have been safe.  Maybe that's even a
guarantee -- I'm not sure what characters are actually allowed in a
Linux hostname.  But quoting correctly still a good habit to get into.

[toc] | [prev] | [next] | [standalone]


#239714 — Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)

FromCindy Sue Causey <butterflybytes@gmail.com>
Date2021-09-05 15:40 +0200
SubjectRe: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)
Message-ID<CU0xj-1p5-1@gated-at.bofh.it>
In reply to#239712
On 9/5/21, Greg Wooledge <greg@wooledge.org> wrote:
> On Sun, Sep 05, 2021 at 12:28:37AM -0500, Intense Red wrote:
>> > In /root/.bashrc I use this to give a red prompt including host and
>> > full path followed by a new line.
>>
>>    I take this idea a bit further, setting a longer prompt and setting
>> workstation hosts for specific colors for user logins, and then doing a
>> red
>> prompt for servers.
>>
>>    Part of my ~/.bashrc:
>>
>> # Set the hostname to a specific color
>> HostName=`hostname -s`
>> if [ $HostName = "capncrunch" ]; then
>>    HostColor="\[\033[1;36m\]" # Bright Cyan
>>  elif [ $HostName = "piglet" ]; then
>>    HostColor="\[\033[1;35m\]" # Bright Purple
>>  elif [ $HostName = "wiseguy" ]; then
>
> Not a big fan of case statements?
>
> HostName=${HOSTNAME%%.*}
> case $HostName in
>   capncrunch) HostColor="\[\033[1;36m\]";; # Bright Cyan
>   piglet)     HostColor="\[\033[1;35m\]";; # Bright Purple
>   ...
> esac
>
> Also, for the record, your quotes are in the wrong place in your [
> commands.  You need quotes around "$HostName" to avoid globbing and
> word splitting.  You don't need quotes around simple strings like
> capncrunch and piglet, unless one of them contains whitespace or
> punctuation that's significant to the shell.
>
> The missing quotes around "$HostName" have never mattered because so
> far all of your hostnames have been safe.  Maybe that's even a
> guarantee -- I'm not sure what characters are actually allowed in a
> Linux hostname.  But quoting correctly still a good habit to get into.


Thank you for the tweak, Greg. That's a biggie because along the way,
we've seen how things change and fail for that very reason. One of my
partitions' terminal root now calls itself by a package name. NO CLUE
how it happened because I wasn't root while the associated package was
being extracted. Confirming placement of quotation marks, if used,
would be one checkpoint for a massive fail like that.

There's also that thing about how terminals will interpret the
different types of quotes (dumb/typewriter/ASCII versus
typographic/curly/smart) very literally. I experienced THAT fail
firsthand and now try to remember to plug anything I copy into a plain
text editor before then recopying over to a terminal.

Am wondering if, am more like hoping that this quotation marks part of
it would have stood out when I research how to further customize my
own setup. This will be a priceless personalization if I can
eventually coerce it to say "(debootstrap) chroot" in place of some of
the characters there. Shh, don't tell me how. Lead a fish to water,
yada-yada. :)

Cindy :)
-- 
Cindy-Sue Causey
Talking Rock, Pickens County, Georgia, USA
* runs with birdseed *

[toc] | [prev] | [next] | [standalone]


#239715 — Re: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)

FromGreg Wooledge <greg@wooledge.org>
Date2021-09-05 15:50 +0200
SubjectRe: Why ``color_prompt`` is only set for ``xterm``? (colored prompt examples)
Message-ID<CU0H0-1sN-5@gated-at.bofh.it>
In reply to#239714
On Sun, Sep 05, 2021 at 09:38:34AM -0400, Cindy Sue Causey wrote:
> There's also that thing about how terminals will interpret the
> different types of quotes (dumb/typewriter/ASCII versus
> typographic/curly/smart) very literally. I experienced THAT fail
> firsthand and now try to remember to plug anything I copy into a plain
> text editor before then recopying over to a terminal.

Quotes matter to the shell, not to the terminal.  And the only kinds
of quotes that the shell cares about are ASCII single and double
quotes (' and " respectively).

Unicode curly quotes are not considered quotes by any shell.  They're
just random un-special characters, like q or 5 or ñ.

> Am wondering if, am more like hoping that this quotation marks part of
> it would have stood out when I research how to further customize my
> own setup. This will be a priceless personalization if I can
> eventually coerce it to say "(debootstrap) chroot" in place of some of
> the characters there. Shh, don't tell me how. Lead a fish to water,
> yada-yada. :)

Look for "chroot" in your ~/.bashrc and see how it's currently being
done.  Make sure you understand each command, and what it does.

[toc] | [prev] | [next] | [standalone]


#239697

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2021-09-05 06:30 +0200
Message-ID<CTRX3-4q2-1@gated-at.bofh.it>
In reply to#239606
On Fri 03 Sep 2021 at 02:45:03 (+0100), piorunz wrote:
> On 03/09/2021 01:34, David Wright wrote:
> > (I use my own customisations for distinct colours on each host,
> > and inverse colours for root's prompt.)
> 
> Can you please share your root prompt invocation? Thanks!

Sure. The colours come from a set of files by hostname:

$ cat /var/local/lib/myhosts/colours/axis 
5 magenta bbarbutton=white,magenta,none:bbarhotkey=magenta,white,none
$ 

5 is for ANSI colours,
magenta is for, eg emacs, mutt, etc,
bbarbutton is for mc.

The lines from /root/.bashrc are:

if [[ $- = *i* ]]; then

    export PROMPT_COMMAND='Myprompt="$? " && [ "$Myprompt" = "0 " ] && Myprompt=""'

    # read value from file, and sanitise it
    Hue=$(head -c 1 /var/local/lib/myhosts/colours/"$HOSTNAME")
    if grep -q ·"$Hue"· <<<"·0·1·2·3·4·5·6·7·8·9·"; then
        PS1='\['$(tput setaf 3)$(tput setab 1)'\]$Myprompt\['$(tput bold)$(tput setaf 7)$(tput setab "$Hue")'\]\H \t \w\['$(tput sgr0)'\]\$ '
    else
        PS1='—$Myprompt—\H \t \w\$ '
    fi

    #hexdump -C <<<"$PS1"

fi

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#239711

FromGreg Wooledge <greg@wooledge.org>
Date2021-09-05 15:10 +0200
Message-ID<CU04i-1ex-9@gated-at.bofh.it>
In reply to#239697
On Sat, Sep 04, 2021 at 11:23:48PM -0500, David Wright wrote:
> $ cat /var/local/lib/myhosts/colours/axis 
> 5 magenta bbarbutton=white,magenta,none:bbarhotkey=magenta,white,none
> $ 
> 
> 5 is for ANSI colours,
> magenta is for, eg emacs, mutt, etc,
> bbarbutton is for mc.
> 
> The lines from /root/.bashrc are:
> 
> if [[ $- = *i* ]]; then
> 
>     export PROMPT_COMMAND='Myprompt="$? " && [ "$Myprompt" = "0 " ] && Myprompt=""'
> 
>     # read value from file, and sanitise it
>     Hue=$(head -c 1 /var/local/lib/myhosts/colours/"$HOSTNAME")
>     if grep -q ·"$Hue"· <<<"·0·1·2·3·4·5·6·7·8·9·"; then

It's strange that you used shell builtins for the $- check but not
for this check.

read -r Hue _ < /var/local/lib/myhosts/colours/"$HOSTNAME"
if [[ $Hue = [0-9] ]]; then

Or if you happen to use the other fields of that file later in the script,
then change the read command to store all 3 fields in shell variables.

[toc] | [prev] | [next] | [standalone]


#239772

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2021-09-07 17:10 +0200
Message-ID<CUKTw-5Po-3@gated-at.bofh.it>
In reply to#239711
On Sun 05 Sep 2021 at 09:06:31 (-0400), Greg Wooledge wrote:
> On Sat, Sep 04, 2021 at 11:23:48PM -0500, David Wright wrote:
> > $ cat /var/local/lib/myhosts/colours/axis 
> > 5 magenta bbarbutton=white,magenta,none:bbarhotkey=magenta,white,none
> > $ 
> > 
> > 5 is for ANSI colours,
> > magenta is for, eg emacs, mutt, etc,
> > bbarbutton is for mc.
> > 
> > The lines from /root/.bashrc are:
> > 
> > if [[ $- = *i* ]]; then
> > 
> >     export PROMPT_COMMAND='Myprompt="$? " && [ "$Myprompt" = "0 " ] && Myprompt=""'
> > 
> >     # read value from file, and sanitise it
> >     Hue=$(head -c 1 /var/local/lib/myhosts/colours/"$HOSTNAME")
> >     if grep -q ·"$Hue"· <<<"·0·1·2·3·4·5·6·7·8·9·"; then
> 
> It's strange that you used shell builtins for the $- check but not
> for this check.
> 
> read -r Hue _ < /var/local/lib/myhosts/colours/"$HOSTNAME"
> if [[ $Hue = [0-9] ]]; then

Yes. The grep would have been copied from another line in my users'
version of processing that file:
grep -q ·"${Hues[1]}"· <<<"·brown·cyan·green·blue·magenta·red·" && Emacshue="${Hues[1]}"
and when I wrote that, I couldn't be bothered with constructing an OR
(IIRC: 'twas a long time ago).

OTOH   if [[ $- = *i* ]]; then   came from your recent
https://lists.debian.org/debian-user/2021/04/msg00381.html

> Or if you happen to use the other fields of that file later in the script,
> then change the read command to store all 3 fields in shell variables.

I do use an array (as above), but root doesn't, because root only uses
the prompt colour from that file. I don't run emacs, mc or mutt as root.

Two other trivia: root insists on no whitespace at the start, and
I don't think I need to export PROMPT_COMMAND because, unlike you,
I set both it and PS1 in .bashrc.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#239720

FromNate Bargmann <n0nb@n0nb.us>
Date2021-09-05 18:50 +0200
Message-ID<CU3vc-3bA-3@gated-at.bofh.it>
In reply to#239603

[Multipart message — attachments visible in raw view] — view raw

I may as well add to the fun here.

Some years back, probably the late '90s or so, there was a short lived
"Bash Prompt Project".  I seem to recall it have a package in Debian
back around that time.  I had previously done colorizing of the MS-DOS
prompt and had desired to do the same in Bash.  Along the way I found
that project and I think made a contribution.  It's dead now but my
colored prompts live on!

A couple of years ago I became fascinated with terminfo and so am using
it rather than specifying ANSI escape codes directly.  Here is the
entirety of my ~/.bashrc that deals with setting the prompt:

---------------------8<-----------------------------------

# Git repository status in the prompt
if [[ -e /usr/lib/git-core/git-sh-prompt ]]; then
    # From https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f
    source /usr/lib/git-core/git-sh-prompt

    export GIT_PS1_SHOWDIRTYSTATE=true     # staged '+', unstaged '*'
    export GIT_PS1_SHOWSTASHSTATE=true     # '$' something is stashed
    export GIT_PS1_SHOWUPSTREAM="auto"     # '<' behind, '>' ahead, '<>' diverged, '=' no difference
    export GIT_PS1_SHOWUNTRACKEDFILES=true # '%' untracked files
fi

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

# Use variables set from terminfo capabilities to make PS1 and LESS_TERMCAP_*
# variables less cryptic.
#
# Colors: 0, Black; 1, Red; 2, Green; 3, Yellow; 4, Blue; 5, Magenta; 6, Cyan; 7, White.
# Foreground (text) colors
BLK=$(tput setaf 0)
RED=$(tput setaf 1)
GRN=$(tput setaf 2)
YEL=$(tput setaf 3)
BLU=$(tput setaf 4)
MAG=$(tput setaf 5)
CYA=$(tput setaf 6)
WHT=$(tput setaf 7)

# Background colors
BLKB=$(tput setab 0)
REDB=$(tput setab 1)
GRNB=$(tput setab 2)
YELB=$(tput setab 3)
BLUB=$(tput setab 4)
MAGB=$(tput setab 5)
CYAB=$(tput setab 6)
WHTB=$(tput setab 7)

# Character attibutes
BLD=$(tput bold)
ITA=$(tput sitm)
NOR=$(tput sgr 0)

# More prompt madness.  Show exit status if command returned non-zero result.
prompt_last_exit_status () {
    PROMPT_LAST_EXIT_STATUS=$?

    if [[ ${PROMPT_LAST_EXIT_STATUS} == "0" ]]; then
        PROMPT_LAST_EXIT_STATUS=
    else
        PROMPT_LAST_EXIT_STATUS='{'$BLD$RED$PROMPT_LAST_EXIT_STATUS$NOR'}'
    fi
}

prompt_command () {
    prompt_last_exit_status

    # From: https://superuser.com/a/180275
    CurDir=$(pwd|sed -e "s!$HOME!~!"|sed -re "s!([^/])[^/]+/!\1/!g")
}

export PROMPT_COMMAND=prompt_command

if [ "$color_prompt" = yes ]; then
    PS1='('
    PS1+='\[$CYA\]$SHLVL\[$NOR\]'
    PS1+=')'
    PS1+='['
    PS1+='\[$YEL\]\j\[$NOR\]'
    PS1+=']'
    PS1+='\[$GRN\]\u\[$NOR\]'
    PS1+='@'
    PS1+='\[$BLD$GRN\]\h\[$NOR\]'
    PS1+=':'
    PS1+='\[$BLD$BLU\]$CurDir\[$NOR\]'
    PS1+=' '
    PS1+='$(__git_ps1 "(\[$MAG\]\[$ITA\]%s\[$NOR\])")'
    PS1+=' '
    PS1+='$PROMPT_LAST_EXIT_STATUS'
    PS1+='\n'
    PS1+='\[$BLD$WHT\]\$\[$NOR\] '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

unset color_prompt force_color_prompt

--------------------->8-----------------------------------

Generally, for root accounts I change GRN to RED and on virtual machines
to MAG.  Of course quite a range is available but the most limiting is
the Linux virtual console driver so I keep things relatively simple.

I also created some posts on my blog about tput[1] and how to reset the
color palettes of the various terminals[2].

- Nate

[1] https://www.n0nb.us/blog/2020/02/tinkering-in-the-terminal-with-tput/

[2] https://www.n0nb.us/blog/2020/02/coping-with-color-on-the-linux-console-and-xterm-and-friends/

-- 
"The optimist proclaims that we live in the best of all
possible worlds.  The pessimist fears this is true."
Web: https://www.n0nb.us
Projects: https://github.com/N0NB
GPG fingerprint: 82D6 4F6B 0E67 CD41 F689 BBA6 FB2C 5130 D55A 8819

[toc] | [prev] | [next] | [standalone]


#239780

FromAndrei POPESCU <andreimpopescu@gmail.com>
Date2021-09-07 20:40 +0200
Message-ID<CUOaK-7FB-3@gated-at.bofh.it>
In reply to#239603

[Multipart message — attachments visible in raw view] — view raw

On Jo, 02 sep 21, 19:34:07, David Wright wrote:
> 
> (I use my own customisations for distinct colours on each host,
> and inverse colours for root's prompt.)

Since we're sharing bash prompts here's mine, as an extract from the 
diff to /etc/skel/.bashrc


--- /etc/skel/.bashrc	2019-04-18 07:12:36.000000000 +0300
+++ .config/bash/bashrc	2020-05-22 09:54:21.882114365 +0300
@@ -38,6 +38,9 @@
 # set a fancy prompt (non-color, unless we know we "want" color)
 case "$TERM" in
     xterm-color|*-256color) color_prompt=yes;;
+    rxvt*) color_prompt=yes;;
+    linux) color_prompt=yes;;
+    screen*) color_prompt=yes;;
 esac
 
 # uncomment for a colored prompt, if the terminal has the capability; turned
@@ -56,8 +59,16 @@
     fi
 fi
 
+# add git branch information to avoid commiting to 'master' by mistake
+if [[ -e /usr/lib/git-core/git-sh-prompt ]]; then
+        . /usr/lib/git-core/git-sh-prompt
+fi
+GIT_PS1_SHOWUPSTREAM="verbose"
+#PROMPT_COMMAND='__git_ps1 "${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;32m\]" "\[\033[00m\]\n└─> \$ "'
+PROMPT_COMMAND='__git_ps1 "${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00;32m\]" "\[\033[00m\]\$ "'
+
 if [ "$color_prompt" = yes ]; then
-    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
+    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
 else
     PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 fi


Lately I've been using fish primarily, which already has that and more 
in it's default selection of prompts (I'm using the "Clasic + VCS").


Kind regards,
Andrei
-- 
http://wiki.debian.org/FAQsFromDebianUser

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web