Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build:
test:
go test ./...

coverage:
cov:
go test -coverprofile=.coverage ./...

clean:
Expand Down
6 changes: 3 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func main () {
response, err := core.GetStatistics(path)

if err != nil {
fmt.Printf("Path %s is not found\n", path)
fmt.Printf("ERROR: path \"%s\" is not found!!!\n", path)
} else {
fmt.Print(internal.GetTable(response.Items, 5, 3, 5))
}

fmt.Print(internal.GetTable(response.Items))
}
}
39 changes: 22 additions & 17 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,54 @@ import (
core "github.com/statloc/core"
)

func GetTable(items map[string]*core.TableItem) string {
func GetTable(
items map[string]*core.TableItem,
titleLength int,
LOCLength int,
filesLength int,
) string {
// im sorry for that
maxTitleLength, maxLOCLength, maxFilesLength := 5, 3, 5
column1width, column2width, column3width := titleLength, LOCLength, filesLength
for title, item := range items {
if len(title) > maxTitleLength {
maxTitleLength = len(title)
if len(title) > int(column1width) {
column1width = len(title)
}

LOC := strconv.FormatUint(item.LOC, 10)
if len(LOC) > maxLOCLength {
maxLOCLength = len(LOC)
if len(LOC) > column2width {
column2width= len(LOC)
}

files := strconv.FormatUint(item.Files, 10)
if len(files) > maxFilesLength {
maxFilesLength = len(files)
if len(files) > column3width {
column3width= len(files)
}
}

separator := fmt.Sprintf(
"+-%s-+-%s-+-%s-+\n",
strings.Repeat("-", maxTitleLength),
strings.Repeat("-", maxLOCLength),
strings.Repeat("-", maxFilesLength),
strings.Repeat("-", column1width),
strings.Repeat("-", column2width),
strings.Repeat("-", column3width),
)

result := fmt.Sprint(
separator,
fmt.Sprintf(
"| Title%s | LOC%s | Files%s |\n",
strings.Repeat(" ", maxTitleLength - 5),
strings.Repeat(" ", maxLOCLength - 3),
strings.Repeat(" ", maxFilesLength - 5),
strings.Repeat(" ", column1width - titleLength),
strings.Repeat(" ", column2width - LOCLength),
strings.Repeat(" ", column3width - filesLength),
),
separator,
)

for title, item := range items {
result += fmt.Sprintf(
"| %s%s | %d%s | %d%s |\n",
title, strings.Repeat(" ", maxTitleLength - len(title)),
item.LOC, strings.Repeat(" ", maxLOCLength - len(strconv.FormatUint(item.LOC, 10))),
item.Files, strings.Repeat(" ", maxFilesLength - len(strconv.FormatUint(item.Files, 10))),
title, strings.Repeat(" ", column1width - len(title)),
item.LOC, strings.Repeat(" ", column2width - len(strconv.FormatUint(item.LOC, 10))),
item.Files, strings.Repeat(" ", column3width - len(strconv.FormatUint(item.Files, 10))),
)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestGetTable(t *testing.T) {
defer file.Close() // nolint:errcheck

statistics, _ := core.GetStatistics("../testdata")
table := internal.GetTable(statistics.Items)
table := internal.GetTable(statistics.Items, 5, 3, 5)

// go line by line
scanner := bufio.NewScanner(file)
Expand Down