| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
package main |
| 25 |
|
| 26 |
import ( |
| 27 |
"fmt" |
| 28 |
"math/rand" |
| 29 |
"os" |
| 30 |
"strconv" |
| 31 |
"strings" |
| 32 |
|
| 33 |
"github.com/gdamore/tcell/v3" |
| 34 |
"github.com/gdamore/tcell/v3/color" |
| 35 |
) |
| 36 |
|
| 37 |
var red = int32(rand.Int() % 256) |
| 38 |
var grn = int32(rand.Int() % 256) |
| 39 |
var blu = int32(rand.Int() % 256) |
| 40 |
var inc = int32(8) |
| 41 |
var redi = int32(inc) |
| 42 |
var grni = int32(inc) |
| 43 |
var blui = int32(inc) |
| 44 |
|
| 45 |
func makebox(s tcell.Screen, name string, fill color.Color) { |
| 46 |
w, h := s.Size() |
| 47 |
|
| 48 |
if w == 0 || h == 0 { |
| 49 |
return |
| 50 |
} |
| 51 |
|
| 52 |
lh := h - 3 |
| 53 |
lw := w |
| 54 |
lx := 0 |
| 55 |
ly := 0 |
| 56 |
st := tcell.StyleDefault |
| 57 |
|
| 58 |
s.Fill(' ', st) |
| 59 |
bg := st.Background(fill) |
| 60 |
for row := 0; row < lh; row++ { |
| 61 |
for col := 0; col < lw; col++ { |
| 62 |
s.Put(lx+col, ly+row, " ", bg) |
| 63 |
} |
| 64 |
} |
| 65 |
cn := fill.Name() |
| 66 |
if cn == "" { |
| 67 |
cn = "rgb" |
| 68 |
} |
| 69 |
msg := fmt.Sprintf("This is %s (#%06x, %s). Terminal supports %d colors.", name, fill.Hex(), cn, s.Colors()) |
| 70 |
if len(msg) < w { |
| 71 |
lx = (w - len(msg)) / 2 |
| 72 |
} else { |
| 73 |
lx = 0 |
| 74 |
} |
| 75 |
s.PutStrStyled(lx, lh+1, msg, st) |
| 76 |
s.Show() |
| 77 |
} |
| 78 |
|
| 79 |
func fatal(v string, args ...any) { |
| 80 |
fmt.Fprintf(os.Stderr, v+"\n", args...) |
| 81 |
os.Exit(1) |
| 82 |
} |
| 83 |
|
| 84 |
func main() { |
| 85 |
|
| 86 |
if len(os.Args) != 2 { |
| 87 |
fatal("usage: %s color", os.Args[0]) |
| 88 |
} |
| 89 |
|
| 90 |
var color_ color.Color |
| 91 |
|
| 92 |
if strings.HasPrefix(os.Args[1], "#") { |
| 93 |
name := os.Args[1][1:] |
| 94 |
if len(name) == 3 { |
| 95 |
hex, err := strconv.ParseUint(name, 16, 12) |
| 96 |
if err != nil { |
| 97 |
fatal("invalid color specification") |
| 98 |
} |
| 99 |
|
| 100 |
r := int32((hex & 0xf00) >> 8) |
| 101 |
g := int32((hex & 0x0f0) >> 4) |
| 102 |
b := int32(hex & 0x00f) |
| 103 |
r = r<<4 + r |
| 104 |
g = g<<4 + g |
| 105 |
b = b<<4 + b |
| 106 |
color_ = color.NewRGBColor(r, g, b) |
| 107 |
} else if len(name) == 6 { |
| 108 |
hex, err := strconv.ParseUint(name, 16, 32) |
| 109 |
if err != nil { |
| 110 |
fatal("invalid color specification") |
| 111 |
} |
| 112 |
color_ = color.NewHexColor(int32(hex)) |
| 113 |
} else { |
| 114 |
fatal("invalid color specification") |
| 115 |
} |
| 116 |
} else if val, err := strconv.Atoi(os.Args[1]); err == nil { |
| 117 |
color_ = color.Black + color.Color(val) |
| 118 |
} else if val, ok := color.Names[strings.ToLower(os.Args[1])]; ok { |
| 119 |
color_ = val |
| 120 |
} else { |
| 121 |
fatal("invalid color specification") |
| 122 |
} |
| 123 |
|
| 124 |
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII) |
| 125 |
s, e := tcell.NewScreen() |
| 126 |
if e != nil { |
| 127 |
fatal("new screen: %v", e.Error()) |
| 128 |
} |
| 129 |
if e = s.Init(); e != nil { |
| 130 |
fatal("new screen: %v", e.Error()) |
| 131 |
} |
| 132 |
|
| 133 |
s.SetStyle(tcell.StyleDefault. |
| 134 |
Foreground(color.Black). |
| 135 |
Background(color.White)) |
| 136 |
s.Clear() |
| 137 |
|
| 138 |
quit := make(chan struct{}) |
| 139 |
go func() { |
| 140 |
for { |
| 141 |
ev := <-s.EventQ() |
| 142 |
switch ev := ev.(type) { |
| 143 |
case *tcell.EventKey: |
| 144 |
switch ev.Key() { |
| 145 |
case tcell.KeyEscape, tcell.KeyEnter: |
| 146 |
close(quit) |
| 147 |
return |
| 148 |
case tcell.KeyCtrlL: |
| 149 |
s.Sync() |
| 150 |
} |
| 151 |
case *tcell.EventResize: |
| 152 |
s.Sync() |
| 153 |
} |
| 154 |
} |
| 155 |
}() |
| 156 |
|
| 157 |
makebox(s, os.Args[1], color_) |
| 158 |
<-quit |
| 159 |
|
| 160 |
s.Fini() |
| 161 |
} |