Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
piping colors into less
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
greenbishop
n00b
n00b


Joined: 24 Sep 2004
Posts: 5
Location: MA

PostPosted: Fri Apr 28, 2006 12:22 am    Post subject: piping colors into less Reply with quote

This isn't an actual operational issue with portage, in particular, but it is a command-line programming issue, and it pops up mostly when using emerge -v...

less (which is aliased less -R to include ANSI characters for color) doesn't show the colors eix and emerge usually show me. The color scheme portage developers put in is pretty conducive to reading the lists well. Is there any way for me to make colors show up properly in less? Less -r, and less without any other options doesn't work either.
Back to top
View user's profile Send private message
ectospasm
l33t
l33t


Joined: 19 Feb 2003
Posts: 711
Location: Mobile, AL, USA

PostPosted: Fri Apr 28, 2006 10:30 am    Post subject: Reply with quote

I think you're stuck, since there's no way to tell emerge that you want it to output ASCII color control codes instead of actually displaying the color, and less -[rR] won't display colors, just the color codes if it can detect them.

Edit: I stand corrected. When I pipe the output of rkhunter through less -r, it shows colors. Getting that to work with emerge may require a feature request bug report.
_________________
Join the adopt an unanswered post initiative today
Join the EFF!
Join the Drug Policy Alliance!
Back to top
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Fri Apr 28, 2006 10:54 am    Post subject: Reply with quote

For eix you've got the --force-color flag which will output colours even if stdout isn't a tty, for emerge you have to change one line in /usr/lib/portage/bin/emerge for it to output colours when stdout isn't a tty, although doing so may break some apps which read emerge's output and expect it not to have any colour escape sequences.

In portage 2.0.54 the following line (76) in /usr/lib/portage/bin/emerge needs changing from this:

Code:
if (not sys.stdout.isatty()) or (portage.settings["NOCOLOR"] in ["yes","true"]):


to this:

Code:
if (portage.settings["NOCOLOR"] in ["yes","true"]):

_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
greenbishop
n00b
n00b


Joined: 24 Sep 2004
Posts: 5
Location: MA

PostPosted: Wed May 03, 2006 10:39 pm    Post subject: Reply with quote

Thank you so much! :D That worked perfectly. Is there any chance that they will make that a default for portage in the future (although I think the response will be something like "that's there for a reason"). Why should having color/not having color rely on whether it's a tty or not?
Back to top
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Thu May 04, 2006 10:35 am    Post subject: Reply with quote

greenbishop wrote:
Thank you so much! :D That worked perfectly. Is there any chance that they will make that a default for portage in the future (although I think the response will be something like "that's there for a reason"). Why should having color/not having color rely on whether it's a tty or not?


If stdout isn't a tty they you're either piping the output to another process or to a file, in most cases you wouldn't want the extra ANSI escape sequences in that output. It would make it harder for that process to parse the output if it's scattered with escape sequences and files like this aren't very easy to read:

