Skip to content

Commit

Permalink
Add underline style and line mode for emphasis
Browse files Browse the repository at this point in the history
Add `Underline` style option.

Fix missing line mode for all text emphasis styles, like bold or italic.
  • Loading branch information
HeavyWombat committed Jun 19, 2019
1 parent 4e94726 commit 2faff69
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
27 changes: 26 additions & 1 deletion convenience.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func Substring(text string, start int, end int) string {
func Bold() StyleOption {
return StyleOption{
postProcess: func(s *String, flags map[string]struct{}) {
_, skipNewLine := flags["skipNewLine"]
for i := range *s {
if skipNewLine && (*s)[i].Symbol == '\n' {
continue
}

(*s)[i].Settings |= 1 << 2
}
},
Expand All @@ -82,20 +87,40 @@ func Bold() StyleOption {
func Italic() StyleOption {
return StyleOption{
postProcess: func(s *String, flags map[string]struct{}) {
_, skipNewLine := flags["skipNewLine"]
for i := range *s {
if skipNewLine && (*s)[i].Symbol == '\n' {
continue
}

(*s)[i].Settings |= 1 << 3
}
},
}
}

// Underline applies the underline text parameter
func Underline() StyleOption {
return StyleOption{
postProcess: func(s *String, flags map[string]struct{}) {
_, skipNewLine := flags["skipNewLine"]
for i := range *s {
if skipNewLine && (*s)[i].Symbol == '\n' {
continue
}

(*s)[i].Settings |= 1 << 4
}
},
}
}

// Foreground sets the given color as the foreground color of the text
func Foreground(color colorful.Color) StyleOption {
return StyleOption{
postProcess: func(s *String, flags map[string]struct{}) {
r, g, b := color.RGB255()
_, skipNewLine := flags["skipNewLine"]

for i := range *s {
if skipNewLine && (*s)[i].Symbol == '\n' {
continue
Expand Down
20 changes: 20 additions & 0 deletions convenience_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,25 @@ var _ = Describe("convenience functions", func() {
Expect(Style("text\ntext", Foreground(Yellow), EachLine())).To(
BeEquivalentTo("\x1b[38;2;255;255;0mtext\ntext\x1b[0m"))
})

It("should support text emphasis both line by line as well as full block mode", func() {
Expect(Style("text\ntext", Bold())).To(
BeEquivalentTo("\x1b[1mtext\ntext\x1b[0m"))

Expect(Style("text\ntext", Italic())).To(
BeEquivalentTo("\x1b[3mtext\ntext\x1b[0m"))

Expect(Style("text\ntext", Underline())).To(
BeEquivalentTo("\x1b[4mtext\ntext\x1b[0m"))

Expect(Style("text\ntext", EachLine(), Bold())).To(
BeEquivalentTo("\x1b[1mtext\x1b[0m\n\x1b[1mtext\x1b[0m"))

Expect(Style("text\ntext", EachLine(), Italic())).To(
BeEquivalentTo("\x1b[3mtext\x1b[0m\n\x1b[3mtext\x1b[0m"))

Expect(Style("text\ntext", EachLine(), Underline())).To(
BeEquivalentTo("\x1b[4mtext\x1b[0m\n\x1b[4mtext\x1b[0m"))
})
})
})

0 comments on commit 2faff69

Please sign in to comment.