Skip to content

Commit

Permalink
Add lib_add_filter option
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujamin authored and mahkoh committed Sep 26, 2016
1 parent cf40b21 commit 8435f19
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Doc/cmus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ icecast_default_charset (ISO-8859-1)

NOTE: This is used only if the metadata is not valid UTF-8.

lib_add_filter (`Filter`)
Apply filter when adding files to the library. See *FILTERS*.

lib_sort (artist album discnumber tracknumber title filename) [`Sort Keys`]
Sort keys for the sorted library view (2).

Expand Down
14 changes: 14 additions & 0 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ char *lib_live_filter = NULL;

struct rb_root lib_shuffle_root;
static struct expr *filter = NULL;
static struct expr *add_filter = NULL;
static int remove_from_hash = 1;

static struct expr *live_filter_expr = NULL;
Expand Down Expand Up @@ -160,10 +161,16 @@ static int is_filtered(struct track_info *ti)

void lib_add_track(struct track_info *ti, void *opaque)
{
if (add_filter && !expr_eval(add_filter, ti)) {
/* filter any files exluded by lib_add_filter */
return;
}

if (!hash_insert(ti)) {
/* duplicate files not allowed */
return;
}

if (!is_filtered(ti))
views_add_track(ti);
}
Expand Down Expand Up @@ -505,6 +512,13 @@ void lib_set_filter(struct expr *expr)
do_lib_filter(clear_before);
}

void lib_set_add_filter(struct expr *expr)
{
if (add_filter)
expr_free(add_filter);
add_filter = expr;
}

static struct tree_track *get_sel_track(void)
{
switch (cur_view) {
Expand Down
1 change: 1 addition & 0 deletions lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ struct track_info *lib_goto_prev(void);
void lib_add_track(struct track_info *track_info, void *opaque);
void lib_set_filter(struct expr *expr);
void lib_set_live_filter(const char *str);
void lib_set_add_filter(struct expr *expr);
int lib_remove(struct track_info *ti);
void lib_clear_store(void);
void lib_reshuffle(void);
Expand Down
25 changes: 25 additions & 0 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ char *window_title_format = NULL;
char *window_title_alt_format = NULL;
char *id3_default_charset = NULL;
char *icecast_default_charset = NULL;
char *lib_add_filter = NULL;

static void buf_int(char *buf, int val, size_t size)
{
Expand Down Expand Up @@ -1079,6 +1080,29 @@ static void toggle_mpris(void *data)
mpris ^= 1;
}

static void get_lib_add_filter(void *data, char *buf, size_t size)
{
strscpy(buf, lib_add_filter ? lib_add_filter : "", size);
}

static void set_lib_add_filter(void *data, const char *buf)
{
struct expr *expr = NULL;

if (strlen(buf) != 0) {
/* parse expression if non-empty string given */
expr = expr_parse(buf);

if (!expr)
return;
}

free(lib_add_filter);
lib_add_filter = xstrdup(buf);

lib_set_add_filter(expr);
}

/* }}} */

/* special callbacks (id set) {{{ */
Expand Down Expand Up @@ -1298,6 +1322,7 @@ static const struct {
DT(skip_track_info)
DT(mouse)
DT(mpris)
DN(lib_add_filter)
{ NULL, NULL, NULL, NULL, 0 }
};

Expand Down

0 comments on commit 8435f19

Please sign in to comment.