Code cleaning, update Readme

This commit is contained in:
probandula
2016-10-26 18:32:11 +02:00
parent 3f7f13785f
commit eef61fca27
9 changed files with 1165 additions and 66 deletions

View File

@@ -1,10 +1,7 @@
package figlet4go
import (
"errors"
"fmt"
"github.com/fatih/color"
"strings"
)
// RenderOptions are used to set color or maybe future
@@ -43,67 +40,65 @@ func (ar *AsciiRender) LoadFont(fontPath string) error {
}
// Render a string with the default options
// Calls the RenderOpts method with a new RenderOptions object
func (ar *AsciiRender) Render(str string) (string, error) {
return ar.render(str, NewRenderOptions())
return ar.RenderOpts(str, NewRenderOptions())
}
// Render a string with special RenderOptions
func (ar *AsciiRender) RenderOpts(str string, opts *RenderOptions) (string, error) {
return ar.render(str, opts)
}
// Can be called from the user (if options wished) or the above Render method
// Contains the whole rendering logic
func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error) {
colored := len(opt.FontColor) > 0
func (this *AsciiRender) convertChar(font *font, char rune) ([]string, error) {
if char < 0 || char > 127 {
return nil, errors.New("Not Ascii")
// Load the font
font, err := ar.fontMgr.getFont(opt.FontName)
if err != nil {
return "", err
}
height := font.height
begintRow := (int(char) - 32) * height
// Slice holding the chars
chars := []*AsciiChar{}
word := make([]string, height, height)
// Index of the current color
curColorIndex := 0
for i := 0; i < height; i++ {
row := font.fontSlice[begintRow+i]
row = strings.Replace(row, "@", "", -1)
row = strings.Replace(row, font.hardblank, " ", -1)
word[i] = row
}
return word, nil
}
func (this *AsciiRender) render(asciiStr string, opt *RenderOptions) (string, error) {
font, _ := this.fontMgr.getFont(opt.FontName)
wordlist := make([][]string, 0)
for _, char := range asciiStr {
word, err := this.convertChar(font, char)
// Foreach char create the ascii char
for _, char := range str {
// AsciiChar
asciiChar, err := NewAsciiChar(font, char)
if err != nil {
return "", err
}
wordlist = append(wordlist, word)
// Set color if given
if colored {
// Start colors from beginning if length is reached
if curColorIndex == len(opt.FontColor) {
curColorIndex = 0
}
// Assign color and increment the index
asciiChar.Color = opt.FontColor[curColorIndex]
curColorIndex++
}
// Append the char to the chars slice
chars = append(chars, asciiChar)
}
// Result which will be returned
result := ""
wordColorFunc := make([]func(a ...interface{}) string, len(wordlist))
for i, _ := range wordColorFunc {
if i < len(opt.FontColor) {
wordColorFunc[i] = color.New(opt.FontColor[i]).SprintFunc()
} else {
wordColorFunc[i] = fmt.Sprint
}
}
for i := 0; i < font.height; i++ {
for j := 0; j < len(wordlist); j++ {
result += wordColorFunc[j]((wordlist[j][i]))
// 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)
}
// A new line at the end
result += "\n"
}
return result, nil
}