summarylogtreecommitdiffstats
path: root/main.go
blob: 056036e29067a78a45d94a18081a4b91d3ed1e98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"math/rand"
	"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 main() {
	info, _ := os.Stdin.Stat()
	args := os.Args[1:]

	if info.Mode()&os.ModeCharDevice != 0 && len(args) != 1 {
		fmt.Println("The command is intended to work with pipes or a file.")
		fmt.Println("Usage: cat your-file.txt | rbcat")
		fmt.Println("Or: rbcat your-file.txt")
		os.Exit(1)
	}

	if len(args) == 1 {
		file, err := os.Open(args[0])
		if err != nil {
			fmt.Println(err)
			return
		}
		defer file.Close()

		r := bufio.NewReader(file)
		j := rand.Intn(999999)
		for {
			input, _, err := r.ReadRune()
			if err != nil && err == io.EOF {
				break
			}
			r, g, b := rgb(j)
			fmt.Printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, input)
			j++
		}
	} else {
		r := bufio.NewReader(os.Stdin)
		j := rand.Intn(999999)
		for {
			input, _, err := r.ReadRune()
			if err != nil && err == io.EOF {
				break
			}
			r, g, b := rgb(j)
			fmt.Printf("\033[38;2;%d;%d;%dm%c\033[0m", r, g, b, input)
			j++
		}
	}
}