Skip to content

Commit

Permalink
Add option to not render terminal window
Browse files Browse the repository at this point in the history
  • Loading branch information
boogeroccam committed Feb 14, 2024
1 parent ceb7ddc commit d88d161
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
9 changes: 5 additions & 4 deletions cmd/termsvg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <input_file>.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)"`
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
28 changes: 16 additions & 12 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions internal/svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
}
}

0 comments on commit d88d161

Please sign in to comment.