add prefix matching method feature (#1237)

* add prefix matching method feature

* Update helper.c

* prefix matching regex memory leak fix

* prefix matching regex memory leak fix

Co-authored-by: francis <oxfrancis@outlook.com>
This commit is contained in:
unisgn 2021-05-29 19:39:31 +08:00 committed by GitHub
parent 24dde58019
commit c3e70d4e1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 17 deletions

View File

@ -410,6 +410,8 @@ Current the following methods are supported.
\fBglob\fP: match a glob pattern \fBglob\fP: match a glob pattern
.IP \(bu 2 .IP \(bu 2
\fBfuzzy\fP: do a fuzzy match \fBfuzzy\fP: do a fuzzy match
.IP \(bu 2
\fBprefix\fP: match prefix
.RE .RE

View File

@ -244,6 +244,7 @@ Current the following methods are supported.
* **regex**: match a regex input * **regex**: match a regex input
* **glob**: match a glob pattern * **glob**: match a glob pattern
* **fuzzy**: do a fuzzy match * **fuzzy**: do a fuzzy match
* **prefix**: match prefix
Default: *normal* Default: *normal*

View File

@ -72,7 +72,7 @@ rofi.parse-hosts: false
rofi.parse-known-hosts: true rofi.parse-known-hosts: true
! "Set the modi to combine in combi mode" Set from: File ! "Set the modi to combine in combi mode" Set from: File
rofi.combi-modi: window,drun,run,ssh rofi.combi-modi: window,drun,run,ssh
! "Set the matching algorithm. (normal, regex, glob, fuzzy)" Set from: Default ! "Set the matching algorithm. (normal, regex, glob, fuzzy, prefix)" Set from: Default
! rofi.matching: normal ! rofi.matching: normal
! "Tokenize input string" Set from: File ! "Tokenize input string" Set from: File
rofi.tokenize: true rofi.tokenize: true

View File

@ -40,7 +40,8 @@ typedef enum
MM_NORMAL = 0, MM_NORMAL = 0,
MM_REGEX = 1, MM_REGEX = 1,
MM_GLOB = 2, MM_GLOB = 2,
MM_FUZZY = 3 MM_FUZZY = 3,
MM_PREFIX = 4
} MatchingMethod; } MatchingMethod;
/** /**

View File

@ -179,6 +179,14 @@ static gchar *fuzzy_to_regex ( const char * input )
return retv; return retv;
} }
static gchar *prefix_regex (const char * input)
{
gchar *r = g_regex_escape_string(input, -1);
char *retv = g_strconcat("\\b", r, NULL);
g_free(r);
return retv;
}
static char *utf8_helper_simplify_string ( const char *s ) static char *utf8_helper_simplify_string ( const char *s )
{ {
gunichar buf2[G_UNICHAR_MAX_DECOMPOSITION_LENGTH] = { 0, }; gunichar buf2[G_UNICHAR_MAX_DECOMPOSITION_LENGTH] = { 0, };
@ -248,6 +256,11 @@ static rofi_int_matcher * create_regex ( const char *input, int case_sensitive )
retv = R ( r, case_sensitive ); retv = R ( r, case_sensitive );
g_free ( r ); g_free ( r );
break; break;
case MM_PREFIX:
r = prefix_regex(input);
retv = R(r, case_sensitive);
g_free(r);
break;
default: default:
r = g_regex_escape_string ( input, -1 ); r = g_regex_escape_string ( input, -1 );
retv = R ( r, case_sensitive ); retv = R ( r, case_sensitive );
@ -632,8 +645,11 @@ int config_sanity_check ( void )
else if ( g_strcmp0 ( config.matching, "normal" ) == 0 ) { else if ( g_strcmp0 ( config.matching, "normal" ) == 0 ) {
config.matching_method = MM_NORMAL;; config.matching_method = MM_NORMAL;;
} }
else if (g_strcmp0 (config.matching, "prefix") == 0) {
config.matching_method = MM_PREFIX;
}
else { else {
g_string_append_printf ( msg, "\t<b>config.matching</b>=%s is not a valid matching strategy.\nValid options are: glob, regex, fuzzy or normal.\n", g_string_append_printf ( msg, "\t<b>config.matching</b>=%s is not a valid matching strategy.\nValid options are: glob, regex, fuzzy, prefix or normal.\n",
config.matching ); config.matching );
found_error = 1; found_error = 1;
} }

View File

@ -169,7 +169,7 @@ static XrmOption xrmOptions[] = {
{ xrm_String, "combi-modi", { .str = &config.combi_modi }, NULL, { xrm_String, "combi-modi", { .str = &config.combi_modi }, NULL,
"Set the modi to combine in combi mode", CONFIG_DEFAULT }, "Set the modi to combine in combi mode", CONFIG_DEFAULT },
{ xrm_String, "matching", { .str = &config.matching }, NULL, { xrm_String, "matching", { .str = &config.matching }, NULL,
"Set the matching algorithm. (normal, regex, glob, fuzzy)", CONFIG_DEFAULT }, "Set the matching algorithm. (normal, regex, glob, fuzzy, prefix)", CONFIG_DEFAULT },
{ xrm_Boolean, "tokenize", { .num = &config.tokenize }, NULL, { xrm_Boolean, "tokenize", { .num = &config.tokenize }, NULL,
"Tokenize input string", CONFIG_DEFAULT }, "Tokenize input string", CONFIG_DEFAULT },
{ xrm_String, "monitor", { .str = &config.monitor }, NULL, { xrm_String, "monitor", { .str = &config.monitor }, NULL,