Skip to content

Commit f107114

Browse files
authored
Merge pull request #14 from GLundh/master
Various smaller JSON-fixes
2 parents eba3595 + 5aa9948 commit f107114

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

main.go

+41-25
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,14 @@ func main() {
136136

137137
var buf bytes.Buffer
138138
switch {
139-
case *jsonFlag && !*breakdownFlag:
140-
printJSON(out, &buf)
141139
case *jsonFlag && *breakdownFlag:
142-
printBreakDown(out, &buf)
140+
printBreakDown(out, &buf, true)
143141
case *breakdownFlag:
144-
printPercents(root, out, &buf, *countMode)
142+
printPercents(root, out, &buf, *countMode, false)
145143
buf.WriteByte('\n')
146-
printBreakDown(out, &buf)
144+
printBreakDown(out, &buf, false)
147145
default:
148-
printPercents(root, out, &buf, *countMode)
146+
printPercents(root, out, &buf, *countMode, *jsonFlag)
149147
}
150148

151149
fmt.Print(buf.String())
@@ -169,29 +167,29 @@ func usage() {
169167
)
170168
}
171169

172-
func printBreakDown(out map[string][]string, buff *bytes.Buffer) {
173-
for name, language := range out {
174-
fmt.Fprintln(buff, name)
175-
for _, file := range language {
176-
fmt.Fprintln(buff, file)
170+
func printBreakDown(out map[string][]string, buff *bytes.Buffer, jsonFlag bool) {
171+
if jsonFlag {
172+
json.NewEncoder(buff).Encode(out)
173+
} else {
174+
for name, language := range out {
175+
fmt.Fprintln(buff, name)
176+
for _, file := range language {
177+
fmt.Fprintln(buff, file)
178+
}
179+
fmt.Fprintln(buff)
177180
}
178-
179-
fmt.Fprintln(buff)
180181
}
181182
}
182183

183-
func printJSON(out map[string][]string, buf *bytes.Buffer) {
184-
json.NewEncoder(buf).Encode(out)
185-
}
186-
187184
// filelistError represents a failed operation that took place across multiple files.
188185
type filelistError []string
189186

190187
func (e filelistError) Error() string {
191188
return fmt.Sprintf("Could not process the following files:\n%s", strings.Join(e, "\n"))
192189
}
193190

194-
func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer, mode string) {
191+
func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer, mode string, isJSON bool) {
192+
195193
// Select the way we quantify 'amount' of code.
196194
reducer := fileCountValues
197195
switch mode {
@@ -225,13 +223,31 @@ func printPercents(root string, fSummary map[string][]string, buff *bytes.Buffer
225223
return fileValues[keys[i]] > fileValues[keys[j]]
226224
})
227225

228-
// Calculate and write percentages of each file type.
229-
for _, fType := range keys {
230-
val := fileValues[fType]
231-
percent := val / total * 100.0
232-
buff.WriteString(fmt.Sprintf("%.2f%%\t%s\n", percent, fType))
233-
if unreadableFiles != nil {
234-
buff.WriteString(fmt.Sprintf("\n%s", unreadableFiles.Error()))
226+
if isJSON {
227+
results := []map[string]interface{}{} // This will hold our language percentages
228+
229+
for _, fType := range keys {
230+
val := fileValues[fType]
231+
percent := val / total * 100.0
232+
results = append(results, map[string]interface{}{
233+
"percentage": fmt.Sprintf("%.2f%%", percent),
234+
"language": fType,
235+
"color": enry.GetColor(fType),
236+
"type": data.TypeForString(fType).String(),
237+
})
238+
}
239+
if err := json.NewEncoder(buff).Encode(results); err != nil {
240+
log.Println("Error encoding JSON:", err)
241+
}
242+
} else {
243+
// The existing code for plain text format goes here
244+
for _, fType := range keys {
245+
val := fileValues[fType]
246+
percent := val / total * 100.0
247+
buff.WriteString(fmt.Sprintf("%.2f%%\t%s\n", percent, fType))
248+
if unreadableFiles != nil {
249+
buff.WriteString(fmt.Sprintf("\n%s", unreadableFiles.Error()))
250+
}
235251
}
236252
}
237253
}

0 commit comments

Comments
 (0)