Don't look up the ANSI sequences each time a colour is changed,

this is extremely inefficient, instead write them all down at the
beginning.

The correct sequence to switch colours off is to first use 'op' if
it exists, otherwise use 'oc'.  If neither of these exist then we
shouldn't be doing colour with this terminal.

Reviewed by:	ache
This commit is contained in:
joe 2000-06-05 03:51:29 +00:00
parent 243ad5208b
commit d65592899e
3 changed files with 31 additions and 11 deletions

View File

@ -55,4 +55,8 @@ int prn_octal __P((char *));
void parsecolors __P((char *cs));
int colortype __P((mode_t mode));
void endcolor __P((void));
char *ansi_fgcol;
char *ansi_bgcol;
char *ansi_coloff;
#endif

View File

@ -119,6 +119,10 @@ int f_type; /* add type character for non-regular files */
int f_whiteout; /* show whiteout entries */
#ifdef COLORLS
int f_color; /* add type in color for non-regular files */
char *ansi_bgcol; /* ANSI sequence to set background colour */
char *ansi_fgcol; /* ANSI sequence to set foreground colour */
char *ansi_coloff; /* ANSI sequence to reset colours */
#endif
int rval;
@ -133,6 +137,12 @@ main(argc, argv)
int ch, fts_options, notused;
char *p;
#ifdef COLORLS
char termcapbuf[1024]; /* termcap definition buffer */
char tcapbuf[512]; /* capability buffer */
char *bp = tcapbuf;
#endif
(void) setlocale(LC_ALL, "");
/* Terminal defaults to -Cq, non-terminal defaults to -1. */
@ -198,8 +208,19 @@ main(argc, argv)
case 'G':
if (isatty(STDOUT_FILENO))
#ifdef COLORLS
if (tgetent(NULL, getenv("TERM")) == 1)
f_color = 1;
if (tgetent(termcapbuf, getenv("TERM")) == 1) {
ansi_fgcol = tgetstr("AF", &bp);
ansi_bgcol = tgetstr("AB", &bp);
/* To switch colours off use 'op' if
* available, otherwise use 'oc', or
* don't do colours at all. */
ansi_coloff = tgetstr("op", &bp);
if (!ansi_coloff)
ansi_coloff = tgetstr("oc", &bp);
if (ansi_fgcol && ansi_bgcol && ansi_coloff)
f_color = 1;
}
#else
(void)fprintf(stderr, "Color support not compiled in.\n");
#endif

View File

@ -352,22 +352,20 @@ printtype(mode)
}
#ifdef COLORLS
static char tcapbuf[512];
void
printcolor(c)
Colors c;
{
char *bp = tcapbuf;
char *ansiseq;
if (colors[c][0] != -1) {
ansiseq = tparm(tgetstr("AF", &bp), colors[c][0]);
ansiseq = tparm(ansi_fgcol, colors[c][0]);
if (ansiseq)
putp(ansiseq);
}
if (colors[c][1] != -1) {
ansiseq = tparm(tgetstr("AB", &bp), colors[c][1]);
ansiseq = tparm(ansi_bgcol, colors[c][1]);
if (ansiseq)
putp(ansiseq);
}
@ -376,11 +374,8 @@ printcolor(c)
void
endcolor()
{
char *bp = tcapbuf;
char *ansiseq;
ansiseq = tgetstr("se", &bp);
if (ansiseq)
putp(ansiseq);
if (ansi_coloff)
putp(ansi_coloff);
}
int