Skip to content

Commit

Permalink
feat(cmd): Add --width and --height flags
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 15, 2024
1 parent e1c1abe commit 3437846
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func run(cmd *cobra.Command, _ []string) error {

g := game.New(
game.WithPattern(pat),
game.WithDimensions(400, 400),
game.WithDimensions(conf.Width, conf.Height),
game.WithPlay(conf.Play),
)

Expand Down
2 changes: 2 additions & 0 deletions docs/cli-of-life.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ cli-of-life [flags]
--completion string Output command-line completion code for the specified shell (one of: bash, zsh, fish, powershell)
-f, --file string Loads a pattern file on startup
--file-format string File format (one of: auto, rle, plaintext) (default "auto")
--height uint Board height. Will be ignored if a larger pattern is loaded. (default 400)
-h, --help help for cli-of-life
--play Play on startup
--rule-string string Rule string to use. This will be ignored if a pattern file is loaded. (default "B3/S23")
--width uint Board width. Will be ignored if a larger pattern is loaded. (default 400)
```

4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Config struct {
FileFormat string
RuleString string
Play bool
Width uint
Height uint

Completion string
}
Expand All @@ -15,5 +17,7 @@ func New() *Config {
return &Config{
FileFormat: "auto",
RuleString: pattern.GameOfLife().String(),
Width: 400,
Height: 400,
}
}
4 changes: 4 additions & 0 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (
FileFormatFlag = "file-format"
RuleStringFlag = "rule-string"
PlayFlag = "play"
WidthFlag = "width"
HeightFlag = "height"
CompletionFlag = "completion"
)

Expand All @@ -20,5 +22,7 @@ func (c *Config) RegisterFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.FileFormat, FileFormatFlag, c.FileFormat, "File format (one of: "+strings.Join(pattern.FormatStrings(), ", ")+")")
fs.StringVar(&c.RuleString, RuleStringFlag, c.RuleString, "Rule string to use. This will be ignored if a pattern file is loaded.")
fs.BoolVar(&c.Play, PlayFlag, c.Play, "Play on startup")
fs.UintVar(&c.Width, WidthFlag, c.Width, "Board width. Will be ignored if a larger pattern is loaded.")
fs.UintVar(&c.Height, HeightFlag, c.Height, "Board height. Will be ignored if a larger pattern is loaded.")
fs.StringVar(&c.Completion, CompletionFlag, c.Completion, "Output command-line completion code for the specified shell (one of: "+strings.Join(shells(), ", ")+")")
}
6 changes: 3 additions & 3 deletions internal/game/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func WithPattern(pat pattern.Pattern) Option {
}
}

func WithDimensions(width, height int) Option {
func WithDimensions(width, height uint) Option {
return func(game *Game) {
newW := max(width, game.BoardW()+100)
newH := max(height, game.BoardH()+100)
newW := max(int(width), game.BoardW()+100)
newH := max(int(height), game.BoardH()+100)
game.Resize(newW, newH, image.Pt(0, 0))
}
}
Expand Down

0 comments on commit 3437846

Please sign in to comment.