-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.go
More file actions
121 lines (104 loc) · 2.93 KB
/
Copy pathoutput.go
File metadata and controls
121 lines (104 loc) · 2.93 KB
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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/mattn/go-isatty"
)
func outputJSON(entry *ChangelogEntry) {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(entry); err != nil {
fmt.Fprintf(os.Stderr, "Error encoding JSON: %v\n", err)
os.Exit(1)
}
}
func outputMarkdown(entry *ChangelogEntry) {
if !entry.ReleasedAt.IsZero() {
fmt.Printf("## %s (%s)\n\n", entry.Version, entry.ReleasedAt.Format("2006-01-02"))
} else {
fmt.Printf("## %s\n\n", entry.Version)
}
// Output sectioned changes
for _, section := range entry.Sections {
fmt.Printf("### %s\n\n", section.Name)
for _, change := range section.Changes {
fmt.Printf("- %s\n", change)
}
fmt.Println()
}
// Output ungrouped changes
for _, change := range entry.Changes {
fmt.Printf("- %s\n", change)
}
}
func outputVersionList(displayName string, entries []ChangelogEntry) {
if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
for _, entry := range entries {
fmt.Println(entry.Version)
}
return
}
fmt.Printf("%s — %d releases\n\n", displayName, len(entries))
rows := make([][]string, len(entries))
for i, entry := range entries {
date := "-"
ago := "-"
if !entry.ReleasedAt.IsZero() {
date = entry.ReleasedAt.Format("2006-01-02")
ago = formatRelativeTime(entry.ReleasedAt)
}
rows[i] = []string{entry.Version, date, ago}
}
headerStyle := lipgloss.NewStyle().Bold(true)
t := table.New().
Border(lipgloss.NormalBorder()).
Headers("Version", "Released", "Ago").
Rows(rows...).
StyleFunc(func(row, col int) lipgloss.Style {
if row == table.HeaderRow {
return headerStyle
}
return lipgloss.NewStyle()
})
fmt.Println(t)
}
func outputRendered(displayName string, entry *ChangelogEntry) {
var dateStr string
if !entry.ReleasedAt.IsZero() {
dateStr = fmt.Sprintf(" (%s)", entry.ReleasedAt.Format("2006-01-02"))
}
md := fmt.Sprintf("# %s %s%s\n\n%s", displayName, entry.Version, dateStr, entry.RawBody)
rendered, err := glamour.RenderWithEnvironmentConfig(md)
if err != nil {
outputPlainText(displayName, entry)
return
}
fmt.Print(rendered)
}
func outputPlainText(displayName string, entry *ChangelogEntry) {
if !entry.ReleasedAt.IsZero() {
fmt.Printf("%s %s (%s)\n", displayName, entry.Version, entry.ReleasedAt.Format("2006-01-02"))
} else {
fmt.Printf("%s %s\n", displayName, entry.Version)
}
fmt.Println(strings.Repeat("-", 40))
// Output sectioned changes
for _, section := range entry.Sections {
fmt.Printf("\n[%s]\n", section.Name)
for _, change := range section.Changes {
fmt.Printf(" * %s\n", change)
}
}
// Output ungrouped changes
if len(entry.Sections) > 0 && len(entry.Changes) > 0 {
fmt.Println()
}
for _, change := range entry.Changes {
fmt.Printf(" * %s\n", change)
}
}