Rename style parsing functions

This commit is contained in:
Caleb Bassi 2019-02-23 16:54:20 -08:00
parent f8e30bacd3
commit fd5a830d7a
5 changed files with 16 additions and 13 deletions

View File

@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## 2019/02/23
### Changed
- s/TextParse/ParseStyles
- Removed AddColorMap in place of modifying StyleParserColorMap directly
## 2019/01/31 ## 2019/01/31
### Added ### Added

View File

@ -31,7 +31,8 @@ const (
parserStateStyledText parserStateStyledText
) )
var colorMap = map[string]Color{ // StyleParserColorMap can be modified to add custom color parsing to text
var StyleParserColorMap = map[string]Color{
"red": ColorRed, "red": ColorRed,
"blue": ColorBlue, "blue": ColorBlue,
"black": ColorBlack, "black": ColorBlack,
@ -49,11 +50,6 @@ var modifierMap = map[string]Modifier{
"reverse": ModifierReverse, "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 // readStyle translates an []rune like `fg:red,mod:bold,bg:white` to a style
func readStyle(runes []rune, defaultStyle Style) Style { func readStyle(runes []rune, defaultStyle Style) Style {
style := defaultStyle style := defaultStyle
@ -63,9 +59,9 @@ func readStyle(runes []rune, defaultStyle Style) Style {
if len(pair) == 2 { if len(pair) == 2 {
switch pair[0] { switch pair[0] {
case tokenFg: case tokenFg:
style.Fg = colorMap[pair[1]] style.Fg = StyleParserColorMap[pair[1]]
case tokenBg: case tokenBg:
style.Bg = colorMap[pair[1]] style.Bg = StyleParserColorMap[pair[1]]
case tokenModifier: case tokenModifier:
style.Modifier = modifierMap[pair[1]] style.Modifier = modifierMap[pair[1]]
} }
@ -74,11 +70,11 @@ func readStyle(runes []rune, defaultStyle Style) Style {
return 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. // Uses defaultStyle for any text without an embedded style.
// Syntax is of the form [text](fg:<color>,mod:<attribute>,bg:<color>). // Syntax is of the form [text](fg:<color>,mod:<attribute>,bg:<color>).
// Ordering does not matter. All fields are optional. // Ordering does not matter. All fields are optional.
func ParseText(s string, defaultStyle Style) []Cell { func ParseStyles(s string, defaultStyle Style) []Cell {
cells := []Cell{} cells := []Cell{}
runes := []rune(s) runes := []rune(s)
state := parserStateDefault state := parserStateDefault

View File

@ -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++ { 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 { if self.WrapText {
cells = WrapCells(cells, uint(self.Inner.Dx())) cells = WrapCells(cells, uint(self.Inner.Dx()))
} }

View File

@ -28,7 +28,7 @@ func NewParagraph() *Paragraph {
func (self *Paragraph) Draw(buf *Buffer) { func (self *Paragraph) Draw(buf *Buffer) {
self.Block.Draw(buf) self.Block.Draw(buf)
cells := ParseText(self.Text, self.TextStyle) cells := ParseStyles(self.Text, self.TextStyle)
if self.WrapText { if self.WrapText {
cells = WrapCells(cells, uint(self.Inner.Dx())) cells = WrapCells(cells, uint(self.Inner.Dx()))
} }

View File

@ -71,7 +71,7 @@ func (self *Table) Draw(buf *Buffer) {
// draw row cells // draw row cells
for j := 0; j < len(row); j++ { for j := 0; j < len(row); j++ {
col := ParseText(row[j], rowStyle) col := ParseStyles(row[j], rowStyle)
// draw row cell // draw row cell
if len(col) > columnWidths[j] || self.TextAlign == AlignLeft { if len(col) > columnWidths[j] || self.TextAlign == AlignLeft {
for _, cx := range BuildCellWithXArray(col) { for _, cx := range BuildCellWithXArray(col) {