Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to disable color highlighting #107

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ infer-out
*.i*86
*.x86_64
*.hex

# VSCode
.vscode
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,24 @@ The default prefix for installation is `/usr/local/bin`.

Building the `tldr` client is pretty straightforward.

#### Requirements
### Requirements

- `clang`/`gcc`
- `libcurl` (`brew install curl` / `apt-get install libcurl-dev` / `apt-get install libcurl4-openssl-dev`)
- `libzip` (`brew install libzip` / `apt-get install libzip-dev`)
- `pkg-config` (`brew install pkg-config` / `apt-get install pkg-config`)

#### Compiling
### Compiling

The [`Makefile`](https://github.com/tldr-pages/tldr-c-client/blob/master/Makefile)
in the root directory has all you need for building the project.

Just call `make` and `tldr` will build itself.

```
```shell
make
```


## Autocompletion

Autocompletion is supported for `bash`, `zsh`, and `fish` and can be added by sourcing
Expand All @@ -73,7 +72,7 @@ the correct autocompletion file.
The files `autocomplete.zsh`, `autocomplete.bash`, and `autocomplete.fish` can be found in the `autocomplete`
folder in the root of the repository.

#### Installation
### Installation

To install the autocompletion, just move the script for your shell to an easy
to access the directory (like your home directory), and source it in your `.bashrc` or `.zshrc`.
Expand All @@ -85,20 +84,24 @@ mv autocomplete/complete.zsh ~/.tldr.complete
echo "source ~/.tldr.complete" >> ~/.zshrc
```


## Usage

```
usage: ./tldr [-v] [OPTION]... SEARCH
```shell
usage: tldr [-v] [OPTION]... SEARCH

available commands:
-v print verbose output
--version print version and exit
-h, --help print this help and exit
-u, --update update local database
-c, --clear-cache clear local database
-p, --platform=PLATFORM select platform, supported are linux / osx / sunos / common
-v print verbose output
--version print version and exit
-h, --help print this help and exit
-u, --update update local database
-c, --clear-cache clear local database
-l, --list list all entries in the local database
-p, --platform=PLATFORM select platform, supported are linux / osx / sunos / windows / common
--linux show command page for Linux
--osx show command page for OSX
--sunos show command page for SunOS
-r, --render=PATH render a local page for testing purposes
-C, --color force color display
```

## Configuration
Expand Down
50 changes: 30 additions & 20 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ construct_path(char *buf, size_t buflen, char const *home, char const *input,
}

int
parse_tldrpage(char const *input)
parse_tldrpage(char const *input, int color_enabled)
{
char c;
int i, len;
Expand All @@ -74,40 +74,49 @@ parse_tldrpage(char const *input)
switch (c) {
case '>':
start = i;
fprintf(stdout, "%s", ANSI_COLOR_EXPLANATION_FG);
if (color_enabled)
fprintf(stdout, "%s", ANSI_COLOR_EXPLANATION_FG);
continue;

case '-':
start = i;
fprintf(stdout, "%s", ANSI_COLOR_COMMENT_FG);
if (color_enabled)
fprintf(stdout, "%s", ANSI_COLOR_COMMENT_FG);
continue;

case '`':
start = i;
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
if (color_enabled)
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
fprintf(stdout, " ");
continue;

case '#':
start = i;
fprintf(stdout, "%s", ANSI_BOLD_ON);
fprintf(stdout, "%s", ANSI_COLOR_TITLE_FG);
if (color_enabled) {
fprintf(stdout, "%s", ANSI_BOLD_ON);
fprintf(stdout, "%s", ANSI_COLOR_TITLE_FG);
}
continue;
}
} else if (start > -1) {
if (input[i] == '{' && input[i + 1] == '{') {
fprintf(stdout, "%.*s", i - (start + 1), input + (start + 1));
fprintf(stdout, "%s", ANSI_BOLD_OFF);
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
fprintf(stdout, "%s", ANSI_COLOR_CODE_PLACEHOLDER_FG);
if (color_enabled) {
fprintf(stdout, "%s", ANSI_BOLD_OFF);
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
fprintf(stdout, "%s", ANSI_COLOR_CODE_PLACEHOLDER_FG);
}

start = i;
for (i = i + 1; i < len; i++) {
if (input[i] == '}' && input[i + 1] == '}') {
fprintf(stdout, "%.*s", i - (start + 2),
input + (start + 2));
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
if (color_enabled) {
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
}
start = i + 1;
break;
}
Expand All @@ -127,9 +136,10 @@ parse_tldrpage(char const *input)
} else {
fprintf(stdout, "%.*s\n", i - (start + 2), input + (start + 2));
}

fprintf(stdout, "%s", ANSI_BOLD_OFF);
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
if (color_enabled) {
fprintf(stdout, "%s", ANSI_BOLD_OFF);
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
}
fprintf(stdout, "\n");
start = -1;
}
Expand All @@ -140,7 +150,7 @@ parse_tldrpage(char const *input)
}

