Add more configuration options to script mode, getting closer to dmenu.

- urgent
 - active
 - prompt
 - message
This commit is contained in:
Dave Davenport 2017-09-27 20:00:33 +02:00
parent b7a36f5ea5
commit a1ea3e268f
6 changed files with 243 additions and 122 deletions

15
Examples/test_script_mode.sh Executable file
View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
echo -en "\0prompt\x1ftesting\n"
echo -en "\0urgent\x1f0,2\n"
echo -en "\0active\x1f1\n"
echo -en "\0message\x1fSpecial message\n"
echo "aap"
echo "noot"
echo "mies"
echo "testing"
if [ -n "$@" ]
then
echo "$@"
fi

View file

@ -29,6 +29,7 @@
#define ROFI_HELPER_H
#include <cairo.h>
#include <rofi-types.h>
/**
* @defgroup HELPERS Helpers
@ -315,4 +316,25 @@ gboolean helper_execute_command ( const char *wd, const char *cmd, gboolean run_
*/
cairo_surface_t *cairo_image_surface_create_from_svg ( const gchar* file, int height );
/**
* Ranges.
*/
/**
* @param input String to parse
* @param list List of ranges
* @param length Length of list.
*
* ranges
*/
void parse_ranges ( char *input, rofi_range_pair **list, unsigned int *length );
/**
* @param format
* @param string
* @param selected_line
* @param filter
*/
void rofi_output_formatted_line ( const char *format, const char *string, int selected_line, const char *filter );
#endif // ROFI_HELPER_H

View file

@ -216,4 +216,13 @@ typedef struct Property
PropertyValue value;
} Property;
/**
* Structure to hold a range.
*/
typedef struct rofi_range_pair
{
unsigned int start;
unsigned int stop;
} rofi_range_pair;
#endif // INCLUDE_ROFI_TYPES_H

View file

