summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQuentin Glidic <sardemff7+git@sardemff7.net>2019-02-28 22:47:41 +0100
committerQuentin Glidic <sardemff7+git@sardemff7.net>2019-03-03 13:04:37 +0100
commitebf05fb16b2178d07914cf29550437a25f2f087b (patch)
tree44d0075713656b115b1050cefc0a5eb4ca66cd2e
parent6bbbd08941f56802d14fea6292669ded3c7fb3ca (diff)
[Script] Add argument historywip/script-arg-history
Useful for submenus Signed-off-by: Quentin Glidic <sardemff7+git@sardemff7.net>
-rwxr-xr-xExamples/test_script_mode.sh22
-rw-r--r--source/dialogs/script.c25
2 files changed, 37 insertions, 10 deletions
diff --git a/Examples/test_script_mode.sh b/Examples/test_script_mode.sh
index d91f6a0f..cb32d486 100755
--- a/Examples/test_script_mode.sh
+++ b/Examples/test_script_mode.sh
@@ -1,14 +1,28 @@
#!/usr/bin/env bash
-if [ x"$@" = x"quit" ]
+if [ x"$1" = x"quit" ]
then
exit 0
fi
-if [ "$@" ]
+if [ "$4" ]
then
- # Override the previously set prompt.
- echo -en "\x00prompt\x1fChange prompt\n"
+ exit 0
+elif [ "$3" ]
+then
+ echo -en "\x00prompt\x1fsubsubsubmenu $3 $2 $1\n"
+ echo "quit"
+elif [ "$2" ]
+then
+ echo -en "\x00prompt\x1fsubsubmenu $2 $1\n"
+ for a in {20..25}
+ do
+ echo "$a"
+ done
+ echo "quit"
+elif [ "$1" ]
+then
+ echo -en "\x00prompt\x1fsubmenu $1\n"
for a in {1..10}
do
echo "$a"
diff --git a/source/dialogs/script.c b/source/dialogs/script.c
index c834167d..a69511c5 100644
--- a/source/dialogs/script.c
+++ b/source/dialogs/script.c
@@ -74,6 +74,8 @@ typedef struct
char *message;
char *prompt;
gboolean do_markup;
+
+ GPtrArray *args_history;
} ScriptModePrivateData;
static void parse_entry_extras ( G_GNUC_UNUSED Mode *sw, ScriptEntry *entry, char *buffer, size_t length )
@@ -126,18 +128,23 @@ static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
}
}
-static ScriptEntry *get_script_output ( Mode *sw, char *command, char *arg, unsigned int *length )
+static ScriptEntry *get_script_output ( Mode *sw, char *command, unsigned int *length )
{
+ ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data;
int fd = -1;
GError *error = NULL;
ScriptEntry *retv = NULL;
char **argv = NULL;
int argc = 0;
+
*length = 0;
+
if ( g_shell_parse_argv ( command, &argc, &argv, &error ) ) {
- argv = g_realloc ( argv, ( argc + 2 ) * sizeof ( char* ) );
- argv[argc] = g_strdup ( arg );
- argv[argc + 1] = NULL;
+ argv = g_realloc ( argv, ( argc + pd->args_history->len + 1 ) * sizeof ( char* ) );
+ guint i;
+ for ( i = 0 ; i < pd->args_history->len ; ++i )
+ argv[argc + i] = g_strdup ( g_ptr_array_index ( pd->args_history, pd->args_history->len - i - 1 ) );
+ argv[argc + i] = NULL;
g_spawn_async_with_pipes ( NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
}
if ( error != NULL ) {
@@ -193,7 +200,9 @@ static ScriptEntry *get_script_output ( Mode *sw, char *command, char *arg, unsi
static ScriptEntry *execute_executor ( Mode *sw, char *result, unsigned int *length )
{
- ScriptEntry *retv = get_script_output ( sw, sw->ed, result, length );
+ ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
+ g_ptr_array_add ( rmpd->args_history, g_strdup ( result ) );
+ ScriptEntry *retv = get_script_output ( sw, sw->ed, length );
return retv;
}
@@ -212,7 +221,8 @@ static int script_mode_init ( Mode *sw )
if ( sw->private_data == NULL ) {
ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
sw->private_data = (void *) pd;
- pd->cmd_list = get_script_output ( sw, (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
+ pd->args_history = g_ptr_array_new ();
+ pd->cmd_list = get_script_output ( sw, (char *) sw->ed, &( pd->cmd_list_length ) );
}
return TRUE;
}
@@ -282,6 +292,9 @@ static void script_mode_destroy ( Mode *sw )
g_free ( rmpd->cmd_list[i].entry );
g_free ( rmpd->cmd_list[i].icon_name );
}
+
+ g_ptr_array_add ( rmpd->args_history, NULL );
+ g_strfreev ( (gchar **) g_ptr_array_free ( rmpd->args_history, FALSE ) );
g_free ( rmpd->cmd_list );
g_free ( rmpd->message );
g_free ( rmpd->prompt );