Skip to content

Commit

Permalink
feat: v1
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 18, 2019
1 parent 0242ccf commit c207a3d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
24 changes: 24 additions & 0 deletions ascii2svg/ascii2svg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ascii2svg

import (
"fmt"
"html"
)

const template = `<?xml version="1.0" encoding="UTF-8"?>
<svg width="800px" height="600px" viewBox="0 0 800 600" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g>
<rect x="0" y="0" width="800" height="600" fill="black" stroke="white"></rect>
<foreignObject x="15" y="5" width="770" height="590">
<div xmlns="http://www.w3.org/1999/xhtml" style="width:800px; height:600px; overflow-y:auto; color: white; font-size: 17px;">
<pre>%s</pre>
</div>
</foreignObject>
</g>
</svg>`

func FromString(input string) (string, error) {
escapedInput := html.EscapeString(input)
out := fmt.Sprintf(template, escapedInput)
return out, nil
}
1 change: 1 addition & 0 deletions ascii2svg/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package ascii2svg // import "moul.io/ascii2svg/ascii2svg"
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module moul.io/ascii2svg

go 1.12

require gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717 h1:OvXt/p4cdwNl+mwcWMq/AxaKFkhdxcjx+tx+qf4EOvY=
gopkg.in/urfave/cli.v2 v2.0.0-20190806201727-b62605953717/go.mod h1:cKXr3E0k4aosgycml1b5z33BVV6hai1Kh7uDgFOkbcs=
37 changes: 35 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
package main // import "moul.io/ascii2svg"

import "fmt"
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"

cli "gopkg.in/urfave/cli.v2"
"moul.io/ascii2svg/ascii2svg"
)

func main() {
fmt.Println("Hello World!")
app := cli.App{
Flags: []cli.Flag{}, // width, height, bgcolor, fgcolor
Action: run,
}
if err := app.Run(os.Args); err != nil {
log.Printf("error: %v\n", err)
os.Exit(1)
}
}

func run(c *cli.Context) error {
input := strings.Join(c.Args().Slice(), " ")
if input == "" {
inBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
input = string(inBytes)
}
out, err := ascii2svg.FromString(input)
if err != nil {
return err
}
fmt.Println(out)
return nil
}
16 changes: 15 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package main

import "os"

func Example() {
os.Args = []string{"hello", "world"}
main()
// Output: Hello World!
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <svg width="800px" height="600px" viewBox="0 0 800 600" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
// <g>
// <rect x="0" y="0" width="800" height="600" fill="black" stroke="white"></rect>
// <foreignObject x="15" y="5" width="770" height="590">
// <div xmlns="http://www.w3.org/1999/xhtml" style="width:800px; height:600px; overflow-y:auto; color: white; font-size: 17px;">
// <pre>world</pre>
// </div>
// </foreignObject>
// </g>
// </svg>
}

0 comments on commit c207a3d

Please sign in to comment.