Skip to content

Commit

Permalink
Add params for custom bg and text colors in export
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefMarlin committed Mar 9, 2023
1 parent 7776b19 commit d0c3e62
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
16 changes: 9 additions & 7 deletions cmd/termsvg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
)

type Cmd struct {
File string `arg:"" type:"existingfile" help:"asciicast file to export"`
Output string `optional:"" short:"o" type:"path" help:"where to save the file. Defaults to <input_file>.svg"`
Mini bool `name:"minify" optional:"" short:"m" help:"minify output file. May be slower"`
File string `arg:"" type:"existingfile" help:"asciicast file to export"`
Output string `optional:"" short:"o" type:"path" help:"where to save the file. Defaults to <input_file>.svg"`
Mini bool `name:"minify" optional:"" short:"m" help:"minify output file. May be slower"`
BackgroundColor string `optional:"" short:"b" help:"background color in hexadecimal format (e.g. #FFFFFF)"`
TextColor string `optional:"" short:"t" help:"text color in hexadecimal format (e.g. #000000)"`
}

func (cmd *Cmd) Run() error {
Expand All @@ -23,7 +25,7 @@ func (cmd *Cmd) Run() error {
output = cmd.File + ".svg"
}

err := export(cmd.File, output, cmd.Mini)
err := export(cmd.File, output, cmd.Mini, cmd.BackgroundColor, cmd.TextColor)
if err != nil {
return err
}
Expand All @@ -33,7 +35,7 @@ func (cmd *Cmd) Run() error {
return nil
}

func export(input, output string, mini bool) error {
func export(input, output string, mini bool, bgColor, textColor string) error {
inputFile, err := os.ReadFile(input)
if err != nil {
return err
Expand All @@ -52,7 +54,7 @@ func export(input, output string, mini bool) error {

if mini {
out := new(bytes.Buffer)
svg.Export(*cast, out)
svg.Export(*cast, out, bgColor, textColor)

m := minify.New()
m.AddFunc("image/svg+xml", msvg.Minify)
Expand All @@ -67,7 +69,7 @@ func export(input, output string, mini bool) error {
return err
}
} else {
svg.Export(*cast, outputFile)
svg.Export(*cast, outputFile, bgColor, textColor)
}

return nil
Expand Down
28 changes: 25 additions & 3 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const (
headerSize = 3
)

// If user passed custom background and text colors, use them
var (
foregroundColorOverride = ""
backgroundColorOverride = ""
)

type Canvas struct {
*svg.SVG
asciicast.Cast
Expand All @@ -33,7 +39,11 @@ type Output interface {
io.Writer
}

func Export(input asciicast.Cast, output Output) {
func Export(input asciicast.Cast, output Output, bgColor, textColor string) {
// Set the custom foreground and background colors
foregroundColorOverride = textColor
backgroundColorOverride = bgColor

input.Compress() // to reduce the number of frames

createCanvas(svg.New(output), input)
Expand Down Expand Up @@ -99,7 +109,13 @@ func (c *Canvas) createWindow() {
buttonColors := [3]string{"#ff5f58", "#ffbd2e", "#18c132"}

c.Start(c.paddedWidth(), c.paddedHeight())
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:#282d35")

// If the user has specified a background color, use that instead of the default
if backgroundColorOverride != "" {
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:"+backgroundColorOverride)
} else {
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:#282d35")
}

for i := range buttonColors {
c.Circle((i*(padding+buttonRadius/2))+padding, padding, buttonRadius, fmt.Sprintf("fill:%s", buttonColors[i]))
Expand All @@ -121,13 +137,19 @@ func (c *Canvas) addStyles() {
"font-size": "20px",
}.String())

// Foreground color gets set here
colors := css.Blocks{}
for color, class := range c.colors {
colors = append(colors, css.Block{Selector: fmt.Sprintf(".%s", class), Rules: css.Rules{"fill": color}})
}

styles := generateKeyframes(c.Cast, int32(c.paddedWidth()))
styles += colors.String()
// If custom colors have been provided, use them instead
if foregroundColorOverride != "" {
styles += fmt.Sprintf(".a{fill:%s}", foregroundColorOverride)
} else {
styles += colors.String()
}
c.Style("text/css", styles)
c.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize))
}
Expand Down
6 changes: 4 additions & 2 deletions internal/svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestExport(t *testing.T) {

var output bytes.Buffer

svg.Export(*cast, &output)
// Pass empty override bg and text colors
svg.Export(*cast, &output, "", "")

g := goldie.New(t)
g.Assert(t, "TestExportOutput", output.Bytes())
Expand All @@ -37,6 +38,7 @@ func BenchmarkExport(b *testing.B) {
for i := 0; i < b.N; i++ {
var output bytes.Buffer

svg.Export(*cast, &output)
// Pass empty override bg and text colors
svg.Export(*cast, &output, "", "")
}
}

0 comments on commit d0c3e62

Please sign in to comment.