-
Notifications
You must be signed in to change notification settings - Fork 12
/
init.go
56 lines (48 loc) · 1.58 KB
/
init.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
// only pure code in this file
package main
import (
"bytes"
"flag"
"strings"
"time"
)
func Init(args []string, env map[string]string) (State, []Command) {
state := *NewState(0, DefaultPhrase)
commandLine := flag.NewFlagSet(args[0], flag.ContinueOnError)
datafile := commandLine.String("f", "", "load word list from `FILE`. \"-\" for stdin.")
commandLine.BoolVar(&state.Codelines, "c", false, "treat -f FILE as lines of code")
commandLine.Bool("d", false, "demo mode for screenshot")
commandLine.Float64Var(&state.NumberProb, "n", 0, "mix in numbers with `PROBABILITY`")
err := commandLine.Parse(args[1:])
if err != nil {
if err == flag.ErrHelp {
buf := new(bytes.Buffer)
commandLine.SetOutput(buf)
commandLine.PrintDefaults()
return State{}, []Command{Exit{Status: 1, GoodbyeMessage: buf.String()}}
}
return State{}, []Command{Exit{Status: 1, GoodbyeMessage: err.Error()}}
}
commands := []Command{}
if len(commandLine.Args()) > 0 {
state.PhraseGenerator = StaticPhrase(strings.Join(commandLine.Args(), " "))
state = resetPhrase(state, false)
} else if *datafile == "" {
commands = append(commands, LoadBuiltinDictionary{})
} else {
commands = append(commands, ReadFile{
Filename: *datafile,
Success: func(data []byte) Message { return Datasource{Data: data} },
Error: PassError,
})
}
home, _ := env["HOME"]
state.Statsfile = home + "/.gotypist.stats"
return state, append(commands,
ReadFile{
Filename: state.Statsfile,
Success: func(data []byte) Message { return StatsData{Data: data} },
},
PeriodicInterrupt{250 * time.Millisecond},
)
}