Skip to content

Commit 80d89c9

Browse files
authored
Merge pull request #70 from hhatto/impl-sort-option
impl: sort option
2 parents adad9a7 + 1ece263 commit 80d89c9

File tree

3 files changed

+101
-23
lines changed

3 files changed

+101
-23
lines changed

cmd/gocloc/main.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"regexp"
8-
"sort"
98
"strings"
109

1110
"github.com/hhatto/gocloc"
@@ -43,7 +42,7 @@ var rowLen = 79
4342
// It is necessary to use notation that follows go-flags.
4443
type CmdOptions struct {
4544
Byfile bool `long:"by-file" description:"report results for every encountered source file"`
46-
SortTag string `long:"sort" default:"code" description:"sort based on a certain column"`
45+
SortTag string `long:"sort" default:"code" description:"sort based on a certain column" choice:"name" choice:"files" choice:"blank" choice:"comment" choice:"code"`
4746
OutputType string `long:"output-type" default:"default" description:"output type [values: default,cloc-xml,sloccount,json]"`
4847
ExcludeExt string `long:"exclude-ext" description:"exclude file name extensions (separated commas)"`
4948
IncludeLang string `long:"include-lang" description:"include language name (separated commas)"`
@@ -103,7 +102,7 @@ func (o *outputBuilder) WriteFooter() {
103102
}
104103
}
105104

