Skip to content

Commit

Permalink
internal: removed syntax for one word (#2)
Browse files Browse the repository at this point in the history
Style builder now uses functions from the color library.
  • Loading branch information
i582 authored Nov 14, 2020
1 parent ae3b1d7 commit 798493d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 167 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* [Install](#install)
* [Usage](#usage)
* [Simple usage](#simple-usage)
* [One word syntax](#one-word-syntax)
* [Complex style](#complex-style)
* [Custom styles](#custom-styles)
* [HEX colors](#hex-colors)
Expand Down Expand Up @@ -50,14 +49,6 @@ If we compare this with the `gookit/color` library, then this example looks more
fmt.Printf("This is a %s", color.Red.Sprintf("red color"))
```

### One word syntax

If you want to highlight one word, then the parentheses can be omitted:

```go
cfmt.Println("This is a red::red color")
```

### Complex style

It is even more convenient if you need complex styles, if you want to make the text also bold, then all you need to add is in the format after `|` write `bold`:
Expand Down
84 changes: 44 additions & 40 deletions internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,51 @@ import (

type styleCallback func(func(string) string) func(string) string

// Map is the custom styles map
var CustomMap = map[string]styleCallback{
"overline": overline,
}

// Map is the styles map
var Map = map[string]styleCallback{
"red": colorTemplate(color.Red),
"cyan": colorTemplate(color.Cyan),
"gray": colorTemplate(color.Gray),
"blue": colorTemplate(color.Blue),
"black": colorTemplate(color.Black),
"green": colorTemplate(color.Green),
"white": colorTemplate(color.White),
"yellow": colorTemplate(color.Yellow),
"magenta": colorTemplate(color.Magenta),
"lightRed": colorTemplate(color.LightRed),
"lightCyan": colorTemplate(color.LightCyan),
"lightBlue": colorTemplate(color.LightBlue),
"lightGreen": colorTemplate(color.LightGreen),
"lightWhite": colorTemplate(color.LightWhite),
"lightYellow": colorTemplate(color.LightYellow),
"lightMagenta": colorTemplate(color.LightMagenta),
var Map = map[string]color.Color{
"red": color.Red,
"cyan": color.Cyan,
"gray": color.Gray,
"blue": color.Blue,
"black": color.Black,
"green": color.Green,
"white": color.White,
"yellow": color.Yellow,
"magenta": color.Magenta,
"lightRed": color.LightRed,
"lightCyan": color.LightCyan,
"lightBlue": color.LightBlue,
"lightGreen": color.LightGreen,
"lightWhite": color.LightWhite,
"lightYellow": color.LightYellow,
"lightMagenta": color.LightMagenta,

"bgRed": colorTemplate(color.BgRed),
"bgGray": colorTemplate(color.BgGray),
"bgCyan": colorTemplate(color.BgCyan),
"bgBlue": colorTemplate(color.BgBlue),
"bgWhite": colorTemplate(color.BgWhite),
"bgBlack": colorTemplate(color.BgBlack),
"bgGreen": colorTemplate(color.BgGreen),
"bgYellow": colorTemplate(color.BgYellow),
"bgMagenta": colorTemplate(color.BgMagenta),
"bgDefault": colorTemplate(color.BgDefault),
"bgDarkGray": colorTemplate(color.BgDarkGray),
"bgLightRed": colorTemplate(color.BgLightRed),
"bgLightCyan": colorTemplate(color.BgLightCyan),
"bgLightBlue": colorTemplate(color.BgLightBlue),
"bgLightWhite": colorTemplate(color.BgLightWhite),
"bgLightGreen": colorTemplate(color.BgLightGreen),
"bgLightYellow": colorTemplate(color.BgLightYellow),
"bgLightMagenta": colorTemplate(color.BgLightMagenta),
"bgRed": color.BgRed,
"bgGray": color.BgGray,
"bgCyan": color.BgCyan,
"bgBlue": color.BgBlue,
"bgWhite": color.BgWhite,
"bgBlack": color.BgBlack,
"bgGreen": color.BgGreen,
"bgYellow": color.BgYellow,
"bgMagenta": color.BgMagenta,
"bgDefault": color.BgDefault,
"bgDarkGray": color.BgDarkGray,
"bgLightRed": color.BgLightRed,
"bgLightCyan": color.BgLightCyan,
"bgLightBlue": color.BgLightBlue,
"bgLightWhite": color.BgLightWhite,
"bgLightGreen": color.BgLightGreen,
"bgLightYellow": color.BgLightYellow,
"bgLightMagenta": color.BgLightMagenta,

"bold": colorTemplate(color.Bold),
"italic": colorTemplate(color.OpItalic),
"crossout": colorTemplate(color.OpStrikethrough),
"underline": colorTemplate(color.OpUnderscore),
"overline": overline,
"bold": color.Bold,
"italic": color.OpItalic,
"crossout": color.OpStrikethrough,
"underline": color.OpUnderscore,
}
163 changes: 70 additions & 93 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package internal
import (
"log"
"strings"
"unicode"
)

// Parse parses the passed format string and applies styles, returning a styled string.
Expand All @@ -12,6 +11,8 @@ func Parse(format string) string {
var resParts = make([]string, 0, strings.Count(format, "::"))
var lastToken string

var inText bool

var inFormat bool
var inStartFormat bool

Expand All @@ -22,47 +23,72 @@ func Parse(format string) string {
var lastIsFormatGroup bool
var needCheckFormatGroupStyle bool

inText = true

for index, s := range format {
// find {{
if s == '{' && index+1 < len(format) && format[index+1] == '{' {
if len(tempToken) != 0 {
resParts = append(resParts, string(tempToken))
tempToken = nil
}
inFormatGroup = true
inStartFormatGroup = true
continue
}
if inText {
// find {{
if s == '{' && index+1 < len(format) && format[index+1] == '{' {
if len(tempToken) != 0 {
resParts = append(resParts, string(tempToken))
tempToken = nil
}
inFormatGroup = true
inFormat = false
inText = false

// skip {
if inStartFormatGroup && s == '{' {
inStartFormat = false
continue
}
inStartFormatGroup = true
continue
}

// skip }
if inEndFormatGroup && s == '}' {
inEndFormatGroup = false
continue
}
// skip }
if inEndFormatGroup && s == '}' {
continue
}

// if after {{}} no :: style
if needCheckFormatGroupStyle {
needCheckFormatGroupStyle = false
// if after {{}} no :: style
if needCheckFormatGroupStyle {
needCheckFormatGroupStyle = false

if !(s == ':' && index+1 < len(format) && format[index+1] == ':' && index+2 < len(format) && format[index+2] != ' ') {
lastIsFormatGroup = false
if !(s == ':' && index+1 < len(format) && format[index+1] == ':' && index+2 < len(format) && format[index+2] != ' ') {
lastIsFormatGroup = false

if lastToken != "" {
resParts = append(resParts, "{{"+lastToken+"}}")
if lastToken != "" {
resParts = append(resParts, "{{"+lastToken+"}}")
}
}
}

// ::
if inEndFormatGroup && s == ':' && index+1 < len(format) && format[index+1] == ':' && index+2 < len(format) && format[index+2] != ' ' {
// lastToken = string(tempToken)
// tempToken = nil

inFormatGroup = false
inFormat = true
inText = false

inStartFormat = true
continue
}

tempToken = append(tempToken, s)
continue
}

if inFormatGroup {
// skip {
if inStartFormatGroup && s == '{' {
inStartFormatGroup = false
continue
}

if s == '}' && index+1 < len(format) && format[index+1] == '}' {

inFormatGroup = false
inStartFormatGroup = false
inFormat = false
inText = true

inEndFormatGroup = true
lastIsFormatGroup = true
needCheckFormatGroupStyle = true
Expand All @@ -77,52 +103,39 @@ func Parse(format string) string {
continue
}

// ::
if s == ':' && index+1 < len(format) && format[index+1] == ':' && index+2 < len(format) && format[index+2] != ' ' {
if !lastIsFormatGroup {
lastToken = string(tempToken)
tempToken = nil
if inFormat {
// skip :
if inStartFormat && s == ':' {
inStartFormat = false
continue
}

inFormat = true
inStartFormat = true
continue
}

// skip :
if inStartFormat && s == ':' {
inStartFormat = false
continue
}

if inFormat {
if !(s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z') && s != '|' && !(s >= '0' && s <= '9') && s != '#' {
if lastIsFormatGroup {
lastIsFormatGroup = false
lastToken = groupStyle(format, lastToken, string(tempToken))
} else {
lastToken = singleStyle(format, lastToken, string(tempToken))
}
lastToken = groupStyle(format, lastToken, string(tempToken))

resParts = append(resParts, lastToken)

tempToken = make([]rune, 0, 20)
tempToken = append(tempToken, s)

inFormatGroup = false
inFormat = false
inText = true

inEndFormatGroup = false
lastIsFormatGroup = false
continue
}
}

tempToken = append(tempToken, s)
tempToken = append(tempToken, s)
continue
}
}

if len(lastToken) != 0 {
if lastIsFormatGroup {
lastToken = groupStyle(format, lastToken, string(tempToken))
resParts = append(resParts, lastToken)
} else if inFormat {
lastToken = singleStyle(format, lastToken, string(tempToken))
resParts = append(resParts, lastToken)
} else {
resParts = append(resParts, string(tempToken))
}
Expand All @@ -131,22 +144,6 @@ func Parse(format string) string {
return strings.Join(resParts, "")
}

func singleStyle(format string, token string, style string) string {
styler, err := styleBuilder(style)
if err != nil {
log.Fatalf("Error parse style string in '%s' format string: %v", format, err)
}
rem, last, oneWord := lastWord(token)

if oneWord {
rem = styler(rem)
} else {
last = styler(last)
}

return rem + last
}

func groupStyle(format string, token string, style string) string {
styler, err := styleBuilder(style)
if err != nil {
Expand All @@ -155,23 +152,3 @@ func groupStyle(format string, token string, style string) string {

return styler(token)
}

func lastWord(text string) (remainingText string, word string, oneWord bool) {
runes := []rune(text)

for i := len(runes) - 1; i >= 0; i-- {
if isWordSeparator(runes[i]) {
startIndex := i + 1
word := string(runes[startIndex:])
remainingText := string(runes[0:startIndex])

return remainingText, word, false
}
}

return text, "", true
}

func isWordSeparator(r rune) bool {
return !unicode.IsDigit(r) && !unicode.IsLetter(r) && r != '_' && r != '-'
}
Loading

0 comments on commit 798493d

Please sign in to comment.