aboutsummaryrefslogtreecommitdiffstats
path: root/ascii.go
blob: da4a3b3a4b4a3866d555f612d2df91016ddd7581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main

import (
	"io/ioutil"
	"strings"

	"github.com/inconshreveable/log15"
	"github.com/kardianos/osext"
)

// ASCIICharacterMap maps from runes to ascii characters represented as strings
type ASCIICharacterMap map[rune]string

var chars ASCIICharacterMap

// ASCIIRuneArray returns an 'ascii-art' representation of the given rune array
func ASCIIRuneArray(characters []rune) string {
	// var asciiString string

	var stringArray [][]string

	for _, aRune := range characters {
		stringArray = append(stringArray, strings.Split(ASCIICharacter(aRune), "\n"))
		// asciiString += ASCIICharacter(aRune)
	}

	var asciiString string
	for i := 0; i < len(stringArray[0]); i++ {
		for j := range stringArray {
			asciiString += stringArray[j][i]
		}
		asciiString += "\n"
	}

	return asciiString
}

// ASCIICharacter returns an 'ascii-art' representation of the given rune
func ASCIICharacter(character rune) string {
	if asciiChar, ok := chars[character]; ok {
		return asciiChar
	}
	return ""
}

func init() {
	dir, err := osext.ExecutableFolder()
	if err != nil {
		log15.Error("getting executable directory", "error", err)
	}

	charmapRaw, err := ioutil.ReadFile(dir + "/charmap.txt")
	if err != nil {
		log15.Error("reading charmap file",
			"file", dir+"/charmap.txt", "error", err)
	}

	charmap := string(charmapRaw[:])

	chars = loadCharMap(charmap)
}

func loadCharMap(charmap string) ASCIICharacterMap {
	newCharMap := make(map[rune]string)

	chars := strings.Split(charmap, "===")

	for i, char := range chars {
		params := strings.Split(char, "---")
		if len(params) != 2 {
			if params[0] == "" || params[0] == "\n" {
				log15.Info("Parsed charmap", "runes", i)
				break
			} else {
				log15.Error("syntax error in charmap", "index", i)
				return newCharMap
			}
		}
		forRunes := []rune(string(params[0][:]))

		for _, aRune := range forRunes {
			newCharMap[aRune] = params[1]
		}
	}

	return newCharMap
}