@ -49,12 +49,6 @@
#include "xrmoptions.h"
#include "view.h"
struct range_pair
{
unsigned int start;
unsigned int stop;
};
static inline unsigned int bitget ( uint32_t *array, unsigned int index )
{
uint32_t bit = index % 32;
@ -78,9 +72,9 @@ typedef struct
unsigned int selected_line;
char *message;
char *format;
struct range_pair * urgent_list;
struct rofi_range_pair * urgent_list;
unsigned int num_urgent_list;
struct range_pair * active_list;
struct rofi_range_pair * active_list;
unsigned int num_active_list;
uint32_t *selected_list;
unsigned int num_selected_list;
@ -206,42 +200,7 @@ static unsigned int dmenu_mode_get_num_entries ( const Mode *sw )
return rmpd->cmd_list_length;
}
static void parse_pair ( char *input, struct range_pair *item )
{
int index = 0;
const char * const sep = "-";
for ( char *token = strsep ( &input, sep ); token != NULL; token = strsep ( &input, sep ) ) {
if ( index == 0 ) {
item->start = item->stop = (unsigned int) strtoul ( token, NULL, 10 );
index++;
}
else {
if ( token[0] == '\0' ) {
item->stop = 0xFFFFFFFF;
}
else{
item->stop = (unsigned int) strtoul ( token, NULL, 10 );
}
}
}
}
static void parse_ranges ( char *input, struct range_pair **list, unsigned int *length )
{
char *endp;
if ( input == NULL ) {
return;
}
const char *const sep = ",";
for ( char *token = strtok_r ( input, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
// Make space.
*list = g_realloc ( ( *list ), ( ( *length ) + 1 ) * sizeof ( struct range_pair ) );
// Parse a single pair.
parse_pair ( token, &( ( *list )[*length] ) );
( *length )++;
}
}
static gchar * dmenu_format_output_string ( const DmenuModePrivateData *pd, const char *input )
{
@ -295,61 +254,6 @@ static char *get_display_data ( const Mode *data, unsigned int index, int *state
return get_entry ? dmenu_format_output_string ( pd, retv[index] ) : NULL;
}
/**
* @param format The format string used. See below for possible syntax.
* @param string The selected entry.
* @param selected_line The selected line index.
* @param filter The entered filter.
*
* Function that outputs the selected line in the user-specified format.
* Currently the following formats are supported:
* * i: Print the index (0-(N-1))
* * d: Print the index (1-N)
* * s: Print input string.
* * q: Print quoted input string.
* * f: Print the entered filter.
* * F: Print the entered filter, quoted
*
* This functions outputs the formatted string to stdout, appends a newline (\n) character and
* calls flush on the file descriptor.
*/
static void dmenu_output_formatted_line ( const char *format, const char *string, int selected_line,
const char *filter )
{
for ( int i = 0; format && format[i]; i++ ) {
if ( format[i] == 'i' ) {
fprintf ( stdout, "%d", selected_line );
}
else if ( format[i] == 'd' ) {
fprintf ( stdout, "%d", ( selected_line + 1 ) );
}
else if ( format[i] == 's' ) {
fputs ( string, stdout );
}
else if ( format[i] == 'q' ) {
char *quote = g_shell_quote ( string );
fputs ( quote, stdout );
g_free ( quote );
}
else if ( format[i] == 'f' ) {
if ( filter ) {
fputs ( filter, stdout );
}
}
else if ( format[i] == 'F' ) {
if ( filter ) {
char *quote = g_shell_quote ( filter );
fputs ( quote, stdout );
g_free ( quote );
}
}
else {
fputc ( format[i], stdout );
}
}
fputc ( '\n', stdout );
fflush ( stdout );
}
static void dmenu_mode_free ( Mode *sw )
{
if ( mode_get_private_data ( sw ) == NULL ) {
@ -525,7 +429,7 @@ static void dmenu_print_results ( DmenuModePrivateData *pd, const char *input )
for ( unsigned int st = 0; st < pd->cmd_list_length; st++ ) {
if ( bitget ( pd->selected_list, st ) ) {
seen = TRUE;
dmenu_output_formatted_line ( pd->format, cmd_list[st], st, input );
rofi_output_formatted_line ( pd->format, cmd_list[st], st, input );
}
}
}
@ -534,7 +438,7 @@ static void dmenu_print_results ( DmenuModePrivateData *pd, const char *input )
if ( pd->selected_line != UINT32_MAX ) {
cmd = cmd_list[pd->selected_line];
}
dmenu_output_formatted_line ( pd->format, cmd, pd->selected_line, input );
rofi_output_formatted_line ( pd->format, cmd, pd->selected_line, input );
}
}
@ -696,7 +600,7 @@ int dmenu_switcher_dialog ( void )
}
}
if ( config.auto_select && cmd_list_length == 1 ) {
dmenu_output_formatted_line ( pd->format, cmd_list[0], 0, config.filter );
rofi_output_formatted_line ( pd->format, cmd_list[0], 0, config.filter );
return TRUE;
}
if ( find_arg ( "-password" ) >= 0 ) {
@ -723,7 +627,7 @@ int dmenu_switcher_dialog ( void )
unsigned int i = 0;
for ( i = 0; i < cmd_list_length; i++ ) {
if ( tokens == NULL || helper_token_match ( tokens, cmd_list[i] ) ) {
dmenu_output_formatted_line ( pd->format, cmd_list[i], i, config.filter );
rofi_output_formatted_line ( pd->format, cmd_list[i], i, config.filter );
}
}
tokenize_free ( tokens );

View file

@ -40,9 +40,67 @@
#include "dialogs/script.h"
#include "helper.h"
#include "widgets/textbox.h"
#include "mode-private.h"
static char **get_script_output ( char *command, char *arg, unsigned int *length )
typedef struct
{
/** ID of the current script. */
unsigned int id;
/** List of visible items. */
char **cmd_list;
/** length list of visible items. */
unsigned int cmd_list_length;
/** Urgent list */
struct rofi_range_pair * urgent_list;
unsigned int num_urgent_list;
/** Active list */
struct rofi_range_pair * active_list;
unsigned int num_active_list;
/** Configuration settings. */
char *message;
char *prompt;
gboolean do_markup;
} ScriptModePrivateData;
static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
{
ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data;
ssize_t length_key = 0;//strlen ( line );
while ( line[length_key] != '\x1f' && length_key <= length ) {
length_key++;
}
if ( length_key < length )
{
line[length_key] = '\0';
char *value = line+length_key+1;
if ( strcasecmp ( line, "message" ) == 0 ) {
g_free ( pd->message );
pd->message = g_strdup ( value );
} else if ( strcasecmp ( line, "prompt" ) == 0 ) {
g_free ( pd->prompt );
pd->prompt = g_strdup ( value );
sw->display_name = pd->prompt;
} else if ( strcasecmp ( line, "markup-rows" ) == 0 ) {
pd->do_markup = ( strcasecmp ( value, "true" ) == 0 );
} else if ( strcasecmp ( line, "urgent" ) == 0 ) {
parse_ranges ( value, &( pd->urgent_list ), &( pd->num_urgent_list ) );
} else if ( strcasecmp ( line, "active" ) == 0 ) {
parse_ranges ( value, &( pd->active_list ), &( pd->num_active_list ) );
}
}
}
static char **get_script_output ( Mode *sw, char *command, char *arg, unsigned int *length )
{
size_t actual_size = 0;
int fd = -1;
GError *error = NULL;
char **retv = NULL;
@ -68,17 +126,23 @@ static char **get_script_output ( char *command, char *arg, unsigned int *length
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* ) );
retv[( *length )] = g_strdup ( buffer );
retv[( *length ) + 1] = NULL;
ssize_t read_length = 0;
while ( (read_length = getline ( &buffer, &buffer_length, inp ) ) > 0 ) {
// Filter out line-end.
if ( retv[( *length )][strlen ( buffer ) - 1] == '\n' ) {
retv[( *length )][strlen ( buffer ) - 1] = '\0';
if ( buffer[read_length-1] == '\n' ) {
buffer[read_length-1] = '\0';
}
if ( buffer[0] == '\0' ) {
parse_header_entry ( sw, &buffer[1], read_length-1 );
} else {
if ( actual_size < ((*length)+2) ){
actual_size += 256;
retv = g_realloc ( retv, ( actual_size ) * sizeof ( char* ) );
}
retv[( *length )] = g_strdup ( buffer );
retv[( *length ) + 1] = NULL;
( *length )++;
}
( *length )++;
}
if ( buffer ) {
free ( buffer );
@ -94,7 +158,7 @@ static char **get_script_output ( char *command, char *arg, unsigned int *length
static char **execute_executor ( Mode *sw, char *result, unsigned int *length )
{
char **retv = get_script_output ( sw->ed, result, length );
char **retv = get_script_output ( sw, sw->ed, result, length );
return retv;
}
@ -108,19 +172,13 @@ 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 ( (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
pd->cmd_list = get_script_output ( sw, (char *) sw->ed, NULL, &( pd->cmd_list_length ) );
}
return TRUE;
}
@ -169,14 +227,31 @@ 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->urgent_list );
g_free ( rmpd->active_list );
g_free ( rmpd );
sw->private_data = NULL;
}
}
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;
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
ScriptModePrivateData *pd = sw->private_data;
for ( unsigned int i = 0; i < pd->num_active_list; i++ ) {
if ( selected_line >= pd->active_list[i].start && selected_line <= pd->active_list[i].stop ) {
*state |= ACTIVE;
}
}
for ( unsigned int i = 0; i < pd->num_urgent_list; i++ ) {
if ( selected_line >= pd->urgent_list[i].start && selected_line <= pd->urgent_list[i].stop ) {
*state |= URGENT;
}
}
if ( pd->do_markup ) {
*state |= MARKUP;
}
return get_entry ? g_strdup ( pd->cmd_list[selected_line] ) : NULL;
}
static int script_token_match ( const Mode *sw, GRegex **tokens, unsigned int index )
@ -184,6 +259,11 @@ 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 = sw->private_data;
return g_strdup ( pd->message );
}
#include "mode-private.h"
Mode *script_switcher_parse_setup ( const char *str )
@ -210,6 +290,7 @@ 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_message = script_get_message;
sw->_get_completion = NULL,
sw->_preprocess_input = NULL,
sw->_get_display_value = _get_display_value;

