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

cowsay.gno

// Package cowsay is an on-chain Gno port of the classic `cowsay` program:
// it builds an ASCII cow with a speech bubble containing a message — as a
// reusable pure package.
//
// Original: cowsay by Tony Monroe (1999), a Perl program later ported to Go
// many times. This package reimplements the core rendering — bubble framing,
// word-wrapping and the cow art — as pure, deterministic string logic.
//
// A live demo of this package (an interactive cow that says whatever you put in
// the path) is at [r/moul/x/daily/cowsaydemo](/r/nym-moultest123/x/daily/cowsaydemo/v0).
package cowsay

import (
	"strings"
)

// defaultMessage is shown when no message is supplied.
const defaultMessage = "Moo!"

// wrapWidth is the maximum bubble text width (classic cowsay uses 40).
const wrapWidth = 40

// Say returns the full cowsay art (speech bubble + cow) for msg.
// An empty msg falls back to the default message.
func Say(msg string) string {
	msg = strings.TrimSpace(msg)
	if msg == "" {
		msg = defaultMessage
	}
	lines := wrap(msg, wrapWidth)
	return balloon(lines) + cow
}

// balloon builds the speech bubble around the given (already wrapped) lines.
func balloon(lines []string) string {
	width := maxLen(lines)

	var b strings.Builder
	// top border: " " + "_"*(width+2)
	b.WriteString(" ")
	b.WriteString(strings.Repeat("_", width+2))
	b.WriteString("\n")

	n := len(lines)
	for i, ln := range lines {
		left, right := borderChars(i, n)
		b.WriteString(left)
		b.WriteString(" ")
		b.WriteString(ln)
		b.WriteString(strings.Repeat(" ", width-len(ln)))
		b.WriteString(" ")
		b.WriteString(right)
		b.WriteString("\n")
	}

	// bottom border: " " + "-"*(width+2)
	b.WriteString(" ")
	b.WriteString(strings.Repeat("-", width+2))
	b.WriteString("\n")
	return b.String()
}

// borderChars picks the left/right bubble characters for line i of n.
// Single line uses < >. Multi-line uses / \ for the first row,
// \ / for the last row, and | | for the middle rows.
func borderChars(i, n int) (string, string) {
	if n == 1 {
		return "<", ">"
	}
	switch i {
	case 0:
		return "/", "\\"
	case n - 1:
		return "\\", "/"
	default:
		return "|", "|"
	}
}

// wrap splits text into lines no longer than width, breaking on spaces.
// Words longer than width are hard-split.
func wrap(text string, width int) []string {
	words := strings.Fields(text)
	if len(words) == 0 {
		return []string{""}
	}

	var lines []string
	cur := ""
	for _, w := range words {
		// hard-split a single word that is too long
		for len(w) > width {
			if cur != "" {
				lines = append(lines, cur)
				cur = ""
			}
			lines = append(lines, w[:width])
			w = w[width:]
		}
		if cur == "" {
			cur = w
		} else if len(cur)+1+len(w) <= width {
			cur = cur + " " + w
		} else {
			lines = append(lines, cur)
			cur = w
		}
	}
	if cur != "" {
		lines = append(lines, cur)
	}
	return lines
}

// maxLen returns the length of the longest line.
func maxLen(lines []string) int {
	m := 0
	for _, ln := range lines {
		if len(ln) > m {
			m = len(ln)
		}
	}
	return m
}

// cow is the classic happy cow, aligned under the bubble.
const cow = `        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
`

cowsay_test.gno

package cowsay

// handrolled-table: the cow's horn line "||----w |" is ASCII art, not a GFM header
// separator. This package renders no table at all, and its production source is live
// on an immutable public path, so the opt-out has to live here.

import (
	"strings"
	"testing"

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

func TestWrap(t *testing.T) {
	tests := []struct {
		name  string
		text  string
		width int
		want  []string
	}{
		{"empty", "", 10, []string{""}},
		{"single", "hello", 10, []string{"hello"}},
		{"wrap two", "aaa bbb ccc", 7, []string{"aaa bbb", "ccc"}},
		{"hard split", "abcdefghij", 4, []string{"abcd", "efgh", "ij"}},
	}
	for _, tt := range tests {
		got := wrap(tt.text, tt.width)
		uassert.Equal(t, len(tt.want), len(got), tt.name+": line count")
		for i := range tt.want {
			uassert.Equal(t, tt.want[i], got[i], tt.name+": line "+tt.want[i])
		}
		// no wrapped line exceeds width
		for _, ln := range got {
			uassert.True(t, len(ln) <= tt.width, tt.name+": within width")
		}
	}
}

func TestBorderChars(t *testing.T) {
	l, r := borderChars(0, 1)
	uassert.Equal(t, "<", l)
	uassert.Equal(t, ">", r)

	l, r = borderChars(0, 3)
	uassert.Equal(t, "/", l)
	uassert.Equal(t, "\\", r)

	l, r = borderChars(1, 3)
	uassert.Equal(t, "|", l)
	uassert.Equal(t, "|", r)

	l, r = borderChars(2, 3)
	uassert.Equal(t, "\\", l)
	uassert.Equal(t, "/", r)
}

func TestMaxLen(t *testing.T) {
	uassert.Equal(t, 0, maxLen([]string{""}))
	uassert.Equal(t, 5, maxLen([]string{"ab", "cde", "fghij"}))
}

func TestSayDefault(t *testing.T) {
	got := Say("")
	uassert.True(t, strings.Contains(got, defaultMessage), "uses default message")
	uassert.True(t, strings.Contains(got, "(oo)"), "contains cow face")
	uassert.True(t, strings.Contains(got, "< Moo! >"), "single-line bubble")
}

func TestSayWrapsMultiline(t *testing.T) {
	msg := "the quick brown fox jumps over the lazy dog many many times over"
	got := Say(msg)
	// multi-line bubbles use slashes, never the single-line angle brackets
	uassert.True(t, strings.Contains(got, "/"), "multi-line top border char")
	uassert.False(t, strings.Contains(got, "< "), "no single-line marker")
}

gnomod.toml

module = "gno.land/p/nym-moultest123/x/daily/cowsay/v0"
gno = "0.9"

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