8
8
9
9
"github.com/gdamore/tcell"
10
10
"github.com/gdamore/tcell/encoding"
11
+ "github.com/sachaos/go-life/format/rle"
11
12
"github.com/sachaos/go-life/preset"
12
13
"github.com/urfave/cli"
13
14
"log"
@@ -30,7 +31,7 @@ func initScreen() tcell.Screen {
30
31
return s
31
32
}
32
33
33
- func startGame (themes []Theme , presets []preset.Preset , themeIndex int , pattern * preset. Preset ) error {
34
+ func startGame (themes []Theme , presets []preset.Preset , themeIndex int , defaultCells [][] bool ) error {
34
35
rand .Seed (time .Now ().Unix ())
35
36
36
37
s := initScreen ()
@@ -40,15 +41,16 @@ func startGame(themes []Theme, presets []preset.Preset, themeIndex int, pattern
40
41
width , height := s .Size ()
41
42
b := NewBoard (height , width / 2 )
42
43
43
- if pattern == nil {
44
+ if len ( defaultCells ) == 0 {
44
45
b .Random ()
45
46
} else {
46
- pwidth , pheight := pattern .Size ()
47
+ pheight := len (defaultCells )
48
+ pwidth := len (defaultCells [0 ])
47
49
if pwidth > width || pheight > height {
48
50
return fmt .Errorf ("Specified pattern is too big\n " )
49
51
}
50
52
51
- b .Set ((width / 2 - pwidth )/ 2 , (height - pheight )/ 2 , pattern . Cells )
53
+ b .Set ((width / 2 - pwidth )/ 2 , (height - pheight )/ 2 , defaultCells )
52
54
}
53
55
54
56
// init ticker
@@ -119,6 +121,10 @@ func main() {
119
121
Name : "pattern" ,
120
122
Usage : "Pattern name (e.g. glider, glider-gun)" ,
121
123
},
124
+ cli.StringFlag {
125
+ Name : "file" ,
126
+ Usage : "Pattern file" ,
127
+ },
122
128
}
123
129
124
130
app .Before = func (c * cli.Context ) error {
@@ -152,6 +158,10 @@ func main() {
152
158
}
153
159
154
160
app .Action = func (c * cli.Context ) error {
161
+ if c .String ("pattern" ) != "" && c .String ("file" ) != "" {
162
+ return fmt .Errorf ("Using pattern and file option is not permitted" )
163
+ }
164
+
155
165
themeIndex := - 1
156
166
for i , theme := range themes {
157
167
if theme .Name == c .String ("theme" ) {
@@ -163,21 +173,31 @@ func main() {
163
173
return fmt .Errorf ("Invalid theme name: %s\n " , c .String ("theme" ))
164
174
}
165
175
166
- var pattern * preset. Preset
176
+ var defaultCells [][] bool
167
177
specifiedPattern := c .String ("pattern" )
168
178
if specifiedPattern != "" {
169
179
for _ , p := range presets {
170
180
if p .Name == specifiedPattern {
171
- pattern = & p
181
+ defaultCells = p . Cells
172
182
break
173
183
}
174
184
}
175
- if pattern . Name == "" {
185
+ if len ( defaultCells ) == 0 {
176
186
return fmt .Errorf ("Invalid pattern name: %s\n " , specifiedPattern )
177
187
}
178
188
}
179
189
180
- return startGame (themes , presets , themeIndex , pattern )
190
+ fileName := c .String ("file" )
191
+ if fileName != "" {
192
+ file , err := os .Open (fileName )
193
+ if err != nil {
194
+ return err
195
+ }
196
+
197
+ defaultCells = rle .Parse (file )
198
+ }
199
+
200
+ return startGame (themes , presets , themeIndex , defaultCells )
181
201
}
182
202
183
203
if err := app .Run (os .Args ); err != nil {
0 commit comments