-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (170 loc) · 4.39 KB
/
main.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
)
func main() {
if len(os.Args) >= 3 {
textFromOutside := os.Args[2]
fileName := ""
if len(os.Args) == 4 {
if os.Args[3] == "shadow" || os.Args[2] == "standard" || os.Args[2] == "thinkertoy" {
fileName = os.Args[3] // if something else standard
}
}
fileLines := ReadStandardTxt(fileName)
// fmt.Println(textFromOutside, fileLines)
asciiTemplates := return2dASCIIArray(fileLines)
// fmt.Println(asciiTemplates)
// printMultipleCharacter(textFromOutside, asciiTemplates)
// fmt.Println(int('\n'))
// test1 := "\nHello\nWorld\n\nMom\n\n\nAnd Dad\n"
// fmt.Println(returnstring2EndlineArray(textFromOutside))
outputFile := os.Args[1]
if checkArgs1(outputFile) {
outputFile = outputFile[9:]
mw := createTextFileWriter(outputFile)
printAllStringASCII(textFromOutside, asciiTemplates, mw)
}
}
}
func checkArgs1(str string) bool {
if len(str) > 9 {
if str[:9] == "--output=" {
return true
}
}
return false
}
func createTextFileWriter(fileName string) io.Writer {
file, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
mw := io.MultiWriter(os.Stdout, file)
return mw
}
func ReadStandardTxt(nameOfFile string) []string {
if nameOfFile == "" {
nameOfFile = "standard"
}
readFile, err := os.Open("artstyles/" + nameOfFile + ".txt")
if err != nil {
fmt.Println(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var fileLines []string
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
readFile.Close()
return fileLines
}
func return2dASCIIArray(fileLines []string) [][]string {
var asciiTemplates [][]string
counter := 0
var tempAsciArray []string
for _, line := range fileLines {
counter++
// fmt.Println(index, line)
if counter != 1 {
tempAsciArray = append(tempAsciArray, line)
}
if counter == 9 {
// fmt.Println("add to list") // but dont include the first line because it is empty line
asciiTemplates = append(asciiTemplates, tempAsciArray)
counter = 0
tempAsciArray = nil
} else {
}
}
return asciiTemplates
}
// problem '\n' logic when we have 2 '\n' or 1 '\n' is different
func printMultipleCharacter(s string, asciiTemplates [][]string, mw io.Writer) {
// for ex 'hello'
// we gonna write all letters index 0 after $ after \n after index 1 after $ after \n
tempIntArrLetter := returnAsciiCodeInt(s)
for i := 0; i < 8; i++ {
for _, v := range tempIntArrLetter {
fmt.Fprint(mw, asciiTemplates[v][i])
}
fmt.Fprintln(mw, "$")
}
}
func returnAsciiCodeInt(s string) []int {
var tempIntArrLetter []int
for _, v := range s {
tempIntArrLetter = append(tempIntArrLetter, (int(v) - 32))
}
return tempIntArrLetter
}
func printAllStringASCII(text string, asciiTemplates [][]string, mw io.Writer) {
/*
if ends w \n it gonna print println $
if you can see text after \n chec;
before \n
if yes println $
if no println
*/
/*
func to uses printMultipleCharacters print whole stringfrom outside
*/
// Split the input string into an array of strings
// split the line into words if there is a "\r\n" symbol
substrings := returnstring2EndlineArray(text)
lenOfsubstrings := len(substrings)
for index, v := range substrings {
if v == "\\n" {
// If it is last one
if index == lenOfsubstrings-1 {
fmt.Fprintln(mw, "$")
} else if index == 0 {
fmt.Fprintln(mw, "$") // no idea CHECK IT POTENTIAL ERROR
} else {
if substrings[index-1] == "\\n" {
fmt.Fprintln(mw, "$")
} else {
// "Hello\nWorld"
}
}
} else {
printMultipleCharacter(v, asciiTemplates, mw)
}
}
fmt.Fprintln(mw, "$")
}
func returnstring2EndlineArray(text string) []string {
substrings := make([]string, 0)
escapedN := "\\n"
newline := "\n"
for {
idx := strings.Index(text, escapedN)
if idx == -1 {
substrings = append(substrings, text)
break
}
substrings = append(substrings, text[:idx])
if idx+len(escapedN) < len(text) && text[idx+len(escapedN)] == 'n' {
substrings = append(substrings, newline)
text = text[idx+len(escapedN)+1:]
} else {
substrings = append(substrings, escapedN)
text = text[idx+len(escapedN):]
}
}
// fmt.Printf("%#v\n", substrings)
var mysubstring2 []string
for _, mysub := range substrings {
if mysub != "" {
mysubstring2 = append(mysubstring2, mysub)
}
}
// fmt.Printf("%#v\n", mysubstring2)
return mysubstring2
}