help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Construct ANSI colour codes


From: pauline-galea
Subject: Re: Construct ANSI colour codes
Date: Thu, 8 Apr 2021 10:13:44 +0200

> Sent: Thursday, April 08, 2021 at 7:19 PM
> From: "Mike Jonkmans" <bashbug@jonkmans.nl>
> To: help-bash@gnu.org
> Subject: Re: Construct ANSI colour codes
>
> On Wed, Apr 07, 2021 at 11:30:10PM +0200, pauline-galea@gmx.com wrote:
> > > From: "Greg Wooledge" <greg@wooledge.org>
> >
> > Thank you for the example.  I had coded the color things some years ago
> > and did not want to break things too much, so I can see if everything
> > works.  Otherwise I could end up with too much work to do on my other
> > scripts.  Originally, I had scalar variables that you mentioned.
> >
> > The thing I'd like to ask again is how to make dim, normal, bright, blink
> > options with this scheme.
> >
> >
> > > declare -A color=(
> > >   [black]="$(tput setaf 0)"
> > >   [red]="$(tput setaf 1)"
> > >   [green]="$(tput setaf 2)"
> > >   [yellow]="$(tput setaf 3)"
> > >   [blue]="$(tput setaf 4)"
> > >   [magenta]="$(tput setaf 5)"
> > >   [cyan]="$(tput setaf 6)"
> > >   [white]="$(tput setaf 7)"
> > > )
> > > sgr0="$(tput sgr0)"
> > >
> > > echo "${color[green]}hello${sgr0} ${color[red]}world${sgr0}"
> > > printf 'Price: %s$%.2f%s\n' "${color[blue]}" "$price" "$sgr0"
> > >
> > > Or you can use scalar variables like "red" "blue" and so on, instead of
> > > the associative array.
> > >
> > > I gave two different examples for output.  You can use either, or both,
> > > or come up with your own.
> > >
> > > If you insist on initializing the color variables inside a function,
> > > and if you go with the array, then you might want to add the -g option
> > > to make it global.
>
> The information on capablities, to be used with tput, can be found via:
>       man terminfo
>
> To echo dim red text, you can use:
>       tput dim; tput setaf 1; echo text; tput sgr0

Ok, got it.

> Or, slightly more efficient:
>       tput -S <<-EOF; echo text; tput sgr0
>               dim
>               setaf 1
>       EOF
>
> Regards, Mike Jonkmans
>
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]