Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnoface.gno

package gnoface

import (
	"math/rand"
	"strconv"
	"strings"

	"gno.land/p/nym-moultest123/entropy/v0"
	"gno.land/p/nt/ufmt/v0"
)

func Render(path string) string {
	seed := uint64(entropy.New().Value())

	path = strings.TrimSpace(path)
	if path != "" {
		s, err := strconv.Atoi(path)
		if err != nil {
			panic(err)
		}
		seed = uint64(s)
	}

	output := ufmt.Sprintf("Gnoface #%d\n", seed)
	output += "```\n" + Draw(seed) + "```\n"
	return output
}

func Draw(seed uint64) 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.New(rand.NewPCG(seed, 0xdeadbeef))

	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.Rand, slice []string) string {
	return slice[r.IntN(len(slice))]
}

// based on https://github.com/moul/pipotron/blob/master/dict/ascii-face.yml

gnoface_test.gno

package gnoface

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
	"gno.land/p/nt/ufmt/v0"
)

func TestDraw(t *testing.T) {
	cases := []struct {
		seed     uint64
		expected string
	}{
		{
			seed: 42,
			expected: `
  |||||||
 |||||||||
 |       |
 | .   ~ |
)| v   v |O
 |       |
 |   L   |
 |       |
 |  ___  |
 |       |
 \~~~~~~~/
`[1:],
		},
		{
			seed: 1337,
			expected: `
  .......
 |||||||||
 |       |
 | .   _ |
D| x   X |O
 |       |
 |   ~   |
 |       |
 |  ~~~  |
 |       |
 \~~~~~~~/
`[1:],
		},
		{
			seed: 123456789,
			expected: `
  .......
 ////////\
 |       |
 | ~   * |
|| x   X |o
 |       |
 |   V   |
 |       |
 |   .   |
 |       |
 \-------/
`[1:],
		},
	}
	for _, tc := range cases {
		name := ufmt.Sprintf("%d", tc.seed)
		t.Run(name, func(t *testing.T) {
			got := Draw(tc.seed)
			uassert.Equal(t, string(tc.expected), got)
		})
	}
}

func TestRender(t *testing.T) {
	cases := []struct {
		path     string
		expected string
	}{
		{
			path: "42",
			expected: "Gnoface #42\n```" + `
  |||||||
 |||||||||
 |       |
 | .   ~ |
)| v   v |O
 |       |
 |   L   |
 |       |
 |  ___  |
 |       |
 \~~~~~~~/
` + "```\n",
		},
		{
			path: "1337",
			expected: "Gnoface #1337\n```" + `
  .......
 |||||||||
 |       |
 | .   _ |
D| x   X |O
 |       |
 |   ~   |
 |       |
 |  ~~~  |
 |       |
 \~~~~~~~/
` + "```\n",
		},
		{
			path: "123456789",
			expected: "Gnoface #123456789\n```" + `
  .......
 ////////\
 |       |
 | ~   * |
|| x   X |o
 |       |
 |   V   |
 |       |
 |   .   |
 |       |
 \-------/
` + "```\n",
		},
	}
	for _, tc := range cases {
		t.Run(tc.path, func(t *testing.T) {
			got := Render(tc.path)
			uassert.Equal(t, tc.expected, got)
		})
	}
}

gnomod.toml

module = "gno.land/r/nym-moultest123/demo/gnoface/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186410
  max_deposit = "5000000ugnot"

render_example_test.gno

package gnoface

func ExampleRender() {
	print(Render(""))
	// Output:
	// Gnoface #2370554733
	// ```
	//    /\ /\
	//  |||||||||
	//  |       |
	//  | _   ~ |
	// || o   _ |>
	//  |       |
	//  |   L   |
	//  |       |
	//  |  <=>  |
	//  |       |
	//  \-------/
	// ```
}