Skip to content

Commit

Permalink
Added parameter to skip the first line
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefMarlin committed Mar 9, 2023
1 parent 1f4eca4 commit 7776b19
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 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,7 @@ 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)
err := rec(cmd.File, cmd.Command, cmd.SkipFirstLine)
if err != nil {
return err
}
Expand All @@ -36,8 +38,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 +70,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 +114,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 +135,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

0 comments on commit 7776b19

Please sign in to comment.