aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pama/models/view.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2023-11-24 16:02:57 +0100
committerRobin Jarry <robin@jarry.cc>2023-12-30 15:42:09 +0100
commit724d3a22cdaae3299df23195788ccfad7e332db8 (patch)
tree842a74f0b2a9b875b5c73b41cb03754c4b92b6ab /lib/pama/models/view.go
parent60c1c7347ad88ad00fc0809acd10ddd31243acd3 (diff)
downloadaerc-724d3a22cdaae3299df23195788ccfad7e332db8.tar.gz
pama: define the entity models
Define the entity models for the patch management. Add a Project and Commit struct and implement the Stringer interface for both. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/pama/models/view.go')
-rw-r--r--lib/pama/models/view.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/pama/models/view.go b/lib/pama/models/view.go
new file mode 100644
index 00000000..093ca6f0
--- /dev/null
+++ b/lib/pama/models/view.go
@@ -0,0 +1,81 @@
+package models
+
+import (
+ "bytes"
+ "io"
+ "strings"
+ "text/template"
+
+ "git.sr.ht/~rjarry/aerc/log"
+)
+
+var templateText = `
+Project {{.Name}} {{if .IsActive}}[active]{{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
+}
+
+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,
+ }
+
+ 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())
+}