-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gno.land/r/demo/art/{millipede,gnoface} (#690)
- Loading branch information
Showing
4 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package gnoface | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
"strings" | ||
|
||
"gno.land/p/demo/rand" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
func Render(path string) string { | ||
seed := std.GetHeight() | ||
|
||
path = strings.TrimSpace(path) | ||
if path != "" { | ||
s, err := strconv.Atoi(path) | ||
if err != nil { | ||
panic(err) | ||
} | ||
seed = int64(s) | ||
} | ||
|
||
output := ufmt.Sprintf("Gnoface #%d\n", seed) | ||
output += "```\n" + Draw(seed) + "```\n" | ||
return output | ||
} | ||
|
||
func Draw(seed int64) string { | ||
var ( | ||
hairs = []string{ | ||
" s", | ||
" .......", | ||
" s s s", | ||
" /\\ /\\", | ||
" |||||||", | ||
} | ||
headtop = []string{ | ||
" /-------\\", | ||
" /~~~~~~~\\", | ||
" /|||||||\\", | ||
" ////////\\", | ||
" |||||||||", | ||
" /\\\\\\\\\\\\\\\\", | ||
} | ||
headspace = []string{ | ||
" | |", | ||
} | ||
eyebrow = []string{ | ||
"~", | ||
"*", | ||
"_", | ||
".", | ||
} | ||
ear = []string{ | ||
"o", | ||
" ", | ||
"D", | ||
"O", | ||
"<", | ||
">", | ||
".", | ||
"|", | ||
")", | ||
"(", | ||
} | ||
eyesmiddle = []string{ | ||
"| o o |", | ||
"| o _ |", | ||
"| _ o |", | ||
"| . . |", | ||
"| O O |", | ||
"| v v |", | ||
"| X X |", | ||
"| x X |", | ||
"| X D |", | ||
"| ~ ~ |", | ||
} | ||
nose = []string{ | ||
" | o |", | ||
" | O |", | ||
" | V |", | ||
" | L |", | ||
" | C |", | ||
" | ~ |", | ||
" | . . |", | ||
" | . |", | ||
} | ||
mouth = []string{ | ||
" | __/ |", | ||
" | \\_/ |", | ||
" | . |", | ||
" | ___ |", | ||
" | ~~~ |", | ||
" | === |", | ||
" | <=> |", | ||
} | ||
headbottom = []string{ | ||
" \\-------/", | ||
" \\~~~~~~~/", | ||
" \\_______/", | ||
} | ||
) | ||
|
||
r := rand.FromSeed(seed) | ||
|
||
return pick(r, hairs) + "\n" + | ||
pick(r, headtop) + "\n" + | ||
pick(r, headspace) + "\n" + | ||
" | " + pick(r, eyebrow) + " " + pick(r, eyebrow) + " |\n" + | ||
pick(r, ear) + pick(r, eyesmiddle) + pick(r, ear) + "\n" + | ||
pick(r, headspace) + "\n" + | ||
pick(r, nose) + "\n" + | ||
pick(r, headspace) + "\n" + | ||
pick(r, mouth) + "\n" + | ||
pick(r, headspace) + "\n" + | ||
pick(r, headbottom) + "\n" | ||
} | ||
|
||
func pick(r *rand.Instance, slice []string) string { | ||
return slice[r.Intn(len(slice))] | ||
} | ||
|
||
// based on https://github.com/moul/pipotron/blob/master/dict/ascii-face.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package gnoface | ||
|
||
import ( | ||
"testing" | ||
|
||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
func TestDraw(t *testing.T) { | ||
cases := []struct { | ||
seed int64 | ||
expected string | ||
}{ | ||
{ | ||
seed: 42, | ||
expected: ` | ||
||||||| | ||
////////\ | ||
| | | ||
| ~ . | | ||
)| X X |. | ||
| | | ||
| C | | ||
| | | ||
| __/ | | ||
| | | ||
\~~~~~~~/ | ||
`[1:], | ||
}, | ||
{ | ||
seed: 1337, | ||
expected: ` | ||
s | ||
/|||||||\ | ||
| | | ||
| . * | | ||
o| ~ ~ |. | ||
| | | ||
| O | | ||
| | | ||
| __/ | | ||
| | | ||
\_______/ | ||
`[1:], | ||
}, | ||
{ | ||
seed: 123456789, | ||
expected: ` | ||
s | ||
/~~~~~~~\ | ||
| | | ||
| ~ . | | ||
<| ~ ~ |< | ||
| | | ||
| V | | ||
| | | ||
| \_/ | | ||
| | | ||
\-------/ | ||
`[1:], | ||
}, | ||
} | ||
for _, tc := range cases { | ||
name := ufmt.Sprintf("%d", tc.seed) | ||
t.Run(name, func(t *testing.T) { | ||
got := Draw(tc.seed) | ||
if got != tc.expected { | ||
t.Errorf("got %s, expected %s", got, tc.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRender(t *testing.T) { | ||
cases := []struct { | ||
path string | ||
expected string | ||
}{ | ||
{ | ||
path: "42", | ||
expected: "Gnoface #42\n```" + ` | ||
||||||| | ||
////////\ | ||
| | | ||
| ~ . | | ||
)| X X |. | ||
| | | ||
| C | | ||
| | | ||
| __/ | | ||
| | | ||
\~~~~~~~/ | ||
` + "```\n", | ||
}, | ||
{ | ||
path: "1337", | ||
expected: "Gnoface #1337\n```" + ` | ||
s | ||
/|||||||\ | ||
| | | ||
| . * | | ||
o| ~ ~ |. | ||
| | | ||
| O | | ||
| | | ||
| __/ | | ||
| | | ||
\_______/ | ||
` + "```\n", | ||
}, | ||
{ | ||
path: "123456789", | ||
expected: "Gnoface #123456789\n```" + ` | ||
s | ||
/~~~~~~~\ | ||
| | | ||
| ~ . | | ||
<| ~ ~ |< | ||
| | | ||
| V | | ||
| | | ||
| \_/ | | ||
| | | ||
\-------/ | ||
` + "```\n", | ||
}, | ||
} | ||
for _, tc := range cases { | ||
t.Run(tc.path, func(t *testing.T) { | ||
got := Render(tc.path) | ||
if got != tc.expected { | ||
t.Errorf("got %s, expected %s", got, tc.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package millipede | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
const ( | ||
minSize = 1 | ||
defaultSize = 20 | ||
maxSize = 100 | ||
) | ||
|
||
func Draw(size int) string { | ||
if size < minSize || size > maxSize { | ||
panic("invalid millipede size") | ||
} | ||
paddings := []string{" ", " ", "", " ", " ", " ", " ", " ", " "} | ||
var b strings.Builder | ||
b.WriteString(" ╚⊙ ⊙╝\n") | ||
for i := 0; i < size; i++ { | ||
b.WriteString(paddings[i%9] + "╚═(███)═╝\n") | ||
} | ||
return b.String() | ||
} | ||
|
||
func Render(path string) string { | ||
size := defaultSize | ||
|
||
path = strings.TrimSpace(path) | ||
if path != "" { | ||
var err error | ||
size, err = strconv.Atoi(path) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
output := "```\n" + Draw(size) + "```\n" | ||
if size > minSize { | ||
output += ufmt.Sprintf("[%d](/r/art/millpede:%d)< ", size-1, size-1) | ||
} | ||
if size < maxSize { | ||
output += ufmt.Sprintf(" >[%d](/r/art/millipede:%d)", size+1, size+1) | ||
} | ||
return output | ||
} | ||
|
||
// based on https://github.com/getmillipede/millipede-go/blob/977f046c39c35a650eac0fd30245e96b22c7803c/main.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package millipede | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestRender(t *testing.T) { | ||
cases := []struct { | ||
path string | ||
expected string | ||
}{ | ||
{ | ||
path: "", | ||
expected: "```" + ` | ||
╚⊙ ⊙╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
` + "```\n[19](/r/art/millpede:19)< >[21](/r/art/millipede:21)", | ||
}, | ||
{ | ||
path: "4", | ||
expected: "```" + ` | ||
╚⊙ ⊙╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
╚═(███)═╝ | ||
` + "```\n[3](/r/art/millpede:3)< >[5](/r/art/millipede:5)", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.path, func(t *testing.T) { | ||
got := Render(tc.path) | ||
if got != tc.expected { | ||
t.Errorf("expected %s, got %s.", tc.expected, got) | ||
} | ||
}) | ||
} | ||
} |