[PATCH v2 7/8] ui-repolist: Restructure internal logic to be more extensible

Tim Nordell tim.nordell at logicpd.com
Sat Mar 5 00:30:30 CET 2016


The internal logic has been restructured so that there is a "walking"
routine that filters the repo list based on the visible criteria, and
subsequently calls a given callback for each repo found.  Additionally,
split out generating a table line for a given repo, and a table line
for a given section.  This makes this more loosely coupled and allows
reuse of more logic for changes to the way the repo list is displayed.

This is for a future patch in this series.

Signed-off-by: Tim Nordell <tim.nordell at logicpd.com>

diff --git a/ui-repolist.c b/ui-repolist.c
index fab589e..93655d4 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -270,8 +270,12 @@ static int sort_repolist(char *field)
 }
 
 struct repolist_ctx {
+	/* From outer contex passed to interior */
 	int columns;
 	int sorted;
+
+	/* Used in interior context
+	 * (Should be reset in repolist_walk_visible()) */
 	int hits;
 	const char *last_section;
 };
@@ -290,6 +294,10 @@ static void html_repository(struct cgit_repo *repo, bool sorted)
 {
 	bool is_toplevel;
 
+	/* Note: cgit_summary_link() ultimately calls repolink(), which is
+	 * dependent on ctx.repo pointing to the repo we're working on */
+	ctx.repo = repo;
+
 	is_toplevel = (NULL != repo->section && '\0' != repo->section[0]);
 	htmlf("<tr><td class='%s'>",
 		(!sorted && is_toplevel) ? "sublevel-repo" : "toplevel-repo");
@@ -352,15 +360,45 @@ static inline bool should_emit_section(struct repolist_ctx *c, struct cgit_repo
 	return true;
 }
 
+static int generate_repolist(struct repolist_ctx *c, struct cgit_repo *repo)
+{
+	c->hits++;
+	if (c->hits <= ctx.qry.ofs)
+		return 0;
+	if (c->hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
+		return 0;
+
+	if (should_emit_section(c, repo))
+		html_section(repo, c->columns);
+	html_repository(repo, c->sorted);
+
+	return 0;
+}
+
+typedef int (*repolist_walk_callback_t)(struct repolist_ctx *c, struct cgit_repo *repo);
+static int repolist_walk_visible(struct repolist_ctx *c, repolist_walk_callback_t callback)
+{
+	struct cgit_repo *repo;
+	int i;
+
+	c->hits = 0;
+	c->last_section = NULL;
+
+	for (i = 0; i < cgit_repolist.count; i++) {
+		repo = &cgit_repolist.repos[i];
+		if (!is_visible(repo))
+			continue;
+		if (NULL != callback)
+			callback(c, repo);
+	}
+	return 0;
+}
+
 void cgit_print_repolist(void)
 {
 	struct repolist_ctx repolist_ctx;
-	struct repolist_ctx *c = &repolist_ctx;
-	int i;
 
 	repolist_ctx.columns      = 3;
-	repolist_ctx.hits         = 0;
-	repolist_ctx.last_section = NULL;
 	repolist_ctx.sorted       = 0;
 
 	if (!any_repos_visible()) {
@@ -388,19 +426,7 @@ void cgit_print_repolist(void)
 
 	html("<table summary='repository list' class='list nowrap'>");
 	print_header();
-	for (i = 0; i < cgit_repolist.count; i++) {
-		ctx.repo = &cgit_repolist.repos[i];
-		if (!is_visible(ctx.repo))
-			continue;
-		c->hits++;
-		if (c->hits <= ctx.qry.ofs)
-			continue;
-		if (c->hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
-			continue;
-		if (should_emit_section(&repolist_ctx, ctx.repo))
-			html_section(ctx.repo, c->columns);
-		html_repository(ctx.repo, repolist_ctx.sorted);
-	}
+	repolist_walk_visible(&repolist_ctx, generate_repolist);
 	html("</table>");
 	if (repolist_ctx.hits > ctx.cfg.max_repo_count)
 		print_pager(repolist_ctx.hits, ctx.cfg.max_repo_count, ctx.qry.search, ctx.qry.sort);
-- 
2.4.9



More information about the CGit mailing list