[PATCH v2 8/8] ui-repolist: Support a trimmed view when several sections are present

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


Trim the view to just a section list if there are multiple
sections present in the output view.  This is only really useful
if one also has a script placing links on each section heading to
trim the view by path.

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

diff --git a/ui-repolist.c b/ui-repolist.c
index 93655d4..ff70ddc 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -278,6 +278,8 @@ struct repolist_ctx {
 	 * (Should be reset in repolist_walk_visible()) */
 	int hits;
 	const char *last_section;
+	int section_cnt;
+	int section_nested_cnt;
 };
 
 static void html_section(struct cgit_repo *repo, int columns)
@@ -375,6 +377,51 @@ static int generate_repolist(struct repolist_ctx *c, struct cgit_repo *repo)
 	return 0;
 }
 
+static int generate_sectionlist(struct repolist_ctx *c, struct cgit_repo *repo)
+{
+	bool is_toplevel;
+
+	is_toplevel = (NULL == repo->section || repo->section[0] == '\0');
+
+	if (!should_emit_section(c, repo) && !is_toplevel)
+		return 0;
+
+	c->hits++;
+
+	if (c->hits <= ctx.qry.ofs)
+		return 0;
+	if (c->hits > ctx.qry.ofs + ctx.cfg.max_repo_count)
+		return 0;
+
+	if (is_toplevel)
+		html_repository(repo, c->sorted);
+	else
+		html_section(repo, c->columns);
+
+	return 0;
+}
+
+static int count_sections(struct repolist_ctx *c, struct cgit_repo *repo)
+{
+	const char *last_section;
+
+	last_section = c->last_section;
+	if (should_emit_section(c, repo)) {
+		c->section_cnt++;
+
+		/* Determine if one section is nested within the other.  This
+		 * is only accurate if this is a sorted list.
+		 */
+		if (NULL != last_section && NULL != repo->section &&
+		    last_section[strspn(last_section, repo->section)] == '\0')
+			c->section_nested_cnt++;
+	}
+
+	c->hits++;
+
+	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)
 {
@@ -383,6 +430,8 @@ static int repolist_walk_visible(struct repolist_ctx *c, repolist_walk_callback_
 
 	c->hits = 0;
 	c->last_section = NULL;
+	c->section_cnt = 0;
+	c->section_nested_cnt = 0;
 
 	for (i = 0; i < cgit_repolist.count; i++) {
 		repo = &cgit_repolist.repos[i];
@@ -396,6 +445,7 @@ static int repolist_walk_visible(struct repolist_ctx *c, repolist_walk_callback_
 
 void cgit_print_repolist(void)
 {
+	bool section_pages = false;
 	struct repolist_ctx repolist_ctx;
 
 	repolist_ctx.columns      = 3;
@@ -421,12 +471,23 @@ void cgit_print_repolist(void)
 
 	if (ctx.qry.sort)
 		repolist_ctx.sorted = sort_repolist(ctx.qry.sort);
-	else if (ctx.cfg.section_sort)
+	else if (ctx.cfg.section_sort) {
 		sort_repolist("section");
+		section_pages = (2 == ctx.cfg.section_sort);
+	}
 
 	html("<table summary='repository list' class='list nowrap'>");
 	print_header();
-	repolist_walk_visible(&repolist_ctx, generate_repolist);
+
+	if (section_pages)
+		repolist_walk_visible(&repolist_ctx, count_sections);
+
+	if (section_pages && repolist_ctx.hits > ctx.cfg.max_repo_count &&
+		repolist_ctx.section_cnt - repolist_ctx.section_nested_cnt > 1)
+		repolist_walk_visible(&repolist_ctx, generate_sectionlist);
+	else
+		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