mirror of
https://github.com/davatorium/rofi.git
synced 2024-11-25 13:55:34 -05:00
First try at highlighting match (regex only)
This commit is contained in:
parent
09437e3f0e
commit
c8a6b26607
5 changed files with 51 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
#define ROFI_HELPER_H
|
||||
#include "rofi.h"
|
||||
|
||||
#include <pango/pango.h>
|
||||
/**
|
||||
* @defgroup HELPERS Helpers
|
||||
*/
|
||||
|
@ -171,5 +172,6 @@ unsigned int levenshtein ( const char *needle, const char *haystack );
|
|||
*/
|
||||
char * rofi_force_utf8 ( gchar *data );
|
||||
char * rofi_latin_to_utf8_strdup ( const char *input, gssize length );
|
||||
PangoAttrList *regex_token_match_get_pango_attr ( char **tokens, const char *input, PangoAttrList *retv );
|
||||
/*@}*/
|
||||
#endif // ROFI_HELPER_H
|
||||
|
|
|
@ -225,6 +225,10 @@ void textbox_delete ( textbox *tb, int pos, int dlen );
|
|||
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h );
|
||||
int textbox_get_estimated_char_height ( void );
|
||||
void textbox_set_pango_context ( PangoContext *p );
|
||||
void textbox_set_pango_attributes ( textbox *tb, PangoAttrList *list );
|
||||
|
||||
PangoAttrList *textbox_get_pango_attributes ( textbox *tb );
|
||||
|
||||
const char *textbox_get_visible_text ( textbox *tb );
|
||||
/*@}*/
|
||||
#endif //ROFI_TEXTBOX_H
|
||||
|
|
|
@ -410,6 +410,29 @@ static int regex_token_match ( char **tokens, const char *input, G_GNUC_UNUSED i
|
|||
return match;
|
||||
}
|
||||
|
||||
PangoAttrList *regex_token_match_get_pango_attr ( char **tokens, const char *input, PangoAttrList *retv )
|
||||
{
|
||||
if ( config.regex == FALSE ) return retv;
|
||||
// Do a tokenized match.
|
||||
if ( tokens ) {
|
||||
for ( int j = 0; tokens[j]; j++ ) {
|
||||
GMatchInfo *gmi = NULL;
|
||||
g_regex_match ( (GRegex *) tokens[j], input, G_REGEX_MATCH_PARTIAL, &gmi );
|
||||
while ( g_match_info_matches ( gmi ) ) {
|
||||
int start, end;
|
||||
g_match_info_fetch_pos ( gmi, 0, &start, &end );
|
||||
PangoAttribute *pa = pango_attr_underline_new ( PANGO_UNDERLINE_SINGLE );
|
||||
pa->start_index = start;
|
||||
pa->end_index = end;
|
||||
pango_attr_list_insert ( retv, pa );
|
||||
g_match_info_next ( gmi, NULL );
|
||||
}
|
||||
g_match_info_free ( gmi );
|
||||
}
|
||||
}
|
||||
return retv;
|
||||
}
|
||||
|
||||
static int glob_token_match ( char **tokens, const char *input, int not_ascii, int case_sensitive )
|
||||
{
|
||||
int match = 1;
|
||||
|
|
|
@ -162,6 +162,18 @@ static void __textbox_update_pango_text ( textbox *tb )
|
|||
pango_layout_set_text ( tb->layout, tb->text, -1 );
|
||||
}
|
||||
}
|
||||
const char *textbox_get_visible_text ( textbox *tb )
|
||||
{
|
||||
return pango_layout_get_text ( tb->layout );
|
||||
}
|
||||
PangoAttrList *textbox_get_pango_attributes ( textbox *tb )
|
||||
{
|
||||
return pango_layout_get_attributes ( tb->layout );
|
||||
}
|
||||
void textbox_set_pango_attributes ( textbox *tb, PangoAttrList *list )
|
||||
{
|
||||
pango_layout_set_attributes ( tb->layout, list );
|
||||
}
|
||||
|
||||
// set the default text to display
|
||||
void textbox_text ( textbox *tb, const char *text )
|
||||
|
|
|
@ -933,6 +933,7 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d )
|
|||
int x_offset = state->border;
|
||||
|
||||
if ( state->rchanged ) {
|
||||
char **tokens = tokenize ( state->text->text, config.case_sensitive );
|
||||
// Move, resize visible boxes and show them.
|
||||
for ( i = 0; i < max_elements && ( i + offset ) < state->filtered_lines; i++ ) {
|
||||
unsigned int ex = ( ( i ) / state->max_rows ) * ( element_width + config.line_margin );
|
||||
|
@ -946,10 +947,19 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d )
|
|||
TextBoxFontType tbft = fstate | ( ( i + offset ) == state->selected ? HIGHLIGHT : type );
|
||||
textbox_font ( state->boxes[i], tbft );
|
||||
textbox_text ( state->boxes[i], text );
|
||||
|
||||
PangoAttrList *list = textbox_get_pango_attributes ( state->boxes[i] );
|
||||
if ( list != NULL )
|
||||
pango_attr_list_ref ( list );
|
||||
else list = pango_attr_list_new ();
|
||||
regex_token_match_get_pango_attr ( tokens, textbox_get_visible_text ( state->boxes[i] ), list );
|
||||
textbox_set_pango_attributes ( state->boxes[i], list );
|
||||
pango_attr_list_unref ( list );
|
||||
g_free ( text );
|
||||
}
|
||||
textbox_draw ( state->boxes[i], d );
|
||||
}
|
||||
tokenize_free ( tokens );
|
||||
state->rchanged = FALSE;
|
||||
}
|
||||
else{
|
||||
|
|
Loading…
Reference in a new issue