colour emerge output:
[[32mThese are the packages that I would merge, in order:[[39;49;00m

Calculating dependencies   HH ...done!
[ebuild   [[33;01mR[[39;49;00m   ] [[32msys-apps/portage-2.0.54[[39;49;00m  [[34;01m-build[[39;49;00m [[34;01m-doc[[39;49;00m [[34;01m(-selinux)[[39;49;00m 229 kB

Total size of downloads: 229 kB


So with the exception of less and some other processes which can interpret the escape sequences, the coulour output would get in the way.

Maybe you should suggest a flag similar to eix's --force-color flag to the portage devs to bugs.gentoo.org (even better if you supply a patch).
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
ClaesBas
n00b
n00b


Joined: 20 Jul 2002
Posts: 38
Location: Stockholm

PostPosted: Tue Aug 01, 2006 10:09 pm    Post subject: Reply with quote

My proposal on a /usr/lib/portage/bin/emerge "patch" to add --force-color option :
Code:
--- emerge      2006-08-01 23:43:15.000000000 +0200
+++ emerge--force-nocolor       2006-08-01 23:57:55.000000000 +0200
@@ -90,11 +90,6 @@
 portage.settings.lock()
 del myvar

-if (not sys.stdout.isatty()) or (portage.settings["NOCOLOR"] in ["yes","true"]):
-       nocolor()
-
-
-
 def normpath(mystr):
        """
                os.path.normpath("//foo") returns "//foo" instead of "/foo"
@@ -205,6 +200,7 @@
 "--digest",
 "--emptytree",
 "--fetchonly",    "--fetch-all-uri",
+"--force-color",
 "--getbinpkg",    "--getbinpkgonly",
 "--help",         "--ignore-default-opts",
 "--noconfmem",
@@ -252,6 +248,14 @@
 tmpcmdline=sys.argv[1:]
 if "--ignore-default-opts" not in tmpcmdline:
        tmpcmdline.extend(portage.settings["EMERGE_DEFAULT_OPTS"].split())
+
+if (("--force-color" in tmpcmdline) and ("--nocolor" in tmpcmdline)):
+        print "!!! Error: You have to make up your mind, which one --force-color or --nocolor?"
+        sys.exit(1)
+
+if ((not sys.stdout.isatty()) and ("--force-color" not in tmpcmdline)) or (portage.settings["NOCOLOR"] in ["yes","true"]):
+        nocolor()
+
 cmdline=[]
 for x in tmpcmdline:
        if x[0:1]=="-" and x[1:2]!="-":


I hope some "dev" picks this up!

I have many times wanted to be able to do a:
Code:
emerge --force-color -vpuDN world | less -r


/Claes
Back to top
View user's profile Send private message
ClaesBas
n00b
n00b


Joined: 20 Jul 2002
Posts: 38
Location: Stockholm

PostPosted: Tue Aug 01, 2006 10:19 pm    Post subject: Reply with quote

The "patch" is against sys-apps/portage-2.1-r1
Back to top
View user's profile Send private message
phatscum
n00b
n00b


Joined: 20 Mar 2006
Posts: 34
Location: Down the sewers

PostPosted: Tue Aug 01, 2006 10:21 pm    Post subject: Reply with quote

ClaesBas wrote:
I have many times wanted to be able to do a:
Code:
emerge --force-color -vpuDN world | less -r


/Claes

Seconded.
Back to top
View user's profile Send private message
ectospasm
l33t
l33t


Joined: 19 Feb 2003
Posts: 711
Location: Mobile, AL, USA

PostPosted: Tue Aug 01, 2006 10:35 pm    Post subject: Reply with quote

Unless you post the patch on bugzilla I don't think they're gonna see it, or care.
_________________
Join the adopt an unanswered post initiative today
Join the EFF!
Join the Drug Policy Alliance!
Back to top
View user's profile Send private message
ClaesBas
n00b
n00b


Joined: 20 Jul 2002
Posts: 38
Location: Stockholm

PostPosted: Tue Aug 01, 2006 10:43 pm    Post subject: Reply with quote

OK, I make a shot with the "patch" in bugzilla!

/Claes
Back to top
View user's profile Send private message
ClaesBas
n00b
n00b


Joined: 20 Jul 2002
Posts: 38
Location: Stockholm

PostPosted: Tue Aug 01, 2006 11:36 pm    Post subject: Reply with quote

I created a bug #142480 (https://bugs.gentoo.org/show_bug.cgi?id=142480)

Got answered very fast!

Quote:
------- Comment #3 From Zac Medico 2006-08-01 16:17 PST [reply] -------

In 2.1.1_pre it's currently possible to force color by exporting NOCOLOR=no in
the environment.

*** This bug has been marked as a duplicate of 42115 ***


And replied again:
Quote:

------- Comment #4 From Claes Strom 2006-08-01 16:24 PST [reply] -------

I think its easier and more logical to do:

emerge --force-color ... | less -r

than:

NOCOLOR=no emerge ... | less -r


What do you say, is the environment way OK or not? (I don't like it)

Maybe some more voices on Bugzilla needed.....

/Claes
Back to top
View user's profile Send private message
phatscum
n00b
n00b


Joined: 20 Mar 2006
Posts: 34
Location: Down the sewers

PostPosted: Tue Aug 01, 2006 11:45 pm    Post subject: Reply with quote

I like it as an environment variable (.bashrc), cause 99% of the time I'd want it enabled and whenever I need it disabled I can just do export blah, instead of using it as a emerge option all the time.
_________________
Computer games don't affect kids, I mean, if pacman affected us as kids we'd all run around in a darkened room munching pills and listening to repetitive music.
Back to top
View user's profile Send private message
ectospasm
l33t
l33t


Joined: 19 Feb 2003
Posts: 711
Location: Mobile, AL, USA

PostPosted: Wed Aug 02, 2006 8:07 am    Post subject: Reply with quote

It's easier to script if you have it as a command line option. Ideally, the default setting should live in a run-control (rc) file somewhere. An environment variable is the next, less permanent way to change the behavior. A command line option only changes it for that invocation.

I like the command line option simply because it seems to be more like a design decision. The environment variable seems like an afterthought, a hack. But that's just me.
_________________
Join the adopt an unanswered post initiative today
Join the EFF!
Join the Drug Policy Alliance!
Back to top
View user's profile Send private message
all-inc.
Tux's lil' helper
Tux's lil' helper


Joined: 03 Jul 2004
Posts: 138
Location: Darmstadt.Germany.EU

PostPosted: Tue Aug 29, 2006 1:18 am    Post subject: Reply with quote

hi,
i was just looking for something like this ;)
IMHO a command line option is also good(hmm better) because:
  • if some apps really read the emerge output and NOCOLOR=no is set, they get into trouble
  • remeber, you can easily add alias emerge='emerge --force-color' (or, --color=yes) to your rc's instead of setting NOCOLOR and this or this way, you have color through pipes enabled with just typing emerge. BUT with the cmdline option it's not possible that the apps get colored data. if the apps invoke emerge via its full path, they don't use the alias set.


perhaps i should add this statement to the bugtracker if someone else didn't already ^^

all-inc.
_________________
# make config; not war
registered linux user # 265707
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum