Skip to content

Commit

Permalink
Merge pull request #233 from boyter/issue203
Browse files Browse the repository at this point in the history
csv formats
  • Loading branch information
boyter authored Feb 23, 2021
2 parents 95cc5a8 + e21995b commit 81f1bf0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 17 deletions.
30 changes: 15 additions & 15 deletions SCC-OUTPUT-REPORT.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<tbody><tr>
<th>Go</th>
<th>34</th>
<th>8424</th>
<th>1377</th>
<th>8511</th>
<th>1389</th>
<th>398</th>
<th>6649</th>
<th>1376</th>
<th>332840</th>
<th>6724</th>
<th>1384</th>
<th>334654</th>
</tr><tr>
<th>Java</th>
<th>24</th>
Expand Down Expand Up @@ -93,12 +93,12 @@
</tr><tr>
<th>Shell</th>
<th>3</th>
<th>1051</th>
<th>139</th>
<th>1069</th>
<th>141</th>
<th>84</th>
<th>828</th>
<th>92</th>
<th>37689</th>
<th>844</th>
<th>94</th>
<th>38381</th>
</tr><tr>
<th>C#</th>
<th>2</th>
Expand Down Expand Up @@ -571,11 +571,11 @@
<tfoot><tr>
<th>Total</th>
<th>168</th>
<th>25580</th>
<th>2922</th>
<th>25685</th>
<th>2936</th>
<th>1718</th>
<th>20940</th>
<th>2325</th>
<th>1773450</th>
<th>21031</th>
<th>2335</th>
<th>1775956</th>
</tr></tfoot>
</table></body></html>
89 changes: 88 additions & 1 deletion processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,93 @@ func toJSON(input chan *FileJob) string {
}

func toCSV(input chan *FileJob) string {
if Files {
return toCSVFiles(input)
}

return toCSVSummary(input)
}

func toCSVSummary(input chan *FileJob) string {
languages := map[string]LanguageSummary{}

for res := range input {
_, ok := languages[res.Language]

if !ok {
files := []*FileJob{}
if Files {
files = append(files, res)
}

languages[res.Language] = LanguageSummary{
Name: res.Language,
Lines: res.Lines,
Code: res.Code,
Comment: res.Comment,
Blank: res.Blank,
Complexity: res.Complexity,
Count: 1,
Files: files,
Bytes: res.Bytes,
}
} else {
tmp := languages[res.Language]
files := tmp.Files
if Files {
files = append(files, res)
}

languages[res.Language] = LanguageSummary{
Name: res.Language,
Lines: tmp.Lines + res.Lines,
Code: tmp.Code + res.Code,
Comment: tmp.Comment + res.Comment,
Blank: tmp.Blank + res.Blank,
Complexity: tmp.Complexity + res.Complexity,
Count: tmp.Count + 1,
Files: files,
Bytes: res.Bytes + tmp.Bytes,
}
}
}

language := []LanguageSummary{}
for _, summary := range languages {
language = append(language, summary)
}
language = sortLanguageSummary(language)

records := [][]string{{
"Language",
"Lines",
"Code",
"Comments",
"Blanks",
"Complexity",
"Bytes"},
}

for _, result := range language {
records = append(records, []string{
result.Name,
fmt.Sprint(result.Lines),
fmt.Sprint(result.Code),
fmt.Sprint(result.Comment),
fmt.Sprint(result.Blank),
fmt.Sprint(result.Complexity),
fmt.Sprint(result.Bytes)})
}

b := &bytes.Buffer{}
w := csv.NewWriter(b)
_ = w.WriteAll(records)
w.Flush()

return b.String()
}

func toCSVFiles(input chan *FileJob) string {
records := [][]string{{
"Language",
"Location",
Expand Down Expand Up @@ -286,7 +373,7 @@ func toCSV(input chan *FileJob) string {

b := &bytes.Buffer{}
w := csv.NewWriter(b)
w.WriteAll(records)
_ = w.WriteAll(records)
w.Flush()

return b.String()
Expand Down
20 changes: 19 additions & 1 deletion test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,25 @@ else
exit
fi

if ./scc --format-multi "tabular:stdout,html:stdout,csv:stdout" | grep -q "Language,Location"; then
if ./scc -f csv | grep -q "Language,Lines,Code,Comments,Blanks,Complexity,Bytes"; then
echo -e "${GREEN}PASSED csv summary"
else
echo -e "${RED}======================================================="
echo -e "FAILED csv summary"
echo -e "=======================================================${NC}"
exit
fi

if ./scc -f csv --by-file | grep -q "Language,Location,Filename,Lines,Code,Comments,Blanks,Complexity,Bytes"; then
echo -e "${GREEN}PASSED csv file"
else
echo -e "${RED}======================================================="
echo -e "FAILED csv file"
echo -e "=======================================================${NC}"
exit
fi

if ./scc --by-file --format-multi "tabular:stdout,html:stdout,csv:stdout" | grep -q "Language,Location"; then
echo -e "${GREEN}PASSED format multi check"
else
echo -e "${RED}======================================================="
Expand Down

0 comments on commit 81f1bf0

Please sign in to comment.