[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: |
Wed, 7 Apr 2021 23:30:10 +0200 |
> Sent: Thursday, April 08, 2021 at 9:03 AM
> From: "Greg Wooledge" <greg@wooledge.org>
> To: help-bash@gnu.org
> Subject: Re: Construct ANSI colour codes
>
> Why are you making this so hard?
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.
>
>
Re: Construct ANSI colour codes, Greg Wooledge, 2021/04/07
Re: Construct ANSI colour codes, Alex fxmbsw7 Ratchev, 2021/04/07