@@ -2,6 +2,7 @@ package aec
22
33import (
44 "fmt"
5+ "strings"
56)
67
78const esc = "\x1b ["
@@ -15,8 +16,8 @@ var empty = newAnsi("")
1516type ANSI interface {
1617 fmt.Stringer
1718
18- // With adapts a given ANSI .
19- With (ANSI ) ANSI
19+ // With adapts given ANSIs .
20+ With (... ANSI ) ANSI
2021
2122 // Apply wraps given string in ANSI.
2223 Apply (string ) string
@@ -29,10 +30,8 @@ func newAnsi(s string) *ansiImpl {
2930 return & r
3031}
3132
32- func (a * ansiImpl ) With (ansi ANSI ) ANSI {
33- s := a .String () + ansi .String ()
34- r := ansiImpl (s )
35- return & r
33+ func (a * ansiImpl ) With (ansi ... ANSI ) ANSI {
34+ return concat (append ([]ANSI {a }, ansi ... ))
3635}
3736
3837func (a * ansiImpl ) Apply (s string ) string {
@@ -42,3 +41,19 @@ func (a *ansiImpl) Apply(s string) string {
4241func (a * ansiImpl ) String () string {
4342 return string (* a )
4443}
44+
45+ // Apply wraps given string in ANSIs.
46+ func Apply (s string , ansi ... ANSI ) string {
47+ if len (ansi ) == 0 {
48+ return s
49+ }
50+ return concat (ansi ).Apply (s )
51+ }
52+
53+ func concat (ansi []ANSI ) ANSI {
54+ strs := make ([]string , 0 , len (ansi ))
55+ for _ , p := range ansi {
56+ strs = append (strs , p .String ())
57+ }
58+ return newAnsi (strings .Join (strs , "" ))
59+ }
0 commit comments