-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (120 loc) · 2.52 KB
/
main.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"bufio"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/yookoala/lottery/reader"
)
// min and max random number of the lottery
var max = flag.Int("max", 0, "Max random number in the lottery")
var min = flag.Int("min", -1, "Min random number in the lottery")
var filename = flag.String("file", "", "Filename of the lottery information")
func init() {
flag.Parse()
if *max <= 0 && *filename == "" {
fmt.Print("Usage: lottery -max <NUMBER> [-min <NUMBER>]\n" +
"or: lottery -file <.XLSX FILENAME>\n\n")
os.Exit(1)
}
if *filename != "" {
// file mode defaults
if *min < 0 {
*min = 2 // default skip header
}
} else {
if *min <= 0 {
*min = 1
}
}
}
func uniqueInt(in <-chan int) <-chan int {
history := make(map[int]bool)
out := make(chan int)
go func() {
for {
n := <-in
_, ok := history[n]
for ; ok; _, ok = history[n] {
n = <-in
}
history[n] = true
out <- n
}
}()
return out
}
func randInts(min, max int) <-chan int {
rand.Seed(time.Now().Unix())
out := make(chan int)
n := max - min + 1
go func() {
for {
out <- rand.Intn(n) + min
}
}()
return out
}
func padNumToStr(pad int) func(int) (string, error) {
fmtStr := fmt.Sprintf("%%%dd", pad)
return func(n int) (str string, err error) {
str = fmt.Sprintf(fmtStr, n)
return
}
}
func formatNum(fn func(int) (string, error), in <-chan int) <-chan string {
out := make(chan string)
go func() {
for {
n := <-in
str, err := fn(n)
if err != nil {
panic(err)
}
out <- str
}
}()
return out
}
func main() {
var seq <-chan string
if *filename != "" {
// file mode
c, err := reader.OpenXLSXSheet(*filename, 0)
if err != nil {
panic(err)
return
}
if *max == 0 {
*max = c.Len() // last row
}
fmter := func(n int) (str string, err error) {
strs, err := reader.ReadMulti(c, 0, 1)(n)
if err != nil {
return
}
str = strings.Join(strs, ", ")
return
}
seq = formatNum(fmter, uniqueInt(randInts(*min-1, *max-1)))
} else {
// raw number mode
l := len(fmt.Sprintf("%d", *max))
seq = formatNum(padNumToStr(l), uniqueInt(randInts(*min, *max)))
}
url := "https://github.com/yookoala/lottery"
fmt.Printf("\nSource code: %s\n\n", url)
fmt.Printf("Max: %d\n", *max)
fmt.Printf("Min: %d\n", *min)
fmt.Printf("=================\n\n")
reader := bufio.NewReader(os.Stdin)
for n := range seq {
t := time.Now().Format("15:04:05.999")
t += strings.Repeat("0", 12-len(t))
fmt.Printf("[%s] %s", t, n)
reader.ReadString('\n')
}
}