Skip to content
Myr

words.go

This file

Path

examples/clickable/words.go

77 lines · 2.2 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package main23import (4	"math/rand"5	"strings"6	"sync"7)89const uncapitalized = " of a an and ’n’ "1011var (12	adjectives = []string{13		"a hot", "a cute", "a fresh", "a nice", "a lovely",14		"an eager", "a soft", "an expensive", "a new", "an old", "a happy",15		"a messy", "a good", "a bad", "a cheesy", "a friendly", "a free",16		"a cold", "a gorgeous", "a glamorous", "a handsome", "an exquisite",17		"a tantalizing", "a suspicious", "an american", "a wooden", "a golden",18		"a dirty", "a hairy", "a lukewarm", "a burning hot", "a shiny",19		"a rogue", "a green", "a late night", "a mass produced", "a handmade",20		"a wild", "a clean", "a rugged", "the #1", "the best", "the worst",21		"a famous", "an infamous", "a clever", "a microwaved", "a 3D printed",22		"your favorite", "your least favorite", "someone’s", "a precious",23		"a fake", "a genuine", "a bejeweled", "a good-smelling",24	}2526	nouns = []string{27		"pear", "banana", "bowl of ramen", "currywurst", "quince",28		"pie", "cake", "burrito", "sushi", "basket of fish ’n’ chips", "burger",29		"kohlrabi", "pineapple", "cantaloupe", "sausage roll", "yuzu",30		"grapefruit", "espresso shot", "sandwich", "bowl of chow mein", "lemon",31		"cup of coffee", "bottle of hot sauce", "can of beer", "glass of wine",32		"muffin", "bagel", "glass of champagne", "bottle of rosé", "pengu",33		"badger", "mango", "okonomiyaki", "meatball", "box of wine",34		"artichoke", "TUI", "linux distro", "dotfile", "weißwurst", "computer",35	}3637	shuffle     sync.Once38	nextWordMtx sync.Mutex39)4041func nextRandomWord() string {42	shuffle.Do(shuffleWords)4344	nextWordMtx.Lock()45	defer nextWordMtx.Unlock()4647	adjectives = cycle(adjectives)48	nouns = cycle(nouns)4950	return capitalize(adjectives[0] + " " + nouns[0])51}5253func shuffleWords() {54	shuf := func(x []string) {55		rand.Shuffle(len(x), func(i, j int) { x[i], x[j] = x[j], x[i] })56	}57	shuf(adjectives)58	shuf(nouns)59}6061func capitalize(s string) string {62	words := strings.Fields(s)6364	for i, w := range words {65		if i > 0 && strings.Contains(uncapitalized, " "+w+" ") {66			words[i] = w67		} else {68			words[i] = strings.Title(w)69		}70	}7172	return strings.Join(words, " ")73}7475func cycle(stack []string) []string {76	return append(stack[1:], stack[0])77}