Skip to content

Commit 9bf362d

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent a11a97f commit 9bf362d

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

textstyle/textstyle.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"fmt"
2222
)
2323

24+
// Attribute represents a console text attribute (color or style).
25+
type Attribute string
26+
2427
const (
2528
Bold Attribute = "1"
2629
Italic Attribute = "3"
@@ -29,6 +32,7 @@ const (
2932
CrossedOut Attribute = "9"
3033
)
3134

35+
// Foreground colors
3236
const (
3337
Black Attribute = "30"
3438
Red Attribute = "31"
@@ -40,6 +44,7 @@ const (
4044
White Attribute = "37"
4145
)
4246

47+
// Background colors
4348
const (
4449
BgBlack Attribute = "40"
4550
BgRed Attribute = "41"
@@ -51,51 +56,54 @@ const (
5156
BgWhite Attribute = "47"
5257
)
5358

54-
type Attribute string
55-
56-
// Sprint returns a string formatted according to console properties.
59+
// Sprint formats a string with this Attribute.
5760
func (attr Attribute) Sprint(a ...any) string {
5861
return wrap([]Attribute{attr}, fmt.Sprint(a...))
5962
}
6063

61-
// Sprintf returns a string formatted according to console properties.
64+
// Sprintf formats a string with this Attribute using fmt.Sprintf syntax.
6265
func (attr Attribute) Sprintf(format string, a ...any) string {
6366
return wrap([]Attribute{attr}, fmt.Sprintf(format, a...))
6467
}
6568

69+
// Text represents a collection of console text attributes.
6670
type Text struct {
6771
attributes []Attribute
6872
}
6973

70-
// NewText returns a new *Text.
74+
// NewText creates a new Text with the given attributes.
7175
func NewText(attributes ...Attribute) *Text {
7276
return &Text{attributes: attributes}
7377
}
7478

75-
// Sprint returns a string formatted according to console properties.
79+
// Sprint formats a string using the Text's attributes.
7680
func (c *Text) Sprint(a ...any) string {
7781
return wrap(c.attributes, fmt.Sprint(a...))
7882
}
7983

80-
// Sprintf returns a string formatted according to console properties.
84+
// Sprintf formats a string using the Text's attributes and fmt.Sprintf syntax.
8185
func (c *Text) Sprintf(format string, a ...any) string {
8286
return wrap(c.attributes, fmt.Sprintf(format, a...))
8387
}
8488

89+
// wrap wraps the given string with ANSI escape codes for the provided attributes.
8590
func wrap(attributes []Attribute, str string) string {
8691
if len(attributes) == 0 {
8792
return str
8893
}
94+
8995
var buf bytes.Buffer
90-
buf.WriteString("\x1b[")
91-
for i := range len(attributes) {
96+
buf.WriteString("\x1b[") // Start ANSI escape code
97+
98+
for i := range attributes {
9299
buf.WriteString(string(attributes[i]))
93100
if i < len(attributes)-1 {
94-
buf.WriteByte(';')
101+
buf.WriteByte(';') // Separate multiple attributes with ';'
95102
}
96103
}
97-
buf.WriteByte('m')
98-
buf.WriteString(str)
99-
buf.WriteString("\x1b[0m")
104+
105+
buf.WriteByte('m') // End of ANSI escape code
106+
buf.WriteString(str) // Add the actual string
107+
buf.WriteString("\x1b[0m") // Reset all attributes
100108
return buf.String()
101109
}

0 commit comments

Comments
 (0)