diff cmd.c @ 40:e3ad9859c51d

Show only groups of matching entries when using ls with filters
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Fri, 09 Aug 2019 14:04:46 +0200
parents 8b55f7b1c6b3
children 0af8d2d8cd1a
line wrap: on
line diff
--- a/cmd.c	Thu Aug 08 22:48:09 2019 +0200
+++ b/cmd.c	Fri Aug 09 14:04:46 2019 +0200
@@ -237,6 +237,18 @@
 	return (0);
 }
 
+static inline int
+filter_matches(regex_t *filter_re, const char *s)
+{
+	if (filter_re == NULL) {
+		return (1);
+	}
+	if (s == NULL) {
+		return (0);
+	}
+	return (regexec(filter_re, s, 0, NULL, 0) == 0);
+}
+
 static enum cmd_return
 cmd_set(struct pwm_ctx *ctx, int argc, char *argv[])
 {
@@ -423,14 +435,11 @@
 	int		errcode;
 	char		*errbuf;
 	size_t		errbuf_size;
+	int		is_filtered;
 	struct pager	*pager = NULL;
 	union list_item	**list = NULL;
 	size_t		j;
-	const char	*group;
-	const char	*title;
-	const char	*username;
-	const char	*notes;
-	const char	*url;
+	const char	*group = NULL;
 	struct record	*record;
 
 	for (i = 1; i < argc; i++) {
@@ -480,36 +489,37 @@
 		}
 	}
 
+	is_filtered = ((group_re != NULL) || (title_re != NULL) ||
+	    (username_re != NULL) || (notes_re != NULL) || (url_re != NULL));
 	pager = pager_create(STDOUT_FILENO);
 	list = pwfile_create_list(ctx);
 	for (j = 0; list[j] != NULL; j++) {
 		if (list[j]->any.type == ITEM_TYPE_GROUP) {
-			pager_printf(pager, "[%s]\n", list[j]->group.group);
-		} else {
+			group = list[j]->group.group;
+			if (!is_filtered) {
+				pager_printf(pager, "[%s]\n", group);
+			}
+		} else if (is_filtered) {
 			record = pwfile_get_record(ctx, list[j]->record.id);
-			group = (record->group != NULL) ? record->group : "";
-			title = (record->title != NULL) ? record->title : "";
-			username = (record->username != NULL) ?
-			    record->username : "";
-			notes = (record->notes != NULL) ? record->notes : "";
-			url = (record->url != NULL) ? record->url : "";
-			if (((group_re == NULL) ||
-			    (regexec(group_re, group, 0, NULL, 0) == 0)) &&
-			    ((title_re == NULL) ||
-			    (regexec(title_re, title, 0, NULL, 0) == 0)) &&
-			    ((username_re == NULL) ||
-			    (regexec(username_re, username, 0, NULL,
-			    0) == 0)) &&
-			    ((notes_re == NULL) ||
-			    (regexec(notes_re, notes, 0, NULL, 0) == 0)) &&
-			    ((url_re == NULL) ||
-			    (regexec(url_re, url, 0, NULL, 0) == 0))) {
+			if (filter_matches(group_re, record->group) &&
+			    filter_matches(title_re, record->title) &&
+			    filter_matches(username_re, record->username) &&
+			    filter_matches(notes_re, record->notes) &&
+			    filter_matches(url_re, record->url)) {
+				if (group != NULL) {
+					pager_printf(pager, "[%s]\n", group);
+					group = NULL;
+				}
 				pager_printf(pager, "%4u %s\n",
 				    list[j]->record.id,
 				    (list[j]->record.title != NULL) ?
 				    list[j]->record.title : "");
 			}
 			pwfile_destroy_record(record);
+		} else {
+			pager_printf(pager, "%4u %s\n", list[j]->record.id,
+			    (list[j]->record.title != NULL) ?
+			    list[j]->record.title : "");
 		}
 	}
 	retval = (pager_show(pager) != IO_SIGNAL) ? CMD_OK : CMD_SIGNAL;