summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2020-04-05 12:55:00 +0200
committerDave Davenport <qball@gmpclient.org>2020-04-05 12:56:33 +0200
commitbacecf3044be0dea3f8c5e06a2354443f09152eb (patch)
treebb452b22afaaef18d4c4c95a45cd2108798feac7
parent5879fd60996b376edfb16f424fc1ffd0e3f49cd3 (diff)
[Script] Add environment variable to indicate state.
* ROFI_RETV shows the state rofi was in before calling rofi.
-rwxr-xr-xExamples/test_script_env.sh4
-rw-r--r--doc/rofi-script.516
-rw-r--r--doc/rofi-script.5.markdown13
-rw-r--r--source/dialogs/script.c35
4 files changed, 54 insertions, 14 deletions
diff --git a/Examples/test_script_env.sh b/Examples/test_script_env.sh
new file mode 100755
index 00000000..d9beff87
--- /dev/null
+++ b/Examples/test_script_env.sh
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+
+
+echo "${ROFI_RETV}"
diff --git a/doc/rofi-script.5 b/doc/rofi-script.5
index 55abbfed..2ebd1ca3 100644
--- a/doc/rofi-script.5
+++ b/doc/rofi-script.5
@@ -57,6 +57,22 @@ echo "quit"
.PP
This shows two entries, reload and quit. When the quit entry is selected, rofi closes.
+.SH Environment
+.PP
+Rofi sets the following environment variable when executing the script:
+
+.SS \fB\fCROFI\_RETV\fR
+.PP
+An integer number with the current state:
+.IP \(bu 2
+\fB0\fP: Initial call of script.
+.IP \(bu 2
+\fB1\fP: Selected an entry.
+.IP \(bu 2
+\fB2\fP: Selected a custom entry.
+.IP \(bu 2
+\fB10\-28\fP: Custom keybinding 1\-19
+
.SH Passing mode options
.PP
Extra options, like setting the prompt, can be set by the script.
diff --git a/doc/rofi-script.5.markdown b/doc/rofi-script.5.markdown
index 7a97b127..63a37d57 100644
--- a/doc/rofi-script.5.markdown
+++ b/doc/rofi-script.5.markdown
@@ -46,6 +46,19 @@ echo "quit"
This shows two entries, reload and quit. When the quit entry is selected, rofi closes.
+## Environment
+
+Rofi sets the following environment variable when executing the script:
+
+### `ROFI_RETV`
+
+An integer number with the current state:
+
+ * **0**: Initial call of script.
+ * **1**: Selected an entry.
+ * **2**: Selected a custom entry.
+ * **10-28**: Custom keybinding 1-19
+
## Passing mode options
Extra options, like setting the prompt, can be set by the script.
diff --git a/source/dialogs/script.c b/source/dialogs/script.c
index 1aa21846..65d86bfb 100644
--- a/source/dialogs/script.c
+++ b/source/dialogs/script.c
@@ -134,7 +134,7 @@ static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
}
}
-static DmenuScriptEntry *get_script_output ( Mode *sw, char *command, char *arg, unsigned int *length )
+static DmenuScriptEntry *execute_executor ( Mode *sw, char *arg, unsigned int *length, int value )
{
ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data;
int fd = -1;
@@ -143,14 +143,25 @@ static DmenuScriptEntry *get_script_output ( Mode *sw, char *command, char *arg,
char **argv = NULL;
int argc = 0;
*length = 0;
- if ( g_shell_parse_argv ( command, &argc, &argv, &error ) ) {
+
+
+ // Environment
+ char ** env = g_get_environ ();
+
+ char *str_value = g_strdup_printf("%d", value);
+ env = g_environ_setenv ( env, "ROFI_RETV", str_value, TRUE);
+ g_free ( str_value );
+
+
+ if ( g_shell_parse_argv ( sw->ed, &argc, &argv, &error ) ) {
argv = g_realloc ( argv, ( argc + 2 ) * sizeof ( char* ) );
argv[argc] = g_strdup ( arg );
argv[argc + 1] = NULL;
- g_spawn_async_with_pipes ( NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
+ g_spawn_async_with_pipes ( NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
}
+ g_strfreev ( env );
if ( error != NULL ) {
- char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", command, error->message );
+ char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", (char*)sw->ed, error->message );
rofi_view_error_dialog ( msg, FALSE );
g_free ( msg );
// print error.
@@ -203,12 +214,6 @@ static DmenuScriptEntry *get_script_output ( Mode *sw, char *command, char *arg,
return retv;
}
-static DmenuScriptEntry *execute_executor ( Mode *sw, char *result, unsigned int *length )
-{
- DmenuScriptEntry *retv = get_script_output ( sw, sw->ed, result, length );
- return retv;
-}
-
static void script_switcher_free ( Mode *sw )
{
if ( sw == NULL ) {
@@ -225,7 +230,7 @@ static int script_mode_init ( Mode *sw )
ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
pd->delim = '\n';
sw->private_data = (void *) pd;
- pd->cmd_list = get_script_output ( sw, (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
+ pd->cmd_list = execute_executor ( sw, NULL, &( pd->cmd_list_length ), 0 );
}
return TRUE;
}
@@ -261,18 +266,20 @@ static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned
retv = PREVIOUS_DIALOG;
}
else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
- retv = ( mretv & MENU_LOWER_MASK );
+ //retv = 1+( mretv & MENU_LOWER_MASK );
+ script_mode_reset_highlight ( sw );
+ new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length,10+( mretv & MENU_LOWER_MASK ) );
}
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
if ( rmpd->cmd_list[selected_line].nonselectable ) {
return RELOAD_DIALOG;
}
script_mode_reset_highlight ( sw );
- new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length );
+ new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length, 1 );
}
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
script_mode_reset_highlight ( sw );
- new_list = execute_executor ( sw, *input, &new_length );
+ new_list = execute_executor ( sw, *input, &new_length, 2 );
}
// If a new list was generated, use that an loop around.