Skip to content

Commit

Permalink
style: fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Feb 14, 2024
1 parent a9f116c commit 8b70e72
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
6 changes: 3 additions & 3 deletions cmd/termsvg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (cmd *Cmd) Run() error {
return nil
}

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

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

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

return nil
Expand Down
16 changes: 8 additions & 8 deletions cmd/termsvg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ import (
"github.com/rs/zerolog/log"
)

type Context struct {
Debug bool
}

type VersionFlag string

var (
// Populated by goreleaser during build
version = "master"
commit = "?"
date = ""
)

type Context struct {
Debug bool
}

type VersionFlag string

func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)

log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, PartsExclude: []string{"time"}})
}

func (v VersionFlag) Decode(ctx *kong.DecodeContext) error { return nil }
func (v VersionFlag) IsBool() bool { return true }
func (v VersionFlag) Decode(_ *kong.DecodeContext) error { return nil }
func (v VersionFlag) IsBool() bool { return true }
func (v VersionFlag) BeforeApply(app *kong.Kong) error {
fmt.Printf("termsvg has version %s built from %s on %s\n", version, commit, date)
app.Exit(0)
Expand Down
41 changes: 21 additions & 20 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ import (
"github.com/mrmarble/termsvg/pkg/css"
)

const (
rowHeight = 25
colWidth = 12
padding = 20
headerSize = 3
)

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

type Canvas struct {
*svg.SVG
asciicast.Cast
Expand All @@ -39,24 +26,37 @@ type Output interface {
io.Writer
}

func Export(input asciicast.Cast, output Output, bgColor, textColor string, no_window bool) {
const (
rowHeight = 25
colWidth = 12
padding = 20
headerSize = 3
)

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

func Export(input asciicast.Cast, output Output, bgColor, textColor string, noWindow 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, no_window)
createCanvas(svg.New(output), input, noWindow)
}

func createCanvas(svg *svg.SVG, cast asciicast.Cast, no_window bool) {
func createCanvas(svg *svg.SVG, cast asciicast.Cast, noWindow 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.Start(canvas.paddedWidth(), canvas.paddedHeight())
if !no_window {
if !noWindow {
canvas.createWindow()
canvas.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize))
} else {
Expand All @@ -65,6 +65,7 @@ func createCanvas(svg *svg.SVG, cast asciicast.Cast, no_window bool) {
} else {
canvas.Rect(0, 0, canvas.paddedWidth(), canvas.paddedHeight(), "fill:"+backgroundColorOverride)
}
//nolint:gomnd
canvas.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, int(padding*1.5)))
}
canvas.addStyles()
Expand Down Expand Up @@ -192,10 +193,10 @@ func (c *Canvas) createFrames() {
if cell.Char == ' ' {
lastColummn = col + 1
continue
} else {
lastColor = cell.FG
lastColummn = col
}
lastColor = cell.FG
lastColummn = col

}

frame += string(cell.Char)
Expand Down
11 changes: 6 additions & 5 deletions pkg/asciicast/asciicast.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,14 @@ func (c *Cast) Compress() {
if i == 0 {
events = append(events, event)
continue
}

if event.Time == events[len(events)-1].Time {
events[len(events)-1].EventData += event.EventData
} else {
if event.Time == events[len(events)-1].Time {
events[len(events)-1].EventData += event.EventData
} else {
events = append(events, event)
}
events = append(events, event)
}

}

c.Events = events
Expand Down
5 changes: 4 additions & 1 deletion pkg/asciicast/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ func TestJSONMarshal(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
output, err := json.Marshal(&tc.input)
input := tc.input

output, err := json.Marshal(&input)
if err != nil {
t.Fatal(err)
}

diff := cmp.Diff(string(output), tc.output)
if diff != "" {
t.Fatalf(diff)
Expand Down

0 comments on commit 8b70e72

Please sign in to comment.