View file

@ -1114,3 +1114,93 @@ cairo_surface_t* cairo_image_surface_create_from_svg ( const gchar* file, int he
return surface;
}
static void parse_pair ( char *input, rofi_range_pair *item )
{
int index = 0;
const char * const sep = "-";
for ( char *token = strsep ( &input, sep ); token != NULL; token = strsep ( &input, sep ) ) {
if ( index == 0 ) {
item->start = item->stop = (unsigned int) strtoul ( token, NULL, 10 );
index++;
}
else {
if ( token[0] == '\0' ) {
item->stop = 0xFFFFFFFF;
}
else{
item->stop = (unsigned int) strtoul ( token, NULL, 10 );
}
}
}
}
void parse_ranges ( char *input, rofi_range_pair **list, unsigned int *length )
{
char *endp;
if ( input == NULL ) {
return;
}
const char *const sep = ",";
for ( char *token = strtok_r ( input, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
// Make space.
*list = g_realloc ( ( *list ), ( ( *length ) + 1 ) * sizeof ( struct rofi_range_pair ) );
// Parse a single pair.
parse_pair ( token, &( ( *list )[*length] ) );
( *length )++;
}
}
/**
* @param format The format string used. See below for possible syntax.
* @param string The selected entry.
* @param selected_line The selected line index.
* @param filter The entered filter.
*
* Function that outputs the selected line in the user-specified format.
* Currently the following formats are supported:
* * i: Print the index (0-(N-1))
* * d: Print the index (1-N)
* * s: Print input string.
* * q: Print quoted input string.
* * f: Print the entered filter.
* * F: Print the entered filter, quoted
*
* This functions outputs the formatted string to stdout, appends a newline (\n) character and
* calls flush on the file descriptor.
*/
void rofi_output_formatted_line ( const char *format, const char *string, int selected_line, const char *filter )
{
for ( int i = 0; format && format[i]; i++ ) {
if ( format[i] == 'i' ) {
fprintf ( stdout, "%d", selected_line );
}
else if ( format[i] == 'd' ) {
fprintf ( stdout, "%d", ( selected_line + 1 ) );
}
else if ( format[i] == 's' ) {
fputs ( string, stdout );
}
else if ( format[i] == 'q' ) {
char *quote = g_shell_quote ( string );
fputs ( quote, stdout );
g_free ( quote );
}
else if ( format[i] == 'f' ) {
if ( filter ) {
fputs ( filter, stdout );
}
}
else if ( format[i] == 'F' ) {
if ( filter ) {
char *quote = g_shell_quote ( filter );
fputs ( quote, stdout );
g_free ( quote );
}
}
else {
fputc ( format[i], stdout );
}
}
fputc ( '\n', stdout );
fflush ( stdout );
}