From ceb7ddc79e3af68aff4cce35430b18460338d74a Mon Sep 17 00:00:00 2001 From: Ilya Bygrimov Date: Wed, 14 Feb 2024 15:26:16 +0100 Subject: [PATCH 1/4] Change colWidth to avoid collisions --- internal/svg/svg.go | 2 +- internal/svg/testdata/TestExportOutput.golden | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/svg/svg.go b/internal/svg/svg.go index fd92904..059addf 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 ) 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 From d88d161b7a402fa04be7b1d974671f7865ba431c Mon Sep 17 00:00:00 2001 From: Ilya Bygrimov Date: Wed, 14 Feb 2024 15:28:31 +0100 Subject: [PATCH 2/4] Add option to not render terminal window --- cmd/termsvg/export/export.go | 9 +++++---- internal/svg/svg.go | 28 ++++++++++++++++------------ internal/svg/svg_test.go | 4 ++-- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cmd/termsvg/export/export.go b/cmd/termsvg/export/export.go index 12a02a5..ff2fcb4 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:"create 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 059addf..0dedbbf 100644 --- a/internal/svg/svg.go +++ b/internal/svg/svg.go @@ -39,23 +39,35 @@ 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 { + // canvas.Roundrect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), 5, 5, "fill:#282d35") + canvas.Rect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), "fill:#282d35") + 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 +120,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 +130,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 +156,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..65c549d 100644 --- a/internal/svg/svg_test.go +++ b/internal/svg/svg_test.go @@ -21,7 +21,7 @@ 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()) @@ -39,6 +39,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) } } From c1352fc83fb000ab976c74ec5f1a77c3cc07cf21 Mon Sep 17 00:00:00 2001 From: Ilya Bygrimov Date: Wed, 14 Feb 2024 15:41:31 +0100 Subject: [PATCH 3/4] Add test & change cli description --- cmd/termsvg/export/export.go | 2 +- internal/svg/svg_test.go | 17 ++++++++++ .../testdata/TestExportOutputNoWindow.golden | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 internal/svg/testdata/TestExportOutputNoWindow.golden diff --git a/cmd/termsvg/export/export.go b/cmd/termsvg/export/export.go index ff2fcb4..312bed9 100644 --- a/cmd/termsvg/export/export.go +++ b/cmd/termsvg/export/export.go @@ -15,7 +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:"create window in svg"` + 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)"` } diff --git a/internal/svg/svg_test.go b/internal/svg/svg_test.go index 65c549d..6340343 100644 --- a/internal/svg/svg_test.go +++ b/internal/svg/svg_test.go @@ -27,6 +27,23 @@ func TestExport(t *testing.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") 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 + + + + From 3aa085946ad537b12b3df550a0328f84f7b004b1 Mon Sep 17 00:00:00 2001 From: Ilya Bygrimov Date: Wed, 14 Feb 2024 15:55:42 +0100 Subject: [PATCH 4/4] Overriding background color for render without term-window --- internal/svg/svg.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/svg/svg.go b/internal/svg/svg.go index 0dedbbf..0afa967 100644 --- a/internal/svg/svg.go +++ b/internal/svg/svg.go @@ -60,8 +60,11 @@ func createCanvas(svg *svg.SVG, cast asciicast.Cast, no_window bool) { canvas.createWindow() canvas.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize)) } else { - // canvas.Roundrect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), 5, 5, "fill:#282d35") - canvas.Rect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), "fill:#282d35") + 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()