106-
func writeResultWithByFile(outputType string, result *gocloc.Result) {
105+
func writeResultWithByFile(opts *CmdOptions, result *gocloc.Result) {
107106
clocFiles := result.Files
108107
total := result.Total
109108
maxPathLen := result.MaxPathLength
@@ -112,9 +111,18 @@ func writeResultWithByFile(outputType string, result *gocloc.Result) {
112111
for _, file := range clocFiles {
113112
sortedFiles = append(sortedFiles, *file)
114113
}
115-
sort.Sort(sortedFiles)
114+
switch opts.SortTag {
115+
case "name":
116+
sortedFiles.SortByName()
117+
case "comment":
118+
sortedFiles.SortByComments()
119+
case "blank":
120+
sortedFiles.SortByBlanks()
121+
default:
122+
sortedFiles.SortByCode()
123+
}
116124

117-
switch outputType {
125+
switch opts.OutputType {
118126
case OutputTypeClocXML:
119127
t := gocloc.XMLTotalFiles{
120128
Code: total.Code,
@@ -166,15 +174,26 @@ func (o *outputBuilder) WriteResult() {
166174
total := o.result.Total
167175

168176
if o.opts.Byfile {
169-
writeResultWithByFile(o.opts.OutputType, o.result)
177+
writeResultWithByFile(o.opts, o.result)
170178
} else {
171179
var sortedLanguages gocloc.Languages
172180
for _, language := range clocLangs {
173181
if len(language.Files) != 0 {
174182
sortedLanguages = append(sortedLanguages, *language)
175183
}
176184
}
177-
sort.Sort(sortedLanguages)
185+
switch o.opts.SortTag {
186+
case "name":
187+
sortedLanguages.SortByName()
188+
case "files":
189+
sortedLanguages.SortByFiles()
190+
case "comment":
191+
sortedLanguages.SortByComments()
192+
case "blank":
193+
sortedLanguages.SortByBlanks()
194+
default:
195+
sortedLanguages.SortByCode()
196+
}
178197

179198
switch o.opts.OutputType {
180199
case OutputTypeClocXML:
@@ -231,6 +250,12 @@ func main() {
231250
return
232251
}
233252

253+
// check sort tag option with other options
254+
if opts.Byfile && opts.SortTag == "files" {
255+
fmt.Println("`--sort files` option cannot be used in conjunction with the `--by-file` option")
256+
os.Exit(1)
257+
}
258+
234259
// setup option for exclude extensions
235260
for _, ext := range strings.Split(opts.ExcludeExt, ",") {
236261
e, ok := gocloc.Exts[ext]

file.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"sort"
89
"strings"
910
"unicode"
1011
)
@@ -21,17 +22,38 @@ type ClocFile struct {
2122
// ClocFiles is gocloc result set.
2223
type ClocFiles []ClocFile
2324

24-
func (cf ClocFiles) Len() int {
25-
return len(cf)
25+
func (cf ClocFiles) SortByName() {
26+
sortFunc := func(i, j int) bool {
27+
return cf[i].Name < cf[j].Name
28+
}
29+
sort.Slice(cf, sortFunc)
2630
}
27-
func (cf ClocFiles) Swap(i, j int) {
28-
cf[i], cf[j] = cf[j], cf[i]
31+
32+
func (cf ClocFiles) SortByComments() {
33+
sortFunc := func(i, j int) bool {
34+
if cf[i].Comments == cf[j].Comments {
35+
return cf[i].Code > cf[j].Code
36+
}
37+
return cf[i].Comments > cf[j].Comments
38+
}
39+
sort.Slice(cf, sortFunc)
2940
}
30-
func (cf ClocFiles) Less(i, j int) bool {
31-
if cf[i].Code == cf[j].Code {
32-
return cf[i].Name < cf[j].Name
41+
42+
func (cf ClocFiles) SortByBlanks() {
43+
sortFunc := func(i, j int) bool {
44+
if cf[i].Blanks == cf[j].Blanks {
45+
return cf[i].Code > cf[j].Code
46+
}
47+
return cf[i].Blanks > cf[j].Blanks
48+
}
49+
sort.Slice(cf, sortFunc)
50+
}
51+
52+
func (cf ClocFiles) SortByCode() {
53+
sortFunc := func(i, j int) bool {
54+
return cf[i].Code > cf[j].Code
3355
}
34-
return cf[i].Code > cf[j].Code
56+
sort.Slice(cf, sortFunc)
3557
}
3658

3759
// AnalyzeFile is analyzing file, this function calls AnalyzeReader() inside.

language.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,48 @@ type Language struct {
3838
// Languages is an array representation of Language.
3939
type Languages []Language
4040

41-
func (ls Languages) Len() int {
42-
return len(ls)
41+
func (ls Languages) SortByName() {
42+
sortFunc := func(i, j int) bool {
43+
return ls[i].Name < ls[j].Name
44+
}
45+
sort.Slice(ls, sortFunc)
4346
}
44-
func (ls Languages) Swap(i, j int) {
45-
ls[i], ls[j] = ls[j], ls[i]
47+
48+
func (ls Languages) SortByFiles() {
49+
sortFunc := func(i, j int) bool {
50+
if len(ls[i].Files) == len(ls[j].Files) {
51+
return ls[i].Code > ls[j].Code
52+
}
53+
return len(ls[i].Files) > len(ls[j].Files)
54+
}
55+
sort.Slice(ls, sortFunc)
4656
}
47-
func (ls Languages) Less(i, j int) bool {
48-
if ls[i].Code == ls[j].Code {
49-
return ls[i].Name < ls[j].Name
57+
58+
func (ls Languages) SortByComments() {
59+
sortFunc := func(i, j int) bool {
60+
if ls[i].Comments == ls[j].Comments {
61+
return ls[i].Code > ls[j].Code
62+
}
63+
return ls[i].Comments > ls[j].Comments
64+
}
65+
sort.Slice(ls, sortFunc)
66+
}
67+
68+
func (ls Languages) SortByBlanks() {
69+
sortFunc := func(i, j int) bool {
70+
if ls[i].Blanks == ls[j].Blanks {
71+
return ls[i].Code > ls[j].Code
72+
}
73+
return ls[i].Blanks > ls[j].Blanks
74+
}
75+
sort.Slice(ls, sortFunc)
76+
}
77+
78+
func (ls Languages) SortByCode() {
79+
sortFunc := func(i, j int) bool {
80+
return ls[i].Code > ls[j].Code
5081
}
51-
return ls[i].Code > ls[j].Code
82+
sort.Slice(ls, sortFunc)
5283
}
5384

5485
var reShebangEnv = regexp.MustCompile(`^#! *(\S+/env) ([a-zA-Z]+)`)

0 commit comments

Comments
 (0)