update: better way to handle yes no prompt

This commit is contained in:
ngn 2024-05-02 00:50:53 +03:00
parent 013ecfb3e7
commit 0d00678c30
6 changed files with 63 additions and 46 deletions

View File

@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2024-05-01 13:34+0300\n"
"Last-Translator: <ngn@ngn.tf>\n" "Last-Translator: <ngn@ngn.tf>\n"
"Language-Team: Turkish <gnome-turk@gnome.org>\n" "Language-Team: Turkish <gnome-turk@gnome.org>\n"
@ -56,67 +56,72 @@ msgstr ""
msgid "Failed to parse configuration file" msgid "Failed to parse configuration file"
msgstr "" msgstr ""
#: src/log.c:150 #: src/log.c:145
msgid "y" msgid "y"
msgstr "" msgstr ""
#: src/log.c:150 #: src/log.c:145
msgid "Y" msgid "Y"
msgstr "" msgstr ""
#: src/log.c:152 #: src/log.c:146
msgid "n" msgid "n"
msgstr "" msgstr ""
#: src/log.c:152 #: src/log.c:146
msgid "N" msgid "N"
msgstr "" 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" msgid "Please answer with y/n"
msgstr "" msgstr ""
#: src/main.c:74 #: src/main.c:72
#, c-format #, c-format
msgid "Failed to access paths (%s)" msgid "Failed to access paths (%s)"
msgstr "" msgstr ""
#: src/main.c:82 #: src/main.c:80
msgid "Failed to lock, mc is already running" msgid "Failed to lock, mc is already running"
msgstr "" msgstr ""
#: src/main.c:85 #: src/main.c:83
msgid "Failed to lock, did you mess up your directory permissions?" msgid "Failed to lock, did you mess up your directory permissions?"
msgstr "" msgstr ""
#: src/main.c:94 #: src/main.c:92
#, c-format #, c-format
msgid "%s: command failed" msgid "%s: command failed"
msgstr "" msgstr ""
#: src/main.c:98 #: src/main.c:96
#, c-format #, c-format
msgid "%s: command successful" msgid "%s: command successful"
msgstr "" msgstr ""
#: src/main.c:102 #: src/main.c:100
#, c-format #, c-format
msgid "MatterLinux Configuration Manager (%s)" msgid "MatterLinux Configuration Manager (%s)"
msgstr "" msgstr ""
#: src/main.c:103 #: src/main.c:101
msgid "Different operations are done using different commands\n" msgid "Different operations are done using different commands\n"
msgstr "" msgstr ""
#: src/main.c:106 #: src/main.c:104
msgid "pull down a configuration" msgid "pull down a configuration"
msgstr "" msgstr ""
#: src/main.c:109 #: src/main.c:107
msgid "build the configuration in the current directory" msgid "build the configuration in the current directory"
msgstr "" msgstr ""
#: src/main.c:111 #: src/main.c:109
msgid "" msgid ""
"Licensed under GPLv3, see <https://www.gnu.org/licenses/> for more " "Licensed under GPLv3, see <https://www.gnu.org/licenses/> for more "
"information" "information"

View File

@ -44,8 +44,8 @@ bool config_is_valid(config_t *config) {
} }
target_t *cur = config->t_first; target_t *cur = config->t_first;
while(cur){ while (cur) {
if(!target_is_valid(cur)) if (!target_is_valid(cur))
return false; return false;
cur = cur->next; 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")) { } else if (MATCH("details", "keywords")) {
clist_from_str(&config->keywords, (char *)value); clist_from_str(&config->keywords, (char *)value);
return 1; return 1;
}else if(eq((char*)section, "details")) { } else if (eq((char *)section, "details")) {
goto UNKNOWN; goto UNKNOWN;
return 1; 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); debug("adding new target => %s", section);
config_add_target(config, strdup(section)); config_add_target(config, strdup(section));
} }

View File

