Skip to content

Commit

Permalink
[DRUN] Add option to exclude categories from drun #2102 (#2104)
Browse files Browse the repository at this point in the history
* Add option to exclude categories from drun

* Fix memory leak when both -drun-categories and -drun-exclude-categories are used

* update docs
  • Loading branch information
J0hannes101 authored Feb 21, 2025
1 parent 8639783 commit a84f04a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Settings config = {
.drun_match_fields = "name,generic,exec,categories,keywords",
/** Only show entries in this category */
.drun_categories = NULL,
/** Exclude entries in this category */
.drun_exclude_categories = NULL,
/** Desktop entry show actions */
.drun_show_actions = FALSE,
/** Desktop format display */
Expand Down
4 changes: 4 additions & 0 deletions doc/rofi.1.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ Tokenize the input.

Only show desktop files that are present in the listed categories.

`-drun-exclude-categories` *category1*,*category2*

Exclude desktop files that are present in the listed categories.

`-drun-match-fields` *field1*,*field2*,...

When using `drun`, match only with the specified Desktop entry fields.
Expand Down
2 changes: 2 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ typedef struct {
char *drun_match_fields;
/** Only show entries in this category */
char *drun_categories;
/** Exclude entries in this category */
char *drun_exclude_categories;
/** Desktop entry show actions */
unsigned int drun_show_actions;
/** Desktop format display */
Expand Down
19 changes: 19 additions & 0 deletions source/modes/drun.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ struct _DRunModePrivateData {
unsigned int expected_line_height;

char **show_categories;
char **exclude_categories;

// Theme
const gchar *icon_theme;
Expand Down Expand Up @@ -722,6 +723,19 @@ static void read_desktop_file(DRunModePrivateData *pd, const char *root,
}
}

if (pd->exclude_categories) {
if (categories == NULL) {
categories = g_key_file_get_locale_string_list(
kf, DRUN_GROUP_NAME, "Categories", NULL, NULL, NULL);
}
if (rofi_strv_contains((const char *const *)categories,
(const char *const *)pd->exclude_categories)) {
g_strfreev(categories);
g_key_file_free(kf);
return;
}
}

size_t nl = ((pd->cmd_list_length) + 1);
if (nl >= pd->cmd_list_length_actual) {
pd->cmd_list_length_actual += 256;
Expand Down Expand Up @@ -1320,6 +1334,10 @@ static int drun_mode_init(Mode *sw) {
pd->show_categories = g_strsplit(config.drun_categories, ",", 0);
}

if (config.drun_exclude_categories && *(config.drun_exclude_categories)) {
pd->exclude_categories = g_strsplit(config.drun_exclude_categories, ",", 0);
}

drun_mode_parse_entry_fields();
drun_mode_parse_display_format();
get_apps(pd);
Expand Down Expand Up @@ -1473,6 +1491,7 @@ static void drun_mode_destroy(Mode *sw) {

g_strfreev(rmpd->current_desktop_list);
g_strfreev(rmpd->show_categories);
g_strfreev(rmpd->exclude_categories);
g_free(rmpd);
mode_set_private_data(sw, NULL);
}
Expand Down
6 changes: 6 additions & 0 deletions source/xrmoptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ static XrmOption xrmOptions[] = {
NULL,
"Only show Desktop entry from these categories",
CONFIG_DEFAULT},
{xrm_String,
"drun-exclude-categories",
{.str = &config.drun_exclude_categories},
NULL,
"Exclude Desktop entries from these categories",
CONFIG_DEFAULT},
{xrm_Boolean,
"drun-show-actions",
{.num = &config.drun_show_actions},
Expand Down

0 comments on commit a84f04a

Please sign in to comment.