summaryrefslogtreecommitdiffstats
path: root/source/helper.c
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2017-09-27 20:00:33 +0200
committerDave Davenport <qball@gmpclient.org>2017-09-27 20:00:33 +0200
commita1ea3e268f92e9edcce6a794d98929effb036675 (patch)
tree4f5f9abe0a810b8f2bb35e8a7900a816dbbe827d /source/helper.c
parentb7a36f5ea50649b514c54591530e1bafc15b3578 (diff)
Add more configuration options to script mode, getting closer to dmenu.
- urgent - active - prompt - message
Diffstat (limited to 'source/helper.c')
-rw-r--r--source/helper.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/source/helper.c b/source/helper.c
index e8c74e34..e6d864df 100644
--- a/source/helper.c
+++ b/source/helper.c
@@ -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 );
+}