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