summaryrefslogtreecommitdiff
path: root/lib/filter.c
blob: d30141547ccc0e4d15e0a9cde69386c16ace70f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * @file filter.c
 */

#include "internal.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>

/**
 * Filter that mimics the vanilla dmenu filtering.
 *
 * @param menu bmMenu instance to filter.
 * @param count unsigned int reference to filtered items count.
 * @param selected unsigned int reference to new selected item index.
 * @return Pointer to array of bmItem pointers.
 */
bmItem** _bmFilterDmenu(bmMenu *menu, unsigned int *count, unsigned int *selected)
{
    assert(menu);
    assert(count);
    assert(selected);
    *count = *selected = 0;

    /* FIXME: not real dmenu like filtering at all */

    bmItem **filtered = calloc(menu->itemsCount, sizeof(bmItem*));
    if (!filtered)
        return NULL;

    unsigned int i, f;
    for (f = i = 0; i < menu->itemsCount; ++i) {
        bmItem *item = menu->items[i];
        if (item->text && strstr(item->text, menu->filter)) {
            if (f == 0 || item == bmMenuGetSelectedItem(menu))
                *selected = f;
            filtered[f++] = item;
        }
    }

    return _bmShrinkItemList(&filtered, menu->itemsCount, (*count = f));
}

/**
 * Filter that mimics the vanilla case-insensitive dmenu filtering.
 *
 * @param menu bmMenu instance to filter.
 * @param count unsigned int reference to filtered items count.
 * @param selected unsigned int reference to new selected item index.
 * @return Pointer to array of bmItem pointers.
 */
bmItem** _bmFilterDmenuCaseInsensitive(bmMenu *menu, unsigned int *count, unsigned int *selected)
{
    assert(menu);
    assert(count);
    assert(selected);
    *count = *selected = 0;

    /* FIXME: stub */

    return NULL;
}

/* vim: set ts=8 sw=4 tw=0 :*/