tty2gif / tty2gif.py

#!/usr/bin/env python

import os
import sys
import struct
import time
from opster import command, dispatch

header = struct.Struct('iii')

@command()
def replay(filename, factor=('f', 1, 'Speedup factor')):
    main(filename, 'replay', factor=factor)

@command()
def typing(filename):
    main(filename, 'typing')

@command()
def inspect(filename):
    main(filename, 'inspect')

def main(filename, action, factor=1):
    winid = int(os.getenv('WINDOWID'))

    script = open(filename)
    basetime = None
    prevtime = None
    stepnum = 0
    args = []
    while True:
        data = script.read(header.size)
        if not data:
            break
        sec, usec, n = header.unpack(data)
        curtime = sec + usec/1000000.0
        if basetime is None:
            basetime = prevtime = curtime
        delay = curtime-prevtime
        prevtime = curtime
        data = script.read(n)
        if action == 'inspect':
            print '%8.4f %4d %s' % (delay, n, `data[:40]`)
            continue
        if action == 'replay':
            time.sleep(delay/factor)
        elif action == 'typing':
            delay = 0
            if n == 1:
                delay = 0.2
            elif data.startswith('\r\n'):
                delay = 0.5
            if delay:
                os.system('import -window %s step%03d.gif' % (winid, stepnum))
                #time.sleep(delay)
                #print '-delay %d step%03d.gif' % (delay*100, stepnum),
                stepnum+=1

        sys.stdout.write(data)
        sys.stdout.flush()


if __name__ == '__main__':
    dispatch()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.