Skip to content

Commit a7cc5e5

Browse files
Vedaant-Rajookbdharun
authored andcommitted
Added an option to disable color highlighting
Disable color display when the output is not a tty. This change should not break the current workflow when tldr is used in terminals. Resolves: #35 Co-authored-by: ksqsf <[email protected]>
1 parent 17c4f42 commit a7cc5e5

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

src/parser.c

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ construct_path(char *buf, size_t buflen, char const *home, char const *input,
5959
}
6060

6161
int
62-
parse_tldrpage(char const *input)
62+
parse_tldrpage(char const *input, int color_enabled)
6363
{
6464
char c;
6565
int i, len;
@@ -74,40 +74,49 @@ parse_tldrpage(char const *input)
7474
switch (c) {
7575
case '>':
7676
start = i;
77-
fprintf(stdout, "%s", ANSI_COLOR_EXPLANATION_FG);
77+
if (color_enabled)
78+
fprintf(stdout, "%s", ANSI_COLOR_EXPLANATION_FG);
7879
continue;
7980

8081
case '-':
8182
start = i;
82-
fprintf(stdout, "%s", ANSI_COLOR_COMMENT_FG);
83+
if (color_enabled)
84+
fprintf(stdout, "%s", ANSI_COLOR_COMMENT_FG);
8385
continue;
8486

8587
case '`':
8688
start = i;
87-
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
89+
if (color_enabled)
90+
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
8891
fprintf(stdout, " ");
8992
continue;
9093

9194
case '#':
9295
start = i;
93-
fprintf(stdout, "%s", ANSI_BOLD_ON);
94-
fprintf(stdout, "%s", ANSI_COLOR_TITLE_FG);
96+
if (color_enabled) {
97+
fprintf(stdout, "%s", ANSI_BOLD_ON);
98+
fprintf(stdout, "%s", ANSI_COLOR_TITLE_FG);
99+
}
95100
continue;
96101
}
97102
} else if (start > -1) {
98103
if (input[i] == '{' && input[i + 1] == '{') {
99104
fprintf(stdout, "%.*s", i - (start + 1), input + (start + 1));
100-
fprintf(stdout, "%s", ANSI_BOLD_OFF);
101-
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
102-
fprintf(stdout, "%s", ANSI_COLOR_CODE_PLACEHOLDER_FG);
105+
if (color_enabled) {
106+
fprintf(stdout, "%s", ANSI_BOLD_OFF);
107+
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
108+
fprintf(stdout, "%s", ANSI_COLOR_CODE_PLACEHOLDER_FG);
109+
}
103110

104111
start = i;
105112
for (i = i + 1; i < len; i++) {
106113
if (input[i] == '}' && input[i + 1] == '}') {
107114
fprintf(stdout, "%.*s", i - (start + 2),
108115
input + (start + 2));
109-
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
110-
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
116+
if (color_enabled) {
117+
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
118+
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
119+
}
111120
start = i + 1;
112121
break;
113122
}
@@ -127,9 +136,10 @@ parse_tldrpage(char const *input)
127136
} else {
128137
fprintf(stdout, "%.*s\n", i - (start + 2), input + (start + 2));
129138
}
130-
131-
fprintf(stdout, "%s", ANSI_BOLD_OFF);
132-
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
139+
if (color_enabled) {
140+
fprintf(stdout, "%s", ANSI_BOLD_OFF);
141+
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
142+
}
133143
fprintf(stdout, "\n");
134144
start = -1;
135145
}
@@ -140,7 +150,7 @@ parse_tldrpage(char const *input)
140150
}
141151

