mirror of
				https://github.com/KevinMidboe/motdGOi.git
				synced 2025-10-29 17:50:24 +00:00 
			
		
		
		
	color support for cli client
This commit is contained in:
		| @@ -83,4 +83,4 @@ Other fonts can mainly be found on [figlet](http://www.figlet.org). You have to | ||||
| - [x] Cli client | ||||
| - [ ] automatic the perfect char margin | ||||
| - [ ] Linebreak possible? | ||||
| - [ ] Colors in the cli client | ||||
| - [x] Colors in the cli client | ||||
| @@ -1,18 +1,20 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"flag" | ||||
| 	"log" | ||||
| 	"errors" | ||||
| 	_"github.com/fatih/color" | ||||
| 	"flag" | ||||
| 	"fmt" | ||||
| 	"github.com/fatih/color" | ||||
| 	"github.com/probandula/figlet4go" | ||||
| 	"log" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	str *string = flag.String("str", "", "String to be converted with FIGlet") | ||||
| 	font *string = flag.String("font", "", "Font name to use") | ||||
| 	str      *string = flag.String("str", "", "String to be converted with FIGlet") | ||||
| 	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 seperated by ';'\n\tPossible colors: black, red, green, yellow, blue, magenta, cyan, white") | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| @@ -37,6 +39,10 @@ func main() { | ||||
| 	// Set the font | ||||
| 	options.FontName = *font | ||||
|  | ||||
| 	// Set colors | ||||
| 	if *colors != "" { | ||||
| 		options.FontColor = getColorSlice(*colors) | ||||
| 	} | ||||
|  | ||||
| 	// Render the string | ||||
| 	renderStr, err := ascii.RenderOpts(*str, options) | ||||
| @@ -47,6 +53,53 @@ func main() { | ||||
| 	fmt.Println(renderStr) | ||||
| } | ||||
|  | ||||
| // Get a slice with colors to give to the RenderOptions | ||||
| // Splits the given string with the seperator ";" | ||||
| func getColorSlice(colorStr string) []color.Attribute { | ||||
|  | ||||
| 	givenColors := strings.Split(colorStr, ";") | ||||
|  | ||||
| 	colors := make([]color.Attribute, len(givenColors)) | ||||
|  | ||||
| 	for i, c := range givenColors { | ||||
| 		if c == "black" { | ||||
| 			colors[i] = color.FgBlack | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "red" { | ||||
| 			colors[i] = color.FgRed | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "green" { | ||||
| 			colors[i] = color.FgGreen | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "yellow" { | ||||
| 			colors[i] = color.FgYellow | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "blue" { | ||||
| 			colors[i] = color.FgBlue | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "magenta" { | ||||
| 			colors[i] = color.FgMagenta | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "cyan" { | ||||
| 			colors[i] = color.FgCyan | ||||
| 			continue | ||||
| 		} | ||||
| 		if c == "white" { | ||||
| 			colors[i] = color.FgWhite | ||||
| 			continue | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return colors | ||||
|  | ||||
| } | ||||
|  | ||||
| // Validate if all required options are given | ||||
| // flag.Parse() must be called before this | ||||
| func validate() error { | ||||
| @@ -54,4 +107,4 @@ func validate() error { | ||||
| 		return errors.New("No string given") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user