Skip to content

Commit

Permalink
Toggle pause with space + step a frame at a time with period
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Nov 25, 2017
1 parent 3cc2a54 commit fb1e25d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -190,6 +190,12 @@ __Replay recorded asciicast in a terminal.__
This command replays given asciicast (as recorded by `rec` command) directly in
your terminal.

Following keyboard shortcuts are available:

- <kbd>Space</kbd> - toggle pause,
- <kbd>.</kbd> - step through a recording a frame at a time (when paused),
- <kbd>Ctrl+C</kbd> - exit.

Playing from a local file:

asciinema play /path/to/asciicast.cast
Expand Down
44 changes: 40 additions & 4 deletions asciinema/player.py
Expand Up @@ -26,14 +26,50 @@ def _play(self, asciicast, idle_time_limit, speed, stdin):
stdout = frames.adjust_speed(stdout, speed)

base_time = time.time()
ctrl_c = False
paused = False
pause_time = None

for t, text in stdout:
delay = t - (time.time() - base_time)

if delay > 0:
data = read_blocking(stdin.fileno(), delay)
if 0x03 in data: # ctrl-c
break
while stdin and not ctrl_c and delay > 0:
if paused:
while True:
data = read_blocking(stdin.fileno(), 1000)

if 0x03 in data: # ctrl-c
ctrl_c = True
break

if 0x20 in data: # space
paused = False
base_time = base_time + (time.time() - pause_time)
break

if 0x2e in data: # period (dot)
delay = 0
pause_time = time.time()
base_time = pause_time - t
break
else:
data = read_blocking(stdin.fileno(), delay)

if not data:
break

if 0x03 in data: # ctrl-c
ctrl_c = True
break

if 0x20 in data: # space
paused = True
pause_time = time.time()
slept = t - (pause_time - base_time)
delay = delay - slept

if ctrl_c:
break

sys.stdout.write(text)
sys.stdout.flush()

0 comments on commit fb1e25d

Please sign in to comment.