cleanLinerInput 清理 liner 输入,移除 ANSI 转义序列和控制字符
(input string)
| 13 | |
| 14 | // cleanLinerInput 清理 liner 输入,移除 ANSI 转义序列和控制字符 |
| 15 | func cleanLinerInput(input string) string { |
| 16 | input = strings.TrimSpace(input) |
| 17 | var cleaned strings.Builder |
| 18 | inEscape := false |
| 19 | for _, r := range input { |
| 20 | if r == '\x1b' || r == '\033' { |
| 21 | inEscape = true |
| 22 | continue |
| 23 | } |
| 24 | if inEscape { |
| 25 | if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') { |
| 26 | inEscape = false |
| 27 | } |
| 28 | continue |
| 29 | } |
| 30 | if r < 32 || r == 127 { |
| 31 | continue |
| 32 | } |
| 33 | if r == '\u3000' || r == '\u00A0' { |
| 34 | continue |
| 35 | } |
| 36 | cleaned.WriteRune(r) |
| 37 | } |
| 38 | return strings.TrimSpace(cleaned.String()) |
| 39 | } |
| 40 | |
| 41 | // ShellContext holds state for shell commands |
| 42 | type ShellContext struct { |
no outgoing calls
no test coverage detected