Skip to content

Commit 0844dea

Browse files
committed
refactoring
1 parent db0b2ec commit 0844dea

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed

cmd/gocloc/main.go

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,64 +101,70 @@ func (o *outputBuilder) WriteFooter() {
101101
}
102102
}
103103

104+
func writeResultWithByFile(outputType string, result *gocloc.Result) {
105+
clocFiles := result.Files
106+
total := result.Total
107+
maxPathLen := result.MaxPathLength
108+
109+
var sortedFiles gocloc.ClocFiles
110+
for _, file := range clocFiles {
111+
sortedFiles = append(sortedFiles, *file)
112+
}
113+
sort.Sort(sortedFiles)
114+
115+
switch outputType {
116+
case OutputTypeClocXML:
117+
t := gocloc.XMLTotalFiles{
118+
Code: total.Code,
119+
Comment: total.Comments,
120+
Blank: total.Blanks,
121+
}
122+
f := &gocloc.XMLResultFiles{
123+
Files: sortedFiles,
124+
Total: t,
125+
}
126+
xmlResult := gocloc.XMLResult{
127+
XMLFiles: f,
128+
}
129+
xmlResult.Encode()
130+
case OutputTypeSloccount:
131+
for _, file := range sortedFiles {
132+
p := ""
133+
if strings.HasPrefix(file.Name, "./") || string(file.Name[0]) == "/" {
134+
splitPaths := strings.Split(file.Name, string(os.PathSeparator))
135+
if len(splitPaths) >= 3 {
136+
p = splitPaths[1]
137+
}
138+
}
139+
fmt.Printf("%v\t%v\t%v\t%v\n",
140+
file.Code, file.Lang, p, file.Name)
141+
}
142+
case OutputTypeJSON:
143+
jsonResult := gocloc.NewJSONFilesResultFromCloc(total, sortedFiles)
144+
buf, err := json.Marshal(jsonResult)
145+
if err != nil {
146+
fmt.Println(err)
147+
panic("json marshal error")
148+
}
149+
os.Stdout.Write(buf)
150+
default:
151+
for _, file := range sortedFiles {
152+
clocFile := file
153+
fmt.Printf("%-[1]*[2]s %21[3]v %14[4]v %14[5]v\n",
154+
maxPathLen, file.Name, clocFile.Blanks, clocFile.Comments, clocFile.Code)
155+
}
156+
}
157+
}
158+
104159
func (o *outputBuilder) WriteResult() {
105160
// write header
106161
o.WriteHeader()
107162

108-
clocFiles := o.result.Files
109163
clocLangs := o.result.Languages
110164
total := o.result.Total
111-
maxPathLen := o.result.MaxPathLength
112165

113166
if o.opts.Byfile {
114-
var sortedFiles gocloc.ClocFiles
115-
for _, file := range clocFiles {
116-
sortedFiles = append(sortedFiles, *file)
117-
}
118-
sort.Sort(sortedFiles)
119-
120-
switch o.opts.OutputType {
121-
case OutputTypeClocXML:
122-
t := gocloc.XMLTotalFiles{
123-
Code: total.Code,
124-
Comment: total.Comments,
125-
Blank: total.Blanks,
126-
}
127-
f := &gocloc.XMLResultFiles{
128-
Files: sortedFiles,
129-
Total: t,
130-
}
131-
xmlResult := gocloc.XMLResult{
132-
XMLFiles: f,
133-
}
134-
xmlResult.Encode()
135-
case OutputTypeSloccount:
136-
for _, file := range sortedFiles {
137-
p := ""
138-
if strings.HasPrefix(file.Name, "./") || string(file.Name[0]) == "/" {
139-
splitPaths := strings.Split(file.Name, string(os.PathSeparator))
140-
if len(splitPaths) >= 3 {
141-
p = splitPaths[1]
142-
}
143-
}
144-
fmt.Printf("%v\t%v\t%v\t%v\n",
145-
file.Code, file.Lang, p, file.Name)
146-
}
147-
case OutputTypeJSON:
148-
jsonResult := gocloc.NewJSONFilesResultFromCloc(total, sortedFiles)
149-
buf, err := json.Marshal(jsonResult)
150-
if err != nil {
151-
fmt.Println(err)
152-
panic("json marshal error")
153-
}
154-
os.Stdout.Write(buf)
155-
default:
156-
for _, file := range sortedFiles {
157-
clocFile := file
158-
fmt.Printf("%-[1]*[2]s %21[3]v %14[4]v %14[5]v\n",
159-
maxPathLen, file.Name, clocFile.Blanks, clocFile.Comments, clocFile.Code)
160-
}
161-
}
167+
writeResultWithByFile(o.opts.OutputType, o.result)
162168
} else {
163169
var sortedLanguages gocloc.Languages
164170
for _, language := range clocLangs {

0 commit comments

Comments
 (0)