From 15785887bd1cf12634fcef8c6b68cb052f988292 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Mon, 3 Apr 2023 22:19:53 +0200 Subject: [PATCH] feat: add gno.land/r/demo/art/{millipede,gnoface} (#690) --- .../gno.land/r/demo/art/gnoface/gnoface.gno | 124 ++++++++++++++++ .../r/demo/art/gnoface/gnoface_test.gno | 136 ++++++++++++++++++ .../r/demo/art/millipede/millipede.gno | 51 +++++++ .../r/demo/art/millipede/millipede_test.gno | 59 ++++++++ 4 files changed, 370 insertions(+) create mode 100644 examples/gno.land/r/demo/art/gnoface/gnoface.gno create mode 100644 examples/gno.land/r/demo/art/gnoface/gnoface_test.gno create mode 100644 examples/gno.land/r/demo/art/millipede/millipede.gno create mode 100644 examples/gno.land/r/demo/art/millipede/millipede_test.gno diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface.gno b/examples/gno.land/r/demo/art/gnoface/gnoface.gno new file mode 100644 index 00000000000..95493b52bf5 --- /dev/null +++ b/examples/gno.land/r/demo/art/gnoface/gnoface.gno @@ -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 diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno new file mode 100644 index 00000000000..630cce85c55 --- /dev/null +++ b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno @@ -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) + } + }) + } +} diff --git a/examples/gno.land/r/demo/art/millipede/millipede.gno b/examples/gno.land/r/demo/art/millipede/millipede.gno new file mode 100644 index 00000000000..dbffd8b3fbb --- /dev/null +++ b/examples/gno.land/r/demo/art/millipede/millipede.gno @@ -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 diff --git a/examples/gno.land/r/demo/art/millipede/millipede_test.gno b/examples/gno.land/r/demo/art/millipede/millipede_test.gno new file mode 100644 index 00000000000..38914707b4a --- /dev/null +++ b/examples/gno.land/r/demo/art/millipede/millipede_test.gno @@ -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) + } + }) + } +}