Nico Sonack: 3 cmd: Add a way to disable mardown rendering when built with lowdown support cmdconfig: Rename check_yes to string_means_yes and return booleans instead of integers cmd: Add a way to disable markdown rendering when built with lowdown 8 files changed, 111 insertions(+), 27 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~herrhotzenplotz/gcli-devel/patches/55552/mbox | git am -3Learn more about email & git
Fixes #240
240 where? please provide a link, also the trailer format is invalid, it needs to be in the form of `Key: value` Fixes: https://gitlab.com/herrhotzenplotz/gcli/issues/240
Signed-off-by: Nico Sonack <nsonack@herrhotzenplotz.de> --- include/gcli/cmd/cmdconfig.h | 1 + src/cmd/cmd.c | 15 +++++++++++---- src/cmd/cmdconfig.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/gcli/cmd/cmdconfig.h b/include/gcli/cmd/cmdconfig.h index 33560557..f8065302 100644 --- a/include/gcli/cmd/cmdconfig.h +++ b/include/gcli/cmd/cmdconfig.h @@ -69,6 +69,7 @@ int gcli_config_get_repo(struct gcli_ctx *ctx, char const **, char const **); int gcli_config_get_remote(struct gcli_ctx *ctx, char **remote); int gcli_config_have_colours(struct gcli_ctx *ctx); int gcli_config_display_progress_spinner(struct gcli_ctx *ctx); +int gcli_config_render_markdown(struct gcli_ctx *ctx); bool gcli_config_enable_experimental(struct gcli_ctx *ctx); struct gcli_config_entries const *gcli_config_get_section_entries( struct gcli_ctx *ctx, char const *section_name); diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index 8981d74c..b0864f53 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -177,8 +177,8 @@ delete_repo(bool always_yes, const char *owner, const char *repo) } #ifdef HAVE_LIBLOWDOWN -void -gcli_pretty_print(char const *input, int indent, int maxlinelen, FILE *stream) +static void +gcli_render_markdown(char const *input, int indent, int maxlinelen, FILE *stream) { size_t input_size; struct lowdown_buf *out; @@ -222,7 +222,8 @@ gcli_pretty_print(char const *input, int indent, int maxlinelen, FILE *stream) lowdown_node_free(n); lowdown_doc_free(doc); } -#else +#endif + static int word_length(const char *x) { @@ -241,6 +242,13 @@ gcli_pretty_print(const char *input, int indent, int maxlinelen, FILE *out) if (!it) return; +#ifdef HAVE_LIBLOWDOWN + if (gcli_config_render_markdown(g_clictx)) { + gcli_render_markdown(input, indent, maxlinelen, out); + return; + } +#endif + while (*it) { int linelength = indent; fprintf(out, "%*.*s", indent, indent, ""); @@ -265,4 +273,3 @@ gcli_pretty_print(const char *input, int indent, int maxlinelen, FILE *out) fputc('\n', out); } } -#endif diff --git a/src/cmd/cmdconfig.c b/src/cmd/cmdconfig.c index b1b0cf43..e9b71174 100644 --- a/src/cmd/cmdconfig.c +++ b/src/cmd/cmdconfig.c @@ -65,6 +65,7 @@ struct gcli_config { int colours_disabled; /* NO_COLOR set or output is not a TTY */ int force_colours; /* -c option was given */ int no_spinner; /* don't show a progress spinner */ + int no_markdown; /* do not render markdown (when built with lowdown) */ int enable_experimental; /* enable experimental features */ sn_sv buffer; @@ -479,6 +480,9 @@ readenv(struct gcli_config *cfg) if ((tmp = getenv("GCLI_NOSPINNER"))) cfg->no_spinner = check_yes(tmp); + if ((tmp = getenv("GCLI_RENDER_MARKDOWN"))) + cfg->no_markdown = !check_yes(tmp);
The logic here always takes me a second to think through, since we want it enabled by default, maybe I miss the ! on first scan, or perhaps check_yes is poorly named and another helper for the inverse should be introduced... or maybe I've just spent too much time in ruby lately :-). if (is_set_to_no(getenv("GCLI_RENDER_MARKDOWN")) cfg->disable_markdown = true;
+ if ((tmp = getenv("GCLI_ENABLE_EXPERIMENTAL"))) cfg->enable_experimental = check_yes(tmp); } @@ -528,6 +532,10 @@ gcli_config_parse_args(struct gcli_ctx *ctx, int *argc, char ***argv) .has_arg = no_argument, .flag = &cfg->no_spinner, .val = 1 }, + { .name = "no-markdown", + .has_arg = no_argument, + .flag = &cfg->no_markdown, + .val = 1 }, { .name = "type", .has_arg = required_argument, .flag = NULL, @@ -1037,6 +1045,29 @@ gcli_config_display_progress_spinner(struct gcli_ctx *ctx) return 1; } +int +gcli_config_render_markdown(struct gcli_ctx *ctx) +{ + ensure_config(ctx); + + struct gcli_config *cfg; + cfg = ctx_config(ctx); + + if (cfg->no_markdown) + return 0;
We agreed to return true/false over 0/1 going forward, to make it easier to grok on a quick code scan. `gcli_config_enable_experimental` currently does this, a couple of the other helpers need updating. -g
+ + sn_sv cfg_entry = gcli_config_find_by_key(ctx, "defaults", "render-markdown"); + if (sn_sv_null(cfg_entry)) + return 1; + + if (!check_yes(sn_sv_to_cstr(cfg_entry))) { + cfg->no_markdown = 1; + return 0; + } + + return 1; +} + bool gcli_config_enable_experimental(struct gcli_ctx *ctx) { -- 2.44.0
As outlined by @gjn this is for improving readability. The integer flags in the config structure haven't been altered to use booleans as we must use integers for getopt_long to work properly. Signed-off-by: Nico Sonack <nsonack@herrhotzenplotz.de> --- include/gcli/cmd/cmdconfig.h | 4 ++-- src/cmd/cmdconfig.c | 38 +++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/include/gcli/cmd/cmdconfig.h b/include/gcli/cmd/cmdconfig.h index 3356055..2cbc9ad 100644 --- a/include/gcli/cmd/cmdconfig.h +++ b/include/gcli/cmd/cmdconfig.h @@ -67,8 +67,8 @@ sn_sv gcli_config_get_override_default_account(struct gcli_ctx *ctx); bool gcli_config_pr_inhibit_delete_source_branch(struct gcli_ctx *ctx); int gcli_config_get_repo(struct gcli_ctx *ctx, char const **, char const **); int gcli_config_get_remote(struct gcli_ctx *ctx, char **remote); -int gcli_config_have_colours(struct gcli_ctx *ctx); -int gcli_config_display_progress_spinner(struct gcli_ctx *ctx); +bool gcli_config_have_colours(struct gcli_ctx *ctx); +bool gcli_config_display_progress_spinner(struct gcli_ctx *ctx); bool gcli_config_enable_experimental(struct gcli_ctx *ctx); struct gcli_config_entries const *gcli_config_get_section_entries( struct gcli_ctx *ctx, char const *section_name); diff --git a/src/cmd/cmdconfig.c b/src/cmd/cmdconfig.c index b1b0cf4..c306380 100644 --- a/src/cmd/cmdconfig.c +++ b/src/cmd/cmdconfig.c @@ -433,7 +433,7 @@ ensure_config(struct gcli_ctx *ctx) /** Check input for a value that indicates yes/true */ static int -check_yes(char const *const tmp) +string_means_true(char const *const tmp) { size_t tmplen = strlen(tmp) + 1; char *tmp_lower = malloc(tmplen); @@ -452,6 +452,12 @@ check_yes(char const *const tmp) return is_yes; } +static bool +string_means_false(char const *const tmp) +{ + return !string_means_true(tmp); +} + /* readenv: Read values of environment variables and pre-populate the * config structure. */ static void @@ -474,13 +480,13 @@ readenv(struct gcli_config *cfg) * violate the definition to get expected and sane behaviour. */ tmp = getenv("NO_COLOR"); if (tmp && tmp[0] != '\0') - cfg->colours_disabled = check_yes(tmp); + cfg->colours_disabled = string_means_true(tmp); if ((tmp = getenv("GCLI_NOSPINNER"))) - cfg->no_spinner = check_yes(tmp); + cfg->no_spinner = string_means_true(tmp); if ((tmp = getenv("GCLI_ENABLE_EXPERIMENTAL"))) - cfg->enable_experimental = check_yes(tmp); + cfg->enable_experimental = string_means_true(tmp); } int @@ -989,19 +995,19 @@ gcli_config_get_repo(struct gcli_ctx *ctx, char const **const owner, return gcli_gitconfig_repo_by_remote(ctx, NULL, owner, repo, NULL); } -int +bool gcli_config_have_colours(struct gcli_ctx *ctx) { - static int tested_tty = 0; + static bool tested_tty = 0; struct gcli_config *cfg; cfg = ctx_config(ctx); if (cfg->force_colours) - return 1; + return true; if (cfg->colours_disabled) - return 0; + return false; if (tested_tty) return !cfg->colours_disabled; @@ -1011,12 +1017,12 @@ gcli_config_have_colours(struct gcli_ctx *ctx) else cfg->colours_disabled = true; - tested_tty = 1; + tested_tty = true; return !cfg->colours_disabled; } -int +bool gcli_config_display_progress_spinner(struct gcli_ctx *ctx) { ensure_config(ctx); @@ -1025,16 +1031,16 @@ gcli_config_display_progress_spinner(struct gcli_ctx *ctx) cfg = ctx_config(ctx); if (cfg->no_spinner) - return 0; + return false; sn_sv cfg_entry = gcli_config_find_by_key(ctx, "defaults", "disable-spinner"); if (sn_sv_null(cfg_entry)) - return 1; + return true; - if (check_yes(sn_sv_to_cstr(cfg_entry))) - return 0; + if (string_means_true(sn_sv_to_cstr(cfg_entry))) + return false; - return 1; + return true; } bool @@ -1052,5 +1058,5 @@ gcli_config_enable_experimental(struct gcli_ctx *ctx) if (sn_sv_null(cfg_entry)) return false; - return check_yes(sn_sv_to_cstr(cfg_entry)); + return string_means_true(sn_sv_to_cstr(cfg_entry)); } -- 2.46.2
This adds a new switch '--no-markdown', a config option 'render-markdown' as well as an environment variable 'GCLI_RENDER_MARKDOWN' that allow the user to opt out of markdown rendering when built with lowdown support. Fixes: https://gitlab.com/herrhotzenplotz/gcli/issues/240 Signed-off-by: Nico Sonack <nsonack@herrhotzenplotz.de> --- include/gcli/cmd/cmdconfig.h | 1 + src/cmd/cmd.c | 17 ++++++++++++----- src/cmd/cmdconfig.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/gcli/cmd/cmdconfig.h b/include/gcli/cmd/cmdconfig.h index 2cbc9ad..f31a5a7 100644 --- a/include/gcli/cmd/cmdconfig.h +++ b/include/gcli/cmd/cmdconfig.h @@ -69,6 +69,7 @@ int gcli_config_get_repo(struct gcli_ctx *ctx, char const **, char const **); int gcli_config_get_remote(struct gcli_ctx *ctx, char **remote); bool gcli_config_have_colours(struct gcli_ctx *ctx); bool gcli_config_display_progress_spinner(struct gcli_ctx *ctx); +bool gcli_config_render_markdown(struct gcli_ctx *ctx); bool gcli_config_enable_experimental(struct gcli_ctx *ctx); struct gcli_config_entries const *gcli_config_get_section_entries( struct gcli_ctx *ctx, char const *section_name); diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index 8981d74..cc0194a 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -1,5 +1,5 @@ /* - * Copyright 2022 Nico Sonack <nsonack@herrhotzenplotz.de> + * Copyright 2022-2024 Nico Sonack <nsonack@herrhotzenplotz.de> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -177,8 +177,8 @@ delete_repo(bool always_yes, const char *owner, const char *repo) } #ifdef HAVE_LIBLOWDOWN -void -gcli_pretty_print(char const *input, int indent, int maxlinelen, FILE *stream) +static void +gcli_render_markdown(char const *input, int indent, int maxlinelen, FILE *stream) { size_t input_size; struct lowdown_buf *out; @@ -222,7 +222,8 @@ gcli_pretty_print(char const *input, int indent, int maxlinelen, FILE *stream) lowdown_node_free(n); lowdown_doc_free(doc); } -#else +#endif + static int word_length(const char *x) { @@ -241,6 +242,13 @@ gcli_pretty_print(const char *input, int indent, int maxlinelen, FILE *out) if (!it) return; +#ifdef HAVE_LIBLOWDOWN + if (gcli_config_render_markdown(g_clictx)) { + gcli_render_markdown(input, indent, maxlinelen, out); + return; + } +#endif + while (*it) { int linelength = indent; fprintf(out, "%*.*s", indent, indent, ""); @@ -265,4 +273,3 @@ gcli_pretty_print(const char *input, int indent, int maxlinelen, FILE *out) fputc('\n', out); } } -#endif diff --git a/src/cmd/cmdconfig.c b/src/cmd/cmdconfig.c index c306380..f45ffb5 100644 --- a/src/cmd/cmdconfig.c +++ b/src/cmd/cmdconfig.c @@ -65,6 +65,7 @@ struct gcli_config { int colours_disabled; /* NO_COLOR set or output is not a TTY */ int force_colours; /* -c option was given */ int no_spinner; /* don't show a progress spinner */ + int no_markdown; /* do not render markdown (when built with lowdown) */ int enable_experimental; /* enable experimental features */ sn_sv buffer; @@ -485,6 +486,9 @@ readenv(struct gcli_config *cfg) if ((tmp = getenv("GCLI_NOSPINNER"))) cfg->no_spinner = string_means_true(tmp); + if ((tmp = getenv("GCLI_RENDER_MARKDOWN"))) + cfg->no_markdown = string_means_false(tmp); + if ((tmp = getenv("GCLI_ENABLE_EXPERIMENTAL"))) cfg->enable_experimental = string_means_true(tmp); } @@ -534,6 +538,10 @@ gcli_config_parse_args(struct gcli_ctx *ctx, int *argc, char ***argv) .has_arg = no_argument, .flag = &cfg->no_spinner, .val = 1 }, + { .name = "no-markdown", + .has_arg = no_argument, + .flag = &cfg->no_markdown, + .val = 1 }, { .name = "type", .has_arg = required_argument, .flag = NULL, @@ -1043,6 +1051,29 @@ gcli_config_display_progress_spinner(struct gcli_ctx *ctx) return true; } +bool +gcli_config_render_markdown(struct gcli_ctx *ctx) +{ + ensure_config(ctx); + + struct gcli_config *cfg; + cfg = ctx_config(ctx); + + if (cfg->no_markdown) + return false; + + sn_sv cfg_entry = gcli_config_find_by_key(ctx, "defaults", "render-markdown"); + if (sn_sv_null(cfg_entry)) + return true; + + if (string_means_false(sn_sv_to_cstr(cfg_entry))) { + cfg->no_markdown = true; + return false; + } + + return true; +} + bool gcli_config_enable_experimental(struct gcli_ctx *ctx) { -- 2.46.2