@ -95,7 +95,7 @@ void details(const char *msg, ...) {
va_list args; va_list args;
va_start(args, msg); va_start(args, msg);
printf(" "COLOR_RESET); printf(" " COLOR_RESET);
vprintf(msg, args); vprintf(msg, args);
printf(COLOR_RESET "\n"); printf(COLOR_RESET "\n");
@ -139,18 +139,32 @@ void input(const char *msg, ...) {
} }
bool yesno(const char *msg){ bool yesno(const char *msg){
char question[strlen(msg)+12], inp[2] = {'n','\0'}, c; if(env_mc_yes())
sprintf(question, "%s [y/n]? ", msg); return true;
char *yes[] = {_("y"), _("Y")};
char *no[] = {_("n"), _("N")};
char question[strlen(msg)+12], c;
sprintf(question, _("%s [y/N] "), msg);
while(true){ while(true){
input(question); input(question);
scanf("%c", &c);
inp[0] = c; int c = getchar();
if(c == '\n')
if(eq(inp, _("y")) || eq(inp, _("Y")))
return true;
else if(eq(inp, _("n")) || eq(inp, _("N")))
return false; 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")); error(_("Please answer with y/n"));
} }

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#define COLOR_RED "\x1b[31m" #define COLOR_RED "\x1b[31m"
#define COLOR_BOLD "\x1b[1m" #define COLOR_BOLD "\x1b[1m"
#define COLOR_BLUE "\x1b[34m" #define COLOR_BLUE "\x1b[34m"
#define COLOR_CYAN "\x1b[36m" #define COLOR_CYAN "\x1b[36m"
#define COLOR_GREEN "\x1b[32m" #define COLOR_GREEN "\x1b[32m"
#define COLOR_MAGENTA "\x1b[35m" #define COLOR_MAGENTA "\x1b[35m"
#define COLOR_UNDERLINE "\x1b[4m" #define COLOR_UNDERLINE "\x1b[4m"
#define COLOR_RESET "\x1b[0m" #define COLOR_RESET "\x1b[0m"

View File

@ -22,7 +22,6 @@
// clang-format on // clang-format on
#include <libintl.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
@ -32,14 +31,13 @@
#include "args.h" #include "args.h"
#include "gen.h" #include "gen.h"
#include "intl.h"
#include "lock.h" #include "lock.h"
#include "log.h" #include "log.h"
#include "paths.h" #include "paths.h"
#include "pull.h" #include "pull.h"
#include "util.h" #include "util.h"
#define _(x) gettext(x)
typedef bool (*cmd)(); typedef bool (*cmd)();
struct CmdMap { struct CmdMap {
char *name; char *name;

View File

@ -17,7 +17,7 @@
#define _(x) gettext(x) #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); bar(stats->indexed_objects, stats->total_objects);
return 0; return 0;
} }
@ -28,9 +28,9 @@ bool pull_cmd() {
return false; return false;
} }
char *repo_url = NULL, *repo_root = NULL; char *repo_url = NULL, *repo_root = NULL;
config_t repo_cfg = {.name = NULL}; config_t repo_cfg = {.name = NULL};
bool ret = false; bool ret = false;
if (url_is_local(args.list[1])) { if (url_is_local(args.list[1])) {
repo_root = args.list[1]; repo_root = args.list[1];
@ -51,7 +51,7 @@ bool pull_cmd() {
git_repository *repo = NULL; git_repository *repo = NULL;
git_clone_options opts = GIT_CLONE_OPTIONS_INIT; git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
opts.fetch_opts.callbacks.transfer_progress = pull_progress; 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); info(_("Cloning %s"), repo_url);
bar_init(); bar_init();
@ -89,13 +89,13 @@ COPY:
printf("%s ", repo_cfg.keywords.c[i]); printf("%s ", repo_cfg.keywords.c[i]);
printf("\n\n"); printf("\n\n");
if(!yesno(_("Do you want to continue?"))) if (!yesno(_("Do you want to continue?")))
goto END; goto END;
info(_("Checking all the targets")); info(_("Checking all the targets"));
target_t *cur = repo_cfg.t_first; target_t *cur = repo_cfg.t_first;
while(NULL != cur){ while (NULL != cur) {
if(!exists(cur->src)){ if (!exists(cur->src)) {
error(_("Failed to access the source for the target \"%s\""), cur->name); error(_("Failed to access the source for the target \"%s\""), cur->name);
goto END; goto END;
} }
@ -105,7 +105,7 @@ COPY:
success(_("All the targets are OK")); success(_("All the targets are OK"));
END: END:
if(repo_cfg.name != NULL) if (repo_cfg.name != NULL)
config_free(&repo_cfg); config_free(&repo_cfg);
free(repo_url); free(repo_url);
return ret; return ret;