Skip to content

Commit

Permalink
add NO_COLOR support
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Jun 5, 2022
1 parent 839b7fa commit 0228991
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gplaces is originally a Gemini port of the [delve](https://github.com/kieselstei
- ~command aliases~ use variables
- sh-style history with $XDG_DATA_HOME/gplaces_history or ~/.gplaces_history
- VT100 compatible with ANSI escape sequences
- with [NO_COLOR](https://no-color.org/) support
- no exotic external dependencies, no NIH
- ~GNU readline is fully optional~ bestline
- openssl or libressl
Expand Down Expand Up @@ -108,4 +109,4 @@ additional documentation and more details are available in `man gplaces`. type `
## Statistic
Language|files|blank|comment|code
:-------|-------:|-------:|-------:|-------:
C|1|232|58|1049
C|1|232|58|1058
27 changes: 18 additions & 9 deletions gplaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,19 @@ static SelectorList subscriptions = SIMPLEQ_HEAD_INITIALIZER(subscriptions);
static SelectorList menu = SIMPLEQ_HEAD_INITIALIZER(menu);
static char prompt[256] = "\33[35m>\33[0m ";
static int interactive;
static int color;


/*============================================================================*/
__attribute__((format(printf, 2, 3)))
static void error(int fatal, const char *fmt, ...) {
va_list va;
if (interactive) fwrite("\33[31m\n", 1, 6, stderr);
if (color) fwrite("\33[31m\n", 1, 6, stderr);
else fputc('\n', stderr);
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
if (interactive) fwrite("\33[0m\n", 1, 5, stderr);
if (color) fwrite("\33[0m\n", 1, 5, stderr);
else fputc('\n', stderr);
if (fatal) exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -455,12 +457,16 @@ static void print_gemtext_line(FILE *fp, Selector *sel, const regex_t *filter, i
switch (sel->type) {
case 'l':
if (i == 0) {
fprintf(fp, "\33[4;36m[%d]\33[0;39m %.*s\n", sel->index, out, &sel->repr[i]);
if (color) fprintf(fp, "\33[4;36m[%d]\33[0;39m %.*s\n", sel->index, out, &sel->repr[i]);
else fprintf(fp, "[%d] %.*s\n", sel->index, out, &sel->repr[i]);
break;
}
/* fall through */
case 'i': fprintf(fp, "%.*s\n", out, &sel->repr[i]); break;
case '#': fprintf(fp, "\33[4m%.*s\33[0m\n", out, &sel->repr[i]); break;
case '#':
if (color) fprintf(fp, "\33[4m%.*s\33[0m\n", out, &sel->repr[i]);
else fprintf(fp, "%.*s\n", out, &sel->repr[i]);
break;
case '`':
fprintf(fp, "%s\n", &sel->repr[i]);
break;
Expand Down Expand Up @@ -670,7 +676,8 @@ static int do_download(Selector *sel, SSL_CTX *ctx, const char *crtpath, const c

case '1':
if (!ask || !*meta) goto fail;
snprintf(buffer, sizeof(buffer), "\33[35m%.*s>\33[0m ", get_terminal_width() - 2, meta);
if (color) snprintf(buffer, sizeof(buffer), "\33[35m%.*s>\33[0m ", get_terminal_width() - 2, meta);
else snprintf(buffer, sizeof(buffer), "%.*s> ", get_terminal_width() - 2, meta);
if (data[1] == '1') bestlineMaskModeEnable();
if ((line = bestline(buffer)) == NULL) goto fail;
if (data[1] != '1' && interactive) bestlineHistoryAdd(line);
Expand All @@ -691,7 +698,8 @@ static int do_download(Selector *sel, SSL_CTX *ctx, const char *crtpath, const c
if (*meta) error(0, "`%s`: %s", sel->host, meta);
else error(0, "client certificate is required for `%s`", sel->host);
if (ask && stat(crtpath, &stbuf) != 0 && errno == ENOENT && stat(keypath, &stbuf) != 0 && errno == ENOENT) {
snprintf(buffer, sizeof(buffer), "\33[35mGenerate client certificate for `%s`? (y/n)>\33[0m ", sel->host);
if (color) snprintf(buffer, sizeof(buffer), "\33[35mGenerate client certificate for `%s`? (y/n)>\33[0m ", sel->host);
else snprintf(buffer, sizeof(buffer), "Generate client certificate for `%s`? (y/n)> ", sel->host);
if ((line = bestline(buffer)) != NULL) {
if (*line == 'y' || *line == 'Y') mkcert(crtpath, keypath);
free(line);
Expand Down Expand Up @@ -972,7 +980,8 @@ static void navigate(Selector *to) {
}

if (SIMPLEQ_EMPTY(&new)) return;
snprintf(prompt, sizeof(prompt), "\33[35m%s>\33[0m ", to->url + off);
if (color) snprintf(prompt, sizeof(prompt), "\33[35m%s>\33[0m ", to->url + off);
else snprintf(prompt, sizeof(prompt), "%s> ", to->url + off);
free_selectors(&menu);
menu = new;
if (interactive) page_gemtext(&menu);
Expand Down Expand Up @@ -1179,8 +1188,7 @@ static char *shell_hints(const char *buf, const char **ansi1, const char **ansi2
char *end;
long index;
int first = -1, last = -1;
(void)ansi1;
(void)ansi2;
if (!color) *ansi1 = *ansi2 = "";
if (strcspn(buf, " ") == 0) {
SIMPLEQ_FOREACH(sel, &menu, next) {
if (sel->type != 'l') continue;
Expand Down Expand Up @@ -1322,6 +1330,7 @@ int main(int argc, char **argv) {
SSL_load_error_strings();

interactive = isatty(STDOUT_FILENO);
if (!(color = interactive && (getenv("NO_COLOR") == NULL))) memcpy(prompt, "> ", 3);

load_rc_files(parse_arguments(argc, argv));

Expand Down

0 comments on commit 0228991

Please sign in to comment.