Skip to content

Commit 089c101

Browse files
committed
Added bytecount
1 parent 20607c0 commit 089c101

5 files changed

Lines changed: 80 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
*~
22
**/*#
3+
boo
34
.cache
45
*.class
56
crash.log
67
_data/build.json
78
dist
89
.DS_Store
910
.env
11+
foo
1012
Gemfile.lock
1113
.idea/
1214
.jekyll-metadata

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
- [x] `asciify`: converts to ASCII using [anyascii](https://github.com/anyascii/anyascii)
1313
- [x] `asciitable`: prints out an table of ASCII characters
14-
- [ ] `bytecount`: counts the number of occurrences of each byte
14+
- [x] `bytecount`: counts the number of occurrences of each byte
1515
- [x] `certinfo`: print info about an x509 (aka SSL/HTTPS) certificate
1616
- [ ] `ghash`: calculate various [hashes available in the Go standard library](https://pkg.go.dev/crypto#Hash)
1717
- [x] `hexdumpc`: generate canonical hexdump (`hexdump -C`) output in case you don't have [`hexdump`](https://man7.org/linux/man-pages/man1/hexdump.1.html) installed

cmd/asciitable/asciitable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func main() {
4242

4343
header := []string{""}
4444
for i := 0; i <= 0xf0; i += 0x10 {
45-
header = append(header, fmt.Sprintf("0x%x", i))
45+
header = append(header, fmt.Sprintf("0x%02X", i))
4646
}
4747
table.Header(header)
4848

4949
for row := 0; row <= 0x0F; row += 0x01 {
50-
data := []string{fmt.Sprintf("0x%02x", row)}
50+
data := []string{fmt.Sprintf("0x%02X", row)}
5151
for col := 0; col <= 0xF0; col += 0x10 {
5252
i := row + col
5353
if i < 0x20 || i == 0x7F {

cmd/bytecount/bytecount.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"github.com/olekukonko/tablewriter"
10+
"github.com/olekukonko/tablewriter/renderer"
11+
"github.com/olekukonko/tablewriter/tw"
12+
"golang.org/x/text/language"
13+
"golang.org/x/text/message"
14+
)
15+
16+
func outputPretty(out io.Writer, counts map[byte]int) {
17+
18+
prettyPrinter := message.NewPrinter(language.English)
19+
// LATER: option to output markdown or pure ASCII (i.e. not using box-drawing characters)
20+
table := tablewriter.NewTable(out,
21+
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
22+
Symbols: tw.NewSymbols(tw.StyleRounded),
23+
})),
24+
tablewriter.WithConfig(tablewriter.Config{
25+
Header: tw.CellConfig{
26+
Formatting: tw.CellFormatting{AutoFormat: tw.Off},
27+
Alignment: tw.CellAlignment{Global: tw.AlignCenter},
28+
},
29+
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight}},
30+
}),
31+
)
32+
33+
header := []string{""}
34+
for i := 0; i <= 0xf0; i += 0x10 {
35+
header = append(header, fmt.Sprintf("0x%02X", i))
36+
}
37+
table.Header(header)
38+
39+
for row := 0; row <= 0x0F; row += 0x01 {
40+
data := []string{fmt.Sprintf("0x%02X", row)}
41+
for col := 0; col <= 0xF0; col += 0x10 {
42+
i := row + col
43+
data = append(data, prettyPrinter.Sprintf("%d", counts[byte(i)]))
44+
}
45+
table.Append(data)
46+
}
47+
48+
table.Render()
49+
50+
}
51+
52+
func main() {
53+
counts := make(map[byte]int)
54+
for i := 0; i < 256; i++ {
55+
counts[byte(i)] = 0
56+
}
57+
58+
reader := bufio.NewReader(os.Stdin)
59+
for {
60+
b, err := reader.ReadByte()
61+
if err == io.EOF {
62+
break
63+
}
64+
if err != nil {
65+
fmt.Fprintf(os.Stderr, "ERROR: unable to read input: %v\n", err)
66+
os.Exit(1)
67+
}
68+
counts[b]++
69+
}
70+
71+
//LATER: other output formats: plain, JSON, CSV
72+
outputPretty(os.Stdout, counts)
73+
}

run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ set -o errexit
77
set -o nounset
88
set -o pipefail
99

10-
CMD="${1:-asciify}"
10+
CMD="${1:-bytecount}"
1111

1212
go build -o "./dist/${CMD}" "./cmd/${CMD}"
1313

14-
./dist/${CMD}
14+
cat README.md | ./dist/${CMD}

0 commit comments

Comments
 (0)