Improve yesno function.

Add a preset paramater to yesno function saying which answer should be the
default. Ref:
http://www.archlinux.org/pipermail/pacman-dev/2007-June/008470.html

This allows us to answer no by default to some questions, like the -Scc one
mentioned in the above thread, and implemented by this patch.

Another advantage is that we don't have to repeat the [Y/n] in every
questions. It's only put once in yesno function. This highly reduces the
chances that YES and NO strings are translated, but not some questions,
which lead to obvious confusions.

Finally, the noconfirm variable only needs to be used in that yesno
function. So all other usages of it were removed.

Signed-off-by: Chantry Xavier <shiningxc@gmail.com>
This commit is contained in:
Chantry Xavier 2008-02-19 00:44:09 +01:00 committed by Dan McGee
parent ca1a187131
commit 143135e666
5 changed files with 49 additions and 55 deletions

View File

@ -251,41 +251,33 @@ void cb_trans_conv(pmtransconv_t event, void *data1, void *data2,
case PM_TRANS_CONV_INSTALL_IGNOREPKG:
if(data2) {
/* TODO we take this route based on data2 being not null? WTF */
*response = yesno(_(":: %s requires installing %s from IgnorePkg/IgnoreGroup. Install anyway? [Y/n] "),
*response = yesno(1, _(":: %s requires installing %s from IgnorePkg/IgnoreGroup. Install anyway?"),
alpm_pkg_get_name(data1),
alpm_pkg_get_name(data2));
} else {
*response = yesno(_(":: %s is in IgnorePkg/IgnoreGroup. Install anyway? [Y/n] "),
*response = yesno(1, _(":: %s is in IgnorePkg/IgnoreGroup. Install anyway?"),
alpm_pkg_get_name(data1));
}
break;
case PM_TRANS_CONV_REMOVE_HOLDPKG:
*response = yesno(_(":: %s is designated as a HoldPkg. Remove anyway? [Y/n] "),
*response = yesno(1, _(":: %s is designated as a HoldPkg. Remove anyway?"),
alpm_pkg_get_name(data1));
break;
case PM_TRANS_CONV_REPLACE_PKG:
if(!config->noconfirm) {
*response = yesno(_(":: Replace %s with %s/%s? [Y/n] "),
alpm_pkg_get_name(data1),
(char *)data3,
alpm_pkg_get_name(data2));
} else {
printf(_("Replacing %s with %s/%s\n"),
alpm_pkg_get_name(data1),
(char *)data3,
alpm_pkg_get_name(data2));
*response = 1;
}
*response = yesno(1, _(":: Replace %s with %s/%s?"),
alpm_pkg_get_name(data1),
(char *)data3,
alpm_pkg_get_name(data2));
break;
case PM_TRANS_CONV_CONFLICT_PKG:
*response = yesno(_(":: %s conflicts with %s. Remove %s? [Y/n] "),
*response = yesno(1, _(":: %s conflicts with %s. Remove %s?"),
(char *)data1,
(char *)data2,
(char *)data2);
break;
case PM_TRANS_CONV_LOCAL_NEWER:
if(!config->op_s_downloadonly) {
*response = yesno(_(":: %s-%s: local version is newer. Upgrade anyway? [Y/n] "),
*response = yesno(1, _(":: %s-%s: local version is newer. Upgrade anyway?"),
alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1));
} else {
@ -293,12 +285,8 @@ void cb_trans_conv(pmtransconv_t event, void *data1, void *data2,
}
break;
case PM_TRANS_CONV_CORRUPTED_PKG:
if(!config->noconfirm) {
*response = yesno(_(":: File %s is corrupted. Do you want to delete it? [Y/n] "),
(char *)data1);
} else {
*response = 1;
}
*response = yesno(1, _(":: File %s is corrupted. Do you want to delete it?"),
(char *)data1);
break;
}
}

View File

@ -75,11 +75,11 @@ int pacman_remove(alpm_list_t *targets)
printf(_(":: group %s:\n"), alpm_grp_get_name(grp));
list_display(" ", pkgnames);
all = yesno(_(" Remove whole content? [Y/n] "));
all = yesno(1, _(" Remove whole content?"));
for(p = pkgnames; p; p = alpm_list_next(p)) {
char *pkg = alpm_list_getdata(p);
if(all || yesno(_(":: Remove %s from group %s? [Y/n] "), pkg, (char *)alpm_list_getdata(i))) {
if(all || yesno(1, _(":: Remove %s from group %s?"), pkg, (char *)alpm_list_getdata(i))) {
finaltargs = alpm_list_add(finaltargs, strdup(pkg));
}
}
@ -155,7 +155,7 @@ int pacman_remove(alpm_list_t *targets)
list_display(_("Targets:"), lst);
FREELIST(lst);
/* get confirmation */
if(yesno(_("\nDo you want to remove these packages? [Y/n] ")) == 0) {
if(yesno(1, _("\nDo you want to remove these packages?")) == 0) {
remove_cleanup();
FREELIST(finaltargs);
return(1);

View File

@ -89,7 +89,7 @@ static int sync_cleandb(const char *dbpath, int keep_used) {
/* We have a directory that doesn't match any syncdb.
* Ask the user if he wants to remove it. */
if(!found) {
if(!yesno(_("Do you want to remove %s? [Y/n] "), path)) {
if(!yesno(1, _("Do you want to remove %s?"), path)) {
continue;
}
@ -108,7 +108,7 @@ static int sync_cleandb_all(void) {
char newdbpath[PATH_MAX];
printf(_("Database directory: %s\n"), dbpath);
if(!yesno(_("Do you want to remove unused repositories? [Y/n] "))) {
if(!yesno(1, _("Do you want to remove unused repositories?"))) {
return(0);
}
/* The sync dbs were previously put in dbpath/, but are now in dbpath/sync/,
@ -138,7 +138,7 @@ static int sync_cleancache(int level)
* package and see if it has an entry in the local DB; if not, delete it.
*/
printf(_("Cache directory: %s\n"), cachedir);
if(!yesno(_("Do you want to remove uninstalled packages from cache? [Y/n] "))) {
if(!yesno(1, _("Do you want to remove uninstalled packages from cache?"))) {
return(0);
}
printf(_("removing old packages from cache... "));
@ -185,7 +185,7 @@ static int sync_cleancache(int level)
} else {
/* full cleanup */
printf(_("Cache directory: %s\n"), cachedir);
if(!yesno(_("Do you want to remove ALL packages from cache? [Y/n] "))) {
if(!yesno(0, _("Do you want to remove ALL packages from cache?"))) {
return(0);
}
printf(_("removing all packages from cache... "));
@ -545,8 +545,8 @@ static int sync_trans(alpm_list_t *targets)
if(strcmp("pacman", alpm_pkg_get_name(spkg)) == 0) {
printf("\n");
printf(_(":: pacman has detected a newer version of itself.\n"));
if(yesno(_(":: Do you want to cancel the current operation\n"
":: and install the new pacman version now? [Y/n] "))) {
if(yesno(1, _(":: Do you want to cancel the current operation\n"
":: and install the new pacman version now?"))) {
if(sync_trans_release() == -1) {
return(1);
}
@ -597,14 +597,14 @@ static int sync_trans(alpm_list_t *targets)
const alpm_list_t *grppkgs = alpm_grp_get_pkgs(grp);
alpm_list_t *pkgs = alpm_list_remove_dupes(grppkgs);
list_display(" ", pkgs);
if(yesno(_(":: Install whole content? [Y/n] "))) {
if(yesno(1, _(":: Install whole content?"))) {
for(k = pkgs; k; k = alpm_list_next(k)) {
targets = alpm_list_add(targets, strdup(alpm_list_getdata(k)));
}
} else {
for(k = pkgs; k; k = alpm_list_next(k)) {
char *pkgname = alpm_list_getdata(k);
if(yesno(_(":: Install %s from group %s? [Y/n] "), pkgname, targ)) {
if(yesno(1, _(":: Install %s from group %s?"), pkgname, targ)) {
targets = alpm_list_add(targets, strdup(pkgname));
}
}
@ -693,19 +693,9 @@ static int sync_trans(alpm_list_t *targets)
printf("\n");
if(config->op_s_downloadonly) {
if(config->noconfirm) {
printf(_("Beginning download...\n"));
confirm = 1;
} else {
confirm = yesno(_("Proceed with download? [Y/n] "));
}
confirm = yesno(1, _("Proceed with download?"));
} else {
if(config->noconfirm) {
printf(_("Beginning upgrade process...\n"));
confirm = 1;
} else {
confirm = yesno(_("Proceed with installation? [Y/n] "));
}
confirm = yesno(1, _("Proceed with installation?"));
}
if(!confirm) {
goto cleanup;

View File

@ -527,28 +527,44 @@ void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local)
}
/* presents a prompt and gets a Y/N answer */
/* TODO there must be a better way */
int yesno(char *fmt, ...)
int yesno(short preset, char *fmt, ...)
{
char response[32];
va_list args;
FILE *stream;
if(config->noconfirm) {
return(1);
stream = stdout;
} else {
/* Use stderr so questions are always displayed when redirecting output */
stream = stderr;
}
va_start(args, fmt);
/* Use stderr so questions are always displayed when redirecting output */
vfprintf(stderr, fmt, args);
vfprintf(stream, fmt, args);
va_end(args);
if(preset) {
fprintf(stream, " %s ", _("[Y/n]"));
} else {
fprintf(stream, " %s ", _("[y/N]"));
}
if(config->noconfirm) {
fprintf(stream, "\n");
return(preset);
}
if(fgets(response, 32, stdin)) {
if(strlen(response) != 0) {
strtrim(response);
strtrim(response);
if(strlen(response) == 0) {
return(preset);
}
if(!strcasecmp(response, _("Y")) || !strcasecmp(response, _("YES")) || strlen(response) == 0) {
if(!strcasecmp(response, _("Y")) || !strcasecmp(response, _("YES"))) {
return(1);
} else if (!strcasecmp(response, _("N")) || !strcasecmp(response, _("NO"))) {
return(0);
}
}
return(0);

View File

@ -50,7 +50,7 @@ alpm_list_t *strsplit(const char *str, const char splitchar);
void string_display(const char *title, const char *string);
void list_display(const char *title, const alpm_list_t *list);
void display_targets(const alpm_list_t *syncpkgs, pmdb_t *db_local);
int yesno(char *fmt, ...);
int yesno(short preset, char *fmt, ...);
int pm_printf(pmloglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...) __attribute__((format(printf,3,4)));
int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list args) __attribute__((format(printf,3,0)));