diff --git a/style.go b/style.go index 46a1914..43dc093 100644 --- a/style.go +++ b/style.go @@ -66,20 +66,20 @@ func NewStyle(fg Color, args ...interface{}) Style { } } -//String returns a string representation of a Style +// String returns a string representation of a Style func (self Style) String() string { styles := make([]string, 0) - if color, ok := textColorMap[self.Fg]; ok && self.Fg != StyleClear.Fg { - styles = append(styles, tokenFg + tokenValueSeparator + color) + if color, ok := textColorMap[self.Fg]; ok && self.Fg != StyleClear.Fg { + styles = append(styles, tokenFg+tokenValueSeparator+color) } - if color, ok := textColorMap[self.Bg]; ok && self.Bg != StyleClear.Bg { - styles = append(styles, tokenBg + tokenValueSeparator + color) + if color, ok := textColorMap[self.Bg]; ok && self.Bg != StyleClear.Bg { + styles = append(styles, tokenBg+tokenValueSeparator+color) } - if mod, ok := textModifierMap[self.Modifier]; ok && self.Modifier != StyleClear.Modifier { - styles = append(styles, tokenModifier + tokenValueSeparator + mod) + if mod, ok := textModifierMap[self.Modifier]; ok && self.Modifier != StyleClear.Modifier { + styles = append(styles, tokenModifier+tokenValueSeparator+mod) } return strings.Join(styles, tokenItemSeparator) diff --git a/style_parser.go b/style_parser.go index f9e6793..be0c6e6 100644 --- a/style_parser.go +++ b/style_parser.go @@ -45,14 +45,14 @@ var StyleParserColorMap = map[string]Color{ } var textColorMap = map[Color]string{ - ColorRed: "red", - ColorBlue: "blue", - ColorBlack: "black", - ColorCyan: "cyan", - ColorYellow: "yellow", - ColorWhite: "white", - ColorClear: "clear", - ColorGreen: "green", + ColorRed: "red", + ColorBlue: "blue", + ColorBlack: "black", + ColorCyan: "cyan", + ColorYellow: "yellow", + ColorWhite: "white", + ColorClear: "clear", + ColorGreen: "green", ColorMagenta: "magenta", } @@ -63,9 +63,9 @@ var modifierMap = map[string]Modifier{ } var textModifierMap = map[Modifier]string{ - ModifierBold: "bold", + ModifierBold: "bold", ModifierUnderline: "underline", - ModifierReverse: "reverse", + ModifierReverse: "reverse", } // readStyle translates an []rune like `fg:red,mod:bold,bg:white` to a style diff --git a/utils.go b/utils.go index 2e18715..332cdeb 100644 --- a/utils.go +++ b/utils.go @@ -188,40 +188,7 @@ func RunesToStyledCells(runes []rune, style Style) []Cell { return cells } -//CellsToText converts []Cell to a string without any formatting tags -func CellsToText(cells []Cell) string { - runes := make([]rune, len(cells)) - for i, cell := range cells { - runes[i] = cell.Rune - } - return string(runes) -} - -//CellsToStyledText converts []Cell to a string preserving the formatting tags -func CellsToStyledText(cells []Cell, defaultStyle Style) string { - sb := strings.Builder{} - runes := make([]rune, len(cells)) - currentStyle := cells[0].Style - var j int - - for _, cell := range cells { - if currentStyle != cell.Style { - writeText(&sb, runes[:j], currentStyle, defaultStyle) - - currentStyle = cell.Style - j=0 - } - - runes[j] = cell.Rune - j++ - } - - //Write the last characters left in runes slice - writeText(&sb, runes[:j], currentStyle, defaultStyle) - - return sb.String() -} - +// CellsToString converts []Cell to a string without any formatting tags func CellsToString(cells []Cell) string { runes := make([]rune, len(cells)) for i, cell := range cells { @@ -230,7 +197,32 @@ func CellsToString(cells []Cell) string { return string(runes) } -func writeText(sb *strings.Builder,runes []rune, currentStyle Style, defaultStyle Style) { +// CellsToStyledString converts []Cell to a string preserving the formatting tags +func CellsToStyledString(cells []Cell, defaultStyle Style) string { + sb := strings.Builder{} + runes := make([]rune, len(cells)) + currentStyle := cells[0].Style + var j int + + for _, cell := range cells { + if currentStyle != cell.Style { + writeStyledText(&sb, runes[:j], currentStyle, defaultStyle) + + currentStyle = cell.Style + j = 0 + } + + runes[j] = cell.Rune + j++ + } + + // Write the last characters left in runes slice + writeStyledText(&sb, runes[:j], currentStyle, defaultStyle) + + return sb.String() +} + +func writeStyledText(sb *strings.Builder, runes []rune, currentStyle Style, defaultStyle Style) { if currentStyle != defaultStyle && currentStyle != StyleClear { sb.WriteByte(tokenBeginStyledText) sb.WriteString(string(runes)) @@ -276,7 +268,7 @@ func JoinCells(cells [][]Cell, r rune) []Cell { length := len(cells) for i, cell := range cells { - if i < length - 1 { + if i < length-1 { cell = append(cell, lb) } joinCells = append(joinCells, cell...) diff --git a/widgets/textbox.go b/widgets/textbox.go index 35cee54..05f0486 100644 --- a/widgets/textbox.go +++ b/widgets/textbox.go @@ -116,18 +116,14 @@ func (self *TextBox) SetText(input string) { self.InsertText(input) } -//GetText gets the text in string format along all its formatting tags -func (self *TextBox) Text() string { - cells := JoinCells(self.text, '\n') - - return CellsToStyledText(cells, self.TextStyle) +// GetText gets the text in string format along all its formatting tags +func (self *TextBox) GetText() string { + return CellsToStyledString(JoinCells(self.text, '\n'), self.TextStyle) } -//GetText gets the text in string format without any formatting tags -func (self *TextBox) RawText() string { - cells := JoinCells(self.text, '\n') - - return CellsToText(cells) +// GetRawText gets the text in string format without any formatting tags +func (self *TextBox) GetRawText() string { + return CellsToString(JoinCells(self.text, '\n')) } func (self *TextBox) MoveCursorLeft() { diff --git a/widgets/textbox_test.go b/widgets/textbox_test.go index e0ab191..95608b9 100644 --- a/widgets/textbox_test.go +++ b/widgets/textbox_test.go @@ -2,58 +2,59 @@ package widgets import ( "testing" + "github.com/stretchr/testify/assert" ) -//TestGetRawText test simple string +// TestGetRawText test simple string func TestGetRawText(t *testing.T) { text := "My Sample RawText" tb := NewTextBox() tb.SetText(text) - assert.Equal(t, text, tb.RawText()) + assert.Equal(t, text, tb.GetRawText()) } -//TestGetRawTextWithLBs test line breaks in the text +// TestGetRawTextWithLBs test line breaks in the text func TestGetRawTextWithLBs(t *testing.T) { text := `My Sample RawText - with - line + with + line breaks` tb := NewTextBox() tb.SetText(text) - assert.Equal(t, text, tb.RawText()) + assert.Equal(t, text, tb.GetRawText()) } -//TestGetStyledText test styled text +// TestGetStyledText test styled text func TestGetStyledText(t *testing.T) { text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a bit more" tb := NewTextBox() tb.SetText(text) - assert.Equal(t, text, tb.Text()) + assert.Equal(t, text, tb.GetText()) } -//TestGetStyledText2 test styled text ending in a styled string +// TestGetStyledText2 test styled text ending in a styled string func TestGetStyledText2(t *testing.T) { text := "[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a [bit more](fg:green)" tb := NewTextBox() tb.SetText(text) - assert.Equal(t, text, tb.Text()) + assert.Equal(t, text, tb.GetText()) } -//TestGetStyledTextWithLBs test styled text with line breaks +// TestGetStyledTextWithLBs test styled text with line breaks func TestGetStyledTextWithLBs(t *testing.T) { - text := `[red text](fg:red,mod:bold) more + text := `[red text](fg:red,mod:bold) more text [blue text](fg:blue,mod:bold) a [bit more](fg:green)` tb := NewTextBox() tb.SetText(text) - assert.Equal(t, text, tb.Text()) -} \ No newline at end of file + assert.Equal(t, text, tb.GetText()) +}