Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Print line number anchors through one helper
Diffstat (limited to '')
-rw-r--r--source/ui-blame.c14
-rw-r--r--source/ui-shared.c16
-rw-r--r--source/ui-shared.h7
-rw-r--r--source/ui-tree.c33
4 files changed, 31 insertions, 39 deletions
diff --git a/source/ui-blame.c b/source/ui-blame.c
index 7254118..b07e57d 100644
--- a/source/ui-blame.c
+++ b/source/ui-blame.c
@@ -153,19 +153,7 @@ static void emit_entry_hash(struct blame_entry *ent)
static void emit_entry_linenumbers(struct blame_entry *ent)
{
- struct strbuf numbers = STRBUF_INIT;
- int lineno = ent->lno;
-
- while (lineno < ent->lno + ent->num_lines) {
- lineno++;
- strbuf_addf(&numbers, "<a id='n%d' href='#n%d'>%d</a>\n", lineno, lineno, lineno);
- if (numbers.len >= HTML_BATCH) {
- html_raw(numbers.buf, numbers.len);
- strbuf_reset(&numbers);
- }
- }
- html_raw(numbers.buf, numbers.len);
- strbuf_release(&numbers);
+ cgit_print_line_anchors((size_t)ent->lno + 1, (size_t)ent->lno + ent->num_lines);
}
static void emit_column(struct blame_scoreboard *sb, const char *class,
diff --git a/source/ui-shared.c b/source/ui-shared.c
index 5e2a9de..81db404 100644
--- a/source/ui-shared.c
+++ b/source/ui-shared.c
@@ -1317,6 +1317,22 @@ void cgit_print_date(timestamp_t date, int tz)
html("</time>");
}
+void cgit_print_line_anchors(size_t first, size_t last)
+{
+ struct strbuf numbers = STRBUF_INIT;
+ size_t lineno;
+
+ for (lineno = first; lineno <= last; lineno++) {
+ strbuf_addf(&numbers, "<a id='n%zu' href='#n%zu'>%zu</a>\n", lineno, lineno, lineno);
+ if (numbers.len >= HTML_BATCH) {
+ html_raw(numbers.buf, numbers.len);
+ strbuf_reset(&numbers);
+ }
+ }
+ html_raw(numbers.buf, numbers.len);
+ strbuf_release(&numbers);
+}
+
void cgit_print_person(const char *name, const char *email, const char *origin, int show_email)
{
cgit_open_filter(ctx.repo->email_filter, email ? email : "", origin);
diff --git a/source/ui-shared.h b/source/ui-shared.h
index ee350f0..32337d9 100644
--- a/source/ui-shared.h
+++ b/source/ui-shared.h
@@ -115,6 +115,13 @@ extern void cgit_print_age(time_t t, int tz, time_t max_relative);
extern void cgit_print_date(timestamp_t date, int tz);
/*
+ * Print the anchor of every line from first to last, each linking to itself.
+ * The anchors are handed over in batches rather than a write per line, and
+ * never built whole, which would come to several times the blob's size.
+ */
+extern void cgit_print_line_anchors(size_t first, size_t last);
+
+/*
* Print name through the email filter, followed by the address when
* show_email is set and enable-plain-email is on. A missing address reaches
* the filter as an empty string, since a filter must not be handed NULL.
diff --git a/source/ui-tree.c b/source/ui-tree.c
index 157aba3..aff4f0f 100644
--- a/source/ui-tree.c
+++ b/source/ui-tree.c
@@ -54,38 +54,19 @@ struct only_child {
size_t count;
};
-static void add_linenumber(struct strbuf *numbers, size_t lineno)
-{
- strbuf_addf(numbers, "<a id='n%zu' href='#n%zu'>%zu</a>\n", lineno, lineno, lineno);
-}
-
-/*
- * The anchors are handed over in batches rather than a write per line, and
- * never built whole, which would come to several times the blob's size.
- */
static void print_linenumbers(const char *buf, size_t size)
{
- struct strbuf numbers = STRBUF_INIT;
- size_t lineno = 0, idx = 0;
+ size_t lines = 0, idx;
if (size) {
- add_linenumber(&numbers, ++lineno);
-
+ lines = 1;
// The newline that ends the last line must not open a line of
- // its own, so the final byte is left out of the scan.
- while (idx < size - 1) {
- if (buf[idx] == '\n') {
- add_linenumber(&numbers, ++lineno);
- if (numbers.len >= HTML_BATCH) {
- html_raw(numbers.buf, numbers.len);
- strbuf_reset(&numbers);
- }
- }
- idx++;
- }
- html_raw(numbers.buf, numbers.len);
+ // its own, so the final byte is left out of the count.
+ for (idx = 0; idx < size - 1; idx++)
+ if (buf[idx] == '\n')
+ lines++;
}
- strbuf_release(&numbers);
+ cgit_print_line_anchors(1, lines);
}
static void print_text_buffer(const char *filename, char *buf, size_t size)