add truecolor support

This commit is contained in:
probandula
2016-11-02 16:46:47 +01:00
parent 9e6c45dd86
commit 76dcd98705
5 changed files with 123 additions and 35 deletions

View File

@@ -1,11 +1,14 @@
# FIGlet for Go # FIGlet for Go
`figlet4go` is a go library forked from [getwe/figlet4go](https://github.com/getwe/figlet4go) which is a port of [FIGlet](http://www.figlet.org/) to Golang. [![Go Report Card](https://goreportcard.com/badge/github.com/probandula/figlet4go)](https://goreportcard.com/report/github.com/probandula/figlet4go)
`figlet4go` is a go library forked from which is a port of [FIGlet](http://www.figlet.org/) to Golang.
With `figlet4go` it's easy to create **ascii text banners** in the command-line or with the given api. With `figlet4go` it's easy to create **ascii text banners** in the command-line or with the given api.
![screenshot](./screenshot/figlet4go.png) ![screenshot](./screenshot/figlet4go.png)
[![Go Report Card](https://goreportcard.com/badge/github.com/probandula/figlet4go)](https://goreportcard.com/report/github.com/probandula/figlet4go)
**This Repository used to be a fork of [getwe/figlet4go](https://github.com/getwe/figlet4go), but changed so much that it's not compatible anymore**
## Installation ## Installation
@@ -38,10 +41,10 @@ fmt.Print(renderStr)
``` ```
### Colored ### Colored
The colors given in the `[]color.Attribute` slice are repeating if the string is longer than the slice. You have to call the `RenderOpts` instead of the `Render` method to give the Renderer the Options. The colors given in the `[]figlet4go.Color` slice are repeating if the string is longer than the slice. You have to call the `RenderOpts` instead of the `Render` method to give the Renderer the Options.
If you use a `TrueColor` color, you have to ensure that your [terminal supports](https://gist.github.com/XVilka/8346728/) it.
```go ```go
import "github.com/probandula/figlet4go" import "github.com/probandula/figlet4go"
import "github.com/fatih/color"
// ... // ...
@@ -49,10 +52,15 @@ ascii := figlet4go.NewAsciiRender()
// Adding the colors to RenderOptions // Adding the colors to RenderOptions
options := figlet4go.NewRenderOptions() options := figlet4go.NewRenderOptions()
options.FontColor = []color.Attribute{ options.FontColor = []figlet4go.Color{
color.FgGreen, // Colors can be given by default ansi color codes...
color.FgYellow, figlet4go.ColorGreen,
color.FgCyan, figlet4go.ColorYellow,
figlet4go.ColorCyan,
// ...or by an hex string...
figlet4go.GetTrueColorFromHexString("885DBA"),
// ...or by an TrueColor object with rgb values
figlet4go.TrueColor{136, 93, 186},
} }
renderStr, _ := ascii.RenderOpts("Hello Colors", options) renderStr, _ := ascii.RenderOpts("Hello Colors", options)
@@ -67,7 +75,6 @@ import "github.com/probandula/figlet4go"
// ... // ...
ascii := figlet4go.NewAsciiRender() ascii := figlet4go.NewAsciiRender()
options := figlet4go.NewRenderOptions() options := figlet4go.NewRenderOptions()
@@ -76,7 +83,7 @@ options.FontName = "larry3d"
// If 'larry3d' wouldn't be included you would have to load your .flf files like that: // If 'larry3d' wouldn't be included you would have to load your .flf files like that:
ascii.LoadFont("/path/to/fonts/") ascii.LoadFont("/path/to/fonts/")
renderStr, _ := ascii.RenderOpts("Hello Colors", options) renderStr, _ := ascii.RenderOpts("Hello Fonts", options)
fmt.Print(renderStr) fmt.Print(renderStr)
``` ```
@@ -98,7 +105,10 @@ Other fonts can mainly be found on [figlet](http://www.figlet.org). You have to
## Todo ## Todo
- [x] Cli client - [x] Cli client
- [ ] automatic the perfect char margin - [ ] automatically the perfect char margin
- [ ] Linebreak possible? - [ ] Linebreak possible?
- [x] Colors in the cli client - [x] Colors in the cli client
- [ ] No dependencies (fatih/color) - [x] No dependencies (fatih/color)
- [x] Truecolor support
- [ ] More parsers (HTML)
- [ ] Tests

15
char.go
View File

@@ -2,7 +2,6 @@ package figlet4go
import ( import (
"errors" "errors"
"github.com/fatih/color"
) )
// Represents a single ascii character // Represents a single ascii character
@@ -10,7 +9,7 @@ type asciiChar struct {
// Slice with the lines of the Char // Slice with the lines of the Char
Lines []string Lines []string
// Color of the char // Color of the char
Color color.Attribute Color Color
} }
// Creates a new ascii character // Creates a new ascii character
@@ -28,9 +27,13 @@ func newAsciiChar(font *font, char rune) (*asciiChar, error) {
// Return a line of the char as string with color if set // Return a line of the char as string with color if set
func (char *asciiChar) GetLine(index int) string { func (char *asciiChar) GetLine(index int) string {
if char.Color != 0 { prefix := ""
colorFunc := color.New(char.Color).SprintFunc() suffix := ""
return colorFunc(char.Lines[index])
if char.Color != nil {
prefix = char.Color.getPrefix()
suffix = char.Color.getSuffix()
} }
return char.Lines[index]
return prefix + char.Lines[index] + suffix
} }

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/fatih/color"
"github.com/probandula/figlet4go" "github.com/probandula/figlet4go"
"log" "log"
"os" "os"
@@ -14,7 +13,7 @@ var (
str *string = flag.String("str", "", "String to be converted with FIGlet") str *string = flag.String("str", "", "String to be converted with FIGlet")
font *string = flag.String("font", "", "Font name to use") font *string = flag.String("font", "", "Font name to use")
fontpath *string = flag.String("fontpath", "", "Font path to load fonts from") fontpath *string = flag.String("fontpath", "", "Font path to load fonts from")
colors *string = flag.String("colors", "", "Character colors separated by ';'\n\tPossible colors: black, red, green, yellow, blue, magenta, cyan, white") colors *string = flag.String("colors", "", "Character colors separated by ';'\n\tPossible colors: black, red, green, yellow, blue, magenta, cyan, white, or any hexcode (f.e. '885DBA')")
) )
func main() { func main() {
@@ -52,30 +51,37 @@ func main() {
// Get a slice with colors to give to the RenderOptions // Get a slice with colors to give to the RenderOptions
// Splits the given string with the separator ";" // Splits the given string with the separator ";"
func getColorSlice(colorStr string) []color.Attribute { func getColorSlice(colorStr string) []figlet4go.Color {
givenColors := strings.Split(colorStr, ";") givenColors := strings.Split(colorStr, ";")
colors := make([]color.Attribute, len(givenColors)) colors := make([]figlet4go.Color, len(givenColors))
for i, c := range givenColors { for i, c := range givenColors {
switch c { switch c {
case "black": case "black":
colors[i] = color.FgBlack colors[i] = figlet4go.ColorBlack
case "red": case "red":
colors[i] = color.FgRed colors[i] = figlet4go.ColorRed
case "green": case "green":
colors[i] = color.FgGreen colors[i] = figlet4go.ColorGreen
case "yellow": case "yellow":
colors[i] = color.FgYellow colors[i] = figlet4go.ColorYellow
case "blue": case "blue":
colors[i] = color.FgBlue colors[i] = figlet4go.ColorBlue
case "magenta": case "magenta":
colors[i] = color.FgMagenta colors[i] = figlet4go.ColorMagenta
case "cyan": case "cyan":
colors[i] = color.FgCyan colors[i] = figlet4go.ColorCyan
case "white": case "white":
colors[i] = color.FgWhite colors[i] = figlet4go.ColorWhite
default:
// Try to parse the TrueColor from the string
color, err := figlet4go.GetTrueColorFromHexString(c)
if err != nil {
log.Fatal(err)
}
colors[i] = color
} }
} }

73
color.go Normal file
View File

@@ -0,0 +1,73 @@
package figlet4go
import (
"fmt"
"errors"
"encoding/hex"
)
// Escape char
const escape string = "\x1b"
var (
ColorBlack AnsiColor = AnsiColor{30}
ColorRed AnsiColor = AnsiColor{31}
ColorGreen AnsiColor = AnsiColor{32}
ColorYellow AnsiColor = AnsiColor{33}
ColorBlue AnsiColor = AnsiColor{34}
ColorMagenta AnsiColor = AnsiColor{35}
ColorCyan AnsiColor = AnsiColor{36}
ColorWhite AnsiColor = AnsiColor{37}
)
// Every color has a pre- and a suffix
type Color interface {
getPrefix() string
getSuffix() string
}
// Ansi color
type AnsiColor struct {
code int
}
// Truecolor with rgb Attributes
type TrueColor struct {
r int
g int
b int
}
// Prefix for ansi color
func (tc TrueColor) getPrefix() string {
return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)
}
// Suffix for ansi color
func (tc TrueColor) getSuffix() string {
return fmt.Sprintf("%v[0m", escape)
}
func GetTrueColorFromHexString(c string) (*TrueColor, error) {
rgb, err := hex.DecodeString(c)
if err != nil {
return nil, errors.New("Invalid color given (" + c + ")")
}
return &TrueColor{
int(rgb[0]),
int(rgb[1]),
int(rgb[2]),
}, nil
}
// Prefix for ansi color
func (ac AnsiColor) getPrefix() string {
return fmt.Sprintf("%v[0;%dm", escape, ac.code)
}
// Suffix for ansi color
func (ac AnsiColor) getSuffix() string {
return fmt.Sprintf("%v[0m", escape)
}

View File

@@ -1,16 +1,12 @@
package figlet4go package figlet4go
import (
"github.com/fatih/color"
)
// RenderOptions are used to set color or maybe future // RenderOptions are used to set color or maybe future
// options to the AsciiRenderer // options to the AsciiRenderer
type RenderOptions struct { type RenderOptions struct {
// Name of the used font // Name of the used font
FontName string FontName string
// Colors of the font // Colors of the font
FontColor []color.Attribute FontColor []Color
} }
// NewRenderOptions creates new RenderOptions // NewRenderOptions creates new RenderOptions