mirror of
https://github.com/KevinMidboe/motdGO.git
synced 2025-10-29 17:50:24 +00:00
various small changes
This commit is contained in:
23
char.go
23
char.go
@@ -3,11 +3,10 @@ package figlet4go
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Represents a single ascii character
|
// Represents a single ascii character
|
||||||
type AsciiChar struct {
|
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
|
||||||
@@ -15,30 +14,20 @@ type AsciiChar struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new ascii character
|
// Creates a new ascii character
|
||||||
func NewAsciiChar(font *font, char rune) (*AsciiChar, error) {
|
func NewAsciiChar(font *font, char rune) (*asciiChar, error) {
|
||||||
// If not ascii, throw an error
|
// If not ascii, throw an error
|
||||||
if char < 0 || char > 127 {
|
if char < 0 || char > 127 {
|
||||||
return nil, errors.New("Not Ascii")
|
return nil, errors.New("Not Ascii")
|
||||||
}
|
}
|
||||||
|
|
||||||
height := font.height
|
// Get the font's representation of the char
|
||||||
beginRow := (int(char) - 32) * height
|
lines := font.getCharSlice(char)
|
||||||
|
|
||||||
lines := make([]string, height)
|
return &asciiChar{Lines: lines}, nil
|
||||||
|
|
||||||
// Get the char lines of the char
|
|
||||||
for i := 0; i < height; i++ {
|
|
||||||
row := font.fontSlice[beginRow+i]
|
|
||||||
row = strings.Replace(row, "@", "", -1)
|
|
||||||
row = strings.Replace(row, font.hardblank, " ", -1)
|
|
||||||
lines[i] = row
|
|
||||||
}
|
|
||||||
|
|
||||||
return &AsciiChar{Lines: lines}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
if char.Color != 0 {
|
||||||
colorFunc := color.New(char.Color).SprintFunc()
|
colorFunc := color.New(char.Color).SprintFunc()
|
||||||
return colorFunc(char.Lines[index])
|
return colorFunc(char.Lines[index])
|
||||||
|
|||||||
23
font.go
23
font.go
@@ -18,6 +18,9 @@
|
|||||||
//
|
//
|
||||||
package figlet4go
|
package figlet4go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Represents a single font
|
// Represents a single font
|
||||||
type font struct {
|
type font struct {
|
||||||
@@ -29,7 +32,21 @@ type font struct {
|
|||||||
fontSlice []string
|
fontSlice []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *font) getCharSlice() []string {
|
// Get a slice of strings containing the chars lines
|
||||||
// TODO here will be the logic of NewAsciiChar
|
func (f *font) getCharSlice(char rune) []string {
|
||||||
return []string{}
|
|
||||||
|
height := f.height
|
||||||
|
beginRow := (int(char) - 32) * height
|
||||||
|
|
||||||
|
lines := make([]string, height)
|
||||||
|
|
||||||
|
// Get the char lines of the char
|
||||||
|
for i := 0; i < height; i++ {
|
||||||
|
row := f.fontSlice[beginRow+i]
|
||||||
|
row = strings.Replace(row, "@", "", -1)
|
||||||
|
row = strings.Replace(row, f.hardblank, " ", -1)
|
||||||
|
lines[i] = row
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ type fontManager struct {
|
|||||||
fontList map[string]string
|
fontList map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new fontmanagerM
|
// Create a new fontmanager
|
||||||
func newFontManager() *fontManager {
|
func newFontManager() *fontManager {
|
||||||
fm := &fontManager{}
|
fm := &fontManager{}
|
||||||
fm.fontLib = make(map[string]*font)
|
fm.fontLib = make(map[string]*font)
|
||||||
@@ -56,7 +56,6 @@ func (fm *fontManager) getFont(fontName string) *font {
|
|||||||
return fm.fontLib[fontName]
|
return fm.fontLib[fontName]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Loads all .flf files recursively in the fontPath path
|
// Loads all .flf files recursively in the fontPath path
|
||||||
// Saves the found font files in a map with the name as the key
|
// Saves the found font files in a map with the name as the key
|
||||||
// and the path as the value. Doesn't load them at this point
|
// and the path as the value. Doesn't load them at this point
|
||||||
@@ -132,8 +131,6 @@ func (fm *fontManager) loadBuildInFont() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Parse a font from a content string
|
// Parse a font from a content string
|
||||||
// Used to load fonts from disk and the builtin fonts
|
// Used to load fonts from disk and the builtin fonts
|
||||||
func parseFontContent(cont string) (*font, error) {
|
func parseFontContent(cont string) (*font, error) {
|
||||||
@@ -162,4 +159,3 @@ func parseFontContent(cont string) (*font, error) {
|
|||||||
|
|
||||||
return font, nil
|
return font, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error
|
|||||||
font := ar.fontMgr.getFont(opt.FontName)
|
font := ar.fontMgr.getFont(opt.FontName)
|
||||||
|
|
||||||
// Slice holding the chars
|
// Slice holding the chars
|
||||||
chars := []*AsciiChar{}
|
chars := []*asciiChar{}
|
||||||
|
|
||||||
// Index of the current color
|
// Index of the current color
|
||||||
curColorIndex := 0
|
curColorIndex := 0
|
||||||
|
|||||||
Reference in New Issue
Block a user