Skip to content

Commit

Permalink
change --image flag to accept an image format png or svg
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Mar 23, 2024
1 parent 13d7d94 commit 37a46fe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
13 changes: 8 additions & 5 deletions cmd/maze/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ func action(ctx *cli.Context) error {
defer termbox.Close()
interactive(maze, config.Format)
} else {
if config.Image {
maze.PrintImage(config.Output, config.Format, config.Scale)
} else if config.SVG {
maze.PrintSVG(config.Output, config.Format, config.Scale)
} else {
switch config.Image {
case "":
maze.Print(config.Output, config.Format)
case "png":
maze.PrintPNG(config.Output, config.Format, config.Scale)
case "svg":
maze.PrintSVG(config.Output, config.Format, config.Scale)
default:
return fmt.Errorf("unsupported image format: %s", config.Image)
}
}
return nil
Expand Down
11 changes: 4 additions & 7 deletions cmd/maze/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type Config struct {
Start *maze.Point
Goal *maze.Point
Interactive bool
Image bool
SVG bool
Image string
Scale int
Solution bool
Format *maze.Format
Expand Down Expand Up @@ -108,14 +107,13 @@ func makeConfig(ctx *cli.Context) (*Config, []error) {
}
}

image := ctx.GlobalBool("image")
if image {
image := ctx.GlobalString("image")
if image != "" {
if file, ok := output.(*os.File); ok && isatty.IsTerminal(file.Fd()) {
errs = append(errs, errors.New("cannot write binary data into the terminal\nuse -output flag"))
errs = append(errs, errors.New("cannot write image data into the terminal\nuse -output flag"))
}
}

svg := ctx.GlobalBool("svg")
scale := ctx.GlobalInt("scale")

seed := int64(ctx.GlobalInt("seed"))
Expand All @@ -134,7 +132,6 @@ func makeConfig(ctx *cli.Context) (*Config, []error) {
Goal: goal,
Interactive: interactive,
Image: image,
SVG: svg,
Scale: scale,
Solution: solution,
Format: format,
Expand Down
8 changes: 2 additions & 6 deletions cmd/maze/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ var flags = []cli.Flag{
Name: "output, o",
Usage: "Output file name",
},
cli.BoolFlag{
cli.StringFlag{
Name: "image",
Usage: "Generate image",
},
cli.BoolFlag{
Name: "svg",
Usage: "Generate svg",
Usage: "Generate image, `png` or `svg`",
},
cli.IntFlag{
Name: "scale",
Expand Down
5 changes: 3 additions & 2 deletions maze.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ func plot(img *image.RGBA, x, y, scale int, c color.Color) {
}
}

// PrintImage outputs the maze to the IO writer as PNG image
func (maze *Maze) PrintImage(writer io.Writer, format *Format, scale int) {
// PrintPNG outputs the maze to the IO writer as PNG image
func (maze *Maze) PrintPNG(writer io.Writer, format *Format, scale int) {
var sb strings.Builder
maze.Print(&sb, format)
lines := strings.Split(strings.TrimSpace(sb.String()), "\n")
Expand Down Expand Up @@ -361,6 +361,7 @@ func (maze *Maze) PrintImage(writer io.Writer, format *Format, scale int) {
png.Encode(writer, img)
}

// PrintSVG outputs the maze to the IO writer as SVG image
func (maze *Maze) PrintSVG(writer io.Writer, format *Format, scale int) {
var sb strings.Builder
maze.Print(&sb, format)
Expand Down

0 comments on commit 37a46fe

Please sign in to comment.