-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_test.go
279 lines (261 loc) · 12.6 KB
/
main_test.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
"testing"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/exp/golden"
"github.com/stretchr/testify/require"
)
var c0c1 = map[string]string{
// c0
"c0": func() string {
var c0codes string
for controlCode := 0x00; controlCode <= 0x1f; controlCode++ {
c0codes = fmt.Sprintf("%s%c", c0codes, controlCode)
}
return c0codes
}(),
// c1
"c1": func() string {
var controlCode byte
c1codes := []byte("")
for controlCode = 0x80; controlCode <= 0x9f; controlCode++ {
// Skip DCS, SOS, CSI, OSC, PM, and APC
switch controlCode {
case ansi.DCS, ansi.SOS, ansi.CSI, ansi.OSC, ansi.PM, ansi.APC:
continue
}
c1codes = append(c1codes, controlCode)
}
return string(c1codes[:])
}(),
}
var ascii = map[string]string{
// space and del
"ascii": fmt.Sprintf(" %c", ansi.DEL),
}
var cursor = map[string]string{
// cursor
"save": ansi.SaveCursor,
"restore": ansi.RestoreCursor,
"request pos": ansi.RequestCursorPosition,
"request extended pos": ansi.RequestExtendedCursorPosition,
"invalid request extended pos": strings.Replace(ansi.RequestExtendedCursorPosition, "6", "7", 1),
"up 1": ansi.CursorUp1,
"up": ansi.CursorUp(5),
"down 1": ansi.CursorDown1,
"down": ansi.CursorDown(3),
"right 1": ansi.CursorRight1,
"right": ansi.CursorRight(3),
"left 1": ansi.CursorLeft1,
"left": ansi.CursorLeft(3),
"next line": ansi.CursorNextLine(3),
"previous line": ansi.CursorPreviousLine(3),
"set pos": ansi.SetCursorPosition(10, 20),
"home pos": ansi.CursorHomePosition,
"save pos": ansi.SaveCursorPosition,
"restore pos": ansi.RestoreCursorPosition,
"style 0": ansi.SetCursorStyle(0),
"style 1": ansi.SetCursorStyle(1),
"style 2": ansi.SetCursorStyle(2),
"style 3": ansi.SetCursorStyle(3),
"style 4": ansi.SetCursorStyle(4),
"style 5": ansi.SetCursorStyle(5),
"style 6": ansi.SetCursorStyle(6),
"style 7": ansi.SetCursorStyle(7),
"pointer shape": ansi.SetPointerShape("crosshair"),
"invalid pointer shape": strings.Replace(ansi.SetPointerShape(""), ";", "", 1),
}
var screen = map[string]string{
"enable alt buffer": ansi.SetAltScreenBufferMode,
"disable alt buffer": ansi.ResetAltScreenBufferMode,
"request alt buffer": ansi.RequestAltScreenBufferMode,
"passthrough": ansi.ScreenPassthrough(ansi.SaveCursor, 0), // TODO: impl
"erase above": ansi.EraseScreenAbove,
"erase below": ansi.EraseScreenBelow,
"erase full": ansi.EraseEntireScreen,
"erase display": ansi.EraseEntireDisplay,
"scrolling region": ansi.SetScrollingRegion(10, 20),
}
var line = map[string]string{
"right": ansi.EraseLineRight,
"left": ansi.EraseLineLeft,
"entire": ansi.EraseEntireLine,
"insert": ansi.InsertLine(3),
"delete": ansi.DeleteLine(5),
"scroll up": ansi.ScrollUp(12),
"scroll down": ansi.ScrollDown(12),
}
var mode = map[string]string{
"enable cursor keys": ansi.SetCursorKeysMode,
"disable cursor keys": ansi.ResetCursorKeysMode,
"request cursor keys": ansi.RequestCursorKeysMode,
"enable cursor visibility": ansi.ShowCursor,
"disable cursor visibility": ansi.HideCursor,
"request cursor visibility": ansi.RequestCursorVisibility,
"enable mouse": ansi.EnableMouse,
"disable mouse": ansi.DisableMouse,
"request mouse": ansi.RequestMouse,
"enable mouse hilite": ansi.EnableMouseHilite,
"disable mouse hilite": ansi.DisableMouseHilite,
"request mouse hilite": ansi.RequestMouseHilite,
"enable mouse cellmotion": ansi.EnableMouseCellMotion,
"disable mouse cellmotion": ansi.DisableMouseCellMotion,
"request mouse cellmotion": ansi.RequestMouseCellMotion,
"enable mouse allmotion": ansi.EnableMouseAllMotion,
"disable mouse allmotion": ansi.DisableMouseAllMotion,
"request mouse allmotion": ansi.RequestMouseAllMotion,
"enable report focus": ansi.SetFocusEventMode,
"disable report focus": ansi.ResetFocusEventMode,
"request report focus": ansi.RequestFocusEventMode,
"enable mouse sgr": ansi.SetSgrExtMouseMode,
"disable mouse sgr": ansi.ResetSgrExtMouseMode,
"request mouse sgr": ansi.RequestSgrExtMouseMode,
"enable altscreen": ansi.SetAltScreenBufferMode,
"disable altscreen": ansi.ResetAltScreenBufferMode,
"request altscreen": ansi.RequestAltScreenBufferMode,
"enable bracketed paste": ansi.SetBracketedPasteMode,
"disable bracketed paste": ansi.ResetBracketedPasteMode,
"request bracketed paste": ansi.RequestBracketedPasteMode,
"enable synchronized output": ansi.SetSynchronizedOutputMode,
"disable synchronized output": ansi.ResetSynchronizedOutputMode,
"request synchronized output": ansi.RequestSynchronizedOutputMode,
"enable grapheme clustering": ansi.SetGraphemeClusteringMode,
"disable grapheme clustering": ansi.ResetGraphemeClusteringMode,
"request grapheme clustering": ansi.RequestGraphemeClusteringMode,
"enable win32 input": ansi.SetWin32InputMode,
"disable win32 input": ansi.ResetWin32InputMode,
"request win32 input": ansi.RequestWin32InputMode,
"invalid": strings.Replace(ansi.ShowCursor, "25", "27", 1),
"non private": strings.Replace(ansi.ShowCursor, "?", "", 1),
}
var kitty = map[string]string{
"set all mode 1": ansi.KittyKeyboard(ansi.KittyAllFlags, 1),
"set all mode 2": ansi.KittyKeyboard(ansi.KittyAllFlags, 2),
"set all mode 3": ansi.KittyKeyboard(ansi.KittyAllFlags, 3),
"set invalid mode": ansi.KittyKeyboard(ansi.KittyAllFlags, 4),
"request": ansi.RequestKittyKeyboard,
"disable": ansi.DisableKittyKeyboard,
"pop": ansi.PopKittyKeyboard(2),
"push 1": ansi.PushKittyKeyboard(1),
"push 2": ansi.PushKittyKeyboard(2),
"push 4": ansi.PushKittyKeyboard(4),
"push 8": ansi.PushKittyKeyboard(8),
"push 16": ansi.PushKittyKeyboard(16),
}
var others = map[string]string{
"request primary device attrs": ansi.RequestPrimaryDeviceAttributes,
"request xt version": ansi.RequestXTVersion,
"termcap": ansi.RequestTermcap("bw", "ccc"),
"invalid termcap": strings.Replace(ansi.RequestTermcap("a"), hex.EncodeToString([]byte("a")), "", 1),
"invalid termcap hex": strings.Replace(ansi.RequestTermcap("a"), hex.EncodeToString([]byte("a")), "a", 1),
"invalid xt": strings.Replace(ansi.RequestXTVersion, "0", "1", 1),
"text": "some text",
"bold text": new(ansi.Style).Bold().String() + "some text" + ansi.ResetStyle,
"esc": fmt.Sprintf("%c", ansi.ESC),
"file sep": fmt.Sprintf("%c", ansi.FS),
"apc": "\x1b_Hello World\x1b\\",
"pm": "\x1b^Hello World\x1b\\",
"sos": "\x1bXHello World\x1b\\",
}
var sgr = map[string]string{
"reset": ansi.ResetStyle + strings.Replace(ansi.ResetStyle, "m", "0m", 1),
"style 1": new(ansi.Style).Bold().Faint().Italic().CurlyUnderline().String(),
"style 2": new(ansi.Style).SlowBlink().Reverse().Strikethrough().String(),
"style 3": new(ansi.Style).RapidBlink().BackgroundColor(ansi.Green).ForegroundColor(ansi.BrightGreen).UnderlineColor(ansi.Blue).String(),
"style 4": new(ansi.Style).BackgroundColor(ansi.BrightYellow).ForegroundColor(ansi.Black).UnderlineColor(ansi.BrightCyan).String(),
"style 5": new(ansi.Style).BackgroundColor(ansi.TrueColor(0xffeeaa)).ForegroundColor(ansi.TrueColor(0xffeeaa)).UnderlineColor(ansi.TrueColor(0xffeeaa)).String(),
"style 6": new(ansi.Style).BackgroundColor(ansi.ExtendedColor(255)).ForegroundColor(ansi.ExtendedColor(255)).UnderlineColor(ansi.ExtendedColor(255)).String(),
"style 7": new(ansi.Style).NoUnderline().NoBold().NoItalic().NormalIntensity().NoBlink().NoConceal().NoReverse().NoStrikethrough().String(),
"style 8": new(ansi.Style).UnderlineStyle(ansi.NoUnderlineStyle).DefaultBackgroundColor().String(),
"style 9": strings.Replace(new(ansi.Style).UnderlineStyle(ansi.SingleUnderlineStyle).DefaultForegroundColor().String(), "[4", "[4:1", 1),
"style 10": new(ansi.Style).UnderlineStyle(ansi.DoubleUnderlineStyle).String(),
"style 11": new(ansi.Style).UnderlineStyle(ansi.CurlyUnderlineStyle).String(),
"style 12": new(ansi.Style).UnderlineStyle(ansi.DottedUnderlineStyle).String(),
"style 13": new(ansi.Style).UnderlineStyle(ansi.DashedUnderlineStyle).Conceal().String(),
"empty values": strings.Replace(new(ansi.Style).Bold().String(), "[", "[;;;", 1),
"underlined text, but no bold": new(ansi.Style).UnderlineStyle(ansi.CurlyUnderlineStyle).Bold().String(),
"mittchels tweet": "\033[;4:3;38;2;175;175;215;58:2::190:80:70m",
}
var title = map[string]string{
"set": ansi.SetWindowTitle("hello"),
"set icon": ansi.SetIconName("terminal"),
"set both": ansi.SetIconNameWindowTitle("terminal"),
"invalid": strings.Replace(ansi.SetWindowTitle("hello"), ";hello", "", 1),
"invalid cmd": strings.Replace(ansi.SetWindowTitle("hello"), "2", "5", 1),
}
var cwd = map[string]string{
"single part": ansi.NotifyWorkingDirectory("localhost", "foo"),
"multiple parts": ansi.NotifyWorkingDirectory("localhost", "foo", "bar"),
"invalid": strings.Replace(ansi.NotifyWorkingDirectory("localhost", "foo"), ";", "", 1),
"invalid url": strings.Replace(ansi.NotifyWorkingDirectory("localhost", "foo"), "file://localhost/foo", "foooooo:/bar", 1),
}
var hyperlink = map[string]string{
"uri only": ansi.SetHyperlink("https://charm.sh"),
"full": ansi.SetHyperlink("https://charm.sh", "my title"),
"reset": ansi.ResetHyperlink("my title"),
"multiple params": ansi.SetHyperlink("https://charm.sh", "my title", "some description"),
"invalid": strings.Replace(ansi.ResetHyperlink(), ";", "", 1),
}
var notify = map[string]string{
"notify": ansi.Notify("notification body"),
"invalid": strings.Replace(ansi.Notify(""), ";", "", 1),
}
var termcolor = map[string]string{
"set bg": ansi.SetBackgroundColor(ansi.Black),
"set fg": ansi.SetForegroundColor(ansi.Red),
"set cursor": ansi.SetCursorColor(ansi.Blue),
"request bg": ansi.RequestBackgroundColor,
"request fg": ansi.RequestForegroundColor,
"request cursor": ansi.RequestCursorColor,
"reset bg": ansi.ResetBackgroundColor,
"reset fg": ansi.ResetForegroundColor,
"reset cursor": ansi.ResetCursorColor,
"invalid set": strings.Replace(ansi.SetBackgroundColor(ansi.Black), ";", "", 1),
"invalid reset": strings.Replace(ansi.ResetBackgroundColor, "111", "111;1", 1),
}
var clipboard = map[string]string{
"request system": ansi.RequestSystemClipboard,
"request primary": ansi.RequestPrimaryClipboard,
"set system": ansi.SetSystemClipboard("hello"),
"set primary": ansi.SetPrimaryClipboard("hello"),
"incomplete": strings.Replace(ansi.RequestPrimaryClipboard, ";?", "", 1),
"invalid": strings.Replace(ansi.SetPrimaryClipboard("hello"), "=", "", 1),
}
func TestSequences(t *testing.T) {
for name, table := range map[string]map[string]string{
"c0c1": c0c1,
"ascii": ascii,
"cursor": cursor,
"screen": screen,
"line": line,
"mode": mode,
"kitty": kitty,
"sgr": sgr,
"title": title,
"cwd": cwd,
"hyperlink": hyperlink,
"notify": notify,
"termcolor": termcolor,
"clipboard": clipboard,
"others": others,
} {
t.Run(name, func(t *testing.T) {
for name, input := range table {
t.Run(name, func(t *testing.T) {
var b bytes.Buffer
cmd := cmd()
cmd.SetOut(&b)
cmd.SetErr(&b)
cmd.SetIn(strings.NewReader(input))
cmd.SetArgs([]string{})
require.NoError(t, cmd.Execute())
golden.RequireEqual(t, b.Bytes())
})
}
})
}
}