From 53f93dcf566b36e3849196459d5c42648723ecf5 Mon Sep 17 00:00:00 2001 From: dmkjfs Date: Mon, 13 Oct 2025 22:31:23 +0300 Subject: [PATCH 1/5] add utils --- internal/utils.go | 59 ++++++++++++++++++++++++++++++++++++++++++ internal/utils_test.go | 26 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 internal/utils.go create mode 100644 internal/utils_test.go diff --git a/internal/utils.go b/internal/utils.go new file mode 100644 index 0000000..74ac671 --- /dev/null +++ b/internal/utils.go @@ -0,0 +1,59 @@ +package internal + +import ( + "fmt" + "strconv" + "strings" + + core "github.com/statloc/core" +) + +func GetTable(items map[string]*core.TableItem) string { + // im sorry for that + maxTitleLength, maxLOCLength, maxFilesLength := 5, 3, 5 + for title, item := range items { + if len(title) > maxTitleLength { + maxTitleLength = len(title) + } + + LOC := strconv.FormatUint(item.LOC, 10) + if len(LOC) > maxLOCLength { + maxLOCLength = len(LOC) + } + + files := strconv.FormatUint(item.Files, 10) + if len(files) > maxFilesLength { + maxFilesLength = len(files) + } + } + + separator := fmt.Sprintf( + "+-%s-+-%s-+-%s-+\n", + strings.Repeat("-", maxTitleLength), + strings.Repeat("-", maxLOCLength), + strings.Repeat("-", maxFilesLength), + ) + + 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), + ), + 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))), + ) + } + + result += separator + return result +} diff --git a/internal/utils_test.go b/internal/utils_test.go new file mode 100644 index 0000000..742c496 --- /dev/null +++ b/internal/utils_test.go @@ -0,0 +1,26 @@ +package internal_test + +import ( + "bufio" + "cli/internal" + "os" + "strings" + "testing" + + core "github.com/statloc/core" + "github.com/stretchr/testify/assert" +) + +func TestGetTable(t *testing.T) { + file, _ := os.Open("../testdata/results.txt") + defer file.Close() // nolint:errcheck + + statistics, _ := core.GetStatistics("../testdata") + table := internal.GetTable(statistics.Items) + + // go line by line + scanner := bufio.NewScanner(file) + for scanner.Scan() { + assert.True(t, strings.Contains(table, scanner.Text())) + } +} From 3994d9d839a2869d87608299dec7929fa3285af6 Mon Sep 17 00:00:00 2001 From: dmkjfs Date: Mon, 13 Oct 2025 22:31:41 +0300 Subject: [PATCH 2/5] add testdata --- testdata/main.go | 7 +++++++ testdata/results.txt | 9 +++++++++ testdata/tests/main.py | 1 + testdata/tests/main.rs | 3 +++ 4 files changed, 20 insertions(+) create mode 100644 testdata/main.go create mode 100644 testdata/results.txt create mode 100644 testdata/tests/main.py create mode 100644 testdata/tests/main.rs diff --git a/testdata/main.go b/testdata/main.go new file mode 100644 index 0000000..b7d2a32 --- /dev/null +++ b/testdata/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, World!") +} diff --git a/testdata/results.txt b/testdata/results.txt new file mode 100644 index 0000000..93bb3e9 --- /dev/null +++ b/testdata/results.txt @@ -0,0 +1,9 @@ ++--------+-----+-------+ +| Title | LOC | Files | ++--------+-----+-------+ +| Tests | 6 | 2 | +| Rust | 4 | 1 | +| Go | 8 | 1 | +| Python | 2 | 1 | +| Total | 24 | 4 | ++--------+-----+-------+ diff --git a/testdata/tests/main.py b/testdata/tests/main.py new file mode 100644 index 0000000..f1a1813 --- /dev/null +++ b/testdata/tests/main.py @@ -0,0 +1 @@ +print("Hello world!") diff --git a/testdata/tests/main.rs b/testdata/tests/main.rs new file mode 100644 index 0000000..47ad8c6 --- /dev/null +++ b/testdata/tests/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello World!"); +} From 5a9f918be5ff809becb19bcd39e73b287c2356e9 Mon Sep 17 00:00:00 2001 From: dmkjfs Date: Mon, 13 Oct 2025 22:31:50 +0300 Subject: [PATCH 3/5] add cmd --- cmd/main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cmd/main.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..28acc49 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "os" + + core "github.com/statloc/core" + + "cli/internal" +) + +func main () { + if len(os.Args) != 2 { + fmt.Println("Error parsing argument: path specified incorrectly") + } else { + path := os.Args[1] + response, err := core.GetStatistics(path) + + if err != nil { + fmt.Printf("Path %s is not found\n", path) + } + + fmt.Print(internal.GetTable(response.Items)) + } +} From a96d45255ccd54031a2e65f19a6b0c59148ab068 Mon Sep 17 00:00:00 2001 From: dmkjfs Date: Mon, 13 Oct 2025 22:39:14 +0300 Subject: [PATCH 4/5] update workflow --- .github/workflows/check.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 5bf932f..6e8b3f6 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -9,24 +9,20 @@ on: jobs: test: - name: test-${{ matrix.os }}-go${{ matrix.go-version }} + name: test-${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: matrix: - go-version: - - "1.25" - - "1.24" os: - "ubuntu-latest" - "windows-latest" - "macos-latest" - steps: - name: checkout uses: actions/checkout@v5 - name: setup go uses: actions/setup-go@v5 with: - go-version: ${{ matrix.go-version } + go-version: 1.25 - name: run tests run: go test ./... From 4861a9c2163b2e44f78ab0b6a867372048f5cdc0 Mon Sep 17 00:00:00 2001 From: dmkjfs Date: Tue, 14 Oct 2025 00:05:13 +0300 Subject: [PATCH 5/5] update readme --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 34da44c..347b02a 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,9 @@ - Simple and fast ### ⚡ Installation -As a go tool +Via `go install` ```shell -go get -tool github.com/statloc/cli +go install github.com/statloc/cli ``` ### 📝 Usage @@ -28,13 +28,9 @@ statloc project_name/ | Item | LOC | Files | +-------------+-------+-------+ | Go | 2339 | 27 | -+-------------+-------+-------+ | Python | 10398 | 112 | -+-------------+-------+-------+ | Rust | 970 | 11 | -+-------------+-------+-------+ | Tests | 4612 | 43 | -+-------------+-------+-------+ | Total | 13737 | 155 | +-------------+-------+-------+