1package tui
2
3import "strings"
4
5const (
6 InputStylePlain = "plain"
7 InputStyleLines = "lines"
8
9 StatusPositionAboveInput = "above_input"
10 StatusPositionBelowInput = "below_input"
11
12 WorkingPositionAboveInput = "above_input"
13 WorkingPositionBelowInput = "below_input"
14)
15
16func NormalizeInputStyle(v string) string {
17 switch strings.ToLower(strings.TrimSpace(v)) {
18 case InputStyleLines, "line", "boxed", "box":
19 return InputStyleLines
20 default:
21 return InputStylePlain
22 }
23}
24
25func NormalizeStatusPosition(v string) string {
26 switch strings.ToLower(strings.TrimSpace(v)) {
27 case StatusPositionBelowInput, "below", "bottom":
28 return StatusPositionBelowInput
29 default:
30 return StatusPositionAboveInput
31 }
32}
33
34func NormalizeWorkingPosition(v string) string {
35 switch strings.ToLower(strings.TrimSpace(v)) {
36 case WorkingPositionBelowInput, "below", "bottom":
37 return WorkingPositionBelowInput
38 default:
39 return WorkingPositionAboveInput
40 }
41}
42
43func InputLines(th Theme, lines []string, width int) []string {
44 if width < 1 {
45 width = 1
46 }
47 rule := th.FG256(th.Muted, strings.Repeat("─", width))
48 out := make([]string, 0, len(lines)+2)
49 out = append(out, rule)
50 out = append(out, lines...)
51 out = append(out, rule)
52 return out
53}
54
55func CursorColor256(index int) string {
56 r, g, b := xterm256RGB(index)
57 return "\x1b]12;rgb:" + hexByte(r) + "/" + hexByte(g) + "/" + hexByte(b) + "\x07"
58}
59
60func ResetCursorColor() string { return "\x1b]112\x07" }
61
62func CursorShapeBlock() string { return "\x1b[1 q" }
63
64func ResetCursorShape() string { return "\x1b[0 q" }
65
66func xterm256RGB(index int) (int, int, int) {
67 if index < 0 {
68 index = 0
69 }
70 if index > 255 {
71 index = 255
72 }
73 ansi := [16][3]int{
74 {0, 0, 0}, {128, 0, 0}, {0, 128, 0}, {128, 128, 0},
75 {0, 0, 128}, {128, 0, 128}, {0, 128, 128}, {192, 192, 192},
76 {128, 128, 128}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0},
77 {0, 0, 255}, {255, 0, 255}, {0, 255, 255}, {255, 255, 255},
78 }
79 if index < 16 {
80 c := ansi[index]
81 return c[0], c[1], c[2]
82 }
83 if index < 232 {
84 idx := index - 16
85 levels := [6]int{0, 95, 135, 175, 215, 255}
86 return levels[idx/36], levels[(idx/6)%6], levels[idx%6]
87 }
88 gray := 8 + (index-232)*10
89 return gray, gray, gray
90}
91
92func hexByte(v int) string {
93 const digits = "0123456789abcdef"
94 if v < 0 {
95 v = 0
96 }
97 if v > 255 {
98 v = 255
99 }
100 return string([]byte{digits[v>>4], digits[v&0xf]})
101}