1package main 2 3import ( 4 "fmt" 5 "sort" 6 "strconv" 7 8 "github.com/mattn/go-colorable" 9 "github.com/mgutz/ansi" 10) 11 12func main() { 13 printColors() 14 print256Colors() 15 printConstants() 16} 17 18func pad(s string, length int) string { 19 for len(s) < length { 20 s += " " 21 } 22 return s 23} 24 25func padColor(s string, styles []string) string { 26 buffer := "" 27 for _, style := range styles { 28 buffer += ansi.Color(pad(s+style, 20), s+style) 29 } 30 return buffer 31} 32 33func printPlain() { 34 ansi.DisableColors(true) 35 bgColors := []string{ 36 "", 37 ":black", 38 ":red", 39 ":green", 40 ":yellow", 41 ":blue", 42 ":magenta", 43 ":cyan", 44 ":white", 45 } 46 for fg := range ansi.Colors { 47 for _, bg := range bgColors { 48 println(padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg})) 49 println(padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"})) 50 println(padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"})) 51 } 52 } 53} 54 55func printColors() { 56 ansi.DisableColors(false) 57 stdout := colorable.NewColorableStdout() 58 59 bgColors := []string{ 60 "", 61 ":black", 62 ":red", 63 ":green", 64 ":yellow", 65 ":blue", 66 ":magenta", 67 ":cyan", 68 ":white", 69 } 70 71 keys := []string{} 72 for fg := range ansi.Colors { 73 _, err := strconv.Atoi(fg) 74 if err != nil { 75 keys = append(keys, fg) 76 } 77 } 78 sort.Strings(keys) 79 80 for _, fg := range keys { 81 for _, bg := range bgColors { 82 fmt.Fprintln(stdout, padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg})) 83 fmt.Fprintln(stdout, padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h", "+s" + bg})) 84 fmt.Fprintln(stdout, padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"})) 85 } 86 } 87} 88 89func print256Colors() { 90 ansi.DisableColors(false) 91 stdout := colorable.NewColorableStdout() 92 93 bgColors := []string{""} 94 for i := 0; i < 256; i++ { 95 key := fmt.Sprintf(":%d", i) 96 bgColors = append(bgColors, key) 97 } 98 99 keys := []string{} 100 for fg := range ansi.Colors { 101 n, err := strconv.Atoi(fg) 102 if err == nil { 103 keys = append(keys, fmt.Sprintf("%3d", n)) 104 } 105 } 106 sort.Strings(keys) 107 108 for _, fg := range keys { 109 for _, bg := range bgColors { 110 fmt.Fprintln(stdout, padColor(fg, []string{"" + bg, "+b" + bg, "+u" + bg})) 111 fmt.Fprintln(stdout, padColor(fg, []string{"+B" + bg, "+Bb" + bg, "+s" + bg})) 112 } 113 } 114} 115 116func printConstants() { 117 stdout := colorable.NewColorableStdout() 118 fmt.Fprintln(stdout, ansi.DefaultFG, "ansi.DefaultFG", ansi.Reset) 119 fmt.Fprintln(stdout, ansi.Black, "ansi.Black", ansi.Reset) 120 fmt.Fprintln(stdout, ansi.Red, "ansi.Red", ansi.Reset) 121 fmt.Fprintln(stdout, ansi.Green, "ansi.Green", ansi.Reset) 122 fmt.Fprintln(stdout, ansi.Yellow, "ansi.Yellow", ansi.Reset) 123 fmt.Fprintln(stdout, ansi.Blue, "ansi.Blue", ansi.Reset) 124 fmt.Fprintln(stdout, ansi.Magenta, "ansi.Magenta", ansi.Reset) 125 fmt.Fprintln(stdout, ansi.Cyan, "ansi.Cyan", ansi.Reset) 126 fmt.Fprintln(stdout, ansi.White, "ansi.White", ansi.Reset) 127 fmt.Fprintln(stdout, ansi.LightBlack, "ansi.LightBlack", ansi.Reset) 128 fmt.Fprintln(stdout, ansi.LightRed, "ansi.LightRed", ansi.Reset) 129 fmt.Fprintln(stdout, ansi.LightGreen, "ansi.LightGreen", ansi.Reset) 130 fmt.Fprintln(stdout, ansi.LightYellow, "ansi.LightYellow", ansi.Reset) 131 fmt.Fprintln(stdout, ansi.LightBlue, "ansi.LightBlue", ansi.Reset) 132 fmt.Fprintln(stdout, ansi.LightMagenta, "ansi.LightMagenta", ansi.Reset) 133 fmt.Fprintln(stdout, ansi.LightCyan, "ansi.LightCyan", ansi.Reset) 134 fmt.Fprintln(stdout, ansi.LightWhite, "ansi.LightWhite", ansi.Reset) 135} 136