-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (101 loc) · 2.49 KB
/
main.go
File metadata and controls
114 lines (101 loc) · 2.49 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)
func usage() {
fmt.Fprintf(os.Stderr, `Usage:
drip
drip DELAY
drip MIN_DELAY MAX_DELAY
drip takes lines of text via standard input and outputs them at a
limited rate. If no argument is given, a delay of 1s will be used
between lines. If one argument is given, a constant delay will be used.
If two arguments are given, a random delay between MIN_DELAY and MAX_DELAY
will be used.
Delays must be numbers, suffixed with a unit: us, ms, s, m or h.
If the delay is below 25ms, multiple lines will be printed at once due
to technical limitations. Delay ranges are not allowed here.
Examples:
yes 'Hello, world!' | drip 500ms
cat /path/to/chat/msgs | drip 10s 1m30s | send_chat_message -to paul
`)
os.Exit(1)
}
var dur1 time.Duration
var dur2 time.Duration
func init() {
if len(os.Args) > 3 {
usage()
} else if len(os.Args) == 1 {
dur1 = time.Second
} else {
var err error
dur1, err = time.ParseDuration(os.Args[1])
if err != nil || dur1 == 0 {
usage()
}
if len(os.Args) == 3 {
dur2, err = time.ParseDuration(os.Args[2])
if err != nil {
usage()
}
if dur2 <= dur1 {
fmt.Fprintf(os.Stderr, "Error: MAX_DELAY is smaller than or equal to MIN_DELAY.\n")
os.Exit(1)
}
}
}
rand.Seed(time.Now().UnixNano())
}
func main() {
// Workaround for the fact that time.Ticker does not work well for
// small durations.
var linesPerTick int64 = 1
if dur1 < 25 * time.Millisecond {
if dur2 > 0 {
fmt.Fprintln(os.Stderr, "Error: Delay ranges are not allowed, if the lowest delay is below 25ms.")
os.Exit(1)
}
factor := 1 + 25 * time.Millisecond / dur1
dur1 *= factor
linesPerTick *= int64(factor)
}
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error: could not read standard input:", err)
os.Exit(1)
}
return
}
// Print first line immediately.
fmt.Println(scanner.Text())
var ticker *time.Ticker
if dur2 > 0 {
ticker = time.NewTicker(randomDelay())
} else {
ticker = time.NewTicker(dur1)
}
defer ticker.Stop()
var i int64
for i = 0; scanner.Scan(); i++ {
if i % linesPerTick == 0 {
<-ticker.C
}
fmt.Println(scanner.Text())
if dur2 > 0 {
ticker.Reset(randomDelay())
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error: could not read standard input:", err)
os.Exit(1)
}
}
func randomDelay() time.Duration {
return time.Duration(rand.Int63n(int64(dur2-dur1))) + dur1
}