Fix documentation, function names, label text alignment

This commit contains a few minor fixes to documentation and function names, for clarity, and adds a
`textLength` attribute to `<text>` elements in labels, thereby ensuring that the text is contained
within the label.

Fixes #1
This commit is contained in:
Alex Palaistras 2018-01-13 14:14:55 +00:00
parent 8516e8e139
commit 852a796f4a
6 changed files with 43 additions and 40 deletions

53
grawkit
View File

@ -15,9 +15,9 @@
# This section contains global helper functions, used across different rules, as
# defined in the next section below.
# > Function `addbranch` adds a new, empty branch to the internal list of branches
# > Function `add_branch` adds a new, empty branch to the internal list of branches
# > to render.
function addbranch(name) {
function add_branch(name) {
branches[len["branches"],"name"] = name
branches[len["branches"],"refs"] = ""
branches[len["branches"],"tags"] = ""
@ -33,9 +33,9 @@ function addbranch(name) {
len["branches"] += 1
}
# > Function `addcommit` adds a new commit, with a specific type and message,
# > Function `add_commit` adds a new commit, with a specific type and message,
# > to the internal list of commits to render.
function addcommit(type, message) {
function add_commit(type, message) {
# Add commit information.
commits[len["commits"],"type"] = type
commits[len["commits"],"message"] = message
@ -57,9 +57,9 @@ function normalize(text) {
return text
}
# > Function `branch` renders pre-defined branch under a specific name to its
# > SVG representation.
function branch(idx, _, buf, tmp, refs, tags, t, i, hspc, vspc) {
# > Function `render_branch` renders pre-defined branch under a specific name
# > to its SVG representation.
function render_branch(idx, _, buf, tmp, refs, tags, t, i, hspc, vspc) {
# Do not render branch with no commits.
if (branches[idx,"refs"] == "") {
return
@ -91,22 +91,25 @@ function branch(idx, _, buf, tmp, refs, tags, t, i, hspc, vspc) {
split(branches[idx,"tags"], tags, ",")
for (i in tags) {
split(tags[i], t, "|")
buf = buf label(t[1], t[2], t[3])
buf = buf render_label(t[1], t[2], t[3])
}
# Add branch name as label on last commit.
buf = buf label(refs[length(refs)], "branch", branches[idx,"name"])
buf = buf render_label(refs[length(refs)], "branch", branches[idx,"name"])
return buf "\n" svg["/g"]
}
# > Function `label` adds a sidebar label at commit index, with a specific class
# > and label name. Multiple labels for the same index will be placed side-by-side.
function label(idx, class, name, _, buf, w, h, hspc, vspc) {
# Calculate width and height for label rectangle. Pitch size is set to 0.5
# here, which is approximately right for 'Inconsolata', but may vary for other
# fonts, as fixed width fonts are usually twice as tall as they are wide.
w = ((style["label/font-size"] * 0.5) * length(name)) + style["label/spacing"]
# > Function `render_label` adds a sidebar label at commit index, with a specific
# > class and label name. Multiple labels for the same index will be placed
# > side-by-side.
function render_label(idx, class, name, _, buf, tw, w, h, hspc, vspc) {
# Set specific length for text. Pitch size is approximately right for most fixed-width fonts,
# which are usually twice as tall as they are wide, but may vary for other fonts.
tw = (style["label/font-size"] * 0.5) * length(name)
# Calculate width and height for label rectangle.
w = tw + style["label/spacing"]
h = style["label/font-size"] + style["label/spacing"]
# Calculate label offsets.
@ -119,14 +122,14 @@ function label(idx, class, name, _, buf, w, h, hspc, vspc) {
# Draw label elements.
buf = buf "\n\t" sprintf(svg["gg"], "label-" class, hspc, vspc)
buf = buf "\n\t\t" sprintf(svg["rect"], 0, style["label/font-size"] * -1, w, h, style["label/round"])
buf = buf "\n\t\t" sprintf(svg["text"], style["label/spacing"] / 2, style["label/spacing"] / 4, name)
buf = buf "\n\t\t" sprintf(svg["text"], style["label/spacing"] / 2, style["label/spacing"] / 4, tw, name)
buf = buf "\n\t" svg["/g"]
return buf
}
# > Function `merge` renders merge paths for a branch pointed to by `idx`.
function merge(idx, _, buf, tmp, refs, fields, m, i, hspc) {
# > Function `render_merge` renders merge paths for a branch pointed to by `idx`.
function render_merge(idx, _, buf, tmp, refs, fields, m, i, hspc) {
# Do not render merge paths for branch with no commits.
if (branches[idx,"merges"] == "") {
return
@ -221,7 +224,7 @@ BEGIN {
svg["path"] = "<path class=\"branch\" d=\"%s\" />"
svg["circle"] = "<circle class=\"commit\" cx=\"%d\" cy=\"%d\" r=\"%s\" />"
svg["rect"] = "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" rx=\"%d\" class=\"label-rect\" />"
svg["text"] = "<text x=\"%d\" y=\"%d\" class=\"label-text\">%s</text>"
svg["text"] = "<text x=\"%d\" y=\"%d\" textLength=\"%d\" class=\"label-text\">%s</text>"
# Branch definitions.
branches[0,"name"] = "master"
@ -253,7 +256,7 @@ $0 ~ rule["commit"] {
match($0, rule["commit/message"], m)
# Add new commit.
addcommit("commit", (2 in m) ? m[2] : "Empty message")
add_commit("commit", (2 in m) ? m[2] : "Empty message")
next
}
@ -275,7 +278,7 @@ $0 ~ rule["branch"] {
}
# Add empty branch as a placeholder.
addbranch(n[1])
add_branch(n[1])
next
}
@ -335,7 +338,7 @@ $0 ~ rule["merge"] {
}
# Add a merge commit to current branch.
addcommit("merge", "Merge commit")
add_commit("merge", "Merge commit")
# Add merge reference to target branch.
if (branches[i,"merges"] == "") {
@ -378,7 +381,7 @@ END {
# Print merge paths for branches.
for (i = len["branches"] - 1; i >= 0; i--) {
body = body merge(i)
body = body render_merge(i)
}
if (body != "") {
@ -388,7 +391,7 @@ END {
# Print each branch and corresponding commits in turn.
for (i = len["branches"] - 1; i >= 0; i--) {
body = body branch(i)
body = body render_branch(i)
}
# Calculate SVG canvas size, removing `master` branch from X offset if it

View File

@ -43,7 +43,7 @@ git commit
<circle class="commit" cx="0" cy="100" r="7.5" />
<g class="label-branch" transform="translate(50,100)">
<rect x="0" y="-14" width="52" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">master</text>
<text x="5" y="2" textLength="42" class="label-text">master</text>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -58,7 +58,7 @@ git commit
<circle class="commit" cx="50" cy="200" r="7.5" />
<g class="label-branch" transform="translate(100,200)">
<rect x="0" y="-14" width="80" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">test-stuff</text>
<text x="5" y="2" textLength="70" class="label-text">test-stuff</text>
</g>
</g>
<g class="branch-master">
@ -68,7 +68,7 @@ git commit
<circle class="commit" cx="0" cy="250" r="7.5" />
<g class="label-branch" transform="translate(100,250)">
<rect x="0" y="-14" width="52" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">master</text>
<text x="5" y="2" textLength="42" class="label-text">master</text>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -56,7 +56,7 @@ git merge test-merging
<circle class="commit" cx="50" cy="100" r="7.5" />
<g class="label-branch" transform="translate(100,100)">
<rect x="0" y="-14" width="94" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">test-merging</text>
<text x="5" y="2" textLength="84" class="label-text">test-merging</text>
</g>
</g>
<g class="branch-master">
@ -66,7 +66,7 @@ git merge test-merging
<circle class="commit" cx="0" cy="200" r="7.5" />
<g class="label-branch" transform="translate(100,200)">
<rect x="0" y="-14" width="52" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">master</text>
<text x="5" y="2" textLength="42" class="label-text">master</text>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@ -77,7 +77,7 @@ git commit
<circle class="commit" cx="150" cy="350" r="7.5" />
<g class="label-branch" transform="translate(200,350)">
<rect x="0" y="-14" width="80" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">test-third</text>
<text x="5" y="2" textLength="70" class="label-text">test-third</text>
</g>
</g>
<g class="branch-test-second">
@ -86,11 +86,11 @@ git commit
<circle class="commit" cx="100" cy="250" r="7.5" />
<g class="label-tag" transform="translate(200,250)">
<rect x="0" y="-14" width="87" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">v.2.0.0-rc1</text>
<text x="5" y="2" textLength="77" class="label-text">v.2.0.0-rc1</text>
</g>
<g class="label-branch" transform="translate(297,250)">
<rect x="0" y="-14" width="87" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">test-second</text>
<text x="5" y="2" textLength="77" class="label-text">test-second</text>
</g>
</g>
<g class="branch-test-first">
@ -98,7 +98,7 @@ git commit
<circle class="commit" cx="50" cy="150" r="7.5" />
<g class="label-branch" transform="translate(200,150)">
<rect x="0" y="-14" width="80" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">test-first</text>
<text x="5" y="2" textLength="70" class="label-text">test-first</text>
</g>
</g>
<g class="branch-master">
@ -108,11 +108,11 @@ git commit
<circle class="commit" cx="0" cy="300" r="7.5" />
<g class="label-tag" transform="translate(200,100)">
<rect x="0" y="-14" width="59" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">v.1.0.0</text>
<text x="5" y="2" textLength="49" class="label-text">v.1.0.0</text>
</g>
<g class="label-branch" transform="translate(200,300)">
<rect x="0" y="-14" width="52" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">master</text>
<text x="5" y="2" textLength="42" class="label-text">master</text>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -81,7 +81,7 @@ git merge feature/ZZ-704_take-it-to-the-limit
<circle class="commit" cx="200" cy="350" r="7.5" />
<g class="label-branch" transform="translate(250,350)">
<rect x="0" y="-14" width="171" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">feature/ABC-66_make-bar</text>
<text x="5" y="2" textLength="161" class="label-text">feature/ABC-66_make-bar</text>
</g>
</g>
<g class="branch-feature-zz-704-take-it-to-the-limit">
@ -89,7 +89,7 @@ git merge feature/ZZ-704_take-it-to-the-limit
<circle class="commit" cx="150" cy="200" r="7.5" />
<g class="label-branch" transform="translate(250,200)">
<rect x="0" y="-14" width="255" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">feature/ZZ-704_take-it-to-the-limit</text>
<text x="5" y="2" textLength="245" class="label-text">feature/ZZ-704_take-it-to-the-limit</text>
</g>
</g>
<g class="branch-feature-xyz-12-fix-foo">
@ -98,7 +98,7 @@ git merge feature/ZZ-704_take-it-to-the-limit
<circle class="commit" cx="100" cy="150" r="7.5" />
<g class="label-branch" transform="translate(250,150)">
<rect x="0" y="-14" width="164" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">feature/XYZ-12_fix-foo</text>
<text x="5" y="2" textLength="154" class="label-text">feature/XYZ-12_fix-foo</text>
</g>
</g>
<g class="branch-develop">
@ -108,7 +108,7 @@ git merge feature/ZZ-704_take-it-to-the-limit
<circle class="commit" cx="50" cy="400" r="7.5" />
<g class="label-branch" transform="translate(250,400)">
<rect x="0" y="-14" width="59" height="24" rx="3" class="label-rect" />
<text x="5" y="2" class="label-text">develop</text>
<text x="5" y="2" textLength="49" class="label-text">develop</text>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB