[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Construct ANSI colour codes
From: |
Mike Jonkmans |
Subject: |
Re: Construct ANSI colour codes |
Date: |
Thu, 8 Apr 2021 09:19:06 +0200 |
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
Or, slightly more efficient:
tput -S <<-EOF; echo text; tput sgr0
dim
setaf 1
EOF
Regards, Mike Jonkmans
Re: Construct ANSI colour codes, Greg Wooledge, 2021/04/07
Re: Construct ANSI colour codes, Alex fxmbsw7 Ratchev, 2021/04/07