Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/window/about/about.go
366 views
1
package about
2
3
import (
4
"context"
5
"fmt"
6
"path"
7
"runtime/debug"
8
"strings"
9
10
"github.com/diamondburned/gotk4-adwaita/pkg/adw"
11
"github.com/diamondburned/gotk4/pkg/gtk/v4"
12
"github.com/diamondburned/gotkit/components/logui"
13
)
14
15
// New creates a new about window.
16
func New(ctx context.Context) *adw.AboutDialog {
17
about := adw.NewAboutDialog()
18
about.SetApplicationName("Dissent")
19
about.SetApplicationIcon("logo")
20
about.SetVersion("git") // TODO: version
21
about.SetWebsite("https://libdb.so/dissent")
22
about.SetCopyright("© 2023 diamondburned and contributors")
23
about.SetLicenseType(gtk.LicenseGPL30)
24
25
about.SetDevelopers([]string{
26
"diamondburned",
27
"Dissent contributors",
28
})
29
30
about.AddCreditSection("Sound Files", []string{
31
"freedesktop.org https://www.freedesktop.org/wiki/",
32
"Lennart Poettering",
33
})
34
35
build, ok := debug.ReadBuildInfo()
36
if ok {
37
about.AddCreditSection("Dependency Authors", modAuthors(build.Deps))
38
about.SetDebugInfo(debugInfo(build))
39
about.SetDebugInfoFilename("dissent-debuginfo")
40
41
version := buildVersion(build.Settings)
42
about.SetVersion(version)
43
44
if strings.HasSuffix(version, "(dirty)") {
45
about.AddCSSClass("devel")
46
about.SetApplicationIcon("logo-nightly")
47
}
48
}
49
50
return about
51
}
52
53
var customVersion string
54
55
// SetVersion sets the custom version string. It overrides the version string
56
// that's automatically generated from the build info.
57
func SetVersion(version string) {
58
customVersion = version
59
}
60
61
func buildVersion(settings []debug.BuildSetting) string {
62
if customVersion != "" {
63
return customVersion
64
}
65
66
find := func(name string) string {
67
for _, setting := range settings {
68
if setting.Key == name {
69
return setting.Value
70
}
71
}
72
return ""
73
}
74
75
vcs := find("vcs")
76
rev := find("vcs.revision")
77
modified := find("vcs.modified")
78
79
if vcs == "" {
80
return ""
81
}
82
83
if rev == "" {
84
return vcs
85
}
86
87
if len(rev) > 7 {
88
rev = rev[:7]
89
}
90
91
version := fmt.Sprintf("%s (%s)", vcs, rev)
92
if modified == "true" {
93
version += " (dirty)"
94
}
95
96
return version
97
}
98
99
func modAuthors(mods []*debug.Module) []string {
100
authors := make([]string, 0, len(mods))
101
authMap := make(map[string]struct{}, len(mods))
102
103
for _, mod := range mods {
104
author := path.Dir(mod.Path)
105
if _, ok := authMap[author]; !ok {
106
authors = append(authors, author)
107
authMap[author] = struct{}{}
108
}
109
}
110
111
return authors
112
}
113
114
func debugInfo(build *debug.BuildInfo) string {
115
var s strings.Builder
116
fmt.Fprintf(&s, "Version: %s", buildVersion(build.Settings))
117
s.WriteString("\n")
118
119
s.WriteString("Build Info:\n")
120
s.WriteString(build.String())
121
s.WriteString("\n\n")
122
123
s.WriteString("Last 50 log lines:\n")
124
s.WriteString(lastNLogLines(50))
125
s.WriteString("\n\n")
126
127
return strings.TrimSpace(s.String())
128
}
129
130
func lastNLogLines(n int) string {
131
handler := logui.DefaultLogHandler()
132
133
logModel := handler.ListModel()
134
nLogs := logModel.Len()
135
136
iter := logModel.RangeItems(max(0, nLogs-n), nLogs)
137
logs := logui.RecordsToString(iter)
138
return logs
139
}
140
141