extensions-tool: Prompt for template when appropriate

When creating an extension interactively, we currently always use
the default template unless the --template option is used.

Instead, display the list of available templates to the user and
prompt them to pick one if it wasn't specified.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/812
This commit is contained in:
Florian Müllner 2019-11-07 15:16:34 +01:00 committed by Florian Müllner
parent 6f6251c0bc
commit d229abf07d

View File

@ -18,6 +18,9 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#define _GNU_SOURCE /* for strcasestr */
#include <string.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
@ -250,14 +253,15 @@ create_extension (const char *uuid, const char *name, const char *description, c
}
static void
prompt_metadata (char **uuid, char **name, char **description)
prompt_metadata (char **uuid, char **name, char **description, char **template)
{
g_autoptr (GInputStream) stdin = NULL;
g_autoptr (GDataInputStream) istream = NULL;
if ((uuid == NULL || *uuid != NULL) &&
(name == NULL || *name != NULL) &&
(description == NULL || *description != NULL))
(description == NULL || *description != NULL) &&
(template == NULL || *template != NULL))
return;
stdin = g_unix_input_stream_new (0, FALSE);
@ -321,6 +325,72 @@ prompt_metadata (char **uuid, char **name, char **description)
g_print ("\n");
}
if (template != NULL && *template == NULL)
{
g_autoptr (GPtrArray) templates = get_templates ();
if (templates->len == 1)
{
GDesktopAppInfo *info = g_ptr_array_index (templates, 0);
*template = g_desktop_app_info_get_string (info, TEMPLATE_KEY);
}
else
{
int i;
g_print (_("Choose one of the available templates:\n"));
for (i = 0; i < templates->len; i++)
{
GAppInfo *info = g_ptr_array_index (templates, i);
g_print ("%d) %-10s %s\n",
i + 1,
g_app_info_get_name (info),
g_app_info_get_description (info));
}
while (*template == NULL)
{
g_autofree char *line = NULL;
g_print ("%s [1-%d]: ", _("Template"), templates->len);
line = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL);
if (line == NULL)
continue;
if (g_ascii_isdigit (*line))
{
long i = strtol (line, NULL, 10);
if (i > 0 && i <= templates->len)
{
GDesktopAppInfo *info;
info = g_ptr_array_index (templates, i - 1);
*template =
g_desktop_app_info_get_string (info, TEMPLATE_KEY);
}
}
else
{
for (i = 0; i < templates->len; i++)
{
GDesktopAppInfo *info = g_ptr_array_index (templates, i);
g_autofree char *cur_template = NULL;
cur_template =
g_desktop_app_info_get_string (info, TEMPLATE_KEY);
if (strcasestr (cur_template, line) != NULL)
*template = g_steal_pointer (&cur_template);
}
}
}
g_print ("\n");
}
}
}
int
@ -403,7 +473,7 @@ handle_create (int argc, char *argv[], gboolean do_help)
}
if (interactive)
prompt_metadata (&uuid, &name, &description);
prompt_metadata (&uuid, &name, &description, &template);
if (uuid == NULL || name == NULL || description == NULL)
{