-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
50 lines (42 loc) · 918 Bytes
/
pipe.go
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
package main
//TODO: Fix the second line always having wrong offset
import (
"fmt"
"io"
"os"
"strings"
)
func rainbowPipe(vars variables) {
var str string
var eof bool = false
var i int = 0
var leftover string = ""
var lastLine int
for !eof {
str, eof = readChunk(os.Stdin)
str = leftover + str
lastLine = strings.LastIndex(str, "\n")
if lastLine == -1 {
leftover = ""
} else {
leftover = str[lastLine:]
}
fmt.Print(rainbow(str[:lastLine], i, vars))
i++
}
}
// basically a copy of io.ReadAll() without the loop
// returns most recent text and if its at EOF
func readChunk(r io.Reader) (string, bool) {
var eof bool = false
var bytes []byte = make([]byte, 1024)
if len(bytes) == cap(bytes) {
bytes = append(bytes, 0)[:len(bytes)]
}
n, err := r.Read(bytes[len(bytes):cap(bytes)])
bytes = bytes[:len(bytes)+n]
if err != nil {
eof = true
}
return string(bytes), eof
}