Skip to content

Commit

Permalink
feat(parameters): output parameter to put the new maze in a file
Browse files Browse the repository at this point in the history
  • Loading branch information
anhgelus committed Jun 3, 2023
1 parent a081cf1 commit dc80296
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Generators/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package Generators

import (
"fmt"
"os"
)

type Maze struct {
Expand Down Expand Up @@ -206,6 +207,11 @@ func (s *Scheme) GenerateText() string {
return f
}

func (m *Maze) Output(path string) error {
scheme := m.ToScheme()
return os.WriteFile(path, []byte(scheme.GenerateText()), 0664)
}

// mergeCells merge two cells and their group
//
// big is the cell with the biggest group.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ This application takes two parameters:
- `-h uint` - Set the height of the maze
- `-w uint` - Set the width of the maze

It has also another parameter:
- `-o string` - Set the output file (if it does not give, no output in file)

## Technologies

- Go 1.20
Expand Down
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/msmp-core/maze-generator-cli/Generators"
"os"
"regexp"
Expand All @@ -22,8 +23,10 @@ func main() {
}
widthO := regexp.MustCompile(`-w [0-9]+`)
height0 := regexp.MustCompile(`-h [0-9]+`)
output0 := regexp.MustCompile(`-o [0-9a-zA-Z/.\-_]+`)
unWidth := widthO.FindString(cli)
unHeight := height0.FindString(cli)
unOutput := output0.FindString(cli)
if unHeight == "" || unWidth == "" {
help()
return
Expand All @@ -44,6 +47,14 @@ func main() {
panic(err)
}
m.RenderWalls()
if unOutput != "" {
output := strings.ReplaceAll(unOutput, "-o ", "")
err = m.Output(output)
if err != nil {
panic(err)
}
fmt.Printf("Successfully outputted at %s\n", output)
}
}

func help() {
Expand Down

0 comments on commit dc80296

Please sign in to comment.