/
main.go
157 lines (135 loc) · 3.49 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/peterh/liner"
)
const (
version = `0.1.0-alpha`
versionDate = `2015-02-20`
historyFilename = "/tmp/.galapagos_liner_history"
)
var (
// TODO add functionality for these missing commands
commandCompletions = []string{"q", "p", "h"}
// TODO wordCompletions = []string{"def", "update!"}
)
func configureLiner(linerState *liner.State) {
linerState.SetCtrlCAborts(true)
linerState.SetCompleter(func(line string) (c []string) {
for _, n := range commandCompletions {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
/* TODO
// WordCompleter takes the currently edited line with the cursor position and
// returns the completion candidates for the partial word to be completed. If
// the line is "Hello, wo!!!" and the cursor is before the first '!',
// ("Hello, wo!!!", 9) is passed to the completer which may returns
// ("Hello, ", {"world", "Word"}, "!!!") to have "Hello, world!!!".
linerState.SetWordCompleter(func(line string, pos int) (head string, completions []string, tail string) {
for _, n := range wordCompletions {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
*/
}
func openLinerHistory(line *liner.State) {
if f, err := os.Open(historyFilename); err == nil {
line.ReadHistory(f)
f.Close()
}
}
func writeLinerHistory(line *liner.State) {
if f, err := os.Create(historyFilename); err != nil {
log.Print("Error writing history file: ", err)
} else {
line.WriteHistory(f)
f.Close()
}
}
func main() {
showHelp := flag.Bool("help", false, "show the help")
flag.Parse()
if showHelp != nil && *showHelp {
fmt.Printf("Usage of galapagos:\n")
flag.PrintDefaults()
return
}
// Setup liner
line := liner.NewLiner()
defer line.Close()
openLinerHistory(line)
configureLiner(line)
// Initialize
fmt.Printf("Galapagos %s (%s)\n", version, versionDate)
fmt.Printf("(Press Ctrl+C or q to exit, h for help)\n\n")
seedArena()
// REPL
for {
input, err := line.Prompt("> ")
if err != nil {
if err.Error() == "prompt aborted" {
fmt.Printf("Quiting...\n")
} else {
fmt.Printf("Prompt error: %s\n", err)
}
return
}
line.AppendHistory(input)
writeLinerHistory(line)
switch {
case strings.HasPrefix(input, "o"):
i, err := strconv.Atoi(input[1:])
if err == nil {
fmt.Println("Fetching original competitor with strainId", i)
fmt.Println(originalArena[i])
} else {
fmt.Println("Error reading number")
}
case input == "q":
return
case input == "s":
fmt.Println(stats())
case input == "r":
seedArena()
fmt.Println("Arena has been reseeded")
case input == "h":
fmt.Println("COMMAND RESULT")
fmt.Println("------- ------------------------")
fmt.Println("<enter> run 1 round + show stats")
fmt.Println("# run # rounds + show stats")
fmt.Println("o# show original competitor with strainId")
fmt.Println("s show stats")
fmt.Println("r reseed arena from scratch")
fmt.Println("q quit")
default:
i, err := strconv.Atoi(input)
if err == nil {
for round := 0; round < i; round++ {
runRound(round)
// Show stats for every tenth round, starting with one
if round%10 == 1 {
fmt.Println("ROUND #", round)
fmt.Println(stats())
}
pruneAndReproduce()
}
} else {
runRound(1)
fmt.Println(stats())
pruneAndReproduce()
}
}
}
}