-
Notifications
You must be signed in to change notification settings - Fork 12
/
command.go
151 lines (120 loc) · 2.65 KB
/
command.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
// contains all commands (side effects)
package main
import (
_ "embed"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/nsf/termbox-go"
)
//go:embed dictionary
var builtinDictionary []byte
type Message interface{}
type Command interface{}
var Noop = []Command{}
type Interrupt struct {
Delay time.Duration
}
type PeriodicInterrupt struct {
Period time.Duration
}
type ReadFile struct {
Filename string
Success func([]byte) Message
Error func(error) Message
}
type AppendFile struct {
Filename string
Data []byte
Success func() Message
Error func(error) Message
}
type Exit struct {
Status int
GoodbyeMessage string
}
type LoadBuiltinDictionary struct{}
func PassError(err error) Message {
return err
}
func RunCommand(cmd Command) []Message {
switch c := cmd.(type) {
case ReadFile:
return readFile(c.Filename, c.Success, c.Error)
case AppendFile:
return appendFile(c.Filename, c.Data, c.Success, c.Error)
case Interrupt:
return interrupt(c.Delay)
case PeriodicInterrupt:
return periodicInterrupt(c.Period)
case Exit:
return exit(c.Status, c.GoodbyeMessage)
case LoadBuiltinDictionary:
return loadBuiltinDictionary()
}
exit(1, fmt.Sprintf("Cannot handle command of type %T", cmd))
return noMessages
}
var noMessages = []Message{}
func appendFile(filename string, data []byte, success func() Message, error func(error) Message) []Message {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
if error != nil {
return []Message{error(err)}
}
return noMessages
}
defer f.Close()
if _, err := f.Write(data); err != nil {
if error != nil {
return []Message{error(err)}
}
return noMessages
}
if success == nil {
return noMessages
}
return []Message{success()}
}
func readFile(filename string, success func([]byte) Message, errorFunc func(error) Message) []Message {
var (
content []byte
err error
)
if filename == "-" {
content, err = ioutil.ReadAll(os.Stdin)
} else {
content, err = ioutil.ReadFile(filename)
}
if err != nil {
if errorFunc != nil {
return []Message{errorFunc(err)}
}
return noMessages
}
return []Message{success(content)}
}
func loadBuiltinDictionary() []Message {
return []Message{Datasource{Data: builtinDictionary}}
}
func periodicInterrupt(d time.Duration) []Message {
go func() {
for range time.Tick(d) {
termbox.Interrupt()
}
}()
return noMessages
}
func interrupt(d time.Duration) []Message {
time.AfterFunc(d, termbox.Interrupt)
return noMessages
}
func exit(status int, message string) []Message {
termbox.Close()
if message != "" {
fmt.Println(message)
}
os.Exit(status)
return noMessages
}