From dca5caaa4778f2e7973372fa33483f724610f2dc Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Mon, 22 May 2017 09:24:05 +0200 Subject: [Theme] Add small caps options (not working?) --- source/dialogs/script.c | 122 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 103 insertions(+), 19 deletions(-) (limited to 'source/dialogs') diff --git a/source/dialogs/script.c b/source/dialogs/script.c index a4daf289..df006ba4 100644 --- a/source/dialogs/script.c +++ b/source/dialogs/script.c @@ -39,28 +39,104 @@ #include "rofi.h" #include "dialogs/script.h" #include "helper.h" - +#include "widgets/textbox.h" #include "mode-private.h" -static char **get_script_output ( const char *command, unsigned int *length ) + +#define START_OF_HEADING '\x01' +#define START_OF_TEXT '\x02' +#define UNIT_SEPARATOR '\x1F' + +typedef enum { + PARSING_ENTRIES = 0, + PARSING_HEADER = 1 + +} ParserMode; + +typedef struct +{ + unsigned int id; + + ParserMode pmode; + char **cmd_list; + unsigned int cmd_list_length; + /** Display msg */ + char *message; + char *prompt; + char *format; + gboolean markup_rows; +} ScriptModePrivateData; + +static void parse_header ( Mode *sw, ScriptModePrivateData *pd, char *buffer, const ssize_t buffer_length ) +{ + if ( *buffer == START_OF_TEXT ) + { + pd->pmode = PARSING_ENTRIES; + return; + } + char *unit_sep = strchr ( buffer, UNIT_SEPARATOR); + if ( unit_sep ) + { + const char *command = buffer; + const char *value = unit_sep+1; + *unit_sep = '\0'; + if ( strcmp ( command, "message" ) == 0 ) { + if ( pd->message ) g_free ( pd->message ); + pd->message = g_strdup(value); + } + else if ( strcmp ( command, "prompt" ) == 0 ) { + if ( pd->prompt ) g_free ( pd->prompt ); + pd->prompt = g_strdup(value); + sw->display_name = pd->prompt; + } + else if ( strcmp ( command, "markup_rows" ) == 0 ){ + pd->markup_rows = strcasecmp( value, "true") == 0; + } + else if ( strcmp ( command, "format" ) == 0 ) { + if ( pd->format ) g_free ( pd->format ); + pd->format = g_strdup(value); + } + } +} + +static char **get_script_output ( Mode *sw, ScriptModePrivateData *pd, const char *command, unsigned int *length ) { + // Default to parsing entries. + pd->pmode = PARSING_ENTRIES; + char **retv = NULL; *length = 0; + unsigned int actual_length = 0; int fd = execute_generator ( command ); if ( fd >= 0 ) { FILE *inp = fdopen ( fd, "r" ); if ( inp ) { char *buffer = NULL; size_t buffer_length = 0; - while ( getline ( &buffer, &buffer_length, inp ) > 0 ) { - retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) ); + ssize_t read_length = 0; + while ( (read_length = getline ( &buffer, &buffer_length, inp )) > 0 ) { + if ( buffer[read_length-1] == '\n' ){ + buffer[read_length-1] = '\0'; + read_length--; + } + if ( buffer[0] == START_OF_HEADING ) { + pd->pmode = PARSING_HEADER; + parse_header( sw, pd, buffer+1, read_length); + continue; + } + if ( pd->pmode == PARSING_HEADER ){ + parse_header(sw, pd, buffer, read_length); + continue; + } + + if ( actual_length < ((*length)+2)) { + actual_length += 128; + retv = g_realloc ( retv, ( actual_length ) * sizeof ( char* ) ); + } + retv[( *length )] = g_strdup ( buffer ); retv[( *length ) + 1] = NULL; - // Filter out line-end. - if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) { - retv[( *length )][strlen ( buffer ) - 1] = '\0'; - } ( *length )++; } @@ -78,9 +154,10 @@ static char **get_script_output ( const char *command, unsigned int *length ) static char **execute_executor ( Mode *sw, const char *result, unsigned int *length ) { + ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data; char *arg = g_shell_quote ( result ); char *command = g_strdup_printf ( "%s %s", (const char *) sw->ed, arg ); - char **retv = get_script_output ( command, length ); + char **retv = get_script_output ( sw, pd, command, length ); g_free ( command ); g_free ( arg ); return retv; @@ -96,19 +173,12 @@ static void script_switcher_free ( Mode *sw ) g_free ( sw ); } -typedef struct -{ - unsigned int id; - char **cmd_list; - unsigned int cmd_list_length; -} ScriptModePrivateData; - 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 ( (const char *) sw->ed, &( pd->cmd_list_length ) ); + pd->cmd_list = get_script_output ( sw,pd, (const char *) sw->ed, &( pd->cmd_list_length ) ); } return TRUE; } @@ -157,6 +227,8 @@ static void script_mode_destroy ( Mode *sw ) ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data; if ( rmpd != NULL ) { g_strfreev ( rmpd->cmd_list ); + g_free ( rmpd->message ); + g_free ( rmpd->prompt ); g_free ( rmpd ); sw->private_data = NULL; } @@ -164,6 +236,9 @@ static void script_mode_destroy ( Mode *sw ) static char *_get_display_value ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry ) { ScriptModePrivateData *rmpd = sw->private_data; + if ( rmpd->markup_rows ) { + *state |= MARKUP; + } return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL; } @@ -172,6 +247,14 @@ static int script_token_match ( const Mode *sw, GRegex **tokens, unsigned int in ScriptModePrivateData *rmpd = sw->private_data; return helper_token_match ( tokens, rmpd->cmd_list[index] ); } +static char *script_get_message ( const Mode *sw ) +{ + ScriptModePrivateData *pd = (ScriptModePrivateData *) mode_get_private_data ( sw ); + if ( pd->message ) { + return g_strdup ( pd->message ); + } + return NULL; +} #include "mode-private.h" Mode *script_switcher_parse_setup ( const char *str ) @@ -198,9 +281,10 @@ Mode *script_switcher_parse_setup ( const char *str ) sw->_result = script_mode_result; sw->_destroy = script_mode_destroy; sw->_token_match = script_token_match; - sw->_get_completion = NULL, - sw->_preprocess_input = NULL, + sw->_get_completion = NULL; + sw->_preprocess_input = NULL; sw->_get_display_value = _get_display_value; + sw->_get_message = script_get_message; return sw; } -- cgit v1.2.3