Skip to content

Commit

Permalink
Merge pull request #3 from chiefMarlin/master
Browse files Browse the repository at this point in the history
Option to skip first line in rec and 2 options to set custom bg and text color for export
  • Loading branch information
MrMarble authored Dec 8, 2023
2 parents 1f4eca4 + 687a10e commit 1aebbfe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 19 deletions.
16 changes: 9 additions & 7 deletions cmd/termsvg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
)

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"`
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"`
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)"`
}

func (cmd *Cmd) Run() error {
Expand All @@ -23,7 +25,7 @@ func (cmd *Cmd) Run() error {
output = cmd.File + ".svg"
}

err := export(cmd.File, output, cmd.Mini)
err := export(cmd.File, output, cmd.Mini, cmd.BackgroundColor, cmd.TextColor)
if err != nil {
return err
}
Expand All @@ -33,7 +35,7 @@ func (cmd *Cmd) Run() error {
return nil
}

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

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

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

return nil
Expand Down
35 changes: 28 additions & 7 deletions cmd/termsvg/rec/rec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"

Expand All @@ -15,8 +16,9 @@ import (
)

type Cmd struct {
File string `arg:"" type:"path" help:"filename/path to save the recording to"`
Command string `short:"c" optional:"" env:"SHELL" help:"Specify command to record, defaults to $SHELL"`
File string `arg:"" type:"path" help:"filename/path to save the recording to"`
Command string `short:"c" optional:"" env:"SHELL" help:"Specify command to record, defaults to $SHELL"`
SkipFirstLine bool `short:"s" help:"Skip the first line of recording"`
}

const readSize = 1024
Expand All @@ -25,7 +27,11 @@ func (cmd *Cmd) Run() error {
log.Info().Str("output", cmd.File).Msg("recording asciicast.")
log.Info().Msg("exit the opened program when you're done.")

err := rec(cmd.File, cmd.Command)
if cmd.SkipFirstLine {
log.Warn().Msg("Skipping the first line of recording.")
}

err := rec(cmd.File, cmd.Command, cmd.SkipFirstLine)
if err != nil {
return err
}
Expand All @@ -36,8 +42,8 @@ func (cmd *Cmd) Run() error {
return nil
}

func rec(file, command string) error {
events, err := run(command)
func rec(file, command string, skipFirstLine bool) error {
events, err := run(command, skipFirstLine)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,8 +74,8 @@ func rec(file, command string) error {
return nil
}

//nolint
func run(command string) ([]asciicast.Event, error) {
// nolint
func run(command string, skipFirstLine bool) ([]asciicast.Event, error) {
// Create arbitrary command.
c := exec.Command("sh", "-c", command)
// Start the command with a pty.
Expand Down Expand Up @@ -112,6 +118,8 @@ func run(command string) ([]asciicast.Event, error) {
p := make([]byte, readSize)
baseTime := time.Now().UnixMicro()

startTriggered := false

for {
n, err := ptmx.Read(p)
event := asciicast.Event{
Expand All @@ -131,6 +139,19 @@ func run(command string) ([]asciicast.Event, error) {

os.Stdout.Write(p[:n])

// Skip the first line
if skipFirstLine {
if !startTriggered {
if strings.Contains(string(p[:n]), "\n") {
startTriggered = true
baseTime = time.Now().UnixMicro()
continue
} else {
continue
}
}
}

events = append(events, event)
}

Expand Down
28 changes: 25 additions & 3 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const (
headerSize = 3
)

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

type Canvas struct {

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

View workflow job for this annotation

GitHub Actions / lint

type must not be placed after const (desired order: type,const,var,func) (decorder)

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

View workflow job for this annotation

GitHub Actions / lint

type must not be placed after const (desired order: type,const,var,func) (decorder)
*svg.SVG
asciicast.Cast
Expand All @@ -33,7 +39,11 @@ type Output interface {
io.Writer
}

func Export(input asciicast.Cast, output Output) {
func Export(input asciicast.Cast, output Output, bgColor, textColor string) {
// Set the custom foreground and background colors
foregroundColorOverride = textColor
backgroundColorOverride = bgColor

input.Compress() // to reduce the number of frames

createCanvas(svg.New(output), input)
Expand Down Expand Up @@ -99,7 +109,13 @@ func (c *Canvas) createWindow() {
buttonColors := [3]string{"#ff5f58", "#ffbd2e", "#18c132"}

c.Start(c.paddedWidth(), c.paddedHeight())
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:#282d35")

// 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)
} else {
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:#282d35")
}

for i := range buttonColors {
c.Circle((i*(padding+buttonRadius/2))+padding, padding, buttonRadius, fmt.Sprintf("fill:%s", buttonColors[i]))
Expand All @@ -121,13 +137,19 @@ func (c *Canvas) addStyles() {
"font-size": "20px",
}.String())

// Foreground color gets set here
colors := css.Blocks{}
for color, class := range c.colors {
colors = append(colors, css.Block{Selector: fmt.Sprintf(".%s", class), Rules: css.Rules{"fill": color}})
}

styles := generateKeyframes(c.Cast, int32(c.paddedWidth()))
styles += colors.String()
// If custom colors have been provided, use them instead
if foregroundColorOverride != "" {
styles += fmt.Sprintf(".a{fill:%s}", foregroundColorOverride)
} else {
styles += colors.String()
}
c.Style("text/css", styles)

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

View workflow job for this annotation

GitHub Actions / lint

expressions should not be cuddled with blocks (wsl)

Check failure on line 153 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))
}
Expand Down
6 changes: 4 additions & 2 deletions internal/svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestExport(t *testing.T) {

var output bytes.Buffer

svg.Export(*cast, &output)
// Pass empty override bg and text colors
svg.Export(*cast, &output, "", "")

g := goldie.New(t)
g.Assert(t, "TestExportOutput", output.Bytes())
Expand All @@ -37,6 +38,7 @@ func BenchmarkExport(b *testing.B) {
for i := 0; i < b.N; i++ {
var output bytes.Buffer

svg.Export(*cast, &output)
// Pass empty override bg and text colors
svg.Export(*cast, &output, "", "")
}
}

0 comments on commit 1aebbfe

Please sign in to comment.