tput.go (815B)
1 package tput // hhvn.uk/prompt/tput 2 3 import ( 4 "os" 5 "fmt" 6 7 ti "github.com/gdamore/tcell/v2/terminfo" 8 tid "github.com/gdamore/tcell/v2/terminfo/dynamic" 9 ) 10 11 var t *ti.Terminfo 12 var err error 13 14 func init() { 15 term := os.Getenv("TERM") 16 17 t, _, err = tid.LoadTerminfo(term) 18 if err != nil { 19 err = fmt.Errorf("%s: %w", term, err) 20 } 21 } 22 23 func rlify(s string) string { 24 return fmt.Sprintf("\001%s\002", s) 25 } 26 27 func Strip(s string) string { 28 r := false 29 j := 0 30 31 b := []byte(s) 32 33 for i := range b { 34 if s[i] == 0x02 && r { 35 r = false 36 } else if r { 37 continue 38 } else if s[i] == 0x01 { 39 r = true 40 } else { 41 b[j] = b[i] 42 j++ 43 } 44 } 45 46 return string(b[:j]) 47 } 48 49 func CheckErr() error { 50 return err 51 } 52 53 func Fg(fg int) string { 54 return rlify(t.TParm(t.SetFg, fg)) 55 } 56 57 func Clear() string { 58 return rlify(t.AttrOff) 59 }