From 2faff69f074100b5a995eefef5cb9fd30ac2030c Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Wed, 19 Jun 2019 23:42:30 +0200 Subject: [PATCH] Add underline style and line mode for emphasis Add `Underline` style option. Fix missing line mode for all text emphasis styles, like bold or italic. --- convenience.go | 27 ++++++++++++++++++++++++++- convenience_test.go | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/convenience.go b/convenience.go index 28b2f6f..544b25b 100644 --- a/convenience.go +++ b/convenience.go @@ -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 } }, @@ -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 diff --git a/convenience_test.go b/convenience_test.go index 4787f77..09a7f94 100644 --- a/convenience_test.go +++ b/convenience_test.go @@ -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")) + }) }) })