Skip to content

Commit

Permalink
Merge pull request #9 from JoaoDanielRufino/feature/add-file-count
Browse files Browse the repository at this point in the history
Added file count
  • Loading branch information
JoaoDanielRufino authored May 16, 2022
2 parents 508c43d + 4db8d0b commit 9e7b6f0
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
run: make build
- name: Build Windows
if: matrix.os == 'windows-latest'
run: go build -o bin/gcloc cmd/main.go
run: go build -o bin/gcloc cmd/gcloc/main.go
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN go mod download -x

COPY . .

RUN go build -o bin/gcloc cmd/main.go
RUN go build -o bin/gcloc cmd/gcloc/main.go

FROM alpine:3.15.0

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY:build
build:
@echo "building the gcloc executable"
go build -o bin/gcloc cmd/main.go
go build -o bin/gcloc cmd/gcloc/main.go
@echo "executable created bin/gcloc"

.PHONY:clean
Expand Down
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ You can install from the stable release by clicking [here](https://github.com/Jo

```
$ gcloc .
Language | Lines | Blank lines | Comments | Code lines
-----------+-------+-------------+----------+-------------
Golang | 1503 | 248 | 12 | 1243
HTML | 576 | 82 | 16 | 478
YAML | 108 | 8 | 0 | 100
Makefile | 22 | 3 | 0 | 19
-----------+-------+-------------+----------+-------------
Total | 2209 | 341 | 28 | 1840
-----------+-------+-------------+----------+-------------
Language | Files | Lines | Blank lines | Comments | Code lines
-----------+-------+-------+-------------+----------+-------------
Golang | 26 | 1588 | 253 | 12 | 1323
HTML | 2 | 576 | 82 | 16 | 478
YAML | 4 | 108 | 8 | 0 | 100
Makefile | 1 | 22 | 3 | 0 | 19
-----------+-------+-------+-------------+----------+-------------
Total | 33 | 2294 | 346 | 28 | 1920
-----------+-------+-------+-------------+----------+-------------
```

### Excluding directories

```
$ gcloc jazzy-bot -e=node_modules --order-by-comment
Language | Lines | Blank lines | Comments | Code lines
-------------+-------+-------------+----------+-------------
YAML | 55 | 8 | 2 | 45
JavaScript | 1181 | 0 | 0 | 1181
TypeScript | 1180 | 189 | 0 | 991
-------------+-------+-------------+----------+-------------
Total | 2416 | 197 | 2 | 2217
-------------+-------+-------------+----------+-------------
Language | Files | Lines | Blank lines | Comments | Code lines
-------------+-------+-------+-------------+----------+-------------
YAML | 1 | 55 | 8 | 2 | 45
JavaScript | 26 | 1181 | 0 | 0 | 1181
TypeScript | 24 | 1180 | 189 | 0 | 991
-------------+-------+-------+-------------+----------+-------------
Total | 51 | 2416 | 197 | 2 | 2217
-------------+-------+-------+-------------+----------+-------------
```

### Via Docker
Expand Down Expand Up @@ -68,6 +68,7 @@ Flags:
--order-by-blank Show results ordered by blank lines
--order-by-code Show results ordered by lines of code
--order-by-comment Show results ordered by comments
--order-by-file Show results ordered by file count
--order-by-lang Show results ordered by language
--order-by-line Show results ordered by lines count
-v, --version version for gcloc
Expand Down
6 changes: 6 additions & 0 deletions internal/app/gcloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func getParams(cmd *cobra.Command, args []string) (gcloc.Params, error) {
return gcloc.Params{}, err
}

orderByFile, err := cmd.Flags().GetBool(constants.OrderByFileFlag)
if err != nil {
return gcloc.Params{}, err
}

orderByCode, err := cmd.Flags().GetBool(constants.OrderByCodeFlag)
if err != nil {
return gcloc.Params{}, err
Expand Down Expand Up @@ -105,6 +110,7 @@ func getParams(cmd *cobra.Command, args []string) (gcloc.Params, error) {
ExcludeExtensions: excludeExtensions,
ByFile: byFile,
OrderByLang: orderByLang,
OrderByFile: orderByFile,
OrderByCode: orderByCode,
OrderByLine: orderByLine,
OrderByBlank: orderByBlank,
Expand Down
5 changes: 5 additions & 0 deletions internal/app/gcloc_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ var gclocFlags = map[string]flags.Flag{
DefaultValue: false,
Description: "Show results ordered by language",
},
constants.OrderByFileFlag: {
Kind: reflect.Bool,
DefaultValue: false,
Description: "Show results ordered by file count",
},
constants.OrderByCodeFlag: {
Kind: reflect.Bool,
DefaultValue: false,
Expand Down
1 change: 1 addition & 0 deletions internal/constants/gcloc_flags_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
ExcludeExtensionsFlag = "exclude-extensions"
ByFileFlag = "by-file"
OrderByLangFlag = "order-by-lang"
OrderByFileFlag = "order-by-file"
OrderByCodeFlag = "order-by-code"
OrderByLineFlag = "order-by-line"
OrderByBlankFlag = "order-by-blank"
Expand Down
7 changes: 7 additions & 0 deletions pkg/gcloc/gcloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Params struct {
ExcludeExtensions []string
ByFile bool
OrderByLang bool
OrderByFile bool
OrderByCode bool
OrderByLine bool
OrderByBlank bool
Expand Down Expand Up @@ -108,6 +109,12 @@ func (gc *GCloc) sortSummary(summary *scanner.Summary) *sorter.SortedSummary {
return gc.sorter.OrderByBlankLines(summary)
}

if params.OrderByFile {
if languageSorter, ok := gc.sorter.(sorter.LanguageSorter); ok {
return languageSorter.OrderByFiles(summary)
}
}

return gc.sorter.OrderByCodeLines(summary)
}

Expand Down
44 changes: 31 additions & 13 deletions pkg/reporter/prompt/prompt_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,52 @@ type PromptReporter struct {
}

func (p PromptReporter) GenerateReportByLanguage(summary *sorter.SortedSummary) error {
tableHeader := []string{
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Language",
"Files",
"Lines",
"Blank lines",
"Comments",
"Code lines",
})
table.SetBorder(false)
table.SetAutoFormatHeaders(false)

for _, file := range summary.Results {
table.Append([]string{
file.Name,
strconv.Itoa(summary.FilesByLanguage[file.Name]),
strconv.Itoa(file.Lines),
strconv.Itoa(file.BlankLines),
strconv.Itoa(file.Comments),
strconv.Itoa(file.CodeLines),
})
}

p.printTable(tableHeader, summary)
table.SetFooter([]string{
"Total",
strconv.Itoa(summary.TotalFiles),
strconv.Itoa(summary.TotalLines),
strconv.Itoa(summary.TotalBlankLines),
strconv.Itoa(summary.TotalComments),
strconv.Itoa(summary.TotalCodeLines),
})

table.Render()

return nil
}

func (p PromptReporter) GenerateReportByFile(summary *sorter.SortedSummary) error {
tableHeader := []string{
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"Path",
"Lines",
"Blank lines",
"Comments",
"Code lines",
}

p.printTable(tableHeader, summary)

return nil
}

func (p PromptReporter) printTable(header []string, summary *sorter.SortedSummary) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
})
table.SetBorder(false)
table.SetAutoFormatHeaders(false)

Expand All @@ -64,4 +80,6 @@ func (p PromptReporter) printTable(header []string, summary *sorter.SortedSummar
})

