Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature/combi format string #1123

Draft
wants to merge 7 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ Settings config = {
.plugin_path = PLUGIN_PATH,
.max_history_size = 25,
.combi_hide_mode_prefix = FALSE,
/** Combi format display */
.combi_display_format = "{mode} {element}",
.combi_no_linebreak_modi = "",
.combi_no_linebreak_str = " ",

.matching_negate_char = '-',

Expand Down
4 changes: 4 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ typedef struct
/** Maximum history length per mode. */
unsigned int max_history_size;
gboolean combi_hide_mode_prefix;
/** Combi mode formatting */
char * combi_display_format;
char * combi_no_linebreak_modi;
char * combi_no_linebreak_str;

char matching_negate_char;

Expand Down
51 changes: 48 additions & 3 deletions source/dialogs/combi.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <pango/pango.h>
#include "mode-private.h"
#include <theme.h>
#include "widgets/textbox.h"

/**
* Combi Mode
Expand All @@ -47,6 +48,7 @@ typedef struct
{
Mode *mode;
gboolean disable;
gboolean print_newline;
} CombiMode;

typedef struct
Expand All @@ -69,6 +71,10 @@ static void combi_mode_parse_switchers ( Mode *sw )
char *switcher_str = g_strdup ( config.combi_modi );
const char * const sep = ",#";
// Split token on ','. This modifies switcher_str.

GHashTable *ht;
ht = g_hash_table_new ( g_str_hash, g_str_equal );

for ( char *token = strtok_r ( switcher_str, sep, &savept ); token != NULL;
token = strtok_r ( NULL, sep, &savept ) ) {
// Resize and add entry.
Expand All @@ -78,14 +84,19 @@ static void combi_mode_parse_switchers ( Mode *sw )
Mode *mode = rofi_collect_modi_search ( token );
if ( mode ) {
pd->switchers[pd->num_switchers].disable = FALSE;
pd->switchers[pd->num_switchers++].mode = mode;
pd->switchers[pd->num_switchers].mode = mode;
pd->switchers[pd->num_switchers].print_newline = TRUE;
g_hash_table_insert( ht, token, &( pd->switchers[pd->num_switchers++] ) );
/* g_hash_table_insert( ht, token, pd->switchers + pd->num_switchers ); */
}
else {
// If not build in, use custom switchers.
Mode *sw = script_switcher_parse_setup ( token );
if ( sw != NULL ) {
pd->switchers[pd->num_switchers].disable = FALSE;
pd->switchers[pd->num_switchers++].mode = sw;
pd->switchers[pd->num_switchers].mode = sw;
pd->switchers[pd->num_switchers].print_newline = TRUE;
g_hash_table_insert( ht, token, &( pd->switchers[pd->num_switchers++] ) );
}
else {
// Report error, don't continue.
Expand All @@ -94,8 +105,21 @@ static void combi_mode_parse_switchers ( Mode *sw )
}
}
}

savept = NULL;
for ( char *token = strtok_r ( config.combi_no_linebreak_modi, sep, &savept ); token != NULL;
token = strtok_r ( NULL, sep, &savept ) ) {
CombiMode *mode = g_hash_table_lookup ( ht, token );
if ( mode != NULL ) {
mode->print_newline = FALSE;
}
else {
g_warning ( "%s is in -combi-no-linebreak-modi but not in -combi-modi.", token );
}
}
// Free string that was modified by strtok_r
g_free ( switcher_str );
g_hash_table_destroy( ht );
}

static int combi_mode_init ( Mode *sw )
Expand Down Expand Up @@ -184,7 +208,11 @@ static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned
return mretv & MENU_LOWER_MASK;
}

unsigned offset = 0;
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
if ( pd->switchers[i].disable ) {
offset += pd->lengths[i];
}
if ( selected_line >= pd->starts[i] &&
selected_line < ( pd->starts[i] + pd->lengths[i] ) ) {
return mode_result ( pd->switchers[i].mode, mretv, input, selected_line - pd->starts[i] );
Expand Down Expand Up @@ -222,9 +250,26 @@ static char * combi_mgrv ( const Mode *sw, unsigned int selected_line, int *stat
char * retv;
char * str = retv = mode_get_display_value ( pd->switchers[i].mode, selected_line - pd->starts[i], state, attr_list, TRUE );
const char *dname = mode_get_display_name ( pd->switchers[i].mode );
char *format_str = g_strdup( config.combi_display_format );
if ( !config.combi_hide_mode_prefix ) {
retv = g_strdup_printf ( "%s %s", dname, str );
if ( ! ( *state & MARKUP ) ) {
// Mode does not use markup, but we want to, so escape output
char * tmp_str = g_markup_escape_text( str, -1 );
g_free(str);
str = tmp_str;
*state |= MARKUP;
}
char *dname_markup = g_markup_escape_text ( dname, -1 );
char *opt_linebreak = g_strdup(pd->switchers[i].print_newline ? "\n" : config.combi_no_linebreak_str);
retv = helper_string_replace_if_exists( format_str,
"{mode}", dname_markup,
"{linebreak}", opt_linebreak,
"{element}", str,
NULL );
g_free ( str );
g_free ( dname_markup );
g_free ( opt_linebreak );
g_free ( format_str );
}

if ( attr_list != NULL ) {
Expand Down
6 changes: 6 additions & 0 deletions source/xrmoptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ static XrmOption xrmOptions[] = {
"Max history size (WARNING: can cause slowdowns when set to high).", CONFIG_DEFAULT },
{ xrm_Boolean, "combi-hide-mode-prefix", { .snum = &config.combi_hide_mode_prefix }, NULL,
"Hide the prefix mode prefix on the combi view.", CONFIG_DEFAULT },
{ xrm_String, "combi-display-format", { .str = &config.combi_display_format }, NULL,
"Combi mode format string. (Supports: mode,element)", CONFIG_DEFAULT },
{ xrm_String, "combi-no-linebreak-modi", { .str = &config.combi_no_linebreak_modi }, NULL,
"Modi that do not use a linbreak for {linebreak} in combi mode, but -combi-no-linebreak-str instead.", CONFIG_DEFAULT },
{ xrm_String, "combi-no-linebreak-str", { .str = &config.combi_no_linebreak_str }, NULL,
"String used instead of linebreak in the output formatting for -combi-no-linebreak-modi.", CONFIG_DEFAULT },
{ xrm_Char, "matching-negate-char", { .charc = &config.matching_negate_char }, NULL,
"Set the character used to negate the matching. ('\\0' to disable)", CONFIG_DEFAULT },
{ xrm_String, "cache-dir", { .str = &config.cache_dir }, NULL,
Expand Down
8 changes: 8 additions & 0 deletions test/run_combi_mode_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rofi -show combi \
-modi "test:./test_script.sh" \
-combi-modi "test,window,drun" -eh 2 \
-drun-display-format "[<span weight='light' size='small'><i>({generic})</i></span>]"$'\n'"{name}" \
-combi-display-format "[<span weight='light' size='small'><i>[{mode}]</i></span>]{linebreak}{element}" \
-combi-no-linebreak-str " " \
-combi-no-linebreak-modi "drun" \
-markup-combi 1