mirror of
				https://github.com/KevinMidboe/motdGO.git
				synced 2025-10-29 17:50:24 +00:00 
			
		
		
		
	better parsers
This commit is contained in:
		
							
								
								
									
										20
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								README.md
									
									
									
									
									
								
							| @@ -88,6 +88,22 @@ renderStr, _ := ascii.RenderOpts("Hello Fonts", options) | |||||||
| fmt.Print(renderStr) | fmt.Print(renderStr) | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  | ### Other parser | ||||||
|  | A Parser can be set through the `GetParser` function with a valid key | ||||||
|  | ```go | ||||||
|  | import "github.com/probandula/figlet4go" | ||||||
|  |  | ||||||
|  | // ... | ||||||
|  |  | ||||||
|  | ascii := figlet4go.NewAsciiRender() | ||||||
|  |  | ||||||
|  | options := figlet4go.NewRenderOptions() | ||||||
|  | options.Parser = figlet4go.GetParser("html") | ||||||
|  |  | ||||||
|  | renderStr, _ := ascii.RenderOpts("Hello Fonts", options) | ||||||
|  | fmt.Print(renderStr) | ||||||
|  | ``` | ||||||
|  |  | ||||||
| ## Parsers | ## Parsers | ||||||
| There a currently these Parsers available: | There a currently these Parsers available: | ||||||
|  |  | ||||||
| @@ -119,5 +135,7 @@ Other fonts can mainly be found on [figlet](http://www.figlet.org). You have to | |||||||
| - [x] Colors in the cli client | - [x] Colors in the cli client | ||||||
| - [x] No dependencies (fatih/color) | - [x] No dependencies (fatih/color) | ||||||
| - [x] Truecolor support | - [x] Truecolor support | ||||||
| - [ ] More parsers (HTML) | - [x] More parsers (HTML) | ||||||
|  | - [x] Better parsers (maybe stored in a map) | ||||||
|  | - [ ] Pointer-Value standarization | ||||||
| - [ ] Tests | - [ ] Tests | ||||||
|   | |||||||
| @@ -37,7 +37,11 @@ func main() { | |||||||
| 	options.FontName = *font | 	options.FontName = *font | ||||||
|  |  | ||||||
| 	// Set the parser | 	// Set the parser | ||||||
| 	options.Parser = getParser(*parser) | 	p, err := figlet4go.GetParser(*parser) | ||||||
|  | 	if err != nil { | ||||||
|  | 		p, _ = figlet4go.GetParser("terminal") | ||||||
|  | 	} | ||||||
|  | 	options.Parser = *p | ||||||
|  |  | ||||||
| 	// Set colors | 	// Set colors | ||||||
| 	if *colors != "" { | 	if *colors != "" { | ||||||
| @@ -53,17 +57,6 @@ 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 { | ||||||
|   | |||||||
							
								
								
									
										20
									
								
								parser.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								parser.go
									
									
									
									
									
								
							| @@ -1,5 +1,7 @@ | |||||||
| package figlet4go | package figlet4go | ||||||
|  |  | ||||||
|  | import "errors" | ||||||
|  |  | ||||||
| // Parser stores some output specific stuff | // Parser stores some output specific stuff | ||||||
| type Parser struct { | type Parser struct { | ||||||
| 	// Name used for switching in colors.go | 	// Name used for switching in colors.go | ||||||
| @@ -14,9 +16,19 @@ type Parser struct { | |||||||
| 	Replaces map[string]string | 	Replaces map[string]string | ||||||
| } | } | ||||||
|  |  | ||||||
| var ( | var parsers map[string]Parser = map[string]Parser { | ||||||
|  |  | ||||||
| 	// Default terminal parser | 	// Default terminal parser | ||||||
| 	ParserTerminal Parser = Parser{"terminal", "", "", "\n", nil} | 	"terminal": Parser{"terminal", "", "", "\n", nil}, | ||||||
| 	// Parser for HTML code | 	// Parser for HTML code | ||||||
| 	ParserHTML Parser = Parser{"html", "<code>", "</code>", "<br>", map[string]string{" ": " "}} | 	"html": Parser{"html", "<code>", "</code>", "<br>", map[string]string{" ": " "}}, | ||||||
| ) | } | ||||||
|  |  | ||||||
|  | // GetParser returns a parser by its key | ||||||
|  | func GetParser(key string) (*Parser, error) { | ||||||
|  | 	parser, ok := parsers[key] | ||||||
|  | 	if !ok { | ||||||
|  | 		return nil, errors.New("Invalid parser key: " + key) | ||||||
|  | 	} | ||||||
|  | 	return &parser, nil | ||||||
|  | } | ||||||
| @@ -14,9 +14,10 @@ type RenderOptions struct { | |||||||
| // NewRenderOptions creates new RenderOptions | // NewRenderOptions creates new RenderOptions | ||||||
| // Sets the default font name | // Sets the default font name | ||||||
| func NewRenderOptions() *RenderOptions { | func NewRenderOptions() *RenderOptions { | ||||||
|  | 	p, _ := GetParser("terminal") | ||||||
| 	return &RenderOptions{ | 	return &RenderOptions{ | ||||||
| 		FontName: defaultFont, | 		FontName: defaultFont, | ||||||
| 		Parser:   ParserTerminal, | 		Parser:   *p, | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user