Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gno.land/r/demo/art/{millipede,gnoface} #690

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions examples/gno.land/r/demo/art/gnoface/gnoface.gno
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
136 changes: 136 additions & 0 deletions examples/gno.land/r/demo/art/gnoface/gnoface_test.gno
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)
}
})
}
}
51 changes: 51 additions & 0 deletions examples/gno.land/r/demo/art/millipede/millipede.gno
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
59 changes: 59 additions & 0 deletions examples/gno.land/r/demo/art/millipede/millipede_test.gno
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)
}
})
}
}