From a3886815241a8d6447ba874d63f808b059b77840 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 30 Nov 2016 21:21:23 +0530 Subject: [PATCH] Code to replay dumped commands --- kitty/client.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ kitty/main.py | 5 +++ 2 files changed, 95 insertions(+) create mode 100644 kitty/client.py diff --git a/kitty/client.py b/kitty/client.py new file mode 100644 index 000000000..07668384d --- /dev/null +++ b/kitty/client.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal + +# Replay the log from --dump-commands. To use first run +# kitty --dump-commands > file.txt +# then run +# kitty --replay-commands file.txt +# will replay the commands and pause at the end waiting for user to press enter + +import sys + + +CSI = '\033[' + + +def write(x): + sys.stdout.write(x) + sys.stdout.flush() + + +def screen_cursor_position(y, x): + write(CSI + '%s;%sH' % (y, x)) + + +def screen_designate_charset(which, to): + which = '()'[int(which)] + to = chr(int(to)) + write('\033' + which + to) + + +def select_graphic_rendition(*a): + write(CSI + '%sm' % ';'.join(map(str, a))) + + +def screen_cursor_to_column(c): + write(CSI + '%dG' % c) + + +def screen_cursor_to_line(l): + write(CSI + '%dd' % l) + + +def screen_set_mode(x, private): + write(CSI + ('?' if private else '') + str(x) + 'h') + + +def screen_reset_mode(x, private): + write(CSI + ('?' if private else '') + str(x) + 'l') + + +def screen_set_margins(t, b): + write(CSI + '%d;%dr' % (t, b)) + + +def screen_erase_in_display(how, private): + write(CSI + ('?' if private else '') + str(how) + 'J') + + +def screen_cursor_up2(count): + write(CSI + '%dA' % count) + + +def screen_carriage_return(): + write('\r') + + +def screen_backspace(): + write('\x08') + + +def draw(*a): + write(' '.join(a)) + + +def replay(raw): + for line in raw.splitlines(): + if line.strip(): + cmd, rest = line.partition(' ')[::2] + if cmd == 'draw': + draw(rest) + else: + rest = map(int, rest.split()) if rest else () + globals()[cmd](*rest) + + +def main(path): + raw = open(path).read() + replay(raw) + input() diff --git a/kitty/main.py b/kitty/main.py index 6fb5fc2d4..596405059 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -34,6 +34,7 @@ def option_parser(): a('--version', action='version', version='{} {} by Kovid Goyal'.format(appname, '.'.join(str_version))) a('--profile', action='store_true', default=False, help=_('Show profiling data after exit')) a('--dump-commands', action='store_true', default=False, help=_('Output commands received from child process to stdout')) + a('--replay-commands', default=None, help=_('Replay previously dumped commands')) a('args', nargs=argparse.REMAINDER, help=_( 'The remaining arguments are used to launch a program other than the default shell. Any further options are passed' ' directly to the program being invoked.' @@ -103,6 +104,10 @@ def main(): if args.cmd: exec(args.cmd) return + if args.replay_commands: + from kitty.client import main + main(args.replay_commands) + return opts = load_config(args.config) glfw_set_error_callback(on_glfw_error) enable_automatic_opengl_error_checking(False)