Skip to content

Commit

Permalink
Merge pull request #7 from BoogerMan2103/window
Browse files Browse the repository at this point in the history
Cmd optional arg to not render terminal window
  • Loading branch information
MrMarble authored Feb 14, 2024
2 parents 1aebbfe + 3aa0859 commit 51921a6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 27 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:"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)"`
}
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 {

Check warning on line 39 in cmd/termsvg/export/export.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; func parameter no_window should be noWindow (revive)
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
33 changes: 20 additions & 13 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const (
rowHeight = 25
colWidth = 11
colWidth = 12
padding = 20
headerSize = 3
)
Expand All @@ -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) {

Check warning on line 42 in internal/svg/svg.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; func parameter no_window should be noWindow (revive)
// 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) {

Check warning on line 52 in internal/svg/svg.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; func parameter no_window should be noWindow (revive)
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 {

Check failure on line 59 in internal/svg/svg.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
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)))

Check failure on line 68 in internal/svg/svg.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 1.5, in <argument> detected (gomnd)
}
canvas.addStyles()

Check failure on line 70 in internal/svg/svg.go

View workflow job for this annotation

GitHub Actions / lint

expressions should not be cuddled with blocks (wsl)
canvas.createFrames()
canvas.Gend() // Transform
canvas.Gend() // Styles
canvas.End()
}

Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -151,7 +159,6 @@ func (c *Canvas) addStyles() {
styles += colors.String()
}
c.Style("text/css", styles)

Check failure on line 161 in internal/svg/svg.go

View workflow job for this annotation

GitHub Actions / lint

expressions should not be cuddled with blocks (wsl)
c.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize))
}

func (c *Canvas) createFrames() {
Expand Down
21 changes: 19 additions & 2 deletions internal/svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)
}
}
16 changes: 8 additions & 8 deletions internal/svg/testdata/TestExportOutput.golden
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
<?xml version="1.0"?>
<!-- Generated by SVGo -->
<svg width="2383" height="1510"
<svg width="2596" height="1510"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="0" y="0" width="2383" height="1510" rx="5" ry="5" style="fill:#282d35" />
<rect x="0" y="0" width="2596" height="1510" rx="5" ry="5" style="fill:#282d35" />
<circle cx="20" cy="20" r="7" style="fill:#ff5f58" />
<circle cx="43" cy="20" r="7" style="fill:#ffbd2e" />
<circle cx="66" cy="20" r="7" style="fill:#18c132" />
<g transform="translate(20,60)" >
<g style="animation-duration:3.35s;animation-iteration-count:infinite;animation-name:k;animation-timing-function:steps(1,end);font-family:Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;font-size:20px">
<style type="text/css">
<![CDATA[
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2383px)}87.777%{transform:translateX(-4766px)}92.767%{transform:translateX(-7149px)}100.000%{transform:translateX(-9532px)}}.a{fill:#e5e5e5}
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2596px)}87.777%{transform:translateX(-5192px)}92.767%{transform:translateX(-7788px)}100.000%{transform:translateX(-10384px)}}.a{fill:#e5e5e5}
]]>
</style>
<g transform="translate(20,60)" >
<g transform="translate(0)">
<text x="0" y="0" class="a" >h</text>
</g>
<g transform="translate(2383)">
<g transform="translate(2596)">
<text x="0" y="0" class="a" >he</text>
</g>
<g transform="translate(4766)">
<g transform="translate(5192)">
<text x="0" y="0" class="a" >hel</text>
</g>
<g transform="translate(7149)">
<g transform="translate(7788)">
<text x="0" y="0" class="a" >hell</text>
</g>
<g transform="translate(9532)">
<g transform="translate(10384)">
<text x="0" y="0" class="a" >hello</text>
</g>
</g>
Expand Down
31 changes: 31 additions & 0 deletions internal/svg/testdata/TestExportOutputNoWindow.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<!-- Generated by SVGo -->
<svg width="2596" height="1510"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<rect x="0" y="0" width="2596" height="1510" style="fill:#282d35" />
<g transform="translate(20,30)" >
<g style="animation-duration:3.35s;animation-iteration-count:infinite;animation-name:k;animation-timing-function:steps(1,end);font-family:Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;font-size:20px">
<style type="text/css">
<![CDATA[
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2596px)}87.777%{transform:translateX(-5192px)}92.767%{transform:translateX(-7788px)}100.000%{transform:translateX(-10384px)}}.a{fill:#e5e5e5}
]]>
</style>
<g transform="translate(0)">
<text x="0" y="0" class="a" >h</text>
</g>
<g transform="translate(2596)">
<text x="0" y="0" class="a" >he</text>
</g>
<g transform="translate(5192)">
<text x="0" y="0" class="a" >hel</text>
</g>
<g transform="translate(7788)">
<text x="0" y="0" class="a" >hell</text>
</g>
<g transform="translate(10384)">
<text x="0" y="0" class="a" >hello</text>
</g>
</g>
</g>
</svg>

0 comments on commit 51921a6

Please sign in to comment.