-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.go
175 lines (144 loc) · 4.37 KB
/
comment.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package todos
import (
"encoding/json"
"fmt"
"html/template"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
)
// WriteFileGroup writes the comments to the io.Writer as a file list
func WriteFileGroup(w io.Writer, comments []Comment, sortby string, desc bool) error {
if w == nil {
return fmt.Errorf("output is nil")
}
tabW := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
fileGroups := make(map[string][]Comment)
for _, comment := range comments {
fileGroups[comment.File] = append(fileGroups[comment.File], comment)
}
keys := make([]string, 0, len(fileGroups))
sort.Strings(keys)
for file, comments := range fileGroups {
fmt.Fprintf(tabW, "%s [%d Comments]:\n", file, len(comments))
if sortby != "" {
sort.Slice(comments, func(i, j int) bool {
switch sortby {
case "author":
return comments[i].Author < comments[j].Author
case "file":
return comments[i].File < comments[j].File
case "line":
return comments[i].Line < comments[j].Line
case "type":
return comments[i].Type < comments[j].Type
case "text":
return comments[i].Text < comments[j].Text
default:
return comments[i].File < comments[j].File
}
})
if desc {
for i, j := 0, len(comments)-1; i < j; i, j = i+1, j-1 {
comments[i], comments[j] = comments[j], comments[i]
}
}
}
for i, comment := range comments {
author := ""
if comment.Author != "" {
author = "(" + comment.Author + ")"
}
fmt.Fprintf(tabW, "%d\t|\t%s%s:\t%s\t\n", comment.Line, comment.Type, author, comment.Text)
if i == len(comments)-1 {
fmt.Fprintf(tabW, "\n")
}
}
}
return nil
}
// WriteJSON writes the comments to the io.Writer as JSON
func WriteJSON(w io.Writer, comments []Comment, sortby string, desc bool) error {
if len(comments) == 0 {
return nil
}
sortComments(comments, sortby, desc)
commentsJSON, err := json.MarshalIndent(comments, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return err
}
if _, err := w.Write(commentsJSON); err != nil {
return err
}
return nil
}
// WriteTable writes the comments to the io.Writer as a table
func WriteTable(w io.Writer, comments []Comment, sortby string, desc bool) error {
if len(comments) == 0 {
return nil
}
sortComments(comments, sortby, desc)
tabW := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
header := fmt.Sprintf("%s\t%s\t%s\t%s", "Type", "Author", "File:Line", "Text")
fmt.Fprintln(tabW, header)
for _, comment := range comments {
commentString := fmt.Sprintf("%s\t%s\t%s:%d\t%s", comment.Author, comment.Type, comment.File, comment.Line, comment.Text)
fmt.Fprintln(tabW, commentString)
}
return tabW.Flush()
}
// WriteMarkdown writes the comments to the io.Writer as a markdown table
func WriteMarkdown(w io.Writer, comments []Comment, sortby string, desc bool) error {
if len(comments) == 0 {
return nil
}
sortComments(comments, sortby, desc)
fmt.Println("| Type | Author | File:Line | Text |")
fmt.Println("| --- | --- | --- | --- |")
for _, comment := range comments {
fmt.Printf("| %s | %s | %s | %s:%d |\n", comment.Type, comment.Author, comment.Text, comment.File, comment.Line)
}
return nil
}
// WriteTemplate writes the comments to the io.Writer using the given template
func WriteTemplate(w io.Writer, comments []Comment, sortby string, desc bool, sourceStr string) error {
if len(comments) == 0 {
return nil
}
sortComments(comments, sortby, desc)
sourceStr = strings.Replace(sourceStr, `\n`, "\n", -1)
sourceStr = strings.Replace(sourceStr, `\t`, "\t", -1)
t, err := template.New("comments").Parse(sourceStr)
if err != nil {
return err
}
return t.Execute(w, comments)
}
// sortComments sorts the comments by the given sortby string and
// reverses the order if the sortby string ends with ":desc"
func sortComments(comments []Comment, sortby string, desc bool) {
sort.Slice(comments, func(i, j int) bool {
switch sortby {
case "author":
return comments[i].Author < comments[j].Author
case "file":
return comments[i].File < comments[j].File
case "line":
return comments[i].Line < comments[j].Line
case "type":
return comments[i].Type < comments[j].Type
case "text":
return comments[i].Text < comments[j].Text
default:
return comments[i].File < comments[j].File
}
})
if desc {
for i, j := 0, len(comments)-1; i < j; i, j = i+1, j-1 {
comments[i], comments[j] = comments[j], comments[i]
}
}
}