mirror of
https://git.FreeBSD.org/src.git
synced 2025-01-27 16:39:08 +00:00
Revamp the colour support to allow for bold characters. Colours
are now defined using the characters a-h and A-H for the bold variants. The old way using 0-7 for the colours still works, but prints a message asking the user to switch. PR: bin/27374
This commit is contained in:
parent
fb717773e7
commit
c1499cf6cf
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=88583
@ -57,4 +57,6 @@ void colorquit __P((int));
|
||||
extern char *ansi_fgcol;
|
||||
extern char *ansi_bgcol;
|
||||
extern char *ansi_coloff;
|
||||
extern char *attrs_off;
|
||||
extern char *enter_bold;
|
||||
#endif
|
||||
|
34
bin/ls/ls.1
34
bin/ls/ls.1
@ -461,22 +461,38 @@ is the background color.
|
||||
The color designators are as follows:
|
||||
.Pp
|
||||
.Bl -tag -width 4n -offset indent -compact
|
||||
.It Sy 0
|
||||
.It Sy a
|
||||
black
|
||||
.It Sy 1
|
||||
.It Sy b
|
||||
red
|
||||
.It Sy 2
|
||||
.It Sy c
|
||||
green
|
||||
.It Sy 3
|
||||
.It Sy d
|
||||
brown
|
||||
.It Sy 4
|
||||
.It Sy e
|
||||
blue
|
||||
.It Sy 5
|
||||
.It Sy f
|
||||
magenta
|
||||
.It Sy 6
|
||||
.It Sy g
|
||||
cyan
|
||||
.It Sy 7
|
||||
.It Sy h
|
||||
light grey
|
||||
.It Sy A
|
||||
bold black, usually shows up as dark grey
|
||||
.It Sy B
|
||||
bold red
|
||||
.It Sy C
|
||||
bold green
|
||||
.It Sy D
|
||||
bold brown, usually shows up as yellow
|
||||
.It Sy E
|
||||
bold blue
|
||||
.It Sy F
|
||||
bold magenta
|
||||
.It Sy G
|
||||
bold cyan
|
||||
.It Sy H
|
||||
bold light grey; looks like bright white
|
||||
.It Sy x
|
||||
default foreground or background
|
||||
.El
|
||||
@ -514,7 +530,7 @@ directory writable to others, with sticky bit
|
||||
directory writable to others, without sticky bit
|
||||
.El
|
||||
.Pp
|
||||
The default is "4x5x2x3x1x464301060203", i.e. blue foreground and
|
||||
The default is "exfxcxdxbxegedabagacad", i.e. blue foreground and
|
||||
default background for regular directories, black foreground and red
|
||||
background for setuid executables, etc.
|
||||
.It Ev LS_COLWIDTHS
|
||||
|
@ -121,6 +121,8 @@ 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 */
|
||||
char *attrs_off; /* ANSI sequence to turn off attributes */
|
||||
char *enter_bold; /* ANSI sequence to set color to bold mode */
|
||||
#endif
|
||||
|
||||
int rval;
|
||||
@ -289,6 +291,8 @@ main(argc, argv)
|
||||
if (tgetent(termcapbuf, getenv("TERM")) == 1) {
|
||||
ansi_fgcol = tgetstr("AF", &bp);
|
||||
ansi_bgcol = tgetstr("AB", &bp);
|
||||
attrs_off = tgetstr("me", &bp);
|
||||
enter_bold = tgetstr("md", &bp);
|
||||
|
||||
/* To switch colours off use 'op' if
|
||||
* available, otherwise use 'oc', or
|
||||
|
@ -94,9 +94,14 @@ typedef enum Colors {
|
||||
C_NUMCOLORS /* just a place-holder */
|
||||
} Colors ;
|
||||
|
||||
char *defcolors = "4x5x2x3x1x464301060203";
|
||||
char *defcolors = "exfxcxdxbxegedabagacad";
|
||||
|
||||
/* colors for file types */
|
||||
static struct {
|
||||
int num[2];
|
||||
int bold;
|
||||
} colors[C_NUMCOLORS];
|
||||
|
||||
static int colors[C_NUMCOLORS][2];
|
||||
#endif
|
||||
|
||||
void
|
||||
@ -386,14 +391,17 @@ printcolor(c)
|
||||
{
|
||||
char *ansiseq;
|
||||
|
||||
if (colors[c][0] != -1) {
|
||||
ansiseq = tgoto(ansi_fgcol, 0, colors[c][0]);
|
||||
if (colors[c].bold)
|
||||
tputs(enter_bold, 1, putch);
|
||||
|
||||
if (colors[c].num[0] != -1) {
|
||||
ansiseq = tgoto(ansi_fgcol, 0, colors[c].num[0]);
|
||||
if (ansiseq)
|
||||
tputs(ansiseq, 1, putch);
|
||||
}
|
||||
|
||||
if (colors[c][1] != -1) {
|
||||
ansiseq = tgoto(ansi_bgcol, 0, colors[c][1]);
|
||||
if (colors[c].num[1] != -1) {
|
||||
ansiseq = tgoto(ansi_bgcol, 0, colors[c].num[1]);
|
||||
if (ansiseq)
|
||||
tputs(ansiseq, 1, putch);
|
||||
}
|
||||
@ -404,6 +412,7 @@ endcolor(sig)
|
||||
int sig;
|
||||
{
|
||||
tputs(ansi_coloff, 1, sig ? writech : putch);
|
||||
tputs(attrs_off, 1, sig ? writech : putch);
|
||||
}
|
||||
|
||||
static int
|
||||
@ -454,10 +463,13 @@ char *cs;
|
||||
{
|
||||
int i, j, len;
|
||||
char c[2];
|
||||
short legacy_warn = 0;
|
||||
|
||||
if (cs == NULL) cs = ""; /* LSCOLORS not set */
|
||||
len = strlen(cs);
|
||||
for (i = 0 ; i < C_NUMCOLORS ; i++) {
|
||||
colors[i].bold = 0;
|
||||
|
||||
if (len <= 2*i) {
|
||||
c[0] = defcolors[2*i];
|
||||
c[1] = defcolors[2*i+1];
|
||||
@ -467,17 +479,29 @@ char *cs;
|
||||
c[1] = cs[2*i+1];
|
||||
}
|
||||
for (j = 0 ; j < 2 ; j++) {
|
||||
if ((c[j] < '0' || c[j] > '7') &&
|
||||
tolower((unsigned char)c[j]) != 'x') {
|
||||
/* Legacy colours used 0-7 */
|
||||
if (c[j] >= '0' && c[j] <= '7') {
|
||||
colors[i].num[j] = c[j] - '0';
|
||||
if (!legacy_warn) {
|
||||
fprintf(stderr,
|
||||
"warn: colors are now defined "
|
||||
"using a-h instead of 0-9. "
|
||||
"see manual page.\n");
|
||||
}
|
||||
legacy_warn = 1;
|
||||
} else if (c[j] >= 'a' && c[j] <= 'h')
|
||||
colors[i].num[j] = c[j] - 'a';
|
||||
else if (c[j] >= 'A' && c[j] <= 'H') {
|
||||
colors[i].num[j] = c[j] - 'A';
|
||||
colors[i].bold = 1;
|
||||
} else if (tolower((unsigned char)c[j] == 'x'))
|
||||
colors[i].num[j] = -1;
|
||||
else {
|
||||
fprintf(stderr,
|
||||
"error: invalid character '%c' in LSCOLORS env var\n",
|
||||
c[j]);
|
||||
c[j] = defcolors[2*i+j];
|
||||
"error: invalid character '%c' in LSCOLORS"
|
||||
" env var\n", c[j]);
|
||||
colors[i].num[j] = defcolors[2*i+j]-'0';
|
||||
}
|
||||
if (tolower((unsigned char)c[j]) == 'x')
|
||||
colors[i][j] = -1;
|
||||
else
|
||||
colors[i][j] = c[j]-'0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user