cli: nmc_get_user_input() util function for getting user input
This commit is contained in:

committed by
Dan Williams

parent
981a687f77
commit
7c629bf0f4
@@ -170,6 +170,32 @@ nmc_terminal_show_progress (const char *str)
|
||||
idx = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ask user for input and return the string.
|
||||
* The caller is responsible for freeing the returned string.
|
||||
*/
|
||||
char *
|
||||
nmc_get_user_input (const char *ask_str)
|
||||
{
|
||||
char *line = NULL;
|
||||
size_t line_ln = 0;
|
||||
ssize_t read;
|
||||
|
||||
fprintf (stdout, "%s", ask_str);
|
||||
read = getline (&line, &line_ln, stdin);
|
||||
|
||||
/* Remove newline from the string */
|
||||
if (read < 1 || (read == 1 && line[0] == '\n')) {
|
||||
g_free (line);
|
||||
line = NULL;
|
||||
} else {
|
||||
if (line[read-1] == '\n')
|
||||
line[read-1] = '\0';
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find out how many columns an UTF-8 string occupies on the screen
|
||||
*/
|
||||
|
@@ -32,6 +32,7 @@ char *nmc_ip4_address_as_string (guint32 ip, GError **error);
|
||||
char *nmc_ip6_address_as_string (const struct in6_addr *ip, GError **error);
|
||||
void nmc_terminal_erase_line (void);
|
||||
void nmc_terminal_show_progress (const char *str);
|
||||
char *nmc_get_user_input (const char *ask_str);
|
||||
int nmc_string_screen_width (const char *start, const char *end);
|
||||
void set_val_str (NmcOutputField fields_array[], guint32 index, char *value);
|
||||
void set_val_arr (NmcOutputField fields_array[], guint32 index, char **value);
|
||||
|
Reference in New Issue
Block a user