-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcg_config.go
93 lines (78 loc) · 2.48 KB
/
tcg_config.go
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
package tcg
import (
"fmt"
"strconv"
"strings"
"github.com/gdamore/tcell/v2"
)
// Opt - options type for New tcg screen
type Opt func(*tcgConfig) error
type tcgConfig struct {
width, height int // screen size in characters
clip geom // clip, width == 0 - without clip
style tcell.Style
}
type geom struct {
x, y, width, height int
}
// WithClip - set clip of screen,
// x, y, w, h - is in screen character coordinates, not pixels
func WithClip(x, y, width, height int) Opt {
return func(cfg *tcgConfig) error {
if width < 1 || height < 1 {
return fmt.Errorf("width (%d) or height (%d) is less than 1 x 1", width, height)
}
if x+width > cfg.width || y+height > cfg.height {
return fmt.Errorf("clip size (%d, %d; %d x %d) does not fit in screen size (%d x %d)", x, y, width, height, cfg.width, cfg.height)
}
cfg.clip = geom{
x: x,
y: y,
width: width,
height: height,
}
return nil
}
}
// WithClipCenter - set clip of screen, placed in the center of screen
// w, h - is in screen character coordinates, not pixels
func WithClipCenter(width, height int) Opt {
return func(cfg *tcgConfig) error {
return WithClip((cfg.width-width)/2, (cfg.height-height)/2, width, height)(cfg)
}
}
// WithColor - set default color of pixels, this will affect the block of pixels per full symbol
// color can be in the form of different options: "blue", "yellow" or "#ffaa11", see tcell.GetColor
func WithColor(colorName string) Opt {
return func(cfg *tcgConfig) error {
cfg.style = cfg.style.Foreground(tcell.GetColor(colorName))
return nil
}
}
// WithBackgroundColor - set default background color of pixels, this will affect the block of pixels per full symbol
// color can be in the form of different options: "blue", "yellow" or "#ffaa11", see tcell.GetColor
func WithBackgroundColor(colorName string) Opt {
return func(cfg *tcgConfig) error {
cfg.style = cfg.style.Background(tcell.GetColor(colorName))
return nil
}
}
// ParseSizeString - parse size in "80x25" format
func ParseSizeString(in string) (int, int, error) {
parts := strings.SplitN(in, "x", 2)
if len(parts) != 2 {
return 0, 0, fmt.Errorf("size not in 'd x d' format: %s, %v", in, parts)
}
w, err := strconv.Atoi(strings.TrimSpace(parts[0]))
if err != nil {
return 0, 0, err
}
h, err := strconv.Atoi(strings.TrimSpace(parts[1]))
if err != nil {
return 0, 0, err
}
if w < 1 || h < 1 {
return 0, 0, fmt.Errorf("width (%d) or height (%d) less than 1", w, h)
}
return w, h, nil
}