diff --git a/cmd/termsvg/export/export.go b/cmd/termsvg/export/export.go index 12a02a5..312bed9 100644 --- a/cmd/termsvg/export/export.go +++ b/cmd/termsvg/export/export.go @@ -15,6 +15,7 @@ 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 .svg"` Mini bool `name:"minify" optional:"" short:"m" help:"minify output file. May be slower"` + NoWindow bool `name:"nowindow" optional:"" short:"n" help:"don't render terminal window in svg"` 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)"` } @@ -25,7 +26,7 @@ func (cmd *Cmd) Run() error { output = cmd.File + ".svg" } - err := export(cmd.File, output, cmd.Mini, cmd.BackgroundColor, cmd.TextColor) + err := export(cmd.File, output, cmd.Mini, cmd.BackgroundColor, cmd.TextColor, cmd.NoWindow) if err != nil { return err } @@ -35,7 +36,7 @@ func (cmd *Cmd) Run() error { return nil } -func export(input, output string, mini bool, bgColor, textColor string) error { +func export(input, output string, mini bool, bgColor, textColor string, no_window bool) error { inputFile, err := os.ReadFile(input) if err != nil { return err @@ -54,7 +55,7 @@ func export(input, output string, mini bool, bgColor, textColor string) error { if mini { out := new(bytes.Buffer) - svg.Export(*cast, out, bgColor, textColor) + svg.Export(*cast, out, bgColor, textColor, no_window) m := minify.New() m.AddFunc("image/svg+xml", msvg.Minify) @@ -69,7 +70,7 @@ func export(input, output string, mini bool, bgColor, textColor string) error { return err } } else { - svg.Export(*cast, outputFile, bgColor, textColor) + svg.Export(*cast, outputFile, bgColor, textColor, no_window) } return nil diff --git a/internal/svg/svg.go b/internal/svg/svg.go index fd92904..0afa967 100644 --- a/internal/svg/svg.go +++ b/internal/svg/svg.go @@ -15,7 +15,7 @@ import ( const ( rowHeight = 25 - colWidth = 11 + colWidth = 12 padding = 20 headerSize = 3 ) @@ -39,23 +39,38 @@ type Output interface { io.Writer } -func Export(input asciicast.Cast, output Output, bgColor, textColor string) { +func Export(input asciicast.Cast, output Output, bgColor, textColor string, no_window bool) { // Set the custom foreground and background colors foregroundColorOverride = textColor backgroundColorOverride = bgColor input.Compress() // to reduce the number of frames - createCanvas(svg.New(output), input) + createCanvas(svg.New(output), input, no_window) } -func createCanvas(svg *svg.SVG, cast asciicast.Cast) { +func createCanvas(svg *svg.SVG, cast asciicast.Cast, no_window bool) { canvas := &Canvas{SVG: svg, Cast: cast, id: uniqueid.New(), colors: make(map[string]string)} canvas.width = cast.Header.Width * colWidth canvas.height = cast.Header.Height * rowHeight parseCast(canvas) - canvas.createWindow() + canvas.Start(canvas.paddedWidth(), canvas.paddedHeight()) + if !no_window { + canvas.createWindow() + canvas.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize)) + } else { + if backgroundColorOverride == "" { + canvas.Rect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), "fill:#282d35") + } else { + canvas.Rect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), "fill:"+backgroundColorOverride) + } + canvas.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, int(padding*1.5))) + } + canvas.addStyles() + canvas.createFrames() + canvas.Gend() // Transform + canvas.Gend() // Styles canvas.End() } @@ -108,8 +123,6 @@ func (c *Canvas) createWindow() { buttonRadius := 7 buttonColors := [3]string{"#ff5f58", "#ffbd2e", "#18c132"} - c.Start(c.paddedWidth(), c.paddedHeight()) - // 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) @@ -120,11 +133,6 @@ func (c *Canvas) createWindow() { for i := range buttonColors { c.Circle((i*(padding+buttonRadius/2))+padding, padding, buttonRadius, fmt.Sprintf("fill:%s", buttonColors[i])) } - - c.addStyles() - c.createFrames() - c.Gend() // Transform - c.Gend() // Styles } func (c *Canvas) addStyles() { @@ -151,7 +159,6 @@ func (c *Canvas) addStyles() { styles += colors.String() } c.Style("text/css", styles) - c.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize)) } func (c *Canvas) createFrames() { diff --git a/internal/svg/svg_test.go b/internal/svg/svg_test.go index 3e9cd34..6340343 100644 --- a/internal/svg/svg_test.go +++ b/internal/svg/svg_test.go @@ -21,12 +21,29 @@ func TestExport(t *testing.T) { var output bytes.Buffer // Pass empty override bg and text colors - svg.Export(*cast, &output, "", "") + svg.Export(*cast, &output, "", "", false) g := goldie.New(t) g.Assert(t, "TestExportOutput", output.Bytes()) } +func TestNoWindow(t *testing.T) { + input := testutils.GoldenData(t, "TestExportInput") + + cast, err := asciicast.Unmarshal(input) + if err != nil { + t.Fatal(err) + } + + var output bytes.Buffer + + // Pass empty override bg and text colors + svg.Export(*cast, &output, "", "", true) + + g := goldie.New(t) + g.Assert(t, "TestExportOutputNoWindow", output.Bytes()) +} + func BenchmarkExport(b *testing.B) { input := testutils.GoldenData(b, "TestExportInput") @@ -39,6 +56,6 @@ func BenchmarkExport(b *testing.B) { var output bytes.Buffer // Pass empty override bg and text colors - svg.Export(*cast, &output, "", "") + svg.Export(*cast, &output, "", "", false) } } diff --git a/internal/svg/testdata/TestExportOutput.golden b/internal/svg/testdata/TestExportOutput.golden index 4d8989d..39fbddc 100644 --- a/internal/svg/testdata/TestExportOutput.golden +++ b/internal/svg/testdata/TestExportOutput.golden @@ -1,32 +1,32 @@ - - + + - h - + he - + hel - + hell - + hello diff --git a/internal/svg/testdata/TestExportOutputNoWindow.golden b/internal/svg/testdata/TestExportOutputNoWindow.golden new file mode 100644 index 0000000..793a4ee --- /dev/null +++ b/internal/svg/testdata/TestExportOutputNoWindow.golden @@ -0,0 +1,31 @@ + + + + + + + + +h + + +he + + +hel + + +hell + + +hello + + + +