commit 1a2bed2ecbbb8819a12589dd08a927c3b1b60bcf parent ee3befdb660177ab4d09c81531e28d93fc8e69f9 Author: hhvn <dev@hhvn.uk> Date: Thu, 18 Apr 2024 22:34:59 +0100 Strip escape codes when comparing strings to "" Diffstat:
M | main.go | | | 2 | +- |
M | tput/tput.go | | | 22 | ++++++++++++++++++++++ |
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/main.go b/main.go @@ -136,7 +136,7 @@ func main(){ continue } - if s[i] == "" { + if tput.Strip(s[i]) == "" { continue } diff --git a/tput/tput.go b/tput/tput.go @@ -24,6 +24,28 @@ func rlify(s string) string { return fmt.Sprintf("\001%s\002", s) } +func Strip(s string) string { + r := false + j := 0 + + b := []byte(s) + + for i := range b { + if s[i] == 0x02 && r { + r = false + } else if r { + continue + } else if s[i] == 0x01 { + r = true + } else { + b[j] = b[i] + j++ + } + } + + return string(b[:j]) +} + func CheckErr() error { return err }