Skip to content

Commit

Permalink
move to v1.1.0 (#4)
Browse files Browse the repository at this point in the history
The number of dependencies has been reduced to one.
Added `concealed` style.
Removed `overline` and `faint` styles.
Added `Fatalln` function.
  • Loading branch information
i582 authored Apr 11, 2021
1 parent b5e8f07 commit 05668e4
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 116 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ cfmt.Println("{{%s}}::flag ", flag)

### HEX colors

If the standard colors are not enough for you, then you can use the colors in the Hex format (note, not all consoles support all colors fully!).
If the standard colors are not enough for you, then you can use the colors in the `HEX` format (note, not all terminals support all colors fully!).

```go
cfmt.Println("This is a {{red color}}::#ff0000")
Expand All @@ -123,7 +123,7 @@ To set the background color, you need to add the prefix `bg` to the color, in th
cfmt.Println("This is a {{red color}}::bgRed")
```

For HEX it will look like this:
For `HEX` it will look like this:

```go
cfmt.Println("This is a {{red color}}::bg#ff0000")
Expand Down Expand Up @@ -178,16 +178,15 @@ cfmt.Print(`

## Supported colors and styles

| Styles |
| ------------------------------------------------------ |
| *italic* |
| **bold** |
| ~~crossout~~ |
| <ins>underline</ins> |
| <span style="text-decoration:overline">overline</span> |
|faint|
|reverse|
|blink|
| Styles |
| -------------------- |
| *italic* |
| **bold** |
| ~~crossout~~ |
| <ins>underline</ins> |
| concealed |
| reverse |
| blink |


| Colors | | | |
Expand All @@ -206,7 +205,7 @@ And colors in HEX format. See [HEX colors](#hex-colors) part.

## Motivation

The existing libraries for styling output are very powerful, and this library is built on two of them (gookit/color and muesli/termenv). However, they are not very convenient to use for styling specific words or sentences, since you need to use `Sprintf` and put the stylized into a format string, which greatly reduces readability if you need to style many elements.
The existing libraries for styling the output are very powerful and this library builds on one of them ([gookit / color] (https://github.com/gookit/color)). However, they are not very useful for styling certain words or sentences, since you need to use `Sprintf` and put the styled ones in a format string, which greatly reduces readability if you need to style many elements.

I believe that the library will be useful primarily for formatting ready-made text, for reference or examples. However, in other cases it should be just as convenient.

Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ module github.com/i582/cfmt
go 1.15

require (
github.com/google/go-cmp v0.5.3
github.com/gookit/color v1.3.2
github.com/muesli/termenv v0.7.4
)
11 changes: 0 additions & 11 deletions internal/bench/bench_test.go

This file was deleted.

10 changes: 4 additions & 6 deletions internal/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import (
)

// CustomMap is the custom styles map
var CustomMap = map[string]func(string) string{
"overline": overline,
"faint": faint,
"reverse": reverse,
"blink": blink,
}
var CustomMap = map[string]func(string) string{}

// Map is the styles map
var Map = map[string]color.Color{
Expand Down Expand Up @@ -54,4 +49,7 @@ var Map = map[string]color.Color{
"italic": color.OpItalic,
"crossout": color.OpStrikethrough,
"underline": color.OpUnderscore,
"blink": color.OpBlink,
"reverse": color.OpReverse,
"concealed": color.OpConcealed,
}
16 changes: 8 additions & 8 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
)

// Parse parses the passed format string and applies styles, returning a styled string.
func Parse(text string) string {
// ParseAndApply parses the passed format string and applies styles, returning a styled string.
func ParseAndApply(text string) string {
var resParts = make([]string, 0, 50)

var err error
Expand Down Expand Up @@ -149,9 +149,9 @@ func Parse(text string) string {

// text from :: or previous | to current |
singleFormat := text[tempTokenStartIndex:tempTokenEndIndex]
lastToken, err = ApplyStyle(lastToken, singleFormat)
lastToken, err = applyStyle(lastToken, singleFormat)
if err != nil {
log.Fatalf("Error parse style string in '%s' text string: %v", text, err)
log.Fatalf("Error parse style string in '%s' text string: %v", strings.ReplaceAll(text, "\n", "\\n"), err)
}

// index after |
Expand All @@ -164,9 +164,9 @@ func Parse(text string) string {

// last format
singleFormat := text[tempTokenStartIndex:tempTokenEndIndex]
lastToken, err = ApplyStyle(lastToken, singleFormat)
lastToken, err = applyStyle(lastToken, singleFormat)
if err != nil {
log.Fatalf("Error parse style string in '%s' text string: %v", text, err)
log.Fatalf("Error parse style string in '%s' text string: %v", strings.ReplaceAll(text, "\n", "\\n"), err)
}

tempTokenStartIndex = index
Expand All @@ -192,9 +192,9 @@ func Parse(text string) string {

if lastIsFormatGroup {
singleFormat := text[tempTokenStartIndex:]
lastToken, err = ApplyStyle(lastToken, singleFormat)
lastToken, err = applyStyle(lastToken, singleFormat)
if err != nil {
log.Fatalf("Error parse style string in '%s' text string: %v", text, err)
log.Fatalf("Error parse style string in '%s' text string: %v", strings.ReplaceAll(text, "\n", "\\n"), err)
}
resParts = append(resParts, lastToken)
} else {
Expand Down
36 changes: 24 additions & 12 deletions internal/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package internal
import (
"fmt"
"strings"

"github.com/gookit/color"
)

// StyleBuilder is a function that returns a styled string based on the supplied style.
Expand All @@ -13,7 +15,7 @@ func StyleBuilder(styles []string, text string) (string, error) {

var err error
for _, style := range styles {
text, err = ApplyStyle(text, style)
text, err = applyStyle(text, style)
if err != nil {
return "", err
}
Expand All @@ -22,14 +24,14 @@ func StyleBuilder(styles []string, text string) (string, error) {
return text, nil
}

// StyleBuilder is a function that apply a style for string.
func ApplyStyle(text, style string) (string, error) {
// applyStyle is a function that apply a style for string.
func applyStyle(text, style string) (string, error) {
if isHex(style) {
err := checkHex(style)
if err != nil {
return "", err
}
text = hexForegroundColorFunc(style, text)
text = hexForegroundColor(style, text)
return text, nil
}

Expand All @@ -39,15 +41,15 @@ func ApplyStyle(text, style string) (string, error) {
if err != nil {
return "", err
}
text = hexBackgroundColorFunc(rawHex, text)
text = hexBackgroundColor(rawHex, text)
return text, nil
}

clr, ok := Map[style]
if !ok {
customStyleFunc, ok := CustomMap[style]
if !ok {
return "", fmt.Errorf("unknown style %s", style)
return "", fmt.Errorf("unknown style '%s'", style)
}
text = customStyleFunc(text)
return text, nil
Expand All @@ -56,17 +58,27 @@ func ApplyStyle(text, style string) (string, error) {
return clr.Sprint(text), nil
}

func hexBackgroundColor(cl string, text string) string {
return color.HEX(cl, true).Sprint(text)
}

func hexForegroundColor(cl string, text string) string {
return color.HEX(cl).Sprint(text)
}

func isHex(val string) bool {
return strings.HasPrefix(val, "#")
}

func isBackgroundHex(val string) bool {
return strings.HasPrefix(val, "bg#")
}

func checkHex(val string) error {
if len(val) != 7 {
return fmt.Errorf("invalid hex: length of hex color must be 6")
rgb := color.HexToRGB(val)
if len(rgb) > 0 {
return nil
}
return nil
}

func isBackgroundHex(val string) bool {
return strings.HasPrefix(val, "bg#")
return fmt.Errorf("invalid '%s' hex color", val)
}
47 changes: 0 additions & 47 deletions internal/styles.go

This file was deleted.

16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func RegisterStyle(name string, fn func(string) string) {
// Sprint is the same as fmt.
func Sprint(a ...interface{}) string {
text := fmt.Sprint(a...)
parsed := internal.Parse(text)
return parsed
return internal.ParseAndApply(text)
}

// Fprint is the same as fmt.
Expand Down Expand Up @@ -59,8 +58,7 @@ func Println(a ...interface{}) (n int, err error) {
// Sprintf is the same as fmt.
func Sprintf(format string, a ...interface{}) string {
text := fmt.Sprintf(format, a...)
parsed := internal.Parse(text)
return parsed
return internal.ParseAndApply(text)
}

// Fprintf is the same as fmt.
Expand All @@ -76,12 +74,18 @@ func Printf(format string, a ...interface{}) (n int, err error) {

// Fatalf is the same as fmt.
func Fatalf(format string, a ...interface{}) {
Printf(format, a...)
_, _ = Printf(format, a...)
os.Exit(1)
}

// Fatal is the same as fmt.
func Fatal(a ...interface{}) {
Print(a...)
_, _ = Print(a...)
os.Exit(1)
}

// Fatalln is the same as fmt.
func Fatalln(a ...interface{}) {
_, _ = Println(a...)
os.Exit(1)
}
5 changes: 3 additions & 2 deletions tests/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ func TestParse(t *testing.T) {
cfmt.Println("{{こんにちは, correct group}}::code sdas")
cfmt.Println("{{привет, correct group}}::red|underline and {{other}}::red")
cfmt.Print("{{error group}} \n")
cfmt.Print("{{overline group}}::overline\n")
cfmt.Print("{{underline group}}::underline\n")
cfmt.Print("{{reverse group, こんにちは}}::reverse\n")
cfmt.Print(cfmt.Sprintln("{{faint group}}::faint sfafs"))
cfmt.Print(cfmt.Sprintln("{{some group}}::red sfafs"))
cfmt.Println(cfmt.Sprint("{{blink group}}::blink"))
cfmt.Printf("{{hex %s}}::#ff00ff sfas\n", "color group")
cfmt.Printf("{{3 hex %s}}::#ff0 sfas\n", "color group")
cfmt.Printf(cfmt.Sprintf("{{background color %s}}::bg#ffff00\n", "hex color"))
cfmt.Printf("{{{hello}}}::red|underline\n")
cfmt.Printf("{{some test struct: %v}}::red|underline\n", TestStruct{"hello", 1})
Expand Down
24 changes: 15 additions & 9 deletions tests/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package tests
import (
"testing"

"github.com/google/go-cmp/cmp"

"github.com/i582/cfmt"
"github.com/i582/cfmt/internal"
)
Expand Down Expand Up @@ -41,20 +39,28 @@ func TestStyleBuilder(t *testing.T) {
Error: "",
},
{
Value: []string{"overline", "reverse", "faint"},
Value: []string{"underline", "reverse"},
Error: "",
},
{
Value: []string{"bg#ff0", "bold"},
Error: "invalid hex: length of hex color must be 6",
Error: "",
},
{
Value: []string{"#ff0", "bold"},
Error: "invalid hex: length of hex color must be 6",
Error: "",
},
{
Value: []string{"#ff01", "bold"},
Error: "invalid '#ff01' hex color",
},
{
Value: []string{"bg#ff01", "bold"},
Error: "invalid '#ff01' hex color",
},
{
Value: []string{"some", "bold"},
Error: "unknown style some",
Error: "unknown style 'some'",
},
{
Value: []string{},
Expand All @@ -65,12 +71,12 @@ func TestStyleBuilder(t *testing.T) {
for _, suite := range suites {
_, err := internal.StyleBuilder(suite.Value, "")
if err == nil && suite.Error != "" {
t.Error(cmp.Diff("", suite.Error))
t.Errorf("mismatch\nwant: ''\nhave: %s", suite.Error)
continue
}

if err != nil && !cmp.Equal(err.Error(), suite.Error) {
t.Error(cmp.Diff(err.Error(), suite.Error))
if err != nil && err.Error() != suite.Error {
t.Errorf("mismatch\nwant: %s\nhave: %s", suite.Error, err.Error())
}
}
}

0 comments on commit 05668e4

Please sign in to comment.