From 5ed4a14ab1c8a9ea76ad210deb4ec25f21ff3ccb Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Mon, 3 Apr 2023 00:46:53 +0200 Subject: [PATCH 1/4] feat: add gno.land/r/demo/art/{millipede,gnoface} Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../gno.land/r/demo/art/gnoface/gnoface.gno | 105 ++++++++++++++++++ .../r/demo/art/gnoface/gnoface_test.gno | 69 ++++++++++++ .../r/demo/art/millipede/millipede.gno | 51 +++++++++ .../r/demo/art/millipede/millipede_test.gno | 59 ++++++++++ 4 files changed, 284 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..fdc8e56ee6b --- /dev/null +++ b/examples/gno.land/r/demo/art/gnoface/gnoface.gno @@ -0,0 +1,105 @@ +package gnoface + +import ( + "strings" + + "gno.land/p/demo/rand" +) + +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) + + output := 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) + return output +} + +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..13cc0062360 --- /dev/null +++ b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno @@ -0,0 +1,69 @@ +package gnoface + +import ( + "testing" + + "gno.land/p/demo/ufmt" +) + +func TestGnoface(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) + } + }) + } +} 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..df5b66119aa --- /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 Millipede(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 := Millipede(size) + 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..fab8f69632c --- /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: ` + ╚⊙ ⊙╝ + ╚═(███)═╝ + ╚═(███)═╝ +╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ +╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ +[19](/r/art/millpede:19)< >[21](/r/art/millipede:21)`[1:], + }, + { + path: "4", + expected: ` + ╚⊙ ⊙╝ + ╚═(███)═╝ + ╚═(███)═╝ +╚═(███)═╝ + ╚═(███)═╝ +[3](/r/art/millpede:3)< >[5](/r/art/millipede:5)`[1:], + }, + } + + 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) + } + }) + } +} From 1e24333f7206fee938d6b8ac0575a11150637969 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Mon, 3 Apr 2023 01:02:34 +0200 Subject: [PATCH 2/4] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../gno.land/r/demo/art/gnoface/gnoface.gno | 20 ++++++ .../r/demo/art/gnoface/gnoface_test.gno | 64 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface.gno b/examples/gno.land/r/demo/art/gnoface/gnoface.gno index fdc8e56ee6b..6354199de2b 100644 --- a/examples/gno.land/r/demo/art/gnoface/gnoface.gno +++ b/examples/gno.land/r/demo/art/gnoface/gnoface.gno @@ -1,11 +1,31 @@ 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 += Draw(seed) + return output +} + func Draw(seed int64) string { var ( hairs = []string{ diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno index 13cc0062360..0a3fc3e34e5 100644 --- a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno +++ b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno @@ -67,3 +67,67 @@ o| ~ ~ |. }) } } + +func TestRender(t *testing.T) { + cases := []struct { + path string + expected string + }{ + { + path: "42", + expected: ` +Gnoface #42 + ||||||| + ////////\ + | | + | ~ . | +)| X X |. + | | + | C | + | | + | __/ | + | | + \~~~~~~~/`[1:], + }, + { + path: "1337", + expected: ` +Gnoface #1337 + s + /|||||||\ + | | + | . * | +o| ~ ~ |. + | | + | O | + | | + | __/ | + | | + \_______/`[1:], + }, + { + path: "123456789", + expected: ` +Gnoface #123456789 + s + /~~~~~~~\ + | | + | ~ . | +<| ~ ~ |< + | | + | V | + | | + | \_/ | + | | + \-------/`[1:], + }, + } + 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) + } + }) + } +} From 787ce073c6dbd7aa4669594a82e137d120ef5f08 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Mon, 3 Apr 2023 01:23:53 +0200 Subject: [PATCH 3/4] chore: fixup Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- .../gno.land/r/demo/art/gnoface/gnoface.gno | 6 ++-- .../r/demo/art/gnoface/gnoface_test.gno | 33 ++++++++++--------- .../r/demo/art/millipede/millipede.gno | 4 +-- .../r/demo/art/millipede/millipede_test.gno | 8 ++--- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface.gno b/examples/gno.land/r/demo/art/gnoface/gnoface.gno index 6354199de2b..3ddc730008e 100644 --- a/examples/gno.land/r/demo/art/gnoface/gnoface.gno +++ b/examples/gno.land/r/demo/art/gnoface/gnoface.gno @@ -104,7 +104,8 @@ func Draw(seed int64) string { r := rand.FromSeed(seed) - output := pick(r, hairs) + "\n" + + return "```\n" + + pick(r, hairs) + "\n" + pick(r, headtop) + "\n" + pick(r, headspace) + "\n" + " | " + pick(r, eyebrow) + " " + pick(r, eyebrow) + " |\n" + @@ -114,8 +115,7 @@ func Draw(seed int64) string { pick(r, headspace) + "\n" + pick(r, mouth) + "\n" + pick(r, headspace) + "\n" + - pick(r, headbottom) - return output + pick(r, headbottom) + "\n```\n" } func pick(r *rand.Instance, slice []string) string { diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno index 0a3fc3e34e5..5c621721e05 100644 --- a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno +++ b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno @@ -13,7 +13,7 @@ func TestGnoface(t *testing.T) { }{ { seed: 42, - expected: ` + expected: "```" + ` ||||||| ////////\ | | @@ -24,11 +24,12 @@ func TestGnoface(t *testing.T) { | | | __/ | | | - \~~~~~~~/`[1:], + \~~~~~~~/ +` + "```\n", }, { seed: 1337, - expected: ` + expected: "```" + ` s /|||||||\ | | @@ -39,11 +40,12 @@ o| ~ ~ |. | | | __/ | | | - \_______/`[1:], + \_______/ +` + "```\n", }, { seed: 123456789, - expected: ` + expected: "```" + ` s /~~~~~~~\ | | @@ -54,7 +56,8 @@ o| ~ ~ |. | | | \_/ | | | - \-------/`[1:], + \-------/ +` + "```\n", }, } for _, tc := range cases { @@ -75,8 +78,7 @@ func TestRender(t *testing.T) { }{ { path: "42", - expected: ` -Gnoface #42 + expected: "Gnoface #42\n```" + ` ||||||| ////////\ | | @@ -87,12 +89,12 @@ Gnoface #42 | | | __/ | | | - \~~~~~~~/`[1:], + \~~~~~~~/ +` + "```\n", }, { path: "1337", - expected: ` -Gnoface #1337 + expected: "Gnoface #1337\n```" + ` s /|||||||\ | | @@ -103,12 +105,12 @@ o| ~ ~ |. | | | __/ | | | - \_______/`[1:], + \_______/ +` + "```\n", }, { path: "123456789", - expected: ` -Gnoface #123456789 + expected: "Gnoface #123456789\n```" + ` s /~~~~~~~\ | | @@ -119,7 +121,8 @@ Gnoface #123456789 | | | \_/ | | | - \-------/`[1:], + \-------/ +` + "```\n", }, } for _, tc := range cases { diff --git a/examples/gno.land/r/demo/art/millipede/millipede.gno b/examples/gno.land/r/demo/art/millipede/millipede.gno index df5b66119aa..dbffd8b3fbb 100644 --- a/examples/gno.land/r/demo/art/millipede/millipede.gno +++ b/examples/gno.land/r/demo/art/millipede/millipede.gno @@ -13,7 +13,7 @@ const ( maxSize = 100 ) -func Millipede(size int) string { +func Draw(size int) string { if size < minSize || size > maxSize { panic("invalid millipede size") } @@ -38,7 +38,7 @@ func Render(path string) string { } } - output := Millipede(size) + output := "```\n" + Draw(size) + "```\n" if size > minSize { output += ufmt.Sprintf("[%d](/r/art/millpede:%d)< ", size-1, size-1) } diff --git a/examples/gno.land/r/demo/art/millipede/millipede_test.gno b/examples/gno.land/r/demo/art/millipede/millipede_test.gno index fab8f69632c..38914707b4a 100644 --- a/examples/gno.land/r/demo/art/millipede/millipede_test.gno +++ b/examples/gno.land/r/demo/art/millipede/millipede_test.gno @@ -12,7 +12,7 @@ func TestRender(t *testing.T) { }{ { path: "", - expected: ` + expected: "```" + ` ╚⊙ ⊙╝ ╚═(███)═╝ ╚═(███)═╝ @@ -34,17 +34,17 @@ func TestRender(t *testing.T) { ╚═(███)═╝ ╚═(███)═╝ ╚═(███)═╝ -[19](/r/art/millpede:19)< >[21](/r/art/millipede:21)`[1:], +` + "```\n[19](/r/art/millpede:19)< >[21](/r/art/millipede:21)", }, { path: "4", - expected: ` + expected: "```" + ` ╚⊙ ⊙╝ ╚═(███)═╝ ╚═(███)═╝ ╚═(███)═╝ ╚═(███)═╝ -[3](/r/art/millpede:3)< >[5](/r/art/millipede:5)`[1:], +` + "```\n[3](/r/art/millpede:3)< >[5](/r/art/millipede:5)", }, } From 94175209ee4d6e630bb98b627fd458c6487c7a21 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Mon, 3 Apr 2023 16:14:59 +0200 Subject: [PATCH 4/4] chore: move triple backticks to Render Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- examples/gno.land/r/demo/art/gnoface/gnoface.gno | 7 +++---- .../gno.land/r/demo/art/gnoface/gnoface_test.gno | 14 +++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface.gno b/examples/gno.land/r/demo/art/gnoface/gnoface.gno index 3ddc730008e..95493b52bf5 100644 --- a/examples/gno.land/r/demo/art/gnoface/gnoface.gno +++ b/examples/gno.land/r/demo/art/gnoface/gnoface.gno @@ -22,7 +22,7 @@ func Render(path string) string { } output := ufmt.Sprintf("Gnoface #%d\n", seed) - output += Draw(seed) + output += "```\n" + Draw(seed) + "```\n" return output } @@ -104,8 +104,7 @@ func Draw(seed int64) string { r := rand.FromSeed(seed) - return "```\n" + - pick(r, hairs) + "\n" + + return pick(r, hairs) + "\n" + pick(r, headtop) + "\n" + pick(r, headspace) + "\n" + " | " + pick(r, eyebrow) + " " + pick(r, eyebrow) + " |\n" + @@ -115,7 +114,7 @@ func Draw(seed int64) string { pick(r, headspace) + "\n" + pick(r, mouth) + "\n" + pick(r, headspace) + "\n" + - pick(r, headbottom) + "\n```\n" + pick(r, headbottom) + "\n" } func pick(r *rand.Instance, slice []string) string { diff --git a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno index 5c621721e05..630cce85c55 100644 --- a/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno +++ b/examples/gno.land/r/demo/art/gnoface/gnoface_test.gno @@ -6,14 +6,14 @@ import ( "gno.land/p/demo/ufmt" ) -func TestGnoface(t *testing.T) { +func TestDraw(t *testing.T) { cases := []struct { seed int64 expected string }{ { seed: 42, - expected: "```" + ` + expected: ` ||||||| ////////\ | | @@ -25,11 +25,11 @@ func TestGnoface(t *testing.T) { | __/ | | | \~~~~~~~/ -` + "```\n", +`[1:], }, { seed: 1337, - expected: "```" + ` + expected: ` s /|||||||\ | | @@ -41,11 +41,11 @@ o| ~ ~ |. | __/ | | | \_______/ -` + "```\n", +`[1:], }, { seed: 123456789, - expected: "```" + ` + expected: ` s /~~~~~~~\ | | @@ -57,7 +57,7 @@ o| ~ ~ |. | \_/ | | | \-------/ -` + "```\n", +`[1:], }, } for _, tc := range cases {