-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
74 lines (59 loc) · 1.29 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
package main
import (
"os"
"strings"
"time"
"github.com/nsf/termbox-go"
)
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
termbox.SetInputMode(termbox.InputEsc)
if len(os.Args) == 2 && os.Args[1] == "-d" {
runDemo()
} else {
loop(os.Args, env())
}
}
func loop(args []string, env map[string]string) {
state, cmds := Init(args, env)
state = runCommands(state, cmds)
for {
render(state, time.Now())
state = reduceMessages(state, waitForEvent(), time.Now())
}
}
func runCommands(state State, commands []Command) State {
for _, command := range commands {
state = reduceMessages(state, RunCommand(command), time.Now())
}
return state
}
func reduceMessages(state State, messages []Message, now time.Time) State {
for _, message := range messages {
newState, commands := reduce(state, message, time.Now())
state = runCommands(newState, commands)
}
return state
}
func waitForEvent() []Message {
ev := termbox.PollEvent()
switch ev.Type {
case termbox.EventKey:
return []Message{ev}
case termbox.EventError:
panic(ev.Err)
case termbox.EventInterrupt:
}
return []Message{}
}
func env() map[string]string {
vars := map[string]string{}
for _, v := range os.Environ() {
pair := strings.SplitN(v, "=", 2)
vars[pair[0]] = pair[1]
}
return vars
}