summaryrefslogtreecommitdiff
path: root/web/tree.go
blob: 85e55369522931008fbe0154c84cde88328bc324 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// tree.go — the repository tree, one fragment shared by every page.
package web

import (
	"fmt"
	"html"
	"path/filepath"
	"strings"
)

type treeNode struct {
	Name      string
	Dirs      []*treeNode
	Files     []*File
	Mask      uint8 // union of traced masks underneath
	HasNotes  bool
	HasTraced bool
}

func aggregate(n *treeNode) {
	for _, f := range n.Files {
		if f.Traced {
			n.HasTraced = true
			n.Mask |= f.Mask
			if len(f.Notes) > 0 {
				n.HasNotes = true
			}
		}
	}
	for _, d := range n.Dirs {
		aggregate(d)
		n.Mask |= d.Mask
		n.HasNotes = n.HasNotes || d.HasNotes
		n.HasTraced = n.HasTraced || d.HasTraced
	}
}

func buildTree(files []*File) *treeNode {
	root := &treeNode{}
	for _, f := range files {
		parts := strings.Split(f.Path, "/")
		node := root
		for _, d := range parts[:len(parts)-1] {
			var next *treeNode
			for _, c := range node.Dirs {
				if c.Name == d {
					next = c
				}
			}
			if next == nil {
				next = &treeNode{Name: d}
				node.Dirs = append(node.Dirs, next)
			}
			node = next
		}
		node.Files = append(node.Files, f)
	}
	return root
}

// treeHTML renders one unified tree: traced files carry coverage bars, dirs
// with traced content underneath get a mixed-color dot (and ◆ for notes) and
// default open. prefix is the dir path so far, used for state persistence;
// link maps a repo path to the href for that file's page.
func treeHTML(b *strings.Builder, n *treeNode, link func(path string) string, prefix string) {
	for _, d := range n.Dirs {
		dp := d.Name
		if prefix != "" {
			dp = prefix + "/" + d.Name
		}
		openAttr, cls := "", ""
		if d.HasTraced {
			openAttr, cls = " open", ` class="t"`
		}
		fmt.Fprintf(b, `<details%s%s data-path="%s"><summary>%s`, cls, openAttr, html.EscapeString(dp), html.EscapeString(d.Name))
		if d.HasTraced {
			fmt.Fprintf(b, `<span class="dot trace-only am%d"></span>`, d.Mask)
		}
		if d.HasNotes {
			b.WriteString(`<span class="nmark notes-only">◆</span>`)
		}
		b.WriteString(`</summary>`)
		treeHTML(b, d, link, dp)
		b.WriteString(`</details>`)
	}
	if len(n.Files) == 0 {
		return
	}
	b.WriteString("<ul>")
	for _, f := range n.Files {
		name := html.EscapeString(filepath.Base(f.Path))
		path := html.EscapeString(f.Path)
		if !f.Traced {
			fmt.Fprintf(b, `<li><a class="file plain" data-path="%s" href="%s">%s</a></li>`, path, html.EscapeString(link(f.Path)), name)
			continue
		}
		noted := ""
		if len(f.Notes) > 0 {
			noted = " has-notes"
		}
		fmt.Fprintf(b,
			`<li><a class="file am%d%s" data-path="%s" href="%s" style="--pct:%d%%" title="%d%% read, %d notes">%s</a></li>`,
			f.Mask, noted, path, html.EscapeString(link(f.Path)), f.Pct, f.Pct, len(f.Notes), name)
	}
	b.WriteString("</ul>")
}

// renderTree is the whole fragment for a file set.
func renderTree(files []*File, link func(string) string) string {
	root := buildTree(files)
	aggregate(root)
	var b strings.Builder
	treeHTML(&b, root, link, "")
	return b.String()
}