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

color.gno

package printfdebugging

// consts copied from https://github.com/fatih/color/blob/main/color.go

// Attribute defines a single SGR Code
type Attribute int

const Escape = "\x1b"

// Base attributes
const (
	Reset Attribute = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

const (
	ResetBold Attribute = iota + 22
	ResetItalic
	ResetUnderline
	ResetBlinking
	_
	ResetReversed
	ResetConcealed
	ResetCrossedOut
)

// Foreground text colors
const (
	FgBlack Attribute = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

// Foreground Hi-Intensity text colors
const (
	FgHiBlack Attribute = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

// Background text colors
const (
	BgBlack Attribute = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

// Background Hi-Intensity text colors
const (
	BgHiBlack Attribute = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

gnomod.toml

module = "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/printfdebugging/v0"
gno = "0.9"

[addpkg]
  creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv"
  height = 751252
  max_deposit = "5000000ugnot"

printfdebugging.gno

// this package is a joke... or not.
package printfdebugging

import (
	"strings"

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

func BigRedLine(args ...string) {
	println(ufmt.Sprintf("%s[%dm####################################%s[%dm %s",
		Escape, int(BgRed), Escape, int(Reset),
		strings.Join(args, " "),
	))
}

func Success() {
	println("   \033[31mS\033[33mU\033[32mC\033[36mC\033[34mE\033[35mS\033[31mS\033[0m   ")
}

printfdebugging_test.gno

package printfdebugging

import (
	"testing"

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

// The SGR codes are transcribed from fatih/color. They are plain `iota` blocks,
// so a single inserted or removed line silently shifts every constant below it
// and the terminal renders the wrong colour. Pin the boundaries and the offsets.

func TestBaseAttributes(t *testing.T) {
	cases := []struct {
		name string
		got  Attribute
		want int
	}{
		{"Reset", Reset, 0},
		{"Bold", Bold, 1},
		{"Faint", Faint, 2},
		{"Italic", Italic, 3},
		{"Underline", Underline, 4},
		{"BlinkSlow", BlinkSlow, 5},
		{"BlinkRapid", BlinkRapid, 6},
		{"ReverseVideo", ReverseVideo, 7},
		{"Concealed", Concealed, 8},
		{"CrossedOut", CrossedOut, 9},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, int(c.got), c.name)
	}
}

// TestResetAttributes covers the block with a deliberate `_` gap at 26.
func TestResetAttributes(t *testing.T) {
	cases := []struct {
		name string
		got  Attribute
		want int
	}{
		{"ResetBold", ResetBold, 22},
		{"ResetItalic", ResetItalic, 23},
		{"ResetUnderline", ResetUnderline, 24},
		{"ResetBlinking", ResetBlinking, 25},
		// 26 is skipped with `_`
		{"ResetReversed", ResetReversed, 27},
		{"ResetConcealed", ResetConcealed, 28},
		{"ResetCrossedOut", ResetCrossedOut, 29},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, int(c.got), c.name)
	}
}

func TestForegroundColors(t *testing.T) {
	cases := []struct {
		name string
		got  Attribute
		want int
	}{
		{"FgBlack", FgBlack, 30},
		{"FgRed", FgRed, 31},
		{"FgGreen", FgGreen, 32},
		{"FgYellow", FgYellow, 33},
		{"FgBlue", FgBlue, 34},
		{"FgMagenta", FgMagenta, 35},
		{"FgCyan", FgCyan, 36},
		{"FgWhite", FgWhite, 37},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, int(c.got), c.name)
	}
}

func TestBackgroundColors(t *testing.T) {
	cases := []struct {
		name string
		got  Attribute
		want int
	}{
		{"BgBlack", BgBlack, 40},
		{"BgRed", BgRed, 41},
		{"BgWhite", BgWhite, 47},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, int(c.got), c.name)
	}
}

func TestHiIntensityColors(t *testing.T) {
	cases := []struct {
		name string
		got  Attribute
		want int
	}{
		{"FgHiBlack", FgHiBlack, 90},
		{"FgHiWhite", FgHiWhite, 97},
		{"BgHiBlack", BgHiBlack, 100},
		{"BgHiWhite", BgHiWhite, 107},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, int(c.got), c.name)
	}
}

func TestEscape(t *testing.T) {
	uassert.Equal(t, "\x1b", Escape)
	uassert.Equal(t, 1, len(Escape))
}

// TestOutputHelpersDoNotPanic exercises the two exported helpers. They only
// println, so there is no return value to assert — this pins that they build
// and run (BigRedLine formats Escape/Attribute through ufmt).
func TestOutputHelpersDoNotPanic(t *testing.T) {
	BigRedLine()
	BigRedLine("one")
	BigRedLine("several", "args", "joined")
	Success()
}