From 13d7d94f7ee87a96aad63f896f185ec3f2e2fd18 Mon Sep 17 00:00:00 2001 From: "Christian Weichel (Chris)" Date: Thu, 7 Mar 2024 17:27:48 +0000 Subject: [PATCH 1/2] add SVG export --- cmd/maze/action.go | 2 ++ cmd/maze/config.go | 3 +++ cmd/maze/flag.go | 4 ++++ maze.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/cmd/maze/action.go b/cmd/maze/action.go index 3cd3785..59b96b4 100644 --- a/cmd/maze/action.go +++ b/cmd/maze/action.go @@ -40,6 +40,8 @@ func action(ctx *cli.Context) error { } 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 { maze.Print(config.Output, config.Format) } diff --git a/cmd/maze/config.go b/cmd/maze/config.go index f85fe41..be6572a 100644 --- a/cmd/maze/config.go +++ b/cmd/maze/config.go @@ -23,6 +23,7 @@ type Config struct { Goal *maze.Point Interactive bool Image bool + SVG bool Scale int Solution bool Format *maze.Format @@ -114,6 +115,7 @@ func makeConfig(ctx *cli.Context) (*Config, []error) { } } + svg := ctx.GlobalBool("svg") scale := ctx.GlobalInt("scale") seed := int64(ctx.GlobalInt("seed")) @@ -132,6 +134,7 @@ func makeConfig(ctx *cli.Context) (*Config, []error) { Goal: goal, Interactive: interactive, Image: image, + SVG: svg, Scale: scale, Solution: solution, Format: format, diff --git a/cmd/maze/flag.go b/cmd/maze/flag.go index 53505e6..2f025fa 100644 --- a/cmd/maze/flag.go +++ b/cmd/maze/flag.go @@ -39,6 +39,10 @@ var flags = []cli.Flag{ Name: "image", Usage: "Generate image", }, + cli.BoolFlag{ + Name: "svg", + Usage: "Generate svg", + }, cli.IntFlag{ Name: "scale", Usage: "Scale of the image", diff --git a/maze.go b/maze.go index 2400221..962da5f 100644 --- a/maze.go +++ b/maze.go @@ -361,6 +361,41 @@ func (maze *Maze) PrintImage(writer io.Writer, format *Format, scale int) { png.Encode(writer, img) } +func (maze *Maze) PrintSVG(writer io.Writer, format *Format, scale int) { + var sb strings.Builder + maze.Print(&sb, format) + 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, "\n", width*scale, height*scale) + fmt.Fprintf(writer, "\n", width*scale, height*scale) + for y := 0; y < height; y++ { + if y >= len(lines) { + continue + } + for x := 0; x < width; x++ { + if x*2 >= len(lines[y]) { + continue + } + switch lines[y][x*2 : x*2+2] { + case "##": + fmt.Fprintf(writer, ``+"\n", x*scale, y*scale, scale, scale) + case "::": + fmt.Fprintf(writer, ``+"\n", x*scale, y*scale, scale, scale) + case "S ", " S", "S:", ":S": + fmt.Fprintf(writer, ``+"\n", x*scale, y*scale, scale, scale) + case "G ", " G", "G:", ":G": + fmt.Fprintf(writer, ``+"\n", x*scale, y*scale, scale, scale) + default: + } + } + } + fmt.Fprintln(writer, "") +} + // Print out the maze to the IO writer func (maze *Maze) Print(writer io.Writer, format *Format) { fmt.Fprint(writer, maze.String(format)) From 37a46fe54f2d02eed76be89df66b6965543cba38 Mon Sep 17 00:00:00 2001 From: itchyny Date: Sat, 23 Mar 2024 09:39:12 +0900 Subject: [PATCH 2/2] change --image flag to accept an image format png or svg --- cmd/maze/action.go | 13 ++++++++----- cmd/maze/config.go | 11 ++++------- cmd/maze/flag.go | 8 ++------ maze.go | 5 +++-- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/cmd/maze/action.go b/cmd/maze/action.go index 59b96b4..bf498a4 100644 --- a/cmd/maze/action.go +++ b/cmd/maze/action.go @@ -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 diff --git a/cmd/maze/config.go b/cmd/maze/config.go index be6572a..9843d0b 100644 --- a/cmd/maze/config.go +++ b/cmd/maze/config.go @@ -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 @@ -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")) @@ -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, diff --git a/cmd/maze/flag.go b/cmd/maze/flag.go index 2f025fa..3bc4036 100644 --- a/cmd/maze/flag.go +++ b/cmd/maze/flag.go @@ -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", diff --git a/maze.go b/maze.go index 962da5f..68e7aa6 100644 --- a/maze.go +++ b/maze.go @@ -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") @@ -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)