Skip to content

Commit a5ef732

Browse files
committed
add reverse
1 parent e2fd67e commit a5ef732

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

cmd/pic2ascii/main.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func main() {
7575
}
7676

7777
if *r {
78-
*chars = reverseString(*chars)
78+
*chars = pic2ascii.ReverseString(*chars)
7979
}
8080

8181
toAscii := func(img image.Image) string {
@@ -106,22 +106,24 @@ func main() {
106106
dds := []string{}
107107
sg := pic2ascii.SliceGIF(img)
108108
for _, v := range sg {
109-
dds = append(dds, fmt.Sprintln(toAscii(v)))
109+
dd := toAscii(v)
110+
dds = append(dds, dd)
110111
}
111112

112-
if *o == "" {
113-
if img.LoopCount == 0 {
114-
img.LoopCount = *m
115-
}
113+
if *o != "" {
114+
ioutil.WriteFile(*o, []byte(strings.Join(dds, "\n")), 0666)
115+
return
116+
}
117+
118+
if img.LoopCount == 0 {
119+
img.LoopCount = *m
120+
}
116121

117-
for i := 0; i != img.LoopCount; i++ {
118-
for k, v := range dds {
119-
fmt.Println(v)
120-
time.Sleep(time.Duration(img.Delay[k]) * time.Second / 100)
121-
}
122+
for i := 0; i != img.LoopCount; i++ {
123+
for k, v := range dds {
124+
fmt.Println(v)
125+
time.Sleep(time.Duration(img.Delay[k]) * time.Second / 100)
122126
}
123-
} else {
124-
ioutil.WriteFile(*o, []byte(strings.Join(dds, "\n")), 0666)
125127
}
126128

127129
default:
@@ -132,20 +134,11 @@ func main() {
132134
}
133135

134136
dd := toAscii(img)
135-
if *o == "" {
136-
fmt.Print(dd)
137-
} else {
137+
if *o != "" {
138138
ioutil.WriteFile(*o, []byte(dd), 0666)
139+
return
139140
}
140-
}
141-
}
142141

143-
func reverseString(s string) string {
144-
str := []rune(s)
145-
l := len(str) / 2
146-
for i := 0; i < l; i++ {
147-
j := len(str) - i - 1
148-
str[i], str[j] = str[j], str[i]
142+
fmt.Print(dd)
149143
}
150-
return string(str)
151144
}

reverse.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pic2ascii
2+
3+
func ReverseString(s string) string {
4+
str := []rune(s)
5+
l := len(str) / 2
6+
for i := 0; i < l; i++ {
7+
j := len(str) - i - 1
8+
str[i], str[j] = str[j], str[i]
9+
}
10+
return string(str)
11+
}

0 commit comments

Comments
 (0)