package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
)

func rgb(i int) (int, int, int) {
	var f = 0.1
	return int(math.Sin(f*float64(i)+0)*127 + 128),
		int(math.Sin(f*float64(i)+2*math.Pi/3)*127 + 128),
		int(math.Sin(f*float64(i)+4*math.Pi/3)*127 + 128)
}

func print(output []rune) {
	for j := 0; j < len(output); j++ {
		r, g, b := rgb(j)
		fmt.Printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, output[j])
	}
	fmt.Println()
}

func main() {
	info, _ := os.Stdin.Stat()
	var output []rune

	if info.Mode()&os.ModeCharDevice != 0 {
		fmt.Println("The command is intended to work with pipes.")
		fmt.Println("Usage: fortune | gorainbow")
	}

	reader := bufio.NewReader(os.Stdin)
	for {
		input, _, err := reader.ReadRune()
		if err != nil && err == io.EOF {
			break
		}
		output = append(output, input)
	}

	print(output)
}

//to run this program - first build it - go build CLI_Rainbow_Text.go
//then echo "Hello, Rainbow World!" | ./CLI_Rainbow_Text
//replace the "Hello, Rainbow World!" with any other text

//something interesting you can do is install the fortune CLI with sudo apt install fortune
// then run this same program like this -
//fortune | ./CLI_Rainbow_Text
// in addition, you can install cowsay with sudo apt install cowsay and run it like this -
// fortune | cowsay | ./CLI_Rainbow_Text
