From fd5a830d7afd683186e6dcf5932bdcfe04384eb2 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Sat, 23 Feb 2019 16:54:20 -0800 Subject: [PATCH] Rename style parsing functions --- CHANGELOG.md | 7 +++++++ text_parser.go => style_parser.go | 16 ++++++---------- widgets/list.go | 2 +- widgets/paragraph.go | 2 +- widgets/table.go | 2 +- 5 files changed, 16 insertions(+), 13 deletions(-) rename text_parser.go => style_parser.go (90%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 901b350..94db0af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## 2019/02/23 + +### Changed + +- s/TextParse/ParseStyles +- Removed AddColorMap in place of modifying StyleParserColorMap directly + ## 2019/01/31 ### Added diff --git a/text_parser.go b/style_parser.go similarity index 90% rename from text_parser.go rename to style_parser.go index dc01424..ce537da 100644 --- a/text_parser.go +++ b/style_parser.go @@ -31,7 +31,8 @@ const ( parserStateStyledText ) -var colorMap = map[string]Color{ +// StyleParserColorMap can be modified to add custom color parsing to text +var StyleParserColorMap = map[string]Color{ "red": ColorRed, "blue": ColorBlue, "black": ColorBlack, @@ -49,11 +50,6 @@ var modifierMap = map[string]Modifier{ "reverse": ModifierReverse, } -// AddColorMap allows users to add/override the string to Color mapping -func AddColorMap(str string, color Color) { - colorMap[str] = color -} - // readStyle translates an []rune like `fg:red,mod:bold,bg:white` to a style func readStyle(runes []rune, defaultStyle Style) Style { style := defaultStyle @@ -63,9 +59,9 @@ func readStyle(runes []rune, defaultStyle Style) Style { if len(pair) == 2 { switch pair[0] { case tokenFg: - style.Fg = colorMap[pair[1]] + style.Fg = StyleParserColorMap[pair[1]] case tokenBg: - style.Bg = colorMap[pair[1]] + style.Bg = StyleParserColorMap[pair[1]] case tokenModifier: style.Modifier = modifierMap[pair[1]] } @@ -74,11 +70,11 @@ func readStyle(runes []rune, defaultStyle Style) Style { return style } -// ParseText parses a string for embedded Styles and returns []Cell with the correct styling. +// ParseStyles parses a string for embedded Styles and returns []Cell with the correct styling. // Uses defaultStyle for any text without an embedded style. // Syntax is of the form [text](fg:,mod:,bg:). // Ordering does not matter. All fields are optional. -func ParseText(s string, defaultStyle Style) []Cell { +func ParseStyles(s string, defaultStyle Style) []Cell { cells := []Cell{} runes := []rune(s) state := parserStateDefault diff --git a/widgets/list.go b/widgets/list.go index f85bbad..a0a2320 100644 --- a/widgets/list.go +++ b/widgets/list.go @@ -42,7 +42,7 @@ func (self *List) Draw(buf *Buffer) { } for row := self.topRow; row < uint(len(self.Rows)) && point.Y < self.Inner.Max.Y; row++ { - cells := ParseText(self.Rows[row], self.TextStyle) + cells := ParseStyles(self.Rows[row], self.TextStyle) if self.WrapText { cells = WrapCells(cells, uint(self.Inner.Dx())) } diff --git a/widgets/paragraph.go b/widgets/paragraph.go index 7eb56bd..2c27f99 100644 --- a/widgets/paragraph.go +++ b/widgets/paragraph.go @@ -28,7 +28,7 @@ func NewParagraph() *Paragraph { func (self *Paragraph) Draw(buf *Buffer) { self.Block.Draw(buf) - cells := ParseText(self.Text, self.TextStyle) + cells := ParseStyles(self.Text, self.TextStyle) if self.WrapText { cells = WrapCells(cells, uint(self.Inner.Dx())) } diff --git a/widgets/table.go b/widgets/table.go index 6095954..d7b2487 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -71,7 +71,7 @@ func (self *Table) Draw(buf *Buffer) { // draw row cells for j := 0; j < len(row); j++ { - col := ParseText(row[j], rowStyle) + col := ParseStyles(row[j], rowStyle) // draw row cell if len(col) > columnWidths[j] || self.TextAlign == AlignLeft { for _, cx := range BuildCellWithXArray(col) {