asciitxt/asciitxt.go

82 lines
1.6 KiB
Go
Raw Normal View History

2021-10-27 14:20:09 +00:00
// ASCIITXT is a simple utility for creating ascii texts.
2021-10-27 12:42:25 +00:00
package asciitxt
import (
"strings"
)
type Style int
const (
2021-10-27 14:20:09 +00:00
StyleStandard Style = iota + 1
2021-10-27 12:42:25 +00:00
)
type Config struct {
Style Style
}
2021-10-27 14:20:09 +00:00
// Generates a multi-line ASCII string with the default style.
// As simple as that.
2021-10-27 12:42:25 +00:00
func New(txt string) string {
2021-10-27 14:20:09 +00:00
return WithConfig(txt, Config{Style: StyleStandard})
2021-10-27 12:42:25 +00:00
}
2021-10-27 14:20:09 +00:00
// Generates a multi-line ASCII string from a defined Config.
// The config itself could be empty. It'll look for its' default value.
// But if you don't intend to put any config whatsoever, please use New().
//
// Please note that this function will panic if the config.Style
// is invalid. Find the styles from the constants with Style prefix.
2021-10-27 12:42:25 +00:00
func WithConfig(txt string, config Config) string {
if config.Style == 0 {
2021-10-27 14:20:09 +00:00
config.Style = StyleStandard
2021-10-27 12:42:25 +00:00
}
2021-10-27 17:08:45 +00:00
if txt == "" {
return ""
}
2021-10-27 12:42:25 +00:00
letters := strings.Split(txt, "")
var arr [][]string
llen := getStyleLength(config.Style)
for i := 0; i < len(letters); i++ {
var temp []string
letter := getStyleLetter(config.Style, letters[i])
for j := 0; j < len(letter); j++ {
temp = append(temp, letter[j])
}
arr = append(arr, temp)
}
2021-10-27 17:21:11 +00:00
var output strings.Builder
2021-10-27 12:42:25 +00:00
for k := 0; k < llen; k++ {
for l := 0; l < len(arr); l++ {
2021-10-27 17:21:11 +00:00
output.WriteString(arr[l][k])
2021-10-27 12:42:25 +00:00
}
2021-10-27 17:21:11 +00:00
output.WriteString("\n")
2021-10-27 12:42:25 +00:00
}
2021-10-27 17:21:11 +00:00
return output.String()
2021-10-27 12:42:25 +00:00
}
2021-10-27 14:20:09 +00:00
//
2021-10-27 12:42:25 +00:00
func getStyleLength(style Style) int {
switch style {
2021-10-27 14:20:09 +00:00
case StyleStandard:
return standardLength
2021-10-27 12:42:25 +00:00
default:
return 0
}
}
func getStyleLetter(style Style, letter string) []string {
switch style {
2021-10-27 14:20:09 +00:00
case StyleStandard:
2021-10-27 12:42:25 +00:00
return getStandardLetter(letter)
default:
panic("invalid style was given")
}
2021-10-27 14:20:09 +00:00
}