table.Render()

return nil
}
14 changes: 11 additions & 3 deletions pkg/scanner/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,30 @@ type fileResult struct {
type Summary struct {
Languages map[string]*languageResult
Files []fileResult
FilesByLanguage map[string]int
TotalFiles int
TotalLines int
TotalCodeLines int
TotalBlankLines int
TotalComments int
}

func (sc *Scanner) Summary(results []scanResult) *Summary {
summary := &Summary{Languages: make(map[string]*languageResult)}
summary := &Summary{
Languages: make(map[string]*languageResult),
FilesByLanguage: make(map[string]int),
TotalFiles: len(results),
}

for _, result := range results {
if value, ok := summary.Languages[result.Metadata.Language]; ok {
language := result.Metadata.Language
if value, ok := summary.Languages[language]; ok {
value.Lines += result.Lines
value.CodeLines += result.CodeLines
value.BlankLines += result.BlankLines
value.Comments += result.Comments
} else {
summary.Languages[result.Metadata.Language] = &languageResult{
summary.Languages[language] = &languageResult{
Lines: result.Lines,
CodeLines: result.CodeLines,
BlankLines: result.BlankLines,
Expand All @@ -49,6 +56,7 @@ func (sc *Scanner) Summary(results []scanResult) *Summary {
BlankLines: result.BlankLines,
Comments: result.Comments,
})
summary.FilesByLanguage[language]++
summary.TotalLines += result.Lines
summary.TotalCodeLines += result.CodeLines
summary.TotalBlankLines += result.BlankLines
Expand Down
38 changes: 38 additions & 0 deletions pkg/sorter/language_sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func (l LanguageSorter) OrderByLanguage(summary *scanner.Summary) *SortedSummary

return &SortedSummary{
Results: results,
FilesByLanguage: summary.FilesByLanguage,
TotalFiles: summary.TotalFiles,
TotalLines: summary.TotalLines,
TotalCodeLines: summary.TotalCodeLines,
TotalBlankLines: summary.TotalBlankLines,
Expand All @@ -50,6 +52,8 @@ func (l LanguageSorter) OrderByCodeLines(summary *scanner.Summary) *SortedSummar

return &SortedSummary{
Results: results,
FilesByLanguage: summary.FilesByLanguage,
TotalFiles: summary.TotalFiles,
TotalLines: summary.TotalLines,
TotalCodeLines: summary.TotalCodeLines,
TotalBlankLines: summary.TotalBlankLines,
Expand All @@ -64,6 +68,8 @@ func (l LanguageSorter) OrderByLines(summary *scanner.Summary) *SortedSummary {

return &SortedSummary{
Results: results,
FilesByLanguage: summary.FilesByLanguage,
TotalFiles: summary.TotalFiles,
TotalLines: summary.TotalLines,
TotalCodeLines: summary.TotalCodeLines,
TotalBlankLines: summary.TotalBlankLines,
Expand All @@ -78,6 +84,8 @@ func (l LanguageSorter) OrderByComments(summary *scanner.Summary) *SortedSummary

return &SortedSummary{
Results: results,
FilesByLanguage: summary.FilesByLanguage,
TotalFiles: summary.TotalFiles,
TotalLines: summary.TotalLines,
TotalCodeLines: summary.TotalCodeLines,
TotalBlankLines: summary.TotalBlankLines,
Expand All @@ -92,6 +100,36 @@ func (l LanguageSorter) OrderByBlankLines(summary *scanner.Summary) *SortedSumma

return &SortedSummary{
Results: results,
FilesByLanguage: summary.FilesByLanguage,
TotalFiles: summary.TotalFiles,
TotalLines: summary.TotalLines,
TotalCodeLines: summary.TotalCodeLines,
TotalBlankLines: summary.TotalBlankLines,
TotalComments: summary.TotalComments,
}
}

func (l LanguageSorter) OrderByFiles(summary *scanner.Summary) *SortedSummary {
results := l.getResults(summary)

if l.sortOrder == "ASC" {
sort.Slice(results, func(i, j int) bool {
a := results[i].Name
b := results[j].Name
return summary.FilesByLanguage[a] < summary.FilesByLanguage[b]
})
} else if l.sortOrder == "DESC" {
sort.Slice(results, func(i, j int) bool {
a := results[i].Name
b := results[j].Name
return summary.FilesByLanguage[a] > summary.FilesByLanguage[b]
})
}

return &SortedSummary{
Results: results,
FilesByLanguage: summary.FilesByLanguage,
TotalFiles: summary.TotalFiles,
TotalLines: summary.TotalLines,
TotalCodeLines: summary.TotalCodeLines,
TotalBlankLines: summary.TotalBlankLines,
Expand Down
2 changes: 2 additions & 0 deletions pkg/sorter/sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Result struct {

type SortedSummary struct {
Results []Result
FilesByLanguage map[string]int
TotalFiles int
TotalLines int
TotalCodeLines int
TotalBlankLines int
Expand Down

0 comments on commit 9e7b6f0

Please sign in to comment.