From 0d00678c305a34db173e0775be177535e1cdebfe Mon Sep 17 00:00:00 2001 From: ngn Date: Thu, 2 May 2024 00:50:53 +0300 Subject: [PATCH] update: better way to handle yes no prompt --- locale/tr/LC_MESSAGES/mc.po | 37 +++++++++++++++++++++---------------- src/config.c | 8 ++++---- src/log.c | 32 +++++++++++++++++++++++--------- src/log.h | 12 ++++++------ src/main.c | 4 +--- src/pull.c | 16 ++++++++-------- 6 files changed, 63 insertions(+), 46 deletions(-) diff --git a/locale/tr/LC_MESSAGES/mc.po b/locale/tr/LC_MESSAGES/mc.po index fd2ad26..7dbf41d 100644 --- a/locale/tr/LC_MESSAGES/mc.po +++ b/locale/tr/LC_MESSAGES/mc.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-05-01 23:04+0300\n" +"POT-Creation-Date: 2024-05-02 00:49+0300\n" "PO-Revision-Date: 2024-05-01 13:34+0300\n" "Last-Translator: \n" "Language-Team: Turkish \n" @@ -56,67 +56,72 @@ msgstr "" msgid "Failed to parse configuration file" msgstr "" -#: src/log.c:150 +#: src/log.c:145 msgid "y" msgstr "" -#: src/log.c:150 +#: src/log.c:145 msgid "Y" msgstr "" -#: src/log.c:152 +#: src/log.c:146 msgid "n" msgstr "" -#: src/log.c:152 +#: src/log.c:146 msgid "N" msgstr "" -#: src/log.c:155 +#: src/log.c:149 +#, c-format +msgid "%s [y/N] " +msgstr "" + +#: src/log.c:169 msgid "Please answer with y/n" msgstr "" -#: src/main.c:74 +#: src/main.c:72 #, c-format msgid "Failed to access paths (%s)" msgstr "" -#: src/main.c:82 +#: src/main.c:80 msgid "Failed to lock, mc is already running" msgstr "" -#: src/main.c:85 +#: src/main.c:83 msgid "Failed to lock, did you mess up your directory permissions?" msgstr "" -#: src/main.c:94 +#: src/main.c:92 #, c-format msgid "%s: command failed" msgstr "" -#: src/main.c:98 +#: src/main.c:96 #, c-format msgid "%s: command successful" msgstr "" -#: src/main.c:102 +#: src/main.c:100 #, c-format msgid "MatterLinux Configuration Manager (%s)" msgstr "" -#: src/main.c:103 +#: src/main.c:101 msgid "Different operations are done using different commands\n" msgstr "" -#: src/main.c:106 +#: src/main.c:104 msgid "pull down a configuration" msgstr "" -#: src/main.c:109 +#: src/main.c:107 msgid "build the configuration in the current directory" msgstr "" -#: src/main.c:111 +#: src/main.c:109 msgid "" "Licensed under GPLv3, see for more " "information" diff --git a/src/config.c b/src/config.c index f2e71e2..37362ed 100644 --- a/src/config.c +++ b/src/config.c @@ -44,8 +44,8 @@ bool config_is_valid(config_t *config) { } target_t *cur = config->t_first; - while(cur){ - if(!target_is_valid(cur)) + while (cur) { + if (!target_is_valid(cur)) return false; cur = cur->next; } @@ -92,12 +92,12 @@ int config_handler(void *data, const char *section, const char *key, const char } else if (MATCH("details", "keywords")) { clist_from_str(&config->keywords, (char *)value); return 1; - }else if(eq((char*)section, "details")) { + } else if (eq((char *)section, "details")) { goto UNKNOWN; return 1; } - if(NULL == config->t_last || (NULL != config->t_last->name && !eq(config->t_last->name, (char*)section))){ + if (NULL == config->t_last || (NULL != config->t_last->name && !eq(config->t_last->name, (char *)section))) { debug("adding new target => %s", section); config_add_target(config, strdup(section)); } diff --git a/src/log.c b/src/log.c index a8423f3..3de4163 100644 --- a/src/log.c +++ b/src/log.c @@ -95,7 +95,7 @@ void details(const char *msg, ...) { va_list args; va_start(args, msg); - printf(" "COLOR_RESET); + printf(" " COLOR_RESET); vprintf(msg, args); printf(COLOR_RESET "\n"); @@ -139,18 +139,32 @@ void input(const char *msg, ...) { } bool yesno(const char *msg){ - char question[strlen(msg)+12], inp[2] = {'n','\0'}, c; - sprintf(question, "%s [y/n]? ", msg); + if(env_mc_yes()) + return true; + + char *yes[] = {_("y"), _("Y")}; + char *no[] = {_("n"), _("N")}; + + char question[strlen(msg)+12], c; + sprintf(question, _("%s [y/N] "), msg); while(true){ input(question); - scanf("%c", &c); - inp[0] = c; - - if(eq(inp, _("y")) || eq(inp, _("Y"))) - return true; - else if(eq(inp, _("n")) || eq(inp, _("N"))) + + int c = getchar(); + if(c == '\n') return false; + getchar(); + + for(int i = 0; i < sizeof(yes)/sizeof(char*); i++){ + if(yes[i][0] == c) + return true; + } + + for(int i = 0; i < sizeof(no)/sizeof(char*); i++){ + if(no[i][0] == c) + return false; + } error(_("Please answer with y/n")); } diff --git a/src/log.h b/src/log.h index ddd4b79..7fdf0b0 100644 --- a/src/log.h +++ b/src/log.h @@ -1,12 +1,12 @@ #pragma once #include -#define COLOR_RED "\x1b[31m" -#define COLOR_BOLD "\x1b[1m" -#define COLOR_BLUE "\x1b[34m" -#define COLOR_CYAN "\x1b[36m" -#define COLOR_GREEN "\x1b[32m" -#define COLOR_MAGENTA "\x1b[35m" +#define COLOR_RED "\x1b[31m" +#define COLOR_BOLD "\x1b[1m" +#define COLOR_BLUE "\x1b[34m" +#define COLOR_CYAN "\x1b[36m" +#define COLOR_GREEN "\x1b[32m" +#define COLOR_MAGENTA "\x1b[35m" #define COLOR_UNDERLINE "\x1b[4m" #define COLOR_RESET "\x1b[0m" diff --git a/src/main.c b/src/main.c index 8bbd8d0..cc3396b 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,6 @@ // clang-format on -#include #include #include #include @@ -32,14 +31,13 @@ #include "args.h" #include "gen.h" +#include "intl.h" #include "lock.h" #include "log.h" #include "paths.h" #include "pull.h" #include "util.h" -#define _(x) gettext(x) - typedef bool (*cmd)(); struct CmdMap { char *name; diff --git a/src/pull.c b/src/pull.c index b234554..8a26dae 100644 --- a/src/pull.c +++ b/src/pull.c @@ -17,7 +17,7 @@ #define _(x) gettext(x) -int pull_progress(const git_indexer_progress *stats, void *data){ +int pull_progress(const git_indexer_progress *stats, void *data) { bar(stats->indexed_objects, stats->total_objects); return 0; } @@ -28,9 +28,9 @@ bool pull_cmd() { return false; } - char *repo_url = NULL, *repo_root = NULL; + char *repo_url = NULL, *repo_root = NULL; config_t repo_cfg = {.name = NULL}; - bool ret = false; + bool ret = false; if (url_is_local(args.list[1])) { repo_root = args.list[1]; @@ -51,7 +51,7 @@ bool pull_cmd() { git_repository *repo = NULL; git_clone_options opts = GIT_CLONE_OPTIONS_INIT; opts.fetch_opts.callbacks.transfer_progress = pull_progress; - opts.fetch_opts.callbacks.payload = NULL; + opts.fetch_opts.callbacks.payload = NULL; info(_("Cloning %s"), repo_url); bar_init(); @@ -89,13 +89,13 @@ COPY: printf("%s ", repo_cfg.keywords.c[i]); printf("\n\n"); - if(!yesno(_("Do you want to continue?"))) + if (!yesno(_("Do you want to continue?"))) goto END; info(_("Checking all the targets")); target_t *cur = repo_cfg.t_first; - while(NULL != cur){ - if(!exists(cur->src)){ + while (NULL != cur) { + if (!exists(cur->src)) { error(_("Failed to access the source for the target \"%s\""), cur->name); goto END; } @@ -105,7 +105,7 @@ COPY: success(_("All the targets are OK")); END: - if(repo_cfg.name != NULL) + if (repo_cfg.name != NULL) config_free(&repo_cfg); free(repo_url); return ret;