-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathterminal-friendly.go
85 lines (78 loc) · 1.72 KB
/
terminal-friendly.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
package main
import (
"fmt"
"strings"
)
func isControlCharacter(char rune) bool {
return (char >= 0 && char <= 31 && char != 9 && char != 10 && char != 13) || // Exclude \t, \n, \r
char == 127 || // DEL (Delete)
(char >= 128 && char <= 159) // Extended ASCII control characters
}
func toCaretNotation(char rune) string {
charCode := uint32(char)
switch {
case charCode <= 31:
return fmt.Sprintf("^%c", charCode+64)
case charCode == 127:
return "^?"
default:
return fmt.Sprintf("\\x%02x", charCode)
}
}
func getTerminalFriendlyString(text string) string {
var result strings.Builder
for _, char := range text {
if isControlCharacter(char) {
result.WriteString(toCaretNotation(char))
} else {
result.WriteRune(char)
}
}
return result.String()
}
func main() {
// Test cases
testCases := []struct {
input string
expected string
}{
{
input: "Hello\x00World\x1F\x7F",
expected: "Hello^@World^_^?",
},
{
input: "\x1B[5;31mEmbrace the Red\x1B[0m",
expected: "^[[5;31mEmbrace the Red^[[0m",
},
{
input: "\x03Control\x04Codes",
expected: "^CControl^DCodes",
},
{
input: "Normal Text",
expected: "Normal Text",
},
{
input: "\x01Start\x02End\x1A",
expected: "^AStart^BEnd^Z",
},
{
input: "Line\rBreak\nTab\t",
expected: "Line\rBreak\nTab\t",
},
}
// Run test cases
for i, tc := range testCases {
result := getTerminalFriendlyString(tc.input)
fmt.Printf("Test Case %d:\n", i+1)
fmt.Printf("Input: %q\n", tc.input)
fmt.Printf("Expected: %q\n", tc.expected)
fmt.Printf("Result: %q\n", result)
if result != tc.expected {
fmt.Println("❌ Test Failed")
} else {
fmt.Println("✅ Test Passed")
}
fmt.Println()
}
}