Re: [gentoo-user] Bash prompt colours

2021-12-07 Thread Frank Steinmetzger
Am Tue, Dec 07, 2021 at 10:26:11AM -0700 schrieb Grant Taylor:
> Some drive-by after-the-fact comments:
> 
> On 12/6/21 4:03 PM, Frank Steinmetzger wrote:
> >  [ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
> >  [ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "
> 
> I've taken to using things like the following:
> 
>PS1_JOBS_COUNT="${PS1_JOBS_COUNT}${MC_SID:+MC }${RANGER_LEVEL:+R }"
> 
> Leverage Bash's (and Zsh's) expansion conditional.  If the variable is set,
> then expand it to a different value.

By jove, you’re right. Maybe I’ve written that stuff before I knew about
default values. I checked in that code in September of 2016. And it could
possibly be even much older, because I clean up my config git repository
only very sporadically, as in two or three times a year.

>${VARIABLE:+alternate text to show if VARIABLE is set}
> 
> > if [[ -z "$PROMPT_COMMAND" ]]; then
> > PROMPT_COMMAND=__jobsprompt
> > else
> > PROMPT_COMMAND="$PROMPT_COMMAND ; __jobsprompt"
> > fi
> 
> Is there a reason to not simply do the following, eliminating the if
> conditional:
> 
>PROMPT_COMMAND=${PROMPT_COMMAND:+${PROMPT_COMMAND} ; __jobsprompt}

Maybe inexperience? :D
Or sometimes, explicit is better than implicit?

-- 
Grüße | Greetings | Qapla’
Please do not share anything from, with or about me on any social network.

I had a problem and used Java. Now I have a ProblemFactory.


signature.asc
Description: PGP signature


Re: [gentoo-user] Bash prompt colours

2021-12-07 Thread Grant Taylor

Some drive-by after-the-fact comments:

On 12/6/21 4:03 PM, Frank Steinmetzger wrote:

 [ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
 [ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "


I've taken to using things like the following:

   PS1_JOBS_COUNT="${PS1_JOBS_COUNT}${MC_SID:+MC }${RANGER_LEVEL:+R }"

Leverage Bash's (and Zsh's) expansion conditional.  If the variable is 
set, then expand it to a different value.


   ${VARIABLE:+alternate text to show if VARIABLE is set}


if [[ -z "$PROMPT_COMMAND" ]]; then
PROMPT_COMMAND=__jobsprompt
else
PROMPT_COMMAND="$PROMPT_COMMAND ; __jobsprompt"
fi


Is there a reason to not simply do the following, eliminating the if 
conditional:


   PROMPT_COMMAND=${PROMPT_COMMAND:+${PROMPT_COMMAND} ; __jobsprompt}



--
Grant. . . .
unix || die



Re: [gentoo-user] Bash prompt colours

2021-12-06 Thread Frank Steinmetzger
Am Fri, Dec 03, 2021 at 02:43:54PM + schrieb Peter Humphrey:

> > > > Hello list,
> > > > 
> > > > Is there a way to set the colour of a bash prompt according to
> > > > whether the user has SSH'd in?
> > > > […]
> > > When you are connected via SSH, the environment variable
> > > SSH_CONNECTION is set. I store the color in a variable and set it to
> > > yellow if `[[ -n "${SSH_CONNECTION}" ]]`. I can't give you the exact
> > > snippet since I use Zsh, but it should be possible to use a variable
> > > as color in bash's prompt?
> > > 
> > > Kind regards, tastytea
> > 
> > This link expands upon tastytea's idea:
> > 
> > https://unix.stackexchange.com/questions/217270/change-ps1-color-when-> 
> > connected-to-other-host-via-ssh
> 
> Thank you both. Now I just have to shoehorn it into /etc/bash/bashrc on the 
> SSH server...

I expanded on that idea somewhat further. When I am in midnight commander
and press Ctrl+O to open a fullscreen shell, I sometimes forget after a
while that it was an mc shell, and so blindly quit it with ctrl+D, which
also kills the mc process. So a long time ago I expanded my PS1 in ~/.bashrc
for those shells to show the number of backgrounded processes and “subshell
type” like so:


__jobsprompt() {
PS1_JOBS_COUNT=`jobs -p | wc -l`
if [ $PS1_JOBS_COUNT -eq 0 ]; then
PS1_JOBS_COUNT=
else
PS1_JOBS_COUNT="$PS1_JOBS_COUNT "
fi
[ "$MC_SID" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}MC "
[ "$RANGER_LEVEL" ] && PS1_JOBS_COUNT="${PS1_JOBS_COUNT}R "
}

if [[ -z "$PROMPT_COMMAND" ]]; then
PROMPT_COMMAND=__jobsprompt
else
PROMPT_COMMAND="$PROMPT_COMMAND ; __jobsprompt"
fi

PS1="$CBPURPLE\u$CDGRAY@$CBGREEN\h$CRESET $CBBLUE\w 
$CRED"'$PS1_JOBS_COUNT'$CRESET"


Those $C… strings of course being shell codes for colours (CB…=bold). Note
the single quotes within the string. PS1 shall contain the actual string
'$PS1_JOBS_COUNT', so that the variable is expanded anew in every prompt.

-- 
Grüße | Greetings | Qapla’
Please do not share anything from, with or about me on any social network.

I just took an IQ test. The results were negative.


signature.asc
Description: PGP signature


Re: [gentoo-user] Bash prompt colours

2021-12-03 Thread Peter Humphrey
On Friday, 3 December 2021 13:30:29 GMT Michael wrote:
> On Friday, 3 December 2021 12:08:05 GMT tastytea wrote:
> > On 2021-12-03 11:17+ Peter Humphrey  
wrote:
> > > Hello list,
> > > 
> > > Is there a way to set the colour of a bash prompt according to
> > > whether the user has SSH'd in?
> > > 
> > > This machine is a compile host for some others on the LAN, and it
> > > would be helpful if it were more obvious that I'm connected to
> > > another machine. Of course, the standard prompt tells me the machine
> > > name, but something more conspicuous would help.
> > 
> > When you are connected via SSH, the environment variable 
SSH_CONNECTION
> > is set. I store the color in a variable and set it to yellow if
> > `[[ -n "${SSH_CONNECTION}" ]]`. I can't give you the exact snippet
> > since I use Zsh, but it should be possible to use a variable as color
> > in bash's prompt?
> > 
> > Kind regards, tastytea
> 
> This link expands upon tastytea's idea:
> 
> https://unix.stackexchange.com/questions/217270/change-ps1-color-when-> 
> connected-to-other-host-via-ssh

Thank you both. Now I just have to shoehorn it into /etc/bash/bashrc on the 
SSH server...

-- 
Regards,
Peter.






Re: [gentoo-user] Bash prompt colours

2021-12-03 Thread Michael
On Friday, 3 December 2021 12:08:05 GMT tastytea wrote:
> On 2021-12-03 11:17+ Peter Humphrey  wrote:
> > Hello list,
> > 
> > Is there a way to set the colour of a bash prompt according to
> > whether the user has SSH'd in?
> > 
> > This machine is a compile host for some others on the LAN, and it
> > would be helpful if it were more obvious that I'm connected to
> > another machine. Of course, the standard prompt tells me the machine
> > name, but something more conspicuous would help.
> 
> When you are connected via SSH, the environment variable SSH_CONNECTION
> is set. I store the color in a variable and set it to yellow if
> `[[ -n "${SSH_CONNECTION}" ]]`. I can't give you the exact snippet
> since I use Zsh, but it should be possible to use a variable as color
> in bash's prompt?
> 
> Kind regards, tastytea

This link expands upon tastytea's idea:

https://unix.stackexchange.com/questions/217270/change-ps1-color-when-connected-to-other-host-via-ssh


signature.asc
Description: This is a digitally signed message part.


Re: [gentoo-user] Bash prompt colours

2021-12-03 Thread tastytea
On 2021-12-03 11:17+ Peter Humphrey  wrote:

> Hello list,
> 
> Is there a way to set the colour of a bash prompt according to
> whether the user has SSH'd in?
> 
> This machine is a compile host for some others on the LAN, and it
> would be helpful if it were more obvious that I'm connected to
> another machine. Of course, the standard prompt tells me the machine
> name, but something more conspicuous would help.

When you are connected via SSH, the environment variable SSH_CONNECTION
is set. I store the color in a variable and set it to yellow if 
`[[ -n "${SSH_CONNECTION}" ]]`. I can't give you the exact snippet
since I use Zsh, but it should be possible to use a variable as color
in bash's prompt?

Kind regards, tastytea

-- 
Get my PGP key with `gpg --locate-keys tasty...@tastytea.de` or at
.


pgpPmG8W_1HhU.pgp
Description: Digitale Signatur von OpenPGP


[gentoo-user] Bash prompt colours

2021-12-03 Thread Peter Humphrey
Hello list,

Is there a way to set the colour of a bash prompt according to whether the 
user has SSH'd in?

This machine is a compile host for some others on the LAN, and it would be 
helpful if it were more obvious that I'm connected to another machine. Of 
course, the standard prompt tells me the machine name, but something more 
conspicuous would help.

-- 
Regards,
Peter.