/
teedate.go
86 lines (73 loc) · 1.93 KB
/
teedate.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import "os"
import flag "github.com/ogier/pflag"
import "time"
import strftime "github.com/jehiah/go-strftime"
func main() {
flagAppend := flag.BoolP("append", "a", false, "append to file")
timeFormat := flag.String("format", "%Y-%m-%d %H:%M:%S", "time format, compatible with strftime(3)")
flag.Parse()
var outStreams []*os.File = []*os.File{os.Stdout}
for i := 0; i < len(flag.Args()); i++ {
if *flagAppend {
f, _ := os.OpenFile(flag.Args()[i], os.O_WRONLY | os.O_CREATE | os.O_APPEND, 0644)
outStreams = append(outStreams, f)
} else {
f, _ := os.OpenFile(flag.Args()[i], os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0644)
outStreams = append(outStreams, f)
}
}
const kBufferSize = 1
inputBuffer := make([]byte, kBufferSize)
outputBuffer := make([]byte, kBufferSize)
var outputBufferSize int = 0
kSeparator := []byte{' '}
const kLF byte = '\x0a'
AppendBuffer := func(buffer []byte) {
var remaining int = len(buffer)
var offset int = 0
for remaining > 0 {
amount := remaining
if outputBufferSize + amount > kBufferSize {
amount = kBufferSize - outputBufferSize
}
for i := 0; i < amount; i++ {
outputBuffer[outputBufferSize] = buffer[i + offset]
outputBufferSize++
}
if outputBufferSize == kBufferSize {
for _, stream := range outStreams {
stream.Write(outputBuffer)
}
outputBufferSize = 0
}
remaining -= amount
offset += amount
}
}
PrintTime := func() {
AppendBuffer([]byte(strftime.Format(*timeFormat, time.Now())))
AppendBuffer(kSeparator)
}
var printTime bool = true
for true {
size, _ := os.Stdin.Read(inputBuffer)
if size == 0 {
break;
}
for i := 0; i < size; i++ {
if printTime {
PrintTime()
printTime = false
}
if inputBuffer[i] == kLF {
printTime = true
}
AppendBuffer(inputBuffer[i:i + 1])
}
}
for _, stream := range outStreams {
stream.Write(outputBuffer[0:outputBufferSize])
stream.Close()
}
}