Skip to content

Commit

Permalink
feat: add commas to numbers in table
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirezfernando committed Apr 14, 2024
1 parent e699bf6 commit 42e9e1d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

Thanks for your interest in contributing to cloc-tool!
Thanks for your interest in contributing!

## Contributing issues

Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module cloc-tool
module ube

go 1.20

require github.com/charmbracelet/bubbletea v0.25.0

require github.com/charmbracelet/lipgloss v0.10.0
require (
github.com/charmbracelet/lipgloss v0.10.0
github.com/charmbracelet/log v0.4.0
)

require (
github.com/charmbracelet/log v0.4.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
)
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand All @@ -31,23 +30,22 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34 changes: 28 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"bytes"
"cloc-tool/src/language"
"cloc-tool/src/terminal"
"ube/src/language"
"ube/src/terminal"
"io"
"io/fs"
"os"
Expand All @@ -22,17 +22,16 @@ import (
)

type languageDetails struct {
// TODO: Change to uint
lines int
files int
}

func main() {
if len(os.Args) != 2 {
log.Fatal(`Usage: cloc <folder>`)
log.Fatal(`Usage: ube <folder>`)
os.Exit(1)
} else if _, err := os.Stat(os.Args[1]); os.IsNotExist(err) {
log.Fatal("cloc: no such file or directory")
log.Fatal("ube: no such file or directory")
os.Exit(1)
}

Expand Down Expand Up @@ -160,14 +159,21 @@ func generateTable(lineCount map[string]languageDetails) table.Model {
lineTotal += details.lines
fileTotal += details.files
}

// Sort by lines of code
sort.Slice(rows, func(i, j int) bool {
li1, _ := strconv.Atoi(rows[i][1])
li2, _ := strconv.Atoi(rows[j][1])
return li1 > li2
})

for i, row := range rows {
rows[i][1] = FormatStringInteger(row[1])
rows[i][2] = FormatStringInteger(row[2])
}

rows = append(rows, table.Row{"", "", ""})
rows = append(rows, table.Row{"Total", strconv.Itoa(lineTotal), strconv.Itoa(fileTotal)})
rows = append(rows, table.Row{"Total", FormatStringInteger(strconv.Itoa(lineTotal)), FormatStringInteger(strconv.Itoa(fileTotal))})

t := table.New(
table.WithColumns(columns),
Expand All @@ -190,3 +196,19 @@ func generateTable(lineCount map[string]languageDetails) table.Model {

return t
}

func FormatStringInteger(n string) string {
if len(n) < 4 {
return n
}

var formatted string
for i, r := range n {
if i != 0 && (len(n)-i)%3 == 0 {
formatted += ","
}
formatted += string(r)
}

return formatted
}
22 changes: 21 additions & 1 deletion src/main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"cloc-tool/src/terminal"
"ube/src/terminal"
"reflect"
"testing"

Expand Down Expand Up @@ -129,3 +129,23 @@ func TestGenerateTable(t *testing.T) {
t.Errorf("Expected %v, but got %v", expectedTable, actualTable)
}
}

func TestFormatStringInteger(t *testing.T) {
tests := []struct {
number string
expectedNumber string
}{
{"1234", "1,234"},
{"123456", "123,456"},
{"1234567", "1,234,567"},
{"0", "0"},
}

for _, tt := range tests {
num := FormatStringInteger(tt.number)

if num != tt.expectedNumber {
t.Errorf("Expected %s lines, but got %s", tt.expectedNumber, num)
}
}
}

0 comments on commit 42e9e1d

Please sign in to comment.