asciinema-edit icon indicating copy to clipboard operation
asciinema-edit copied to clipboard

Add pause command

Open KrzysztofKowalczyk opened this issue 5 years ago • 3 comments

It would be nice to have dedicated command for pausing after a frame pause --at 3 for --0.3

KrzysztofKowalczyk avatar May 28 '19 13:05 KrzysztofKowalczyk

+1

paulk-asert avatar Jun 30 '19 22:06 paulk-asert

@paulk-asert I have a groovy script that can pause some lines. It might be slightly confusing as it also can "type" a line - add a delay to every char on given line.

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

import java.math.RoundingMode

// Take a line and "type it" with given time

File file = "recording.asci" as File
JsonSlurper slurper = new JsonSlurper()
// pretends to type a line with given number, useful when recording is driven by a pasted script
Integer lineToType = 0
BigDecimal typeDelay = 0.03

// accumulator for increasing delays of a line
BigDecimal delay = 0

// lines to pause
Map<Integer, BigDecimal> pause = [
    :
//    188: 2,
//    121: 2
]

def output = []
BigDecimal lastTime = 0

file.readLines().eachWithIndex { line, i ->
    def lineNumber = i+1 // as in editor - starting with 1
    def parsed = slurper.parseText(line)

    if(parsed instanceof List) {
        // round timestamp to 3 decimal places
        def newTime = BigDecimal.valueOf(parsed[0]).setScale(3, RoundingMode.HALF_UP)

        if(pause.containsKey(lineNumber)) {
            delay += pause[lineNumber]
        }

        // add delay
        parsed[0] = newTime + delay
    }

    if(lineNumber  == lineToType) {
        assert parsed instanceof List
        assert parsed[1] == "o"

        def text = parsed[2]
        assert text instanceof String

        // don't do anything if one character or less
        if(text.size() > 1) {
            text.each { c ->
                // increase total delay X ms per character
                delay += typeDelay
                def newTime = parsed[0] + delay
                def newLine = [newTime, "o", c]

                output << JsonOutput.toJson(newLine)
            }
        }
    } else {
        output << JsonOutput.toJson(parsed)
    }
}

file.text = output.join("\n")

KrzysztofKowalczyk avatar Jul 01 '19 09:07 KrzysztofKowalczyk

Nice! I was in the process of creating almost the same Groovy script. You've saved me the effort! :-)

paulk-asert avatar Jul 01 '19 10:07 paulk-asert