|
Home > Archive > Unix Shell > March 2004 > reading/setting current foreground color
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
reading/setting current foreground color
|
|
|
| I am trying to determine/set, from a bash script, the
current foreground color.
Dont know how to read that. I tried using tcl's
source cmd to set a persistent parent environment variable
with a little script (ie., source set-fgcolor 34), but that only
works for a single child nesting. If I call source from my main
script, it is already on a child's env, so the master
env does not pass the fgcolor (not exist yet) and I cant
again pass value back to the parent.
Is there a more sane way of doing this?
Thanks for clues
TonyB
--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb@sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers
| |
|
| On Mon, 22 Mar 2004 12:19:08 -0800
Joaco <tonyb@bogus.net> wrote:
> I am trying to determine/set, from a bash script, the
> current foreground color.
>
> with a little script (ie., source set-fgcolor 34), but that
> only works for a single child nesting.
I also tried a trap on EXIT(signal 0) to run the parent env
setting script from the child script, by including
source set-fgcolor 34
where set-fgcolor is
# script executed with tcl source command
# to set persistent environment variable
#
FGCOLOR=$1
export FGCOLOR
but, whereas that script works from parent command line
(single nesting) it does nothing from a trap (trap "source
set-fgcolor 34" 0), when called from a child script...
>
> --
> __ __ _ I N C. http://www.sysdev.org
> / __|\\// __|| \ __ __ / tonyb@sysdev.org
> \__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
> |___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers
>
--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb@sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers
| |
| Joaco 2004-03-23, 10:36 am |
| On Mon, 22 Mar 2004 12:19:08 -0800
Joaco <tonyb@bogus.net> wrote:
> I am trying to determine/set, from a bash script, the
> current foreground color.
Well, I was able to do it. But I had to split the script
into 2. One setcolour [fg [bg]] to be used with the tcl
source program to post to the environment:
source `setcolour fg bg` works because setcolour takes user
input and returns a file with the export commands that source
needs to post at the parent SHLVL=1
The second scripts just does the fancy painting, clr screen,
etc. using the environment vars and ANSI sequences with the
assistance of setterm -store.
You can get the scripts at http://www.sysdev.org bundled in the
linuxTheTools v0.6.0 archive.
--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb@sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers
| |
| Stephane CHAZELAS 2004-03-23, 2:37 pm |
| 2004-03-22, 12:19(-08), Joaco:
> I am trying to determine/set, from a bash script, the
> current foreground color.
For terminals with ANSI colors:
To set:
tput setaf <n>
Most terminals with color support are ANSI based, so that should
work. Some other kinds of terminals have different color policies
(you define a color pair for bg/fg (with tput initp), and then
use it, with tput setp <n> ). Some terminals have support for 16,
88 or 256 colors (try elinks (a text web browser) on XFree86
xterm for instance, and you'll soon find out that you can do
without mozilla). On some terminals, the "bold" attribute
affects the current foreground color, too. Some terminals have
default colors...
I don't think any terminal will let you query the current
foreground color. It's up to you to maintain that information.
Note, that in most places where color is supported,
printf '%b%s%b\n' '\033[31m' 'text' '\033[m'
should display "text" in red.
Note that the zsh shell has builtin ANSI color support (see info
page).
--
Stéphane ["Stephane.Chazelas" at "free.fr"]
| |
| John DuBois 2004-03-24, 12:35 am |
| In article <slrnc612n3.5n.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS <this.address@is.invalid> wrote:
>I don't think any terminal will let you query the current
>foreground color. It's up to you to maintain that information.
The SCO UNIX console and anything that emulates it correctly (for example,
SCO's terminal emulator for X, scoterm) have a RAS (Return Attribute Setting)
sequence: CSIS [0-2] M.
If you e.g. echo '\033[=0M', you'll get "0 7<return>" (or whatever) as though
it were typed at the keyboard.
I don't know if any other terminals have such a sequence, but I wouldn't be
surprised. Not that it's very useful without a standard terminfo entry for it.
John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
| |
|
| On Tue, 23 Mar 2004 20:07:15 +0100
Stephane CHAZELAS <this.address@is.invalid> wrote:
> I don't think any terminal will let you query the current
> foreground color. It's up to you to maintain that information.
I had no problem displaying colors, but my rendering script
needs to know the background while its filling in different
color artifacts. Tried, the '\033]=0M', the gentelman below
suggested, but that just echos itself.
Finally, opted for keeping the info myself, but didnt want
to use the filesystem and instead wanted memory. So tried
using the env for storage, but that gave me problems with
SHLLVL's (process mem space is different when tring to set
env vars from a script). Using tcl's source program helped
a little. Now thinking of some kind of IPC strategy, to get
the info to the process that can set its own env.
Tony
--
__ __ _ I N C. http://www.sysdev.org
/ __|\\// __|| \ __ __ / tonyb@sysdev.org
\__ \ \/\__ \||)|/ O_)\/ / \/ System Tools / Utilities
|___/ || ___/|_ /\___|\_/ WIntel / Linux Device Drivers
| |
| Stephane CHAZELAS 2004-03-24, 5:35 am |
| 2004-03-24, 01:43(+00), Joaco:
> On Tue, 23 Mar 2004 20:07:15 +0100
> Stephane CHAZELAS <this.address@is.invalid> wrote:
>
>
> I had no problem displaying colors, but my rendering script
> needs to know the background while its filling in different
> color artifacts. Tried, the '\033]=0M', the gentelman below
> suggested, but that just echos itself.
Just reset the background. There's no standard way to know what
the background color is (and even what the default background
is).
tput setab 7
should set a white background.
tput setab 0
should set a black background.
(unfortunately, that's not always true, you may need to play
with standout (tput smso), just make tests).
See the man page of terminfo in sections 5, for details.
--
Stéphane ["Stephane.Chazelas" at "free.fr"]
|
|
|
|
|