diff options
author | joe <joe@FreeBSD.org> | 2000-06-05 03:51:29 +0000 |
---|---|---|
committer | joe <joe@FreeBSD.org> | 2000-06-05 03:51:29 +0000 |
commit | d65592899eb950dfecec6616ebeb08a77ce8fccd (patch) | |
tree | da43619f0b5ea8506d31a26bba4ec0ef41c564d9 /bin/ls | |
parent | 243ad5208b2df8a252932d583baf6d4647a576a9 (diff) | |
download | FreeBSD-src-d65592899eb950dfecec6616ebeb08a77ce8fccd.zip FreeBSD-src-d65592899eb950dfecec6616ebeb08a77ce8fccd.tar.gz |
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
Diffstat (limited to 'bin/ls')
-rw-r--r-- | bin/ls/extern.h | 4 | ||||
-rw-r--r-- | bin/ls/ls.c | 25 | ||||
-rw-r--r-- | bin/ls/print.c | 13 |
3 files changed, 31 insertions, 11 deletions
diff --git a/bin/ls/extern.h b/bin/ls/extern.h index 82e0207..9707ea7 100644 --- a/bin/ls/extern.h +++ b/bin/ls/extern.h @@ -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 diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 7f278b3..ee9da2c 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -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 diff --git a/bin/ls/print.c b/bin/ls/print.c index e1925ca..3474408 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -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 |