Skip to content

Commit 69cbc69

Browse files
authored
ENHANCEMENT: improve utils (#4)
1 parent f3bcd2c commit 69cbc69

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main () {
1919
if err != nil {
2020
fmt.Printf("ERROR: path \"%s\" is not found!!!\n", path)
2121
} else {
22-
fmt.Print(internal.GetTable(response.Items))
22+
fmt.Print(internal.GetTable(response.Items, 5, 3, 5))
2323
}
2424
}
2525
}

internal/utils.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,54 @@ import (
88
core "github.com/statloc/core"
99
)
1010

11-
func GetTable(items map[string]*core.TableItem) string {
11+
func GetTable(
12+
items map[string]*core.TableItem,
13+
titleLength int,
14+
LOCLength int,
15+
filesLength int,
16+
) string {
1217
// im sorry for that
13-
maxTitleLength, maxLOCLength, maxFilesLength := 5, 3, 5
18+
column1width, column2width, column3width := titleLength, LOCLength, filesLength
1419
for title, item := range items {
15-
if len(title) > maxTitleLength {
16-
maxTitleLength = len(title)
20+
if len(title) > int(column1width) {
21+
column1width = len(title)
1722
}
1823

1924
LOC := strconv.FormatUint(item.LOC, 10)
20-
if len(LOC) > maxLOCLength {
21-
maxLOCLength = len(LOC)
25+
if len(LOC) > column2width {
26+
column2width= len(LOC)
2227
}
2328

2429
files := strconv.FormatUint(item.Files, 10)
25-
if len(files) > maxFilesLength {
26-
maxFilesLength = len(files)
30+
if len(files) > column3width {
31+
column3width= len(files)
2732
}
2833
}
2934

3035
separator := fmt.Sprintf(
3136
"+-%s-+-%s-+-%s-+\n",
32-
strings.Repeat("-", maxTitleLength),
33-
strings.Repeat("-", maxLOCLength),
34-
strings.Repeat("-", maxFilesLength),
37+
strings.Repeat("-", column1width),
38+
strings.Repeat("-", column2width),
39+
strings.Repeat("-", column3width),
3540
)
3641

3742
result := fmt.Sprint(
3843
separator,
3944
fmt.Sprintf(
4045
"| Title%s | LOC%s | Files%s |\n",
41-
strings.Repeat(" ", maxTitleLength - 5),
42-
strings.Repeat(" ", maxLOCLength - 3),
43-
strings.Repeat(" ", maxFilesLength - 5),
46+
strings.Repeat(" ", column1width - titleLength),
47+
strings.Repeat(" ", column2width - LOCLength),
48+
strings.Repeat(" ", column3width - filesLength),
4449
),
4550
separator,
4651
)
4752

4853
for title, item := range items {
4954
result += fmt.Sprintf(
5055
"| %s%s | %d%s | %d%s |\n",
51-
title, strings.Repeat(" ", maxTitleLength - len(title)),
52-
item.LOC, strings.Repeat(" ", maxLOCLength - len(strconv.FormatUint(item.LOC, 10))),
53-
item.Files, strings.Repeat(" ", maxFilesLength - len(strconv.FormatUint(item.Files, 10))),
56+
title, strings.Repeat(" ", column1width - len(title)),
57+
item.LOC, strings.Repeat(" ", column2width - len(strconv.FormatUint(item.LOC, 10))),
58+
item.Files, strings.Repeat(" ", column3width - len(strconv.FormatUint(item.Files, 10))),
5459
)
5560
}
5661

internal/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestGetTable(t *testing.T) {
1616
defer file.Close() // nolint:errcheck
1717

1818
statistics, _ := core.GetStatistics("../testdata")
19-
table := internal.GetTable(statistics.Items)
19+
table := internal.GetTable(statistics.Items, 5, 3, 5)
2020

2121
// go line by line
2222
scanner := bufio.NewScanner(file)

0 commit comments

Comments
 (0)