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
8 changes: 2 additions & 6 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 |
+-------------+-------+-------+

Expand Down
25 changes: 25 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
59 changes: 59 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
@@ -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()))
}
}
7 changes: 7 additions & 0 deletions testdata/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
9 changes: 9 additions & 0 deletions testdata/results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+--------+-----+-------+
| Title | LOC | Files |
+--------+-----+-------+
| Tests | 6 | 2 |
| Rust | 4 | 1 |
| Go | 8 | 1 |
| Python | 2 | 1 |
| Total | 24 | 4 |
+--------+-----+-------+
1 change: 1 addition & 0 deletions testdata/tests/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello world!")
3 changes: 3 additions & 0 deletions testdata/tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello World!");
}