142152
int
143-
print_tldrpage(char const *input, char const *poverride)
153+
print_tldrpage(char const *input, char const *poverride, int color_enabled)
144154
{
145155
char *output;
146156
char url[URLBUFSIZ];
@@ -178,15 +188,15 @@ print_tldrpage(char const *input, char const *poverride)
178188
construct_path(url, URLBUFSIZ, homedir, input, platform);
179189
if (stat(url, &sb) == 0 && S_ISREG(sb.st_mode)) {
180190
if (!get_file_content(url, &output, 0)) {
181-
parse_tldrpage(output);
191+
parse_tldrpage(output, color_enabled);
182192
free(output);
183193
return 0;
184194
}
185195
} else {
186196
construct_path(url, URLBUFSIZ, homedir, input, "common");
187197
if (stat(url, &sb) == 0 && S_ISREG(sb.st_mode)) {
188198
if (!get_file_content(url, &output, 0)) {
189-
parse_tldrpage(output);
199+
parse_tldrpage(output, color_enabled);
190200
free(output);
191201
return 0;
192202
}
@@ -209,7 +219,7 @@ print_tldrpage(char const *input, char const *poverride)
209219
return 1;
210220
}
211221

212-
parse_tldrpage(output);
222+
parse_tldrpage(output, color_enabled);
213223

214224
free(output);
215225

@@ -293,11 +303,11 @@ parse_tldrlist(char const *path, char const *platform)
293303
}
294304

295305
int
296-
print_localpage(char const *path)
306+
print_localpage(char const *path, int color_enabled)
297307
{
298308
char *output = NULL;
299309
if (!get_file_content(path, &output, 0)) {
300-
parse_tldrpage(output);
310+
parse_tldrpage(output, color_enabled);
301311
free(output);
302312
return 0;
303313
}

src/tldr.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ static int clear_flag;
3333
static int platform_flag;
3434
static int list_flag;
3535
static int render_flag;
36+
static int color_flag;
3637
static char pbuf[STRBUFSIZ];
3738
static struct option long_options[] = {
38-
{ "help", no_argument, &help_flag, 1 },
39-
{ "version", no_argument, &version_flag, 1 },
40-
{ "verbose", no_argument, &verbose_flag, 1 },
41-
42-
{ "update", no_argument, &update_flag, 1 },
43-
{ "clear-cache", no_argument, &clear_flag, 1 },
44-
{ "platform", required_argument, 0, 'p' },
45-
{ "linux", no_argument, 0, 'p' },
46-
{ "osx", no_argument, 0, 'p' },
47-
{ "sunos", no_argument, 0, 'p' },
48-
{ "list", no_argument, &list_flag, 'l'},
49-
{ "render", required_argument, 0, 'r'}, {0, 0, 0, 0 }
50-
};
39+
{"help", no_argument, &help_flag, 1},
40+
{"version", no_argument, &version_flag, 1},
41+
{"verbose", no_argument, &verbose_flag, 1},
42+
{"update", no_argument, &update_flag, 1},
43+
{"clear-cache", no_argument, &clear_flag, 1},
44+
{"platform", required_argument, 0, 'p'},
45+
{"linux", no_argument, 0, 'p'},
46+
{"osx", no_argument, 0, 'p'},
47+
{"sunos", no_argument, 0, 'p'},
48+
{"list", no_argument, &list_flag, 'l'},
49+
{"render", required_argument, 0, 'r'},
50+
{"color", no_argument, &color_flag, 'C'}, {0, 0, 0, 0}};
5151

5252
int
5353
main(int argc, char **argv)
@@ -60,7 +60,7 @@ main(int argc, char **argv)
6060
print_usage(argv[0]);
6161
return EXIT_FAILURE;
6262
}
63-
63+
color_flag = isatty(fileno(stdout));
6464
while (1) {
6565
option_index = 0;
6666
c = getopt_long_only(argc, argv, "v", long_options, &option_index);
@@ -108,6 +108,10 @@ main(int argc, char **argv)
108108
render_flag = 1;
109109
} break;
110110

111+
case 'C':
112+
color_flag = 1;
113+
break;
114+
111115
default:
112116
abort();
113117
}
@@ -150,7 +154,7 @@ main(int argc, char **argv)
150154
return EXIT_SUCCESS;
151155
}
152156
if (render_flag) {
153-
if (print_localpage(pbuf))
157+
if (print_localpage(pbuf, 1))
154158
return EXIT_FAILURE;
155159
return EXIT_SUCCESS;
156160
}
@@ -174,7 +178,7 @@ main(int argc, char **argv)
174178

175179
if (!has_localdb())
176180
update_localdb(verbose_flag);
177-
if (print_tldrpage(buf, pbuf[0] != 0 ? pbuf : NULL)) {
181+
if (print_tldrpage(buf, pbuf[0] != 0 ? pbuf : NULL, color_flag)) {
178182
fprintf(stdout, "This page doesn't exist yet!\n");
179183
fprintf(stdout, "Submit new pages here: https://github.com/tldr-pages/tldr\n");
180184
if (getenv(PREVENT_UPDATE_ENV_VARIABLE)) {
@@ -222,6 +226,7 @@ print_usage(char const *arg)
222226
fprintf(stdout, " %-20s %-30s\n", "--sunos", "show command page for SunOS");
223227
fprintf(stdout, " %-20s %-30s\n", "-r, --render=PATH",
224228
"render a local page for testing purposes");
229+
fprintf(stdout, " %-20s %-30s\n", "-C, --color", "force color display");
225230
/* *INDENT-ON* */
226231
}
227232

src/tldr.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ int construct_url (char *buf, size_t buflen,
7272
char const *platform);
7373
int construct_path (char *buf, size_t buflen, char const *home,
7474
char const *input, char const *platform);
75-
int parse_tldrpage (char const *input);
76-
int print_tldrpage (char const *input, char const *platform);
75+
int parse_tldrpage (char const *input, int color_enabled);
76+
int print_tldrpage (char const *input, char const *platform,
77+
int color_enabled);
7778
int print_tldrlist (char const *platform);
7879
int parse_tldrlist (char const *path, char const *platform);
79-
int print_localpage (char const *path);
80+
int print_localpage (char const *path, int color_enabled);
8081

8182
/* utils.c */
8283
#define RMOPT_IGNORE_NOFILE (0x1)

0 commit comments

Comments
 (0)