Skip to content

Commit

Permalink
Allow rl_get_y_or_n() to get a default answer
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-arch committed Dec 11, 2024
1 parent b68af69 commit 53ae886
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/bookmarks.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ check_bm_shortcut(const char *shortcut, const int add)
static int
bookmark_add(char *file, char *name, char *shortcut)
{
if (check_bm_path(file) == 1 && rl_get_y_or_n("Continue? [y/n] ") == 0)
if (check_bm_path(file) == 1 && rl_get_y_or_n(_("Continue? [y/n] "), 0) == 0)
return FUNC_SUCCESS;

int exit_status = FUNC_FAILURE;
Expand Down
6 changes: 3 additions & 3 deletions src/bulk_rename.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ rename_file(char *oldpath, char *newpath)
struct stat a;
if (lstat(npath, &a) == 0) {
xerror("br: '%s': %s\n", newpath, strerror(EEXIST));
if (rl_get_y_or_n(_("Overwrite this file? [y/n] ")) == 0) {
if (rl_get_y_or_n(_("Overwrite this file? [y/n] "), 0) == 0) {
free(npath);
return EEXIST;
}
Expand Down Expand Up @@ -378,7 +378,7 @@ check_dups(FILE *fp)
free(fnames[i]);
free(fnames);

if (dups > 0 && rl_get_y_or_n(_("Continue? [y/n] ")) == 0)
if (dups > 0 && rl_get_y_or_n(_("Continue? [y/n] "), 0) == 0)
return dups;

return 0;
Expand Down Expand Up @@ -472,7 +472,7 @@ bulk_rename(char **args, size_t *renamed, const size_t reload_list)
goto ERROR;

/* Ask the user for confirmation. */
if (rl_get_y_or_n(_("Continue? [y/n] ")) == 0)
if (rl_get_y_or_n(_("Continue? [y/n] "), 0) == 0)
goto ERROR;

/* Make sure the tmp file we're about to read is the same we originally
Expand Down
16 changes: 8 additions & 8 deletions src/file_operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ ask_and_create_file(void)

if (validate_filename(&filename, 0) == 0) {
xerror(_("new: '%s': Unsafe file name\n"), filename);
if (rl_get_y_or_n(_("Continue? [y/n] ")) == 0) {
if (rl_get_y_or_n(_("Continue? [y/n] "), 0) == 0) {
free(filename);
return FUNC_SUCCESS;
}
Expand Down Expand Up @@ -822,7 +822,7 @@ create_files(char **args, const int is_md)
if (validate_filename(&args[i], is_md) == 0) {
xerror(_("%s: '%s': Unsafe file name\n"),
is_md ? "md" : "new", args[i]);
if (rl_get_y_or_n(_("Continue? [y/n] ")) == 0)
if (rl_get_y_or_n(_("Continue? [y/n] "), 0) == 0)
continue;
}

Expand Down Expand Up @@ -1130,7 +1130,7 @@ edit_link(char *link)
/* Check new_path existence and warn the user if it does not exist. */
if (lstat(new_path, &attr) == -1) {
xerror("'%s': %s\n", new_path, strerror(errno));
if (rl_get_y_or_n(_("Relink as broken symbolic link? [y/n] ")) == 0) {
if (rl_get_y_or_n(_("Relink as broken symbolic link? [y/n] "), 0) == 0) {
free(new_path);
return FUNC_SUCCESS;
}
Expand Down Expand Up @@ -1343,13 +1343,13 @@ symlink_file(char **args)

if (lstat(target, &a) == -1) {
printf("link: '%s': %s\n", target, strerror(errno));
if (rl_get_y_or_n(_("Create broken symbolic link? [y/n] ")) == 0)
if (rl_get_y_or_n(_("Create broken symbolic link? [y/n] "), 0) == 0)
return FUNC_SUCCESS;
}

if (lstat(link_name, &a) != -1 && S_ISLNK(a.st_mode)) {
printf("link: '%s': %s\n", link_name, strerror(EEXIST));
if (rl_get_y_or_n(_("Overwrite this file? [y/n] ")) == 0)
if (rl_get_y_or_n(_("Overwrite this file? [y/n] "), 0) == 0)
return FUNC_SUCCESS;

if (unlinkat(XAT_FDCWD, link_name, 0) == -1) {
Expand Down Expand Up @@ -1440,7 +1440,7 @@ static int
vv_create_new_dir(const char *dir)
{
fprintf(stderr, _("vv: '%s': directory does not exist.\n"), dir);
if (rl_get_y_or_n(_("Create it? [y/n] ")) == 0)
if (rl_get_y_or_n(_("Create it? [y/n] "), 0) == 0)
return (-1);

char tmp[PATH_MAX + 1];
Expand Down Expand Up @@ -1816,7 +1816,7 @@ rm_confirm(struct rm_info *info, const size_t start, const int have_dirs)
for (i = start; info[i].name; i++)
print_file_name(info[i].name, info[i].dir);

return rl_get_y_or_n(_("Continue? [y/n] "));
return rl_get_y_or_n(_("Continue? [y/n] "), 0);
}

static int
Expand All @@ -1839,7 +1839,7 @@ check_rm_files(struct rm_info *info, const size_t start, const char *errname)
}

if (ret == FUNC_FAILURE) {
return (rl_get_y_or_n(_("Remove files anyway? [y/n] ")) == 0
return (rl_get_y_or_n(_("Remove files anyway? [y/n] "), 0) == 0
? FUNC_FAILURE : FUNC_SUCCESS);
}

Expand Down
2 changes: 1 addition & 1 deletion src/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ edit_history(char **args)
static int
clear_history_func(char **args)
{
if (rl_get_y_or_n(_("Clear history? [y/n] ")) == 0)
if (rl_get_y_or_n(_("Clear history? [y/n] "), 0) == 0)
return FUNC_SUCCESS;

/* Let's overwrite whatever was there. */
Expand Down
2 changes: 1 addition & 1 deletion src/keybinds.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ bind_kb_func(const char *func_name)
}

printf(_("New key: %s\n"), kb);
if (rl_get_y_or_n(_("Bind to this new key? [y/n] ")) == 0) {
if (rl_get_y_or_n(_("Bind to this new key? [y/n] "), 0) == 0) {
free(kb);
return FUNC_SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ confirm_sudo_cmd(char **cmd)
if (i > 0)
putchar('\n');

return rl_get_y_or_n(_("Run command? [y/n] "));
return rl_get_y_or_n(_("Run command? [y/n] "), 0);
}

/* Launch a new instance using CMD. If CMD is NULL, try "CONF.TERM clifm".
Expand Down
2 changes: 1 addition & 1 deletion src/name_cleaner.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ bleach_files(char **names)
* be done. */
if (do_edit == 1) {
printf(_("%zu file name(s) will be bleached\n"), f);
if (rl_get_y_or_n(_("Continue? [y/n] ")) != 1) {
if (rl_get_y_or_n(_("Continue? [y/n] "), 0) != 1) {
for (i = 0; i < f; i++) {
free(bfiles[i].original);
free(bfiles[i].replacement);
Expand Down
18 changes: 16 additions & 2 deletions src/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ static struct dirent **tagged_files = (struct dirent **)NULL;
static int tagged_files_n = 0;
#endif /* !_NO_TAGS */
static int cb_running = 0;
static char rl_default_answer = 0;

/* Get user input (y/n, uppercase is allowed) using _MSG as message.
* The question will be repeated until 'y' or 'n' is entered.
* If DEFAULT_ANSWER isn't zero, it will be used in case the user just
* presses Enter on an empty line.
* Returns 1 if 'y' or 0 if 'n'. */
int
rl_get_y_or_n(const char *msg_str)
rl_get_y_or_n(const char *msg_str, char default_answer)
{
rl_default_answer = default_answer != 0 ? default_answer : 0;

char *answer = (char *)NULL;
while (!answer) {
answer = rl_no_hist(msg_str, 0);
Expand Down Expand Up @@ -663,6 +667,16 @@ cb_linehandler(char *line)
rl_callback_handler_input = savestring(line, strlen(line));
rl_callback_handler_remove();
cb_running = 0;
} else {
/* Enter on empty line. If we have a default answer (set via
* rl_get_y_or_no()), let's return this answer. */
if (rl_default_answer != 0) {
rl_callback_handler_input = xnmalloc(2, sizeof(char));
*rl_callback_handler_input = rl_default_answer;
rl_callback_handler_input[1] = '\0';
rl_callback_handler_remove();
cb_running = 0;
}
}

free(line);
Expand Down
3 changes: 1 addition & 2 deletions src/readline.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ int initialize_readline(void);
int is_quote_char(const char c);
char **my_rl_completion(const char *text, int start, int end);
char *my_rl_path_completion(const char *text, int state);
int rl_get_y_or_n(const char *msg_str);
//char *rl_no_hist(const char *prompt_str);
int rl_get_y_or_n(const char *msg_str, char default_answer);
char *rl_no_hist(const char *prompt_str, const int tabcomp);
char *secondary_prompt(const char *prompt_str, const char *line);

Expand Down
4 changes: 2 additions & 2 deletions src/trash.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ confirm_removal(const size_t n)
char msg[256]; /* Big enough, in case of translations. */
snprintf(msg, sizeof(msg), _("Remove %zu file(s)? [y/n] "), n);

return rl_get_y_or_n(msg);
return rl_get_y_or_n(msg, 0);
}

/* We have removed N files from the trash can. Print the results. */
Expand Down Expand Up @@ -1111,7 +1111,7 @@ ask_for_confirmation(char **args)
free(name);
}

if (rl_get_y_or_n("Continue? [y/n] ") == 0)
if (rl_get_y_or_n(_("Continue? [y/n] "), 0) == 0)
return 0;

return i;
Expand Down

0 comments on commit 53ae886

Please sign in to comment.