Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3977121

Browse files
author
morikuni
authored
Merge pull request #1 from kyoh86/variadic-functions
Append variadic parameters to some functions
2 parents f268d39 + 7792b9c commit 3977121

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

ansi.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package aec
22

33
import (
44
"fmt"
5+
"strings"
56
)
67

78
const esc = "\x1b["
@@ -15,8 +16,8 @@ var empty = newAnsi("")
1516
type 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

3837
func (a *ansiImpl) Apply(s string) string {
@@ -42,3 +41,19 @@ func (a *ansiImpl) Apply(s string) string {
4241
func (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+
}

builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ type Builder struct {
99
var EmptyBuilder *Builder
1010

1111
// NewBuilder creates a Builder from existing ANSI.
12-
func NewBuilder(a ANSI) *Builder {
13-
return &Builder{a}
12+
func NewBuilder(a ...ANSI) *Builder {
13+
return &Builder{concat(a)}
1414
}
1515

1616
// With is a syntax for With.
17-
func (builder *Builder) With(a ANSI) *Builder {
18-
return NewBuilder(builder.ANSI.With(a))
17+
func (builder *Builder) With(a ...ANSI) *Builder {
18+
return NewBuilder(builder.ANSI.With(a...))
1919
}
2020

2121
// Up is a syntax for Up.

0 commit comments

Comments
 (0)