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
|
package models
import (
"bytes"
"io"
"strings"
"text/template"
"git.sr.ht/~rjarry/aerc/log"
)
var templateText = `
Project {{.Name}} {{if .IsActive}}[active]{{end}} {{if .IsWorktree}}[Linked worktree to {{.WorktreeParent}}]{{end}}
Directory {{.Root}}
Base {{with .Base.ID}}{{if ge (len .) 40}}{{printf "%-6.6s" .}}{{else}}{{.}}{{end}}{{end}}
{{$notes := .Notes}}{{$commits := .Commits}}
{{- range $index, $patch := .Patches}}
{{$patch}}:
{{- range (index $commits $patch)}}
{{with (index $notes .ID)}}[{{.}}] {{end}}{{. -}}
{{end}}
{{end -}}
`
var viewRenderer = template.Must(template.New("ProjectToText").Parse(templateText))
type view struct {
Name string
Root string
Base Commit
// Patches are the unique tag names.
Patches []string
// Commits is a map where the tag names are keys and the associated
// commits the values.
Commits map[string][]Commit
// Notes contain annotations of the commits where the commit hash is
// the key and the annotation is the value.
Notes map[string]string
// IsActive is true if the current project is selected.
IsActive bool
IsWorktree bool
WorktreeParent string
}
func newView(p Project, active bool, notes map[string]string) view {
v := view{
Name: p.Name,
Root: p.Root,
Base: p.Base,
Commits: make(map[string][]Commit),
Notes: notes,
IsActive: active,
IsWorktree: p.Worktree.Root != "" && p.Worktree.Name != "",
WorktreeParent: p.Worktree.Name,
}
for _, commit := range p.Commits {
patch := commit.Tag
commits, ok := v.Commits[patch]
if !ok {
v.Patches = append(v.Patches, patch)
}
commits = append(commits, commit)
v.Commits[patch] = commits
}
return v
}
func (v view) String() string {
var buf bytes.Buffer
err := viewRenderer.Execute(&buf, v)
if err != nil {
log.Errorf("failed to run template: %v", err)
}
return buf.String()
}
func (p Project) String() string {
return newView(p, false, nil).String()
}
func (p Project) NewReader(isActive bool, notes map[string]string) io.Reader {
return strings.NewReader(newView(p, isActive, notes).String())
}
|