Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experiments with asciicast transformations
import sys
import asciinema.asciicast as asciicast
import asciinema.asciicast.events as ev
# predicates
def _(actual):
return True
def any():
return lambda actual: True
def eq(value):
return lambda actual: actual == value
def gt(value):
return lambda actual: actual > value
# updaters
def constantly(value):
return lambda old: value
def wrap(thing):
if callable(thing):
return thing
else:
return eq(thing)
def identity(thing):
return thing
def matches(pattern, event):
return all(map(lambda f, v: f(v), pattern, event))
def keep(pattern):
pattern = [wrap(e) for e in pattern]
return lambda events: (event for event in events if matches(pattern, event))
def drop(pattern):
pattern = [wrap(e) for e in pattern]
return lambda events: (event for event in events if not matches(pattern, event))
def drop_last(pattern):
pattern = [wrap(e) for e in pattern]
def f(events):
prev = None
for event in events:
if prev is not None:
yield prev
prev = event
if prev is not None and not matches(pattern, prev):
yield prev
return f
def update(pattern=[_, _, _], via=None, ts=identity, type=identity, data=identity):
pattern = [wrap(e) for e in pattern]
ts = ts if callable(ts) else constantly(ts)
type = type if callable(type) else constantly(type)
data = data if callable(data) else constantly(data)
f = via or (lambda event: [ts(event[0]), type(event[1]), data(event[2])])
return lambda events: (f(event) if matches(pattern, event) else event for event in events)
import math
asciicast.transform(sys.argv[1], sys.argv[2], [
keep([_, "o", _]),
drop_last([_, _, "exit\r\n"]),
ev.to_relative_time,
# update([_, "i", _], data="LOL"),
# update([gt(0.1), _, _], ts=0.5),
update([gt(1), _, _], ts=lambda t: 1 + math.log(t)),
ev.to_absolute_time,
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment