Skip to content

Commit 1aebbfe

Browse files
authored
Merge pull request #3 from chiefMarlin/master
Option to skip first line in rec and 2 options to set custom bg and text color for export
2 parents 1f4eca4 + 687a10e commit 1aebbfe

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

cmd/termsvg/export/export.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
)
1313

1414
type Cmd struct {
15-
File string `arg:"" type:"existingfile" help:"asciicast file to export"`
16-
Output string `optional:"" short:"o" type:"path" help:"where to save the file. Defaults to <input_file>.svg"`
17-
Mini bool `name:"minify" optional:"" short:"m" help:"minify output file. May be slower"`
15+
File string `arg:"" type:"existingfile" help:"asciicast file to export"`
16+
Output string `optional:"" short:"o" type:"path" help:"where to save the file. Defaults to <input_file>.svg"`
17+
Mini bool `name:"minify" optional:"" short:"m" help:"minify output file. May be slower"`
18+
BackgroundColor string `optional:"" short:"b" help:"background color in hexadecimal format (e.g. #FFFFFF)"`
19+
TextColor string `optional:"" short:"t" help:"text color in hexadecimal format (e.g. #000000)"`
1820
}
1921

2022
func (cmd *Cmd) Run() error {
@@ -23,7 +25,7 @@ func (cmd *Cmd) Run() error {
2325
output = cmd.File + ".svg"
2426
}
2527

26-
err := export(cmd.File, output, cmd.Mini)
28+
err := export(cmd.File, output, cmd.Mini, cmd.BackgroundColor, cmd.TextColor)
2729
if err != nil {
2830
return err
2931
}
@@ -33,7 +35,7 @@ func (cmd *Cmd) Run() error {
3335
return nil
3436
}
3537

36-
func export(input, output string, mini bool) error {
38+
func export(input, output string, mini bool, bgColor, textColor string) error {
3739
inputFile, err := os.ReadFile(input)
3840
if err != nil {
3941
return err
@@ -52,7 +54,7 @@ func export(input, output string, mini bool) error {
5254

5355
if mini {
5456
out := new(bytes.Buffer)
55-
svg.Export(*cast, out)
57+
svg.Export(*cast, out, bgColor, textColor)
5658

5759
m := minify.New()
5860
m.AddFunc("image/svg+xml", msvg.Minify)
@@ -67,7 +69,7 @@ func export(input, output string, mini bool) error {
6769
return err
6870
}
6971
} else {
70-
svg.Export(*cast, outputFile)
72+
svg.Export(*cast, outputFile, bgColor, textColor)
7173
}
7274

7375
return nil

cmd/termsvg/rec/rec.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"os/signal"
8+
"strings"
89
"syscall"
910
"time"
1011

@@ -15,8 +16,9 @@ import (
1516
)
1617

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

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

28-
err := rec(cmd.File, cmd.Command)
30+
if cmd.SkipFirstLine {
31+
log.Warn().Msg("Skipping the first line of recording.")
32+
}
33+
34+
err := rec(cmd.File, cmd.Command, cmd.SkipFirstLine)
2935
if err != nil {
3036
return err
3137
}
@@ -36,8 +42,8 @@ func (cmd *Cmd) Run() error {
3642
return nil
3743
}
3844

39-
func rec(file, command string) error {
40-
events, err := run(command)
45+
func rec(file, command string, skipFirstLine bool) error {
46+
events, err := run(command, skipFirstLine)
4147
if err != nil {
4248
return err
4349
}
@@ -68,8 +74,8 @@ func rec(file, command string) error {
6874
return nil
6975
}
7076

71-
//nolint
72-
func run(command string) ([]asciicast.Event, error) {
77+
// nolint
78+
func run(command string, skipFirstLine bool) ([]asciicast.Event, error) {
7379
// Create arbitrary command.
7480
c := exec.Command("sh", "-c", command)
7581
// Start the command with a pty.
@@ -112,6 +118,8 @@ func run(command string) ([]asciicast.Event, error) {
112118
p := make([]byte, readSize)
113119
baseTime := time.Now().UnixMicro()
114120

121+
startTriggered := false
122+
115123
for {
116124
n, err := ptmx.Read(p)
117125
event := asciicast.Event{
@@ -131,6 +139,19 @@ func run(command string) ([]asciicast.Event, error) {
131139

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

142+
// Skip the first line
143+
if skipFirstLine {
144+
if !startTriggered {
145+
if strings.Contains(string(p[:n]), "\n") {
146+
startTriggered = true
147+
baseTime = time.Now().UnixMicro()
148+
continue
149+
} else {
150+
continue
151+
}
152+
}
153+
}
154+
134155
events = append(events, event)
135156
}
136157

internal/svg/svg.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const (
2020
headerSize = 3
2121
)
2222

23+
// If user passed custom background and text colors, use them
24+
var (
25+
foregroundColorOverride = ""
26+
backgroundColorOverride = ""
27+
)
28+
2329
type Canvas struct {
2430
*svg.SVG
2531
asciicast.Cast
@@ -33,7 +39,11 @@ type Output interface {
3339
io.Writer
3440
}
3541

36-
func Export(input asciicast.Cast, output Output) {
42+
func Export(input asciicast.Cast, output Output, bgColor, textColor string) {
43+
// Set the custom foreground and background colors
44+
foregroundColorOverride = textColor
45+
backgroundColorOverride = bgColor
46+
3747
input.Compress() // to reduce the number of frames
3848

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

101111
c.Start(c.paddedWidth(), c.paddedHeight())
102-
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:#282d35")
112+
113+
// If the user has specified a background color, use that instead of the default
114+
if backgroundColorOverride != "" {
115+
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:"+backgroundColorOverride)
116+
} else {
117+
c.Roundrect(0, 0, c.paddedWidth(), c.paddedHeight(), windowRadius, windowRadius, "fill:#282d35")
118+
}
103119

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

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

129146
styles := generateKeyframes(c.Cast, int32(c.paddedWidth()))
130-
styles += colors.String()
147+
// If custom colors have been provided, use them instead
148+
if foregroundColorOverride != "" {
149+
styles += fmt.Sprintf(".a{fill:%s}", foregroundColorOverride)
150+
} else {
151+
styles += colors.String()
152+
}
131153
c.Style("text/css", styles)
132154
c.Group(fmt.Sprintf(`transform="translate(%d,%d)"`, padding, padding*headerSize))
133155
}

internal/svg/svg_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func TestExport(t *testing.T) {
2020

2121
var output bytes.Buffer
2222

23-
svg.Export(*cast, &output)
23+
// Pass empty override bg and text colors
24+
svg.Export(*cast, &output, "", "")
2425

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

40-
svg.Export(*cast, &output)
41+
// Pass empty override bg and text colors
42+
svg.Export(*cast, &output, "", "")
4143
}
4244
}

0 commit comments

Comments
 (0)