Skip to content

Commit b9cf5df

Browse files
committed
Added asciitable
1 parent 0a659c5 commit b9cf5df

6 files changed

Lines changed: 122 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
## To Do
1111

1212
- [ ] `anyascii`: converts to ASCII using [anyascii](https://github.com/anyascii/anyascii)
13-
- [ ] `asciitable`: prints out an table of ASCII characters
13+
- [x] `asciitable`: prints out an table of ASCII characters
1414
- [ ] `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)

bin/run-certinfo.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
#
3+
# run locally
4+
#
5+
6+
set -o errexit
7+
set -o nounset
8+
set -o pipefail
9+
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
REPO_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
12+
13+
cd "${REPO_DIR}"
14+
15+
go build -o ./dist/certinfo ./cmd/certinfo
16+
17+
./dist/certinfo https://www.fileformat.info/
18+
./dist/certinfo tmp/cert.pem
19+
./dist/certinfo tmp/cert.der

cmd/asciitable/asciitable.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/olekukonko/tablewriter"
8+
"github.com/olekukonko/tablewriter/renderer"
9+
"github.com/olekukonko/tablewriter/tw"
10+
"golang.org/x/text/encoding/charmap"
11+
)
12+
13+
var asciiMap = map[int]string{
14+
0x00: "NUL", 0x01: "SOH", 0x02: "STX", 0x03: "ETX",
15+
0x04: "EOT", 0x05: "ENQ", 0x06: "ACK", 0x07: "BEL",
16+
0x08: "BS", 0x09: "HT", 0x0A: "LF", 0x0B: "VT",
17+
0x0C: "FF", 0x0D: "CR", 0x0E: "SO", 0x0F: "SI",
18+
0x10: "DLE", 0x11: "DC1", 0x12: "DC2", 0x13: "DC3",
19+
0x14: "DC4", 0x15: "NAK", 0x16: "SYN", 0x17: "ETB",
20+
0x18: "CAN", 0x19: "EM", 0x1A: "SUB", 0x1B: "ESC",
21+
0x1C: "FS", 0x1D: "GS", 0x1E: "RS", 0x1F: "US",
22+
0x7F: "DEL"}
23+
24+
func main() {
25+
26+
// LATER: option to output markdown or pure ASCII (i.e. not using box-drawing characters)
27+
table := tablewriter.NewTable(os.Stdout,
28+
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
29+
Symbols: tw.NewSymbols(tw.StyleRounded),
30+
})),
31+
tablewriter.WithConfig(tablewriter.Config{
32+
Header: tw.CellConfig{
33+
Formatting: tw.CellFormatting{AutoFormat: tw.Off},
34+
Alignment: tw.CellAlignment{Global: tw.AlignCenter},
35+
},
36+
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}},
37+
}),
38+
)
39+
40+
// LATER: option to use a different code page
41+
decoder := charmap.CodePage437.NewDecoder()
42+
43+
header := []string{""}
44+
for i := 0; i <= 0xf0; i += 0x10 {
45+
header = append(header, fmt.Sprintf("0x%x", i))
46+
}
47+
table.Header(header)
48+
49+
for row := 0; row <= 0x0F; row += 0x01 {
50+
data := []string{fmt.Sprintf("0x%02x", row)}
51+
for col := 0; col <= 0xF0; col += 0x10 {
52+
i := row + col
53+
if i < 0x20 || i == 0x7F {
54+
data = append(data, asciiMap[i])
55+
} else if i > 0x7F {
56+
utf8, err := decoder.Bytes([]byte{byte(i)})
57+
if err != nil {
58+
data = append(data, fmt.Sprintf("0x%02x", i))
59+
} else {
60+
data = append(data, string(utf8))
61+
}
62+
} else {
63+
data = append(data, string(rune(i)))
64+
}
65+
}
66+
table.Append(data)
67+
}
68+
69+
table.Render()
70+
}

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ go 1.24
55
require golang.org/x/crypto v0.38.0
66

77
require (
8+
github.com/fatih/color v1.15.0 // indirect
9+
github.com/mattn/go-colorable v0.1.13 // indirect
10+
github.com/mattn/go-isatty v0.0.19 // indirect
11+
github.com/mattn/go-runewidth v0.0.16 // indirect
12+
github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6 // indirect
13+
github.com/olekukonko/ll v0.0.8 // indirect
14+
github.com/olekukonko/tablewriter v1.0.7 // indirect
15+
github.com/rivo/uniseg v0.2.0 // indirect
816
github.com/spf13/pflag v1.0.6 // indirect
917
golang.org/x/sys v0.33.0 // indirect
18+
golang.org/x/text v0.26.0 // indirect
1019
)

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
2+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
7+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
9+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
10+
github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6 h1:r3FaAI0NZK3hSmtTDrBVREhKULp8oUeqLT5Eyl2mSPo=
11+
github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
12+
github.com/olekukonko/ll v0.0.8 h1:sbGZ1Fx4QxJXEqL/6IG8GEFnYojUSQ45dJVwN2FH2fc=
13+
github.com/olekukonko/ll v0.0.8/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
14+
github.com/olekukonko/tablewriter v1.0.7 h1:HCC2e3MM+2g72M81ZcJU11uciw6z/p82aEnm4/ySDGw=
15+
github.com/olekukonko/tablewriter v1.0.7/go.mod h1:H428M+HzoUXC6JU2Abj9IT9ooRmdq9CxuDmKMtrOCMs=
16+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
17+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
118
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
219
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
320
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
421
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
22+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
524
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
625
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
26+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
27+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=

run.sh

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

10-
go build -o ./dist/certinfo ./cmd/certinfo
10+
go build -o ./dist/asciitable ./cmd/asciitable
1111

12-
./dist/certinfo https://www.fileformat.info/
13-
./dist/certinfo tmp/cert.pem
14-
./dist/certinfo tmp/cert.der
12+
./dist/asciitable

0 commit comments

Comments
 (0)