Write some tests for the style wrapper

This commit is contained in:
Kovid Goyal
2022-08-27 14:15:43 +05:30
parent 91c61478dd
commit 42a8ca0842
3 changed files with 73 additions and 20 deletions

30
tools/utils/style/api.go Normal file
View File

@@ -0,0 +1,30 @@
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package style
import (
"fmt"
"strings"
)
type Context struct {
AllowEscapeCodes bool
}
func (self *Context) SprintFunc(spec string) func(args ...interface{}) string {
p := prefix_for_spec(spec)
s := suffix_for_spec(spec)
return func(args ...interface{}) string {
body := fmt.Sprint(args...)
if !self.AllowEscapeCodes {
return body
}
b := strings.Builder{}
b.Grow(len(p) + len(body) + len(s))
b.WriteString(p)
b.WriteString(body)
b.WriteString(s)
return b.String()
}
}