File: showcolor.go

package info (click to toggle)
golang-github-gdamore-tcell.v2 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,064 kB
  • sloc: javascript: 219; sh: 16; makefile: 2
file content (176 lines) | stat: -rw-r--r-- 3,942 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//go:build ignore
// +build ignore

// Copyright 2019 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// colors just displays a single centered rectangle that should pulse
// through available colors.  It uses the RGB color cube, bumping at
// predefined larger intervals (values of about 8) in order that the
// changes happen quickly enough to be appreciable.
//
// Press ESC to exit the program.
package main

import (
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"strings"

	"github.com/gdamore/tcell/v2"
	"github.com/mattn/go-runewidth"
)

var red = int32(rand.Int() % 256)
var grn = int32(rand.Int() % 256)
var blu = int32(rand.Int() % 256)
var inc = int32(8) // rate of color change
var redi = int32(inc)
var grni = int32(inc)
var blui = int32(inc)

func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
	for _, c := range str {
		var comb []rune
		w := runewidth.RuneWidth(c)
		if w == 0 {
			comb = []rune{c}
			c = ' '
			w = 1
		}
		s.SetContent(x, y, c, comb, style)
		x += w
	}
}

func makebox(s tcell.Screen, name string, color tcell.Color) {
	w, h := s.Size()

	if w == 0 || h == 0 {
		return
	}

	lh := h - 3
	lw := w
	lx := 0
	ly := 0
	st := tcell.StyleDefault
	gl := ' '

	s.Fill(' ', st)
	bg := st.Background(color)
	for row := 0; row < lh; row++ {
		for col := 0; col < lw; col++ {
			s.SetCell(lx+col, ly+row, bg, gl)
		}
	}
	cn := color.Name()
	if cn == "" {
		cn = "rgb"
	}
	msg := fmt.Sprintf("This is %s (#%06x, %s). Terminal supports %d colors.", name, color.Hex(), cn, s.Colors())
	if len(msg) < w {
		lx = (w - len(msg)) / 2
	} else {
		lx = 0
	}
	emitStr(s, lx, lh+1, st, msg)
	s.Show()
}

func fatal(v string, args ...any) {
	fmt.Fprintf(os.Stderr, v+"\n", args...)
	os.Exit(1)
}

func main() {

	if len(os.Args) != 2 {
		fatal("usage: %s color", os.Args[0])
	}

	var color tcell.Color

	if strings.HasPrefix(os.Args[1], "#") {
		name := os.Args[1][1:]
		if len(name) == 3 {
			hex, err := strconv.ParseUint(name, 16, 12)
			if err != nil {
				fatal("invalid color specification")
			}
			// expand 12 bit color to 24 bit
			r := int32((hex & 0xf00) >> 8)
			g := int32((hex & 0x0f0) >> 4)
			b := int32(hex & 0x00f)
			r = r<<4 + r
			g = g<<4 + g
			b = b<<4 + b
			color = tcell.NewRGBColor(r, g, b)
		} else if len(name) == 6 {
			hex, err := strconv.ParseUint(name, 16, 32)
			if err != nil {
				fatal("invalid color specification")
			}
			color = tcell.NewHexColor(int32(hex))
		} else {
			fatal("invalid color specification")
		}
	} else if val, err := strconv.Atoi(os.Args[1]); err == nil {
		color = tcell.ColorBlack + tcell.Color(val)
	} else if val, ok := tcell.ColorNames[strings.ToLower(os.Args[1])]; ok {
		color = val
	} else {
		fatal("invalid color specification")
	}

	tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
	s, e := tcell.NewScreen()
	if e != nil {
		fatal("new screen: %v", e.Error())
	}
	if e = s.Init(); e != nil {
		fatal("new screen: %v", e.Error())
	}

	s.SetStyle(tcell.StyleDefault.
		Foreground(tcell.ColorBlack).
		Background(tcell.ColorWhite))
	s.Clear()

	quit := make(chan struct{})
	go func() {
		for {
			ev := s.PollEvent()
			switch ev := ev.(type) {
			case *tcell.EventKey:
				switch ev.Key() {
				case tcell.KeyEscape, tcell.KeyEnter:
					close(quit)
					return
				case tcell.KeyCtrlL:
					s.Sync()
				}
			case *tcell.EventResize:
				s.Sync()
			}
		}
	}()

	makebox(s, os.Args[1], color)
	<-quit

	s.Fini()
}