From 697abdfdfc5a3aee299827d8c883f54f26f5a994 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Thu, 3 Dec 2015 18:21:23 +0100 Subject: [PATCH] [DMenu] Add option to read from file instead of stdin. --- Changelog | 1 + doc/rofi-manpage.markdown | 6 +++++- source/dialogs/dmenu.c | 22 +++++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Changelog b/Changelog index 21a04531..213c7223 100644 --- a/Changelog +++ b/Changelog @@ -7,6 +7,7 @@ - Autodetect number of HW-threads. - Disabled by default. - Highlight multiple selected rows (#287) + - Dmenu can read from file instead of stdin. Improvements: - Improve speed of reading stdin in dmenu mode. - Correctly handle modifier keys now. Should now support most weird keyboard layouts and switching between them. diff --git a/doc/rofi-manpage.markdown b/doc/rofi-manpage.markdown index 409a57fc..2cd83d69 100644 --- a/doc/rofi-manpage.markdown +++ b/doc/rofi-manpage.markdown @@ -29,7 +29,7 @@ [ -display *display* ] [ -bc *color* ] [ -bw *width* ] -[ -dmenu [ -p *prompt* ] [ -sep *separator* ] [ -l *selected line* ] [ -mesg ] [ -select ] ] +[ -dmenu [ -p *prompt* ] [ -sep *separator* ] [ -l *selected line* ] [ -mesg ] [ -select ] [ -input *input* ] ] [ -filter *filter* ] [ -ssh-client *client* ] [ -ssh-command *command* ] @@ -632,6 +632,10 @@ Dump the filtered list to stdout and quit. This can be used to get the list as **rofi** would filter it. Use together with `-filter` command. +`-input` *file* + +Reads from *file* instead of stdin. + ### Message dialog `-e` *message* diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index 781e6564..3c888a8c 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "rofi.h" #include "dialogs/dmenu.h" #include "helper.h" @@ -63,7 +64,7 @@ typedef struct _DmenuModePrivateData unsigned int cmd_list_length; } DmenuModePrivateData; -static char **get_dmenu ( unsigned int *length ) +static char **get_dmenu ( FILE *fd, unsigned int *length ) { TICK_N ( "Read stdin START" ); char **retv = NULL; @@ -73,7 +74,7 @@ static char **get_dmenu ( unsigned int *length ) gchar *data = NULL; size_t data_l = 0; ssize_t l = 0; - while ( ( l = getdelim ( &data, &data_l, config.separator, stdin ) ) > 0 ) { + while ( ( l = getdelim ( &data, &data_l, config.separator, fd ) ) > 0 ) { if ( rvlength < ( *length + 2 ) ) { rvlength *= 2; retv = g_realloc ( retv, ( rvlength ) * sizeof ( char* ) ); @@ -286,7 +287,22 @@ static void dmenu_mode_init ( Mode *sw ) if ( find_arg ( "-i" ) >= 0 ) { config.case_sensitive = FALSE; } - pd->cmd_list = get_dmenu ( &( pd->cmd_list_length ) ); + FILE *fd = NULL; + str = NULL; + if ( find_arg_str ("-input", &str)) { + char *estr = rofi_expand_path(str); + fd = fopen(str, "r"); + if(fd == NULL ){ + char *msg = g_markup_printf_escaped("Failed to open file: %s:\n\t%s", estr, strerror(errno)); + error_dialog (msg,TRUE); + g_free(msg); + g_free(estr); + return; + } + g_free(estr); + } + pd->cmd_list = get_dmenu ( fd == NULL?stdin:fd , &( pd->cmd_list_length ) ); + if(fd != NULL ) fclose(fd); } static int dmenu_token_match ( const Mode *sw, char **tokens, int not_ascii, int case_sensitive, unsigned int index )