Skip to content

Commit

Permalink
feat(count): Add flag for human readable bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed May 20, 2024
1 parent b7b2f51 commit 84120b8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
6 changes: 4 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (c *versionCmd) Run() error {
return nil
}

type countBytesCmd struct{}
type countBytesCmd struct {
Format bool `optional:"" default:"false" short:"f" help:"Human readable format"`
}

type countCharsCmd struct {
Char string `optional:"" short:"c" help:"Count only a specific character"`
Expand All @@ -83,7 +85,7 @@ func (c *countBytesCmd) Run(globals *Globals) error {
if err != nil {
return err
}
printOutput(countBytes(in), globals.Trim)
printOutput(countBytes(in, c.Format), globals.Trim)
return nil
}

Expand Down
10 changes: 8 additions & 2 deletions count.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

import (
"strconv"
"strings"
"unicode/utf8"

"github.com/dustin/go-humanize"
)

func countBytes(s string) int {
return len(s)
func countBytes(s string, human bool) string {
if human {
return humanize.Bytes(uint64(len(s)))
}
return strconv.Itoa(len(s))
}

func countChars(str string, chars ...rune) int {
Expand Down
18 changes: 10 additions & 8 deletions count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import (
func TestCountBytes(t *testing.T) {
tests := []struct {
input string
expected int
format bool
expected string
}{
{"Hello, World!", 13},
{"Hello, 世界", 13},
{"", 0},
{"abc", 3},
{"こんにちは", 15}, // 5 characters, 3 bytes each in UTF-8
{"Hello, World!", false, "13"},
{"Hello, 世界", false, "13"},
{"", false, "0"},
{"abc", false, "3"},
{"こんにちは", false, "15"}, // 5 characters, 3 bytes each in UTF-8
{"Hello, World!", true, "13 B"},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := countBytes(tt.input)
result := countBytes(tt.input, tt.format)
if result != tt.expected {
t.Errorf("countBytes(%q) = %d; want %d", tt.input, result, tt.expected)
t.Errorf("countBytes(%q) = %s; want %s", tt.input, result, tt.expected)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ require (
golang.org/x/crypto v0.23.0
golang.org/x/text v0.15.0
)

require github.com/dustin/go-humanize v1.0.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
Expand Down

0 comments on commit 84120b8

Please sign in to comment.