Skip to content

Commit

Permalink
refactor(svg): use interface for better testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Mar 8, 2022
1 parent 62410db commit b5f4831
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
15 changes: 11 additions & 4 deletions cmd/termsvg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,28 @@ func (cmd *Cmd) Run() error {
}

func export(input, output string) error {
file, err := os.ReadFile(input)
inputFile, err := os.ReadFile(input)
if err != nil {
return err
}

if output == "" {
output = input
output = input + ".svg"
}

cast, err := asciicast.Unmarshal(file)
cast, err := asciicast.Unmarshal(inputFile)
if err != nil {
return err
}

svg.Export(*cast, output)
outputFile, err := os.Create(output)
if err != nil {
return err
}

defer outputFile.Close()

svg.Export(*cast, outputFile)

return nil
}
18 changes: 8 additions & 10 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package svg

import (
"fmt"
"os"
"io"
"strings"

svg "github.com/ajstarks/svgo"
Expand All @@ -27,6 +27,10 @@ type Canvas struct {
bgColors map[string]byte
}

type Output interface {
io.Writer
}

func createSVG(svg *svg.SVG, cast asciicast.Cast) {
canvas := &Canvas{SVG: svg, Cast: cast}
canvas.width = cast.Header.Width * colWidth
Expand Down Expand Up @@ -125,16 +129,10 @@ func (c *Canvas) createFrames() {
}
}

func Export(input asciicast.Cast, output string) {
file, err := os.Create(fmt.Sprintf("%s.svg", output))
if err != nil {
panic(err)
}

defer file.Close()
input.Compress()
func Export(input asciicast.Cast, output Output) {
input.Compress() // to reduce the number of frames

createSVG(svg.New(file), input)
createSVG(svg.New(output), input)
}

func (c *Canvas) addBG(bg vt10x.Color) {
Expand Down

0 comments on commit b5f4831

Please sign in to comment.