mirror of
https://github.com/KevinMidboe/motdGO.git
synced 2025-10-29 17:50:24 +00:00
add parsers
This commit is contained in:
15
README.md
15
README.md
@@ -22,7 +22,7 @@ $ go get -u github.com/probandula/figlet4go/...
|
|||||||
You can use the `figlet4go` command in the command-line.
|
You can use the `figlet4go` command in the command-line.
|
||||||
For example (generates the banner on top):
|
For example (generates the banner on top):
|
||||||
```bash
|
```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`.
|
For a usage instruction read the commands usage with `figlet4go -h`.
|
||||||
|
|
||||||
@@ -42,7 +42,8 @@ fmt.Print(renderStr)
|
|||||||
|
|
||||||
### Colored
|
### 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.
|
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 `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
|
```go
|
||||||
import "github.com/probandula/figlet4go"
|
import "github.com/probandula/figlet4go"
|
||||||
|
|
||||||
@@ -58,7 +59,7 @@ options.FontColor = []figlet4go.Color{
|
|||||||
figlet4go.ColorYellow,
|
figlet4go.ColorYellow,
|
||||||
figlet4go.ColorCyan,
|
figlet4go.ColorCyan,
|
||||||
// ...or by an hex string...
|
// ...or by an hex string...
|
||||||
figlet4go.GetTrueColorFromHexString("885DBA"),
|
figlet4go.NewTrueColorFromHexString("885DBA"),
|
||||||
// ...or by an TrueColor object with rgb values
|
// ...or by an TrueColor object with rgb values
|
||||||
figlet4go.TrueColor{136, 93, 186},
|
figlet4go.TrueColor{136, 93, 186},
|
||||||
}
|
}
|
||||||
@@ -87,6 +88,14 @@ renderStr, _ := ascii.RenderOpts("Hello Fonts", options)
|
|||||||
fmt.Print(renderStr)
|
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
|
## Fonts
|
||||||
|
|
||||||
### Builtin
|
### Builtin
|
||||||
|
|||||||
23
char.go
23
char.go
@@ -2,6 +2,7 @@ package figlet4go
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Represents a single ascii character
|
// 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
|
// 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 := ""
|
prefix := ""
|
||||||
suffix := ""
|
suffix := ""
|
||||||
|
|
||||||
|
line := handleReplaces(char.Lines[index], p)
|
||||||
|
|
||||||
if char.Color != nil {
|
if char.Color != nil {
|
||||||
prefix = char.Color.getPrefix()
|
prefix = char.Color.getPrefix(p)
|
||||||
suffix = char.Color.getSuffix()
|
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")
|
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, or any hexcode (f.e. '885DBA')")
|
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() {
|
func main() {
|
||||||
@@ -35,6 +36,9 @@ func main() {
|
|||||||
// Set the font
|
// Set the font
|
||||||
options.FontName = *font
|
options.FontName = *font
|
||||||
|
|
||||||
|
// Set the parser
|
||||||
|
options.Parser = getParser(*parser)
|
||||||
|
|
||||||
// Set colors
|
// Set colors
|
||||||
if *colors != "" {
|
if *colors != "" {
|
||||||
options.FontColor = getColorSlice(*colors)
|
options.FontColor = getColorSlice(*colors)
|
||||||
@@ -49,6 +53,17 @@ func main() {
|
|||||||
fmt.Print(renderStr)
|
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
|
// 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) []figlet4go.Color {
|
func getColorSlice(colorStr string) []figlet4go.Color {
|
||||||
@@ -77,7 +92,7 @@ func getColorSlice(colorStr string) []figlet4go.Color {
|
|||||||
colors[i] = figlet4go.ColorWhite
|
colors[i] = figlet4go.ColorWhite
|
||||||
default:
|
default:
|
||||||
// Try to parse the TrueColor from the string
|
// Try to parse the TrueColor from the string
|
||||||
color, err := figlet4go.GetTrueColorFromHexString(c)
|
color, err := figlet4go.NewTrueColorFromHexString(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
92
color.go
92
color.go
@@ -1,14 +1,15 @@
|
|||||||
package figlet4go
|
package figlet4go
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"errors"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Escape char
|
// Escape char
|
||||||
const escape string = "\x1b"
|
const escape string = "\x1b"
|
||||||
|
|
||||||
|
// Terminal AnsiColors
|
||||||
var (
|
var (
|
||||||
ColorBlack AnsiColor = AnsiColor{30}
|
ColorBlack AnsiColor = AnsiColor{30}
|
||||||
ColorRed AnsiColor = AnsiColor{31}
|
ColorRed AnsiColor = AnsiColor{31}
|
||||||
@@ -20,18 +21,32 @@ var (
|
|||||||
ColorWhite AnsiColor = AnsiColor{37}
|
ColorWhite AnsiColor = AnsiColor{37}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Every color has a pre- and a suffix
|
// TrueColor lookalikes for displaying AnsiColor f.e. with the HTML parser
|
||||||
type Color interface {
|
// Colors based on http://clrs.cc/
|
||||||
getPrefix() string
|
// "TrueColorForAnsiColor"
|
||||||
getSuffix() string
|
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 {
|
type AnsiColor struct {
|
||||||
code int
|
code int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truecolor with rgb Attributes
|
// TrueColor with rgb Attributes
|
||||||
type TrueColor struct {
|
type TrueColor struct {
|
||||||
r int
|
r int
|
||||||
g int
|
g int
|
||||||
@@ -39,16 +54,35 @@ type TrueColor struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prefix for ansi color
|
// Prefix for ansi color
|
||||||
func (tc TrueColor) getPrefix() string {
|
func (tc TrueColor) getPrefix(p Parser) string {
|
||||||
return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)
|
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
|
// Suffix for ansi color
|
||||||
func (tc TrueColor) getSuffix() string {
|
func (tc TrueColor) getSuffix(p Parser) string {
|
||||||
return fmt.Sprintf("%v[0m", escape)
|
switch p.Name {
|
||||||
|
|
||||||
|
case "terminal":
|
||||||
|
return fmt.Sprintf("%v[0m", escape)
|
||||||
|
|
||||||
|
case "html":
|
||||||
|
return "</span>"
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTrueColorFromHexString(c string) (*TrueColor, error) {
|
// NewTrueColorFromHexString returns a Truecolor object based on a hexadezimal string
|
||||||
|
func NewTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||||
rgb, err := hex.DecodeString(c)
|
rgb, err := hex.DecodeString(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("Invalid color given (" + c + ")")
|
return nil, errors.New("Invalid color given (" + c + ")")
|
||||||
@@ -61,13 +95,35 @@ func GetTrueColorFromHexString(c string) (*TrueColor, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Prefix for ansi color
|
// Prefix for ansi color
|
||||||
func (ac AnsiColor) getPrefix() string {
|
func (ac AnsiColor) getPrefix(p Parser) string {
|
||||||
return fmt.Sprintf("%v[0;%dm", escape, ac.code)
|
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
|
// Suffix for ansi color
|
||||||
func (ac AnsiColor) getSuffix() string {
|
func (ac AnsiColor) getSuffix(p Parser) string {
|
||||||
return fmt.Sprintf("%v[0m", escape)
|
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
|
FontName string
|
||||||
// Colors of the font
|
// Colors of the font
|
||||||
FontColor []Color
|
FontColor []Color
|
||||||
|
// Parser
|
||||||
|
Parser Parser
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRenderOptions creates new RenderOptions
|
// NewRenderOptions creates new RenderOptions
|
||||||
@@ -14,6 +16,7 @@ type RenderOptions struct {
|
|||||||
func NewRenderOptions() *RenderOptions {
|
func NewRenderOptions() *RenderOptions {
|
||||||
return &RenderOptions{
|
return &RenderOptions{
|
||||||
FontName: defaultFont,
|
FontName: defaultFont,
|
||||||
|
Parser: ParserTerminal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,15 +86,19 @@ func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error
|
|||||||
// Result which will be returned
|
// Result which will be returned
|
||||||
result := ""
|
result := ""
|
||||||
|
|
||||||
|
result += opt.Parser.Prefix
|
||||||
|
|
||||||
// Foreach line of the font height
|
// Foreach line of the font height
|
||||||
for curLine := 0; curLine < font.height; curLine++ {
|
for curLine := 0; curLine < font.height; curLine++ {
|
||||||
// Add the current line of the char to the result
|
// Add the current line of the char to the result
|
||||||
for i := range chars {
|
for i := range chars {
|
||||||
result += chars[i].GetLine(curLine)
|
result += chars[i].GetLine(curLine, opt.Parser)
|
||||||
}
|
}
|
||||||
// A new line at the end
|
// A new line at the end
|
||||||
result += "\n"
|
result += opt.Parser.NewLine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result += opt.Parser.Suffix
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user