int
print_tldrpage(char const *input, char const *poverride)
print_tldrpage(char const *input, char const *poverride, int color_enabled)
{
char *output;
char url[URLBUFSIZ];
Expand Down Expand Up @@ -178,15 +188,15 @@ print_tldrpage(char const *input, char const *poverride)
construct_path(url, URLBUFSIZ, homedir, input, platform);
if (stat(url, &sb) == 0 && S_ISREG(sb.st_mode)) {
if (!get_file_content(url, &output, 0)) {
parse_tldrpage(output);
parse_tldrpage(output, color_enabled);
free(output);
return 0;
}
} else {
construct_path(url, URLBUFSIZ, homedir, input, "common");
if (stat(url, &sb) == 0 && S_ISREG(sb.st_mode)) {
if (!get_file_content(url, &output, 0)) {
parse_tldrpage(output);
parse_tldrpage(output, color_enabled);
free(output);
return 0;
}
Expand All @@ -209,7 +219,7 @@ print_tldrpage(char const *input, char const *poverride)
return 1;
}

parse_tldrpage(output);
parse_tldrpage(output, color_enabled);

free(output);

Expand Down Expand Up @@ -293,11 +303,11 @@ parse_tldrlist(char const *path, char const *platform)
}

int
print_localpage(char const *path)
print_localpage(char const *path, int color_enabled)
{
char *output = NULL;
if (!get_file_content(path, &output, 0)) {
parse_tldrpage(output);
parse_tldrpage(output, color_enabled);
free(output);
return 0;
}
Expand Down
79 changes: 43 additions & 36 deletions src/tldr.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h> //for isatty

#define VERSION_TAG "v1.6.0"
#ifndef VERSION
#define VERSION_PRETTY ""
#define VERSION_PRETTY ""
#else
#define VERSION_PRETTY VERSION
#define VERSION_PRETTY VERSION
#endif

/* Help and usage */
void print_version (char const *arg);
void print_usage (char const *arg);
void print_version(char const *arg);
void print_usage(char const *arg);

/* getopt */
static int help_flag;
Expand All @@ -33,25 +34,24 @@ static int clear_flag;
static int platform_flag;
static int list_flag;
static int render_flag;
static int color_flag;
static char pbuf[STRBUFSIZ];
static struct option long_options[] = {
{ "help", no_argument, &help_flag, 1 },
{ "version", no_argument, &version_flag, 1 },
{ "verbose", no_argument, &verbose_flag, 1 },

{ "update", no_argument, &update_flag, 1 },
{ "clear-cache", no_argument, &clear_flag, 1 },
{ "platform", required_argument, 0, 'p' },
{ "linux", no_argument, 0, 'p' },
{ "osx", no_argument, 0, 'p' },
{ "sunos", no_argument, 0, 'p' },
{ "list", no_argument, &list_flag, 'l'},
{ "render", required_argument, 0, 'r'}, {0, 0, 0, 0 }
};

int
main(int argc, char **argv)
{
{"help", no_argument, &help_flag, 1},
{"version", no_argument, &version_flag, 1},
{"verbose", no_argument, &verbose_flag, 1},
{"update", no_argument, &update_flag, 1},
{"clear-cache", no_argument, &clear_flag, 1},
{"platform", required_argument, 0, 'p'},
{"linux", no_argument, 0, 'p'},
{"osx", no_argument, 0, 'p'},
{"sunos", no_argument, 0, 'p'},
{"list", no_argument, &list_flag, 'l'},
{"render", required_argument, 0, 'r'},
{"color", no_argument, &color_flag, 'C'},
{0, 0, 0, 0}};

int main(int argc, char **argv) {
int c;
int missing_arg;
int option_index;
Expand All @@ -61,6 +61,13 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}

char *no_color = getenv("NO_COLOR");
if (no_color == NULL || no_color[0] == '\0') {
color_flag = isatty(fileno(stdout));
} else {
color_flag = 0;
}

while (1) {
option_index = 0;
c = getopt_long_only(argc, argv, "v", long_options, &option_index);
Expand All @@ -79,12 +86,12 @@ main(int argc, char **argv)
break;

case '?':
/* do not set help flag, only show getopt error */
/* do not set the help flag, only show getopt error */
/* help_flag = 1; */
break;

case 'p': {
const char* platform_name = long_options[option_index].name;
const char *platform_name = long_options[option_index].name;
if (strcmp(platform_name, "platform") == 0) {
size_t len = strlen(optarg);
if (len > STRBUFSIZ)
Expand All @@ -108,6 +115,10 @@ main(int argc, char **argv)
render_flag = 1;
} break;

case 'C':
color_flag = 1;
break;

default:
abort();
}
Expand All @@ -117,7 +128,7 @@ main(int argc, char **argv)
check_localdate();
}

/* show help, if platform was supplied, but no further argument */
/* show help, if the platform was supplied, but no further argument */
missing_arg = (platform_flag && !list_flag && (optind == argc));
if (help_flag || missing_arg) {
print_usage(argv[0]);
Expand Down Expand Up @@ -150,7 +161,7 @@ main(int argc, char **argv)
return EXIT_SUCCESS;
}
if (render_flag) {
if (print_localpage(pbuf))
if (print_localpage(pbuf, 1))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
Expand All @@ -162,7 +173,7 @@ main(int argc, char **argv)
sum = 0;
while (optind < argc) {
len = strlen(argv[optind]);
if (sum+len >= 4096)
if (sum + len >= 4096)
exit(EXIT_FAILURE);
memcpy(buf + sum, argv[optind], len);
memcpy(buf + sum + len, "-", 1);
Expand All @@ -174,7 +185,7 @@ main(int argc, char **argv)

if (!has_localdb())
update_localdb(verbose_flag);
if (print_tldrpage(buf, pbuf[0] != 0 ? pbuf : NULL)) {
if (print_tldrpage(buf, pbuf[0] != 0 ? pbuf : NULL, color_flag)) {
fprintf(stdout, "This page doesn't exist yet!\n");
fprintf(stdout, "Submit new pages here: https://github.com/tldr-pages/tldr\n");
if (getenv(PREVENT_UPDATE_ENV_VARIABLE)) {
Expand All @@ -188,22 +199,18 @@ main(int argc, char **argv)
return EXIT_SUCCESS;
}

void
print_version(char const *arg)
{
void print_version(char const *arg) {
/* *INDENT-OFF* */
if (strcmp("", VERSION_PRETTY) == 0)
fprintf(stdout, "%s %s\n", arg, VERSION_TAG);
fprintf(stdout, "%s %s\n", arg, VERSION_TAG);
else
fprintf(stdout, "%s %s (%s)\n", arg, VERSION_TAG, VERSION_PRETTY);;
fprintf(stdout, "%s %s (%s)\n", arg, VERSION_TAG, VERSION_PRETTY);;
fprintf(stdout, "Copyright (C) 2016 Arvid Gerstmann\n");
fprintf(stdout, "Source available at https://github.com/tldr-pages/tldr-c-client\n");
/* *INDENT-ON* */
}

void
print_usage(char const *arg)
{
void print_usage(char const *arg) {
char const *out = "usage: %s [-v] [OPTION]... SEARCH\n\n";

/* *INDENT-OFF* */
Expand All @@ -222,6 +229,6 @@ print_usage(char const *arg)
fprintf(stdout, " %-20s %-30s\n", "--sunos", "show command page for SunOS");
fprintf(stdout, " %-20s %-30s\n", "-r, --render=PATH",
"render a local page for testing purposes");
fprintf(stdout, " %-20s %-30s\n", "-C, --color", "force color display");
/* *INDENT-ON* */
}

Loading