Skip to content

Instantly share code, notes, and snippets.

@shonnoronha
Created July 25, 2024 17:05
Show Gist options
  • Save shonnoronha/ce4c45b7e3ee958c25b29782cfb0e70c to your computer and use it in GitHub Desktop.
Save shonnoronha/ce4c45b7e3ee958c25b29782cfb0e70c to your computer and use it in GitHub Desktop.
A simple lolcat implementation in golang.
package main
import (
"bufio"
"fmt"
"io"
"math"
"math/rand"
"os"
"time"
)
func rgb(i int, trigFunc func (float64) float64, f float64) (int, int, int) {
return int(trigFunc(f*float64(i)+0)*127 + 128),
int(trigFunc(f*float64(i)+2*math.Pi/3)*127 + 128),
int(trigFunc(f*float64(i)+4*math.Pi/3)*127 + 128)
}
func printRGB(textContent []rune, f float64){
for i := 0; i < len(textContent); i++ {
r, g, b := rgb(i, math.Sin, f)
fmt.Print("\033[38;2;", r, ";", g, ";", b, "m", string(textContent[i]), "\033[0m")
}
}
func main() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
info, _ := os.Stdin.Stat()
var input []rune
if info.Mode()&os.ModeCharDevice != 0 {
fmt.Println("The command is intended to work with pipes.")
fmt.Println("Usage: fortune | gocat")
return
}
reader := bufio.NewReader(os.Stdin)
for {
content , _, err := reader.ReadRune()
if err != nil || err == io.EOF {
break
}
input = append(input, content)
}
// frequency factor
var f = r.Float64()
printRGB(input, f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment