diff --git a/cmd/main.go b/cmd/main.go index 90114fc..97c1acc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,7 +19,7 @@ func main () { if err != nil { fmt.Printf("ERROR: path \"%s\" is not found!!!\n", path) } else { - fmt.Print(internal.GetTable(response.Items)) + fmt.Print(internal.GetTable(response.Items, 5, 3, 5)) } } } diff --git a/internal/utils.go b/internal/utils.go index 74ac671..e7ee504 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -8,39 +8,44 @@ 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, ) @@ -48,9 +53,9 @@ func GetTable(items map[string]*core.TableItem) string { 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))), ) } diff --git a/internal/utils_test.go b/internal/utils_test.go index 742c496..710efe5 100644 --- a/internal/utils_test.go +++ b/internal/utils_test.go @@ -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)