author | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 13:08:51 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 13:08:51 (UTC) |
commit | f250c1ca2ea7f35d65f639e42e8b8f0657515e5d (patch) (unidiff) | |
tree | 47abd9e5029d687c01e3758c4891a1a995fd630a | |
parent | cbac02c8b056e47b3e0092949c480c7ec64a3e0f (diff) | |
download | cgit-f250c1ca2ea7f35d65f639e42e8b8f0657515e5d.zip cgit-f250c1ca2ea7f35d65f639e42e8b8f0657515e5d.tar.gz cgit-f250c1ca2ea7f35d65f639e42e8b8f0657515e5d.tar.bz2 |
ui-repolist: add support for sorting any column
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | ui-repolist.c | 92 |
1 files changed, 83 insertions, 9 deletions
diff --git a/ui-repolist.c b/ui-repolist.c index 436a774..0de328b 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -75,19 +75,29 @@ int is_in_url(struct cgit_repo *repo) | |||
75 | return 1; | 75 | return 1; |
76 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) | 76 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) |
77 | return 1; | 77 | return 1; |
78 | return 0; | 78 | return 0; |
79 | } | 79 | } |
80 | 80 | ||
81 | void print_sort_header(const char *title, const char *sort) | ||
82 | { | ||
83 | htmlf("<th class='left'><a href='./?s=%s", sort); | ||
84 | if (ctx.qry.search) { | ||
85 | html("&q="); | ||
86 | html_url_arg(ctx.qry.search); | ||
87 | } | ||
88 | htmlf("'>%s</a></th>", title); | ||
89 | } | ||
90 | |||
81 | void print_header(int columns) | 91 | void print_header(int columns) |
82 | { | 92 | { |
83 | html("<tr class='nohover'>" | 93 | html("<tr class='nohover'>"); |
84 | "<th class='left'>Name</th>" | 94 | print_sort_header("Name", "name"); |
85 | "<th class='left'>Description</th>" | 95 | print_sort_header("Description", "desc"); |
86 | "<th class='left'>Owner</th>" | 96 | print_sort_header("Owner", "owner"); |
87 | "<th class='left'><a href=\"?s=1\">Idle</a></th>"); | 97 | print_sort_header("Idle", "idle"); |
88 | if (ctx.cfg.enable_index_links) | 98 | if (ctx.cfg.enable_index_links) |
89 | html("<th class='left'>Links</th>"); | 99 | html("<th class='left'>Links</th>"); |
90 | html("</tr>\n"); | 100 | html("</tr>\n"); |
91 | } | 101 | } |
92 | 102 | ||
93 | 103 | ||
@@ -98,28 +108,92 @@ void print_pager(int items, int pagelen, char *search) | |||
98 | for(i = 0; i * pagelen < items; i++) | 108 | for(i = 0; i * pagelen < items; i++) |
99 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, | 109 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, |
100 | search, i * pagelen); | 110 | search, i * pagelen); |
101 | html("</div>"); | 111 | html("</div>"); |
102 | } | 112 | } |
103 | 113 | ||
104 | static int cgit_reposort_modtime(const void *a, const void *b) | 114 | static int cmp(const char *s1, const char *s2) |
115 | { | ||
116 | if (s1 && s2) | ||
117 | return strcmp(s1, s2); | ||
118 | if (s1 && !s2) | ||
119 | return 1; | ||
120 | if (s2 && !s1) | ||
121 | return -1; | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | static int sort_name(const void *a, const void *b) | ||
126 | { | ||
127 | const struct cgit_repo *r1 = a; | ||
128 | const struct cgit_repo *r2 = b; | ||
129 | |||
130 | return cmp(r1->name, r2->name); | ||
131 | } | ||
132 | |||
133 | static int sort_desc(const void *a, const void *b) | ||
134 | { | ||
135 | const struct cgit_repo *r1 = a; | ||
136 | const struct cgit_repo *r2 = b; | ||
137 | |||
138 | return cmp(r1->desc, r2->desc); | ||
139 | } | ||
140 | |||
141 | static int sort_owner(const void *a, const void *b) | ||
142 | { | ||
143 | const struct cgit_repo *r1 = a; | ||
144 | const struct cgit_repo *r2 = b; | ||
145 | |||
146 | return cmp(r1->owner, r2->owner); | ||
147 | } | ||
148 | |||
149 | static int sort_idle(const void *a, const void *b) | ||
105 | { | 150 | { |
106 | const struct cgit_repo *r1 = a; | 151 | const struct cgit_repo *r1 = a; |
107 | const struct cgit_repo *r2 = b; | 152 | const struct cgit_repo *r2 = b; |
108 | time_t t1, t2; | 153 | time_t t1, t2; |
109 | 154 | ||
110 | t1 = t2 = 0; | 155 | t1 = t2 = 0; |
111 | get_repo_modtime(r1, &t1); | 156 | get_repo_modtime(r1, &t1); |
112 | get_repo_modtime(r2, &t2); | 157 | get_repo_modtime(r2, &t2); |
113 | return t2 - t1; | 158 | return t2 - t1; |
114 | } | 159 | } |
115 | 160 | ||
161 | struct sortcolumn { | ||
162 | const char *name; | ||
163 | int (*fn)(const void *a, const void *b); | ||
164 | }; | ||
165 | |||
166 | struct sortcolumn sortcolumn[] = { | ||
167 | {"name", sort_name}, | ||
168 | {"desc", sort_desc}, | ||
169 | {"owner", sort_owner}, | ||
170 | {"idle", sort_idle}, | ||
171 | {NULL, NULL} | ||
172 | }; | ||
173 | |||
174 | int sort_repolist(char *field) | ||
175 | { | ||
176 | struct sortcolumn *column; | ||
177 | |||
178 | for (column = &sortcolumn[0]; column->name; column++) { | ||
179 | if (strcmp(field, column->name)) | ||
180 | continue; | ||
181 | qsort(cgit_repolist.repos, cgit_repolist.count, | ||
182 | sizeof(struct cgit_repo), column->fn); | ||
183 | return 1; | ||
184 | } | ||
185 | return 0; | ||
186 | } | ||
187 | |||
188 | |||
116 | void cgit_print_repolist() | 189 | void cgit_print_repolist() |
117 | { | 190 | { |
118 | int i, columns = 4, hits = 0, header = 0; | 191 | int i, columns = 4, hits = 0, header = 0; |
119 | char *last_group = NULL; | 192 | char *last_group = NULL; |
193 | int sorted = 0; | ||
120 | 194 | ||
121 | if (ctx.cfg.enable_index_links) | 195 | if (ctx.cfg.enable_index_links) |
122 | columns++; | 196 | columns++; |
123 | 197 | ||
124 | ctx.page.title = ctx.cfg.root_title; | 198 | ctx.page.title = ctx.cfg.root_title; |
125 | cgit_print_http_headers(&ctx); | 199 | cgit_print_http_headers(&ctx); |
@@ -127,13 +201,13 @@ void cgit_print_repolist() | |||
127 | cgit_print_pageheader(&ctx); | 201 | cgit_print_pageheader(&ctx); |
128 | 202 | ||
129 | if (ctx.cfg.index_header) | 203 | if (ctx.cfg.index_header) |
130 | html_include(ctx.cfg.index_header); | 204 | html_include(ctx.cfg.index_header); |
131 | 205 | ||
132 | if(ctx.qry.sort) | 206 | if(ctx.qry.sort) |
133 | qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime); | 207 | sorted = sort_repolist(ctx.qry.sort); |
134 | 208 | ||
135 | html("<table summary='repository list' class='list nowrap'>"); | 209 | html("<table summary='repository list' class='list nowrap'>"); |
136 | for (i=0; i<cgit_repolist.count; i++) { | 210 | for (i=0; i<cgit_repolist.count; i++) { |
137 | ctx.repo = &cgit_repolist.repos[i]; | 211 | ctx.repo = &cgit_repolist.repos[i]; |
138 | if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) | 212 | if (!(is_match(ctx.repo) && is_in_url(ctx.repo))) |
139 | continue; | 213 | continue; |
@@ -141,25 +215,25 @@ void cgit_print_repolist() | |||
141 | if (hits <= ctx.qry.ofs) | 215 | if (hits <= ctx.qry.ofs) |
142 | continue; | 216 | continue; |
143 | if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) | 217 | if (hits > ctx.qry.ofs + ctx.cfg.max_repo_count) |
144 | continue; | 218 | continue; |
145 | if (!header++) | 219 | if (!header++) |
146 | print_header(columns); | 220 | print_header(columns); |
147 | if (!ctx.qry.sort && | 221 | if (!sorted && |
148 | ((last_group == NULL && ctx.repo->group != NULL) || | 222 | ((last_group == NULL && ctx.repo->group != NULL) || |
149 | (last_group != NULL && ctx.repo->group == NULL) || | 223 | (last_group != NULL && ctx.repo->group == NULL) || |
150 | (last_group != NULL && ctx.repo->group != NULL && | 224 | (last_group != NULL && ctx.repo->group != NULL && |
151 | strcmp(ctx.repo->group, last_group)))) { | 225 | strcmp(ctx.repo->group, last_group)))) { |
152 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", | 226 | htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", |
153 | columns); | 227 | columns); |
154 | html_txt(ctx.repo->group); | 228 | html_txt(ctx.repo->group); |
155 | html("</td></tr>"); | 229 | html("</td></tr>"); |
156 | last_group = ctx.repo->group; | 230 | last_group = ctx.repo->group; |
157 | } | 231 | } |
158 | htmlf("<tr><td class='%s'>", | 232 | htmlf("<tr><td class='%s'>", |
159 | ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); | 233 | !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); |
160 | cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); | 234 | cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); |
161 | html("</td><td>"); | 235 | html("</td><td>"); |
162 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); | 236 | html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); |
163 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); | 237 | html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); |
164 | html_link_close(); | 238 | html_link_close(); |
165 | html("</td><td>"); | 239 | html("</td><td>"); |