ask-string: support prefilling a string query with an explicit string

This commit is contained in:
Lennart Poettering
2026-05-20 16:34:21 +02:00
parent 183051f23f
commit a80024e96d
8 changed files with 30 additions and 4 deletions
+12 -2
View File
@@ -283,6 +283,7 @@ static void clear_by_backspace(size_t n) {
int ask_string_full(
char **ret,
const char *prefill,
GetCompletionsCallback get_completions,
void *userdata,
const char *text, ...) {
@@ -296,8 +297,17 @@ int ask_string_full(
_cleanup_free_ char *string = NULL;
size_t n = 0;
if (get_completions) {
/* Figure out what string to preselect the query with */
if (prefill) {
/* Prefill query with explicit data if specified */
string = strdup(prefill);
if (!string)
return -ENOMEM;
n = strlen(string);
} else if (get_completions) {
/* Otherwise, figure out what string to preselect the query with */
_cleanup_strv_free_ char **completions = NULL;
r = get_completions("", GET_COMPLETIONS_PRESELECT, &completions, userdata);
if (r < 0)
+2 -2
View File
@@ -95,8 +95,8 @@ typedef enum GetCompletionsFlags {
} GetCompletionsFlags;
typedef int (*GetCompletionsCallback)(const char *key, GetCompletionsFlags flags, char ***ret_list, void *userdata);
int ask_string_full(char **ret, GetCompletionsCallback get_completions, void *userdata, const char *text, ...) _printf_(4, 5);
#define ask_string(ret, text, ...) ask_string_full(ret, NULL, NULL, text, ##__VA_ARGS__)
int ask_string_full(char **ret, const char *prefill, GetCompletionsCallback get_completions, void *userdata, const char *text, ...) _printf_(5, 6);
#define ask_string(ret, text, ...) ask_string_full(ret, NULL, NULL, NULL, text, ##__VA_ARGS__)
bool any_key_to_proceed(void);
int show_menu(char **x, size_t n_columns, size_t column_width, unsigned ellipsize_percentage, const char *grey_prefix, bool with_numbers);