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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package main
import (
"fmt"
"github.com/fatih/color"
"strings"
"time"
"github.com/hako/durafmt"
)
// === Main Sections === //
func PrintHeader(data *statistics) {
printTypeNameAndDesc(data.Type, data.Name, data.Desc)
fmt.Println()
}
func PrintUserInteractionSummary(data *statistics) {
a := printInstallDesc(
data.Type == "formula",
data.InstalledAsDependency,
data.InstalledOnRequest,
data.InstalledVersion,
data.ReverseDependencies)
b := printRecentActivity(
data.InstalledVersion,
data.LastAccessTime,
data.BinaryExists)
if a || b { // add empty line only if any of above prints info
fmt.Println()
}
}
func PrintMetadata(data *statistics) {
printVersionInfo(
data.InstalledVersion,
data.Outdated)
printHomeAndLicense(data.Homepage, data.License)
}
// === #Section: Header# === //
func printTypeNameAndDesc(typ string, name string, desc string) {
var icon string
switch typ {
case "formula":
icon = "🍺"
case "cask":
icon = "☕️"
}
fmt.Printf("%s ", icon)
color.New(color.FgYellow).Printf("(%s) ", typ)
color.New(color.Bold).Printf("%s", name)
fmt.Printf(" - %s\n", desc)
}
// === #Section: User Interaction Summary# === //
// answers questions like "how it got here?"
func printInstallDesc(
isFormula bool,
asDependency bool,
onRequest bool,
version string,
reverseDependencies []string,
) (printed bool) {
// formula only
if !isFormula {
return false
}
if version == "" {
return false
} else if onRequest {
color.Blue("► You installed this package by running `brew install`.\n")
} else if asDependency {
color.Blue("► This package was installed automatically as a dependency.\n")
fmt.Printf("Used by: %s\n",
strings.Join(reverseDependencies, ", "))
}
return true
}
// answers questions like "have I used this recently?"
func printRecentActivity(
installed string,
atime time.Time,
binary bool) (printed bool) {
// skip if not installed or no related binary
if installed == "" || !binary {
return false
}
// print usage info
duratext := durafmt.ParseShort(time.Since(atime)).String()
if atime.Nanosecond() == 0 {
color.Blue("► You never used this.")
} else if time.Since(atime) < 24*7*time.Hour {
color.Blue("► You used this in %s.", duratext)
} else {
color.Blue("► You haven't used this for %s.", duratext)
}
return true
}
// === #Section: Metadata# === //
// prints badges like [Outdated] [Up to date] ...
// used by printMetadata
func printVersionInfo(installed string, outdated bool) {
if installed != "" {
var installBadge string
var installBadgeColor color.Attribute
if outdated {
installBadge = "[Outdated]"
installBadgeColor = color.FgYellow
} else {
installBadge = "[Up to date]"
installBadgeColor = color.FgGreen
}
fmt.Printf("📦 %s", installed)
color.New(installBadgeColor).Printf(" %s\n", installBadge)
} else {
color.Red("📦 [Not installed]")
}
}
func printHomeAndLicense(homepage string, license string) {
if homepage != "" {
fmt.Printf("🔗 %s\n", homepage)
}
if license != "" {
fmt.Printf("📜 %s\n", license)
}
}
|