Skip to content

Commit

Permalink
exit with error if both --color and --image flags are specified
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Mar 23, 2024
1 parent 94de479 commit c006a8a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
6 changes: 2 additions & 4 deletions cmd/maze/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ func action(ctx *cli.Context) error {
case "":
maze.Print(config.Output, config.Format)
case "png":
maze.PrintPNG(config.Output, config.Format, config.Scale)
maze.PrintPNG(config.Output, config.Scale)
case "svg":
maze.PrintSVG(config.Output, config.Format, config.Scale)
default:
return fmt.Errorf("unsupported image format: %s", config.Image)
maze.PrintSVG(config.Output, config.Scale)
}
}
return nil
Expand Down
14 changes: 12 additions & 2 deletions cmd/maze/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"fmt"
"io"
"os"
"strconv"
Expand Down Expand Up @@ -108,10 +109,19 @@ func makeConfig(ctx *cli.Context) (*Config, []error) {
}

image := ctx.GlobalString("image")
if image != "" {
switch image {
case "":
case "png":
if file, ok := output.(*os.File); ok && isatty.IsTerminal(file.Fd()) {
errs = append(errs, errors.New("cannot write image data into the terminal\nuse -output flag"))
errs = append(errs, errors.New("cannot write PNG data to the terminal\nuse --output flag or redirect the output to a file"))
}
fallthrough
case "svg":
if format != maze.Default {
errs = append(errs, errors.New("cannot use --color and --image flags together"))
}
default:
errs = append(errs, fmt.Errorf("unsupported image format: %s", image))
}

scale := ctx.GlobalInt("scale")
Expand Down
12 changes: 6 additions & 6 deletions maze.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ func plot(img *image.RGBA, x, y, scale int, c color.Color) {
}

// PrintPNG outputs the maze to the IO writer as PNG image
func (maze *Maze) PrintPNG(writer io.Writer, format *Format, scale int) {
func (maze *Maze) PrintPNG(writer io.Writer, scale int) {
var sb strings.Builder
maze.Print(&sb, format)
maze.Print(&sb, Default)
lines := strings.Split(strings.TrimSpace(sb.String()), "\n")
for i, line := range lines {
lines[i] = strings.TrimSpace(line)
Expand Down Expand Up @@ -400,17 +400,17 @@ func (maze *Maze) PrintPNG(writer io.Writer, format *Format, scale int) {
}

// PrintSVG outputs the maze to the IO writer as SVG image
func (maze *Maze) PrintSVG(writer io.Writer, format *Format, scale int) {
func (maze *Maze) PrintSVG(writer io.Writer, scale int) {
var sb strings.Builder
maze.Print(&sb, format)
maze.Print(&sb, Default)
lines := strings.Split(strings.TrimSpace(sb.String()), "\n")
for i, line := range lines {
lines[i] = strings.TrimSpace(line)
}
width := len(lines[0]) / 2
height := len(lines)
fmt.Fprintf(writer, "<svg viewBox=\"0 0 %d %d\" xmlns=\"http://www.w3.org/2000/svg\">\n", width*scale, height*scale)
fmt.Fprintf(writer, "<rect width=\"%d\" height=\" %d\" fill=\"white\" />\n", width*scale, height*scale)
fmt.Fprintf(writer, `<svg viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg">`+"\n", width*scale, height*scale)
fmt.Fprintf(writer, `<rect width="%d" height="%d" fill="white" />`+"\n", width*scale, height*scale)
for y := 0; y < height; y++ {
if y >= len(lines) {
continue
Expand Down

0 comments on commit c006a8a

Please sign in to comment.