Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 198461 - eselect-1.0.10 outputs colour codes even if stdout is not a tty
Summary: eselect-1.0.10 outputs colour codes even if stdout is not a tty
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Hosted Projects
Classification: Unclassified
Component: eselect (show other bugs)
Hardware: All Linux
: High normal (vote)
Assignee: Gentoo eselect Team
URL:
Whiteboard:
Keywords: InVCS
Depends on:
Blocks:
 
Reported: 2007-11-08 16:13 UTC by Ulrich Müller
Modified: 2009-04-18 08:30 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Proposed patch for eselect (eselect.patch,1.65 KB, patch)
2007-11-08 16:16 UTC, Ulrich Müller
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ulrich Müller gentoo-dev 2007-11-08 16:13:57 UTC
The summary says it all.
Comment 1 Ulrich Müller gentoo-dev 2007-11-08 16:16:05 UTC
Created attachment 135500 [details, diff]
Proposed patch for eselect

This will disable colour codes if output is not going to a terminal.
Comment 2 Ulrich Müller gentoo-dev 2007-11-08 16:20:48 UTC
Alternatively, one could follow the convention used in programs like "ls" or "grep", i.e. use an option --color=WHEN or --colour=WHEN:

   By  default,  color is not used to distinguish types of files.  That is
   equivalent to using --color=none.  Using the --color option without the
   optional  WHEN  argument  is  equivalent to using --color=always.  With
   --color=auto, color codes are output only if standard  output  is  con-
   nected  to  a  terminal  (tty).
Comment 3 Ulrich Müller gentoo-dev 2008-02-22 19:56:05 UTC
Any news here?

What solution should be taken, the simple one from attachment 135500 [details, diff], or the more elaborate one suggested in comment #2? I can prepare a patch for the latter if needed.
Comment 4 michael@smith-li.com 2008-02-22 21:03:02 UTC
Some related notes on terminals and colors:

The following should be used in addition to (not instead of) checking [[ -t 1 ]]

tput queries terminfo to determine the capability of the terminal and output things like color escapes only if the terminal supports it. If terminfo is correct then tput's output is guaranteed to be term-safe, so it's safer to use tput instead of bare escapes.

The BSD color convention is to set CLICOLOR=1 if you want colors, so if you can somehow check if a box is BSD, and CLICOLOR is unset or zero, it would be nice to automatically not print colors.

Comment 5 Ulrich Müller gentoo-dev 2008-02-23 08:39:25 UTC
The following colour definitions (in output.bash.in) work for me:

COLOUR_LIST_HEADER=$(tput -S <<<$'bold\nsetaf 2')
COLOUR_LIST_LEFT=$(tput bold)
COLOUR_LIST_RIGHT=$(tput sgr0)
COLOUR_ERROR=$(tput -S <<<$'bold\nsetaf 1')
COLOUR_NORMAL=$(tput sgr0)
COLOUR_HI=$(tput -S <<<$'bold\nsetaf 4')
COLOUR_WARN=$(tput -S <<<$'bold\nsetaf 1')

For completeness, sys-libs/ncurses should be added to RDEPEND of eselect.
Comment 6 Jeremy Olexa (darkside) (RETIRED) archtester gentoo-dev Security 2009-04-08 05:40:22 UTC
please do. another option for a release or -r3 material. your call.
Comment 7 Ulrich Müller gentoo-dev 2009-04-13 19:11:57 UTC
Committed to trunk (r401) and 1.0.x branch (r402).
Comment 8 Ulrich Müller gentoo-dev 2009-04-14 22:20:27 UTC
Another related problem:
Function get_column_width() calls "stty size" even if output is redirected, and will return a width of 0 in some situations (e.g., inside an Emacs shell window).

If there are no objections, I'll commit the following fix for output.bash:

 get_column_width() {
     if [[ -z "${COLS}" ]] ; then
-        COLS=( $(stty size 2>/dev/null) )
-        is_number "${COLS[1]}" && COLS=${COLS[1]} || COLS=79
+        COLS=79
+        if [[ -t 1 ]] ; then
+            COLS=( $(stty size 2>/dev/null) )
+            is_number "${COLS[1]}" && COLS=${COLS[1]} || COLS=79
+        fi
     fi
Comment 9 Ulrich Müller gentoo-dev 2009-04-15 21:46:16 UTC
(In reply to comment #8)
> If there are no objections, I'll commit the following fix for output.bash:

Committed to trunk (r423) and 1.0.x branch (r424).
Comment 10 Ulrich Müller gentoo-dev 2009-04-18 08:30:16 UTC
Fixed in 1.0.12.