From d229abf07dda281dee3d13e8a544676821a8ea14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Thu, 7 Nov 2019 15:16:34 +0100 Subject: [PATCH] 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 --- .../extensions-tool/src/command-create.c | 76 ++++++++++++++++++- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/subprojects/extensions-tool/src/command-create.c b/subprojects/extensions-tool/src/command-create.c index 37a3118bc..9886afe6d 100644 --- a/subprojects/extensions-tool/src/command-create.c +++ b/subprojects/extensions-tool/src/command-create.c @@ -18,6 +18,9 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ +#define _GNU_SOURCE /* for strcasestr */ +#include + #include #include #include @@ -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) {