|
| 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 | +} |
0 commit comments