mirror of
https://github.com/KevinMidboe/motdGO.git
synced 2025-10-29 17:50:24 +00:00
add parsers
This commit is contained in:
13
README.md
13
README.md
@@ -22,7 +22,7 @@ $ go get -u github.com/probandula/figlet4go/...
|
||||
You can use the `figlet4go` command in the command-line.
|
||||
For example (generates the banner on top):
|
||||
```bash
|
||||
$ figlet4go -str "figlet4go" -font "larry3d" -colors "green;yellow;cyan"
|
||||
$ figlet4go -str "figlet4go" -font "larry3d" -colors "green;FF9900;cyan"
|
||||
```
|
||||
For a usage instruction read the commands usage with `figlet4go -h`.
|
||||
|
||||
@@ -43,6 +43,7 @@ fmt.Print(renderStr)
|
||||
### Colored
|
||||
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.
|
||||
If you use a `AnsiColor` with an `TrueColor` only parser (f.e. `ParserHTML`), `TrueColor` objects are automatically generated.
|
||||
```go
|
||||
import "github.com/probandula/figlet4go"
|
||||
|
||||
@@ -58,7 +59,7 @@ options.FontColor = []figlet4go.Color{
|
||||
figlet4go.ColorYellow,
|
||||
figlet4go.ColorCyan,
|
||||
// ...or by an hex string...
|
||||
figlet4go.GetTrueColorFromHexString("885DBA"),
|
||||
figlet4go.NewTrueColorFromHexString("885DBA"),
|
||||
// ...or by an TrueColor object with rgb values
|
||||
figlet4go.TrueColor{136, 93, 186},
|
||||
}
|
||||
@@ -87,6 +88,14 @@ renderStr, _ := ascii.RenderOpts("Hello Fonts", options)
|
||||
fmt.Print(renderStr)
|
||||
```
|
||||
|
||||
## Parsers
|
||||
There a currently these Parsers available:
|
||||
|
||||
| Parser | What does it do? |
|
||||
| --------- | ------ |
|
||||
| ParserTerminal | Parses the result directly |
|
||||
| ParserHTML | Parses a pasteable `<code>` html block |
|
||||
|
||||
## Fonts
|
||||
|
||||
### Builtin
|
||||
|
||||
23
char.go
23
char.go
@@ -2,6 +2,7 @@ package figlet4go
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Represents a single ascii character
|
||||
@@ -26,14 +27,28 @@ func newAsciiChar(font *font, char rune) (*asciiChar, error) {
|
||||
}
|
||||
|
||||
// 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, p Parser) string {
|
||||
prefix := ""
|
||||
suffix := ""
|
||||
|
||||
line := handleReplaces(char.Lines[index], p)
|
||||
|
||||
if char.Color != nil {
|
||||
prefix = char.Color.getPrefix()
|
||||
suffix = char.Color.getSuffix()
|
||||
prefix = char.Color.getPrefix(p)
|
||||
suffix = char.Color.getSuffix(p)
|
||||
}
|
||||
|
||||
return prefix + char.Lines[index] + suffix
|
||||
return prefix + line + suffix
|
||||
}
|
||||
|
||||
// Replace all parser specific things
|
||||
func handleReplaces(str string, p Parser) string {
|
||||
if p.Replaces == nil {
|
||||
return str
|
||||
}
|
||||
// Replace for each entry
|
||||
for old, new := range p.Replaces {
|
||||
str = strings.Replace(str, old, new, -1)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ var (
|
||||
font *string = flag.String("font", "", "Font name to use")
|
||||
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, or any hexcode (f.e. '885DBA')")
|
||||
parser *string = flag.String("parser", "terminal", "Parser to use\tPossible parsers: terminal (default), html")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -35,6 +36,9 @@ func main() {
|
||||
// Set the font
|
||||
options.FontName = *font
|
||||
|
||||
// Set the parser
|
||||
options.Parser = getParser(*parser)
|
||||
|
||||
// Set colors
|
||||
if *colors != "" {
|
||||
options.FontColor = getColorSlice(*colors)
|
||||
@@ -49,6 +53,17 @@ func main() {
|
||||
fmt.Print(renderStr)
|
||||
}
|
||||
|
||||
// Get the parser for given flag argument
|
||||
func getParser(parserStr string) figlet4go.Parser {
|
||||
switch parserStr {
|
||||
case "html":
|
||||
return figlet4go.ParserHTML
|
||||
// Terminal parser is default
|
||||
default:
|
||||
return figlet4go.ParserTerminal
|
||||
}
|
||||
}
|
||||
|
||||
// Get a slice with colors to give to the RenderOptions
|
||||
// Splits the given string with the separator ";"
|
||||
func getColorSlice(colorStr string) []figlet4go.Color {
|
||||
@@ -77,7 +92,7 @@ func getColorSlice(colorStr string) []figlet4go.Color {
|
||||
colors[i] = figlet4go.ColorWhite
|
||||
default:
|
||||
// Try to parse the TrueColor from the string
|
||||
color, err := figlet4go.GetTrueColorFromHexString(c)
|
||||
color, err := figlet4go.NewTrueColorFromHexString(c)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
84
color.go
84
color.go
@@ -1,14 +1,15 @@
|
||||
package figlet4go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Escape char
|
||||
const escape string = "\x1b"
|
||||
|
||||
// Terminal AnsiColors
|
||||
var (
|
||||
ColorBlack AnsiColor = AnsiColor{30}
|
||||
ColorRed AnsiColor = AnsiColor{31}
|
||||
@@ -20,18 +21,32 @@ var (
|
||||
ColorWhite AnsiColor = AnsiColor{37}
|
||||
)
|
||||
|
||||
// Every color has a pre- and a suffix
|
||||
type Color interface {
|
||||
getPrefix() string
|
||||
getSuffix() string
|
||||
// TrueColor lookalikes for displaying AnsiColor f.e. with the HTML parser
|
||||
// Colors based on http://clrs.cc/
|
||||
// "TrueColorForAnsiColor"
|
||||
var tcfac map[AnsiColor]TrueColor = map[AnsiColor]TrueColor{
|
||||
ColorBlack: TrueColor{0, 0, 0},
|
||||
ColorRed: TrueColor{255, 65, 54},
|
||||
ColorGreen: TrueColor{149, 189, 64},
|
||||
ColorYellow: TrueColor{255, 220, 0},
|
||||
ColorBlue: TrueColor{0, 116, 217},
|
||||
ColorMagenta: TrueColor{177, 13, 201},
|
||||
ColorCyan: TrueColor{105, 206, 245},
|
||||
ColorWhite: TrueColor{255, 255, 255},
|
||||
}
|
||||
|
||||
// Ansi color
|
||||
// Every color has a pre- and a suffix
|
||||
type Color interface {
|
||||
getPrefix(p Parser) string
|
||||
getSuffix(p Parser) string
|
||||
}
|
||||
|
||||
// AnsiColor representation
|
||||
type AnsiColor struct {
|
||||
code int
|
||||
}
|
||||
|
||||
// Truecolor with rgb Attributes
|
||||
// TrueColor with rgb Attributes
|
||||
type TrueColor struct {
|
||||
r int
|
||||
g int
|
||||
@@ -39,16 +54,35 @@ type TrueColor struct {
|
||||
}
|
||||
|
||||
// Prefix for ansi color
|
||||
func (tc TrueColor) getPrefix() string {
|
||||
func (tc TrueColor) getPrefix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)
|
||||
|
||||
case "html":
|
||||
return fmt.Sprintf("<span style='color: rgb(%d,%d,%d);'>", tc.r, tc.g, tc.b)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Suffix for ansi color
|
||||
func (tc TrueColor) getSuffix() string {
|
||||
func (tc TrueColor) getSuffix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[0m", escape)
|
||||
|
||||
case "html":
|
||||
return "</span>"
|
||||
}
|
||||
|
||||
func GetTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||
return ""
|
||||
}
|
||||
|
||||
// NewTrueColorFromHexString returns a Truecolor object based on a hexadezimal string
|
||||
func NewTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||
rgb, err := hex.DecodeString(c)
|
||||
if err != nil {
|
||||
return nil, errors.New("Invalid color given (" + c + ")")
|
||||
@@ -61,13 +95,35 @@ func GetTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
// Prefix for ansi color
|
||||
func (ac AnsiColor) getPrefix() string {
|
||||
func (ac AnsiColor) getPrefix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[0;%dm", escape, ac.code)
|
||||
|
||||
case "html":
|
||||
// Get the TrueColor for the AnsiColor
|
||||
tc := tcfac[ac]
|
||||
return tc.getPrefix(p)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
// Suffix for ansi color
|
||||
func (ac AnsiColor) getSuffix() string {
|
||||
func (ac AnsiColor) getSuffix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[0m", escape)
|
||||
|
||||
case "html":
|
||||
// Get the TrueColor for the AnsiColor
|
||||
tc := tcfac[ac]
|
||||
return tc.getSuffix(p)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
22
parser.go
Normal file
22
parser.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package figlet4go
|
||||
|
||||
// Parser stores some output specific stuff
|
||||
type Parser struct {
|
||||
// Name used for switching in colors.go
|
||||
Name string
|
||||
// Parser prefix
|
||||
Prefix string
|
||||
// Parser suffix
|
||||
Suffix string
|
||||
// Newline representation
|
||||
NewLine string
|
||||
// Things to be replaced (f.e. \n to <br>)
|
||||
Replaces map[string]string
|
||||
}
|
||||
|
||||
var (
|
||||
// Default terminal parser
|
||||
ParserTerminal Parser = Parser{"terminal", "", "", "\n", nil}
|
||||
// Parser for HTML code
|
||||
ParserHTML Parser = Parser{"html", "<code>", "</code>", "<br>", map[string]string{" ": " "}}
|
||||
)
|
||||
11
render.go
11
render.go
@@ -7,6 +7,8 @@ type RenderOptions struct {
|
||||
FontName string
|
||||
// Colors of the font
|
||||
FontColor []Color
|
||||
// Parser
|
||||
Parser Parser
|
||||
}
|
||||
|
||||
// NewRenderOptions creates new RenderOptions
|
||||
@@ -14,6 +16,7 @@ type RenderOptions struct {
|
||||
func NewRenderOptions() *RenderOptions {
|
||||
return &RenderOptions{
|
||||
FontName: defaultFont,
|
||||
Parser: ParserTerminal,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,15 +86,19 @@ func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error
|
||||
// Result which will be returned
|
||||
result := ""
|
||||
|
||||
result += opt.Parser.Prefix
|
||||
|
||||
// Foreach line of the font height
|
||||
for curLine := 0; curLine < font.height; curLine++ {
|
||||
// Add the current line of the char to the result
|
||||
for i := range chars {
|
||||
result += chars[i].GetLine(curLine)
|
||||
result += chars[i].GetLine(curLine, opt.Parser)
|
||||
}
|
||||
// A new line at the end
|
||||
result += "\n"
|
||||
result += opt.Parser.NewLine
|
||||
}
|
||||
|
||||
result += opt.Parser.Suffix
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user