2022-11-11 09:06:22 +00:00
|
|
|
package utils
|
|
|
|
|
2022-11-14 04:15:16 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2022-12-11 10:00:15 +00:00
|
|
|
var CtrlChar = map[byte]string{
|
|
|
|
0: "NUL",
|
|
|
|
1: "SOH",
|
|
|
|
2: "STX",
|
|
|
|
3: "ETX",
|
|
|
|
4: "EOT",
|
|
|
|
5: "ENQ",
|
|
|
|
6: "ACK",
|
|
|
|
7: "BEL",
|
|
|
|
8: "BS",
|
|
|
|
9: "HT",
|
|
|
|
10: "LF",
|
|
|
|
11: "VT",
|
|
|
|
12: "FF",
|
|
|
|
13: "CR",
|
|
|
|
14: "SO",
|
|
|
|
15: "SI",
|
|
|
|
16: "DLE",
|
|
|
|
17: "DCI",
|
|
|
|
18: "DC2",
|
|
|
|
19: "DC3",
|
|
|
|
20: "DC4",
|
|
|
|
21: "NAK",
|
|
|
|
22: "SYN",
|
|
|
|
23: "TB",
|
|
|
|
24: "CAN",
|
|
|
|
25: "EM",
|
|
|
|
26: "SUB",
|
|
|
|
27: "ESC",
|
|
|
|
28: "FS",
|
|
|
|
29: "GS",
|
|
|
|
30: "RS",
|
|
|
|
31: "US",
|
|
|
|
32: "SPACE",
|
|
|
|
127: "DEL",
|
|
|
|
}
|
|
|
|
|
|
|
|
var CtrlCharGroup = map[string]string{
|
|
|
|
"1B5B41": "UP",
|
|
|
|
"1B5B42": "DOWN",
|
|
|
|
"1B5B43": "RIGHT",
|
|
|
|
"1B5B44": "LEFT",
|
|
|
|
|
|
|
|
// shell control codes
|
|
|
|
// codes[0]+codes[1]+codes[n-1]
|
|
|
|
// for example:
|
|
|
|
// [1B(ESC) 5B([) 32(2) 3B(;) 35(5) 52(R)]: row 2 col 5
|
|
|
|
// [1B(ESC) 5B([) 31(1) 30(0) 3B(;) 35(5) 52(R)]: row 10 col 5
|
|
|
|
// [1B(ESC) 5B([) 32(2) 32(2) 3B(;) 35(5) 52(R)]: row 22 col 5
|
|
|
|
"1B5B52": "",
|
|
|
|
|
|
|
|
// maybe there will be more control char group
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatWriteLog(codes []byte, line *string) {
|
2022-11-14 04:15:16 +00:00
|
|
|
n := len(codes)
|
2022-12-11 10:00:15 +00:00
|
|
|
// when user uses the keyboard arrow keys
|
|
|
|
// arrow keys are combination of 3 ASCII codes
|
2022-11-14 04:15:16 +00:00
|
|
|
if n == 3 {
|
2022-12-11 10:00:15 +00:00
|
|
|
if str, exist := ASCIIGroupToStr(fmt.Sprintf("%X", codes)); exist {
|
2022-11-14 04:15:16 +00:00
|
|
|
*line += str
|
2022-12-11 10:00:15 +00:00
|
|
|
return
|
2022-11-14 04:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-11 10:00:15 +00:00
|
|
|
// for some shells
|
|
|
|
// they will automatically send some control characters
|
|
|
|
// after typing the command and pressing Enter
|
|
|
|
// which indicate the current row and column.
|
2022-11-14 04:15:16 +00:00
|
|
|
if n >= 6 {
|
2022-12-11 10:00:15 +00:00
|
|
|
if str, exist := ASCIIGroupToStr(fmt.Sprintf("%X", []byte{codes[0], codes[1], codes[n-1]})); exist {
|
2022-11-14 04:15:16 +00:00
|
|
|
*line += str
|
2022-12-11 10:00:15 +00:00
|
|
|
return
|
2022-11-14 04:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:00:15 +00:00
|
|
|
str := ASCIIToStr(codes)
|
|
|
|
*line += str
|
2022-11-14 04:15:16 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-11 10:00:15 +00:00
|
|
|
func ASCIIToStr(codes []byte) (str string) {
|
2022-11-14 04:15:16 +00:00
|
|
|
for _, code := range codes {
|
2022-12-11 10:00:15 +00:00
|
|
|
if value, ok := CtrlChar[code]; ok {
|
2022-11-14 04:15:16 +00:00
|
|
|
str += value
|
|
|
|
} else {
|
|
|
|
str += string(code)
|
2022-11-11 09:06:22 +00:00
|
|
|
}
|
2022-11-14 04:15:16 +00:00
|
|
|
}
|
2022-11-11 09:06:22 +00:00
|
|
|
|
2022-12-11 10:00:15 +00:00
|
|
|
return
|
2022-11-14 04:15:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 10:00:15 +00:00
|
|
|
func ASCIIGroupToStr(group string) (string, bool) {
|
|
|
|
if value, ok := CtrlCharGroup[group]; ok {
|
2022-11-14 04:15:16 +00:00
|
|
|
return value, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
2022-11-11 09:06:22 +00:00
|
|
|
}
|