├── rlundo ├── __init__.py ├── interps │ ├── undoableadventurenorewrite.py │ ├── undoableadventure.py │ ├── __init__.py │ ├── undoableipython.py │ └── undoablepython.py ├── termhelpers.py ├── rlundoable.py ├── __main__.py ├── findcursor.py ├── undoreadline.py ├── pity.py └── termrewrite.py ├── test ├── __init__.py ├── context.py ├── input_with_colours.txt ├── scenarioscript.py ├── test_terminal_dsl.py ├── test_termrewrite.py ├── test_tmux.py ├── tmux.py ├── test_rlundo.py ├── terminal_dsl.py └── test_rewrite.py ├── .travis.script.sh ├── setup.cfg ├── requirements.txt ├── .travis.tmux.sh ├── .gitignore ├── .travis.yml ├── rewrite.py ├── testideas ├── rlundoable ├── rlundolibpath.c ├── rlundopatched.c ├── readline.diff ├── rlundo.c ├── Makefileosx ├── test.c ├── Makefilelinux ├── rlundoable.c └── readme.md ├── py_logger.py ├── memory_monitor.py ├── readme.md └── COPYING /rlundo/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.travis.script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | set -x 4 | 5 | nosetests -v test 6 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [nosetests] 2 | with-doctest=1 3 | doctest-tests=1 4 | nologcapture=1 5 | with-flaky=1 6 | ignore-files=memory_monitor.py 7 | [bdist_wheel] 8 | universal = 1 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | argcomplete==0.9.0 2 | blessings==1.6 3 | colorama==0.3.3 4 | ipython==3.1.0 5 | kaptan==0.5.8 6 | nose==1.3.7 7 | flaky==2.1.1 8 | PyYAML==3.11 9 | tmuxp==0.8.1 10 | wheel==0.24.0 11 | -------------------------------------------------------------------------------- /test/context.py: -------------------------------------------------------------------------------- 1 | # As suggested by Kenneth Rietz at http://www.kennethreitz.org/essays/repository-structure-and-python 2 | import os 3 | import sys 4 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) 5 | 6 | import rlundo 7 | -------------------------------------------------------------------------------- /.travis.tmux.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | set -x 4 | 5 | wget https://github.com/tmux/tmux/releases/download/2.2/tmux-2.2.tar.gz 6 | tar xzvf tmux-2.2.tar.gz 7 | cd tmux-2.2 8 | ./configure 9 | make 10 | chmod +x tmux 11 | sudo mv tmux /usr/local/bin 12 | -------------------------------------------------------------------------------- /rlundo/interps/undoableadventurenorewrite.py: -------------------------------------------------------------------------------- 1 | from .. import undoreadline 2 | 3 | 4 | def start_undoable_adventure(): 5 | undoreadline.monkeypatch_input_no_rewrite() 6 | import adventure.__main__ 7 | 8 | 9 | if __name__ == '__main__': 10 | start_undoable_adventure() 11 | -------------------------------------------------------------------------------- /rlundo/interps/undoableadventure.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from .. import undoreadline 4 | 5 | 6 | def start_undoable_adventure(): 7 | undoreadline.monkeypatch_readline() 8 | import adventure.__main__ 9 | 10 | 11 | if __name__ == '__main__': 12 | start_undoable_adventure() 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # for not you're supposed to link to the right Makefile 2 | Makefile 3 | 4 | *.dSYM 5 | *.o 6 | *.so 7 | *.so.* 8 | *.dylib 9 | *.out 10 | *.tar.gz 11 | 12 | rlundolibpath 13 | testrlundoable 14 | rlundopatched 15 | 16 | readline-6.3 17 | modified-readline-6.3 18 | *.egg-info 19 | *.pyc 20 | 21 | .noseids 22 | *.log 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | sudo: true 3 | before_install: 4 | - ./.travis.tmux.sh 5 | 6 | python: 7 | - "2.7" 8 | - "3.4" 9 | - "3.5" 10 | - "pypy" 11 | 12 | matrix: 13 | allow_failures: 14 | - python: "pypy" 15 | 16 | install: 17 | - "pip install blessings==1.6 termcast-client==0.1.3 tmuxp==0.8.1 ipython==3.2.1 nose==1.3.7 flaky==2.1.1" 18 | 19 | script: 20 | - ./.travis.script.sh 21 | -------------------------------------------------------------------------------- /test/input_with_colours.txt: -------------------------------------------------------------------------------- 1 | \x1b[0;31m---------------------------------------------------------------------------\x1b[0m 2 | \x1b[0;31mNameError\x1b[0m Traceback (most recent call last) 3 | \x1b[0;32m\x1b[0m in \x1b[0;36m\x1b[0;34m()\x1b[0m 4 | \x1b[0;32m----> 1\x1b[0;31m \x1b[0ma\x1b[0m\x1b[0;34m\x1b[0m\x1b[0m 5 | \x1b[0m 6 | \x1b[0;31mNameError\x1b[0m: name 'a' is not defined 7 | -------------------------------------------------------------------------------- /rewrite.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from rlundo.termrewrite import run_with_listeners 4 | 5 | if __name__ == '__main__': 6 | parser = argparse.ArgumentParser(description="Run a command in a pty, saving and restoring the terminal state") 7 | parser.add_argument('--save-addr', action='store', default=None) 8 | parser.add_argument('--restore-addr', action='store', default=None) 9 | parser.add_argument('command', nargs='*') 10 | args = parser.parse_args() 11 | if args.command == []: 12 | args.command = ['python', '-c', "while True: (raw_input if '' == b'' else input)('>')"] 13 | run_with_listeners(args.command, save_addr=args.save_addr, restore_addr=args.restore_addr) 14 | -------------------------------------------------------------------------------- /testideas: -------------------------------------------------------------------------------- 1 | """Testing finding the location of the cursor""" 2 | # Tests: 3 | # We don't have to predict resize patterns anymore. 4 | # Instead: Resize something and add some text. 5 | # Given 2 terminal states and the characters it took to get from one to the other 6 | # play back keypresses from initial state 7 | # manually linewrap that and count how many rows added 8 | # check that that's the old cursor position 9 | # check that last two terminals have same dimensions 10 | # ignore first 11 | # playing process in blank terminal should give reverse of diff of 1 and 2 12 | # 13 | # Grander plan: use diagrams to describe the way that tmux window will be 14 | # modified! 15 | # 16 | # Plan for now: just send keys and use 17 | 18 | 19 | -------------------------------------------------------------------------------- /rlundo/termhelpers.py: -------------------------------------------------------------------------------- 1 | import fcntl 2 | import os 3 | import termios 4 | import tty 5 | 6 | 7 | class Cbreak(object): 8 | def __init__(self, stream): 9 | self.stream = stream 10 | 11 | def __enter__(self): 12 | self.original_stty = termios.tcgetattr(self.stream) 13 | tty.setcbreak(self.stream, termios.TCSANOW) 14 | 15 | def __exit__(self, *args): 16 | termios.tcsetattr(self.stream, termios.TCSANOW, self.original_stty) 17 | 18 | 19 | class Nonblocking(object): 20 | 21 | def __init__(self, fd): 22 | self.fd = fd 23 | 24 | def __enter__(self): 25 | self.orig_fl = fcntl.fcntl(self.fd, fcntl.F_GETFL) 26 | fcntl.fcntl(self.fd, fcntl.F_SETFL, self.orig_fl | os.O_NONBLOCK) 27 | 28 | def __exit__(self, *args): 29 | fcntl.fcntl(self.fd, fcntl.F_SETFL, self.orig_fl) 30 | 31 | 32 | -------------------------------------------------------------------------------- /rlundoable/rlundolibpath.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Runs its arguments, in an environment such that rlundoable's readline 4 | * will be used instead of normal readline 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main(int argc, char **argv) { 12 | // Check that we have at least one arg. 13 | if (argc == 1) { 14 | printf("You must supply a program which uses readline to run\n"); 15 | printf("\n"); 16 | printf("Example: %s /bin/racket\n", argv[0]); 17 | return 1; 18 | } 19 | // TODO: allow names without paths 20 | 21 | #ifdef __APPLE__ 22 | putenv("DYLD_LIBRARY_PATH=."); 23 | #else 24 | putenv("LD_LIBRARY_PATH=."); 25 | #endif 26 | 27 | execv(argv[1], argv + 1); /* Note that exec() will not return on success. */ 28 | perror("exec() failed"); 29 | 30 | return 2; 31 | } 32 | -------------------------------------------------------------------------------- /rlundo/rlundoable.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | from subprocess import Popen 4 | 5 | 6 | def modify_env_with_modified_rl(): 7 | """Modifiy enviornment to enable undo features in the repl.""" 8 | modified_readline_path = os.path.join(os.path.dirname(os.path.realpath('__file__')), 9 | 'rlundoable/modified-readline-6.3/shlib') 10 | 11 | if sys.platform == 'darwin': 12 | os.environ["DYLD_LIBRARY_PATH"] = modified_readline_path 13 | else: 14 | os.environ["LD_LIBRARY_PATH"] = ':'.join([modified_readline_path]) 15 | os.environ["LD_PRELOAD"] = '/lib/x86_64-linux-gnu/libtinfo.so.5' 16 | 17 | 18 | def run_with_modified_rl(args): 19 | modify_env_with_modified_rl() 20 | 21 | p = Popen(args) 22 | p.communicate() 23 | 24 | if __name__ == '__main__': 25 | run_with_modified_rl(sys.argv[1:]) 26 | -------------------------------------------------------------------------------- /rlundoable/rlundopatched.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Runs its arguments, in an environment such that a patched 3 | * version of readline will be used instead of normal readline 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | int main(int argc, char **argv) { 11 | // Check that we have at least one arg. 12 | if (argc == 1) { 13 | printf("You must supply a program which uses readline to run\n"); 14 | printf("\n"); 15 | printf("Example: %s /bin/irb\n", argv[0]); 16 | return 1; 17 | } 18 | // TODO: allow names without paths 19 | 20 | #ifdef __APPLE__ 21 | WHOOPS not implemented 22 | #else 23 | putenv("LD_PRELOAD=./modified-readline-6.3/shlib/libreadline.so.6.3:/lib/x86_64-linux-gnu/libtinfo.so.5"); 24 | #endif 25 | 26 | execv(argv[1], argv + 1); /* Note that exec() will not return on success. */ 27 | perror("exec() failed"); 28 | 29 | return 2; 30 | } 31 | -------------------------------------------------------------------------------- /rlundoable/readline.diff: -------------------------------------------------------------------------------- 1 | 335c335 2 | < readline (prompt) 3 | --- 4 | > actual_readline (prompt) 5 | 388a389,422 6 | > 7 | > char * last_command = "with no command, so exiting"; 8 | > 9 | > char * 10 | > readline (prompt) 11 | > const char *prompt; 12 | > { 13 | > char *value; 14 | > 15 | > system("nc -U $RLUNDO_SAVE"); 16 | > value = actual_readline(prompt); 17 | > if(!strcmp(value, "undo")){ 18 | > //printf("undoing '%s'\n", last_command); 19 | > system("nc -U $RLUNDO_RESTORE"); 20 | > exit(42); 21 | > } 22 | > pid_t pid = fork(); 23 | > 24 | > if (pid == 0) { 25 | > last_command = strdup(value); 26 | > return value; 27 | > } else { 28 | > int status; 29 | > waitpid(pid, &status, 0); 30 | > int exitstatus = WEXITSTATUS(status); 31 | > 32 | > if(exitstatus == 42){ 33 | > return readline(prompt); 34 | > } else { 35 | > exit(exitstatus); 36 | > } 37 | > } 38 | > } 39 | > 40 | -------------------------------------------------------------------------------- /rlundoable/rlundo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Runs its arguments, in an environment such that rlundoable's readline 3 | * will be used instead of normal readline 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | int main(int argc, char **argv) { 11 | // Check that we have at least one arg. 12 | if (argc == 1) { 13 | printf("You must supply a program which uses readline to run\n"); 14 | printf("\n"); 15 | printf("Example: %s /bin/irb\n", argv[0]); 16 | return 1; 17 | } 18 | // TODO: allow names without paths 19 | 20 | #ifdef __APPLE__ 21 | putenv("DYLD_FORCE_FLAT_NAMESPACE=1"); 22 | putenv("DYLD_INSERT_LIBRARIES=./librlundoable.dylib"); 23 | #else 24 | putenv("LD_PRELOAD=./librlundoable.so:/lib/x86_64-linux-gnu/libtinfo.so.5"); 25 | #endif 26 | 27 | execv(argv[1], argv + 1); /* Note that exec() will not return on success. */ 28 | perror("exec() failed"); 29 | 30 | return 2; 31 | } 32 | -------------------------------------------------------------------------------- /rlundo/interps/__init__.py: -------------------------------------------------------------------------------- 1 | """Alternate interpreters to use that follow the rlundo protocol. 2 | 3 | useful if an interpreter does not dynamically load readline.c 4 | in an easily interceptable way""" 5 | 6 | import os 7 | 8 | 9 | def is_python(path): 10 | return os.path.basename(path) == "python" 11 | 12 | 13 | def is_ipython(path): 14 | """Check if the terminal to be opened is ipython.""" 15 | return os.path.basename(path) == "ipython" 16 | 17 | def is_adventure(path): 18 | return path == 'adventure' 19 | 20 | def is_adventure_no_rewrite(path): 21 | return path == 'adventure_no_rewrite' 22 | 23 | 24 | from . import undoablepython 25 | from . import undoableipython 26 | 27 | interpreters = [ 28 | (['python3', '-m', 'rlundo.interps.undoableadventurenorewrite'], is_adventure_no_rewrite), 29 | (['python3', '-m', 'rlundo.interps.undoableadventure'], is_adventure), 30 | (['python', os.path.abspath(undoablepython.__file__)], is_python), 31 | (['python', os.path.abspath(undoableipython.__file__)], is_ipython), 32 | ] 33 | -------------------------------------------------------------------------------- /rlundoable/Makefileosx: -------------------------------------------------------------------------------- 1 | all: test.out modified-readline-6.3/shlib/libreadline.6.3.dylib 2 | test.out: test.c 3 | gcc -c test.c 4 | gcc -v test.o -lreadline -o test.out 5 | 6 | # creating patched readline 7 | readline-6.3: 8 | ftp 'ftp://ftp.cwru.edu/pub/bash/readline-6.3.tar.gz' 9 | tar xzvf readline-6.3.tar.gz 10 | modified-readline-6.3: readline.diff readline-6.3 11 | rm -rf modified-readline-6.3 12 | cp -r readline-6.3 modified-readline-6.3 13 | patch modified-readline-6.3/readline.c readline.diff 14 | cd modified-readline-6.3; ./configure 15 | modified-readline-6.3/shlib/libreadline.6.3.dylib: modified-readline-6.3 16 | cd modified-readline-6.3; make 17 | # other things to call libreadline.dylib 18 | cd modified-readline-6.3/shlib; ln -sf libreadline.6.3.dylib libreadline.5.dylib 19 | cd modified-readline-6.3/shlib; ln -sf libreadline.6.3.dylib libedit.3.dylib 20 | patched-readline: modified-readline-6.3/shlib/libreadline.6.3.dylib 21 | 22 | # not included in make all - rebuild the diff based on edits to modified 23 | refresh-diff: readline-6.3 24 | diff readline-6.3/readline.c modified-readline-6.3/readline.c > readline.diff 25 | 26 | clean: 27 | rm -rf *.dylib modified-readline-6.3 28 | -------------------------------------------------------------------------------- /rlundoable/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * A simple program that uses readline. 3 | * 4 | * To use this to test, somehow get this executable to load 5 | * the rlundoable.so/rlundoable.so instead of normal readline. 6 | * like LD_PRELOAD for linux 7 | * or DLYD_INSERT_LIBRARIES and DYLD_FORCE_FLAT_NAMESPACE on osx 8 | * Then type "undo" 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | int main(){ 20 | char* line; 21 | 22 | //char *(*original_readline)(const char*) = NULL; 23 | //original_readline = dlsym(RTLD_NEXT, "readline"); 24 | //printf("here's the original_readline: %d\n", (int)original_readline); 25 | 26 | const char* prompt = "enter a string: "; 27 | while (1) { 28 | line = readline(prompt); // readline allocates space for returned string 29 | printf("done calling readline\n"); 30 | if(line != NULL) { 31 | printf("You entered: %s\n", line); 32 | if(!strcmp(line, "undo")){ 33 | printf("looks like we're using normal readline\n"); 34 | } 35 | free(line); // but you are responsible for freeing the space 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /py_logger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | py_logger 5 | 6 | Write a log of what's what bytes are being written by a command 7 | 8 | Use it from the command line: `python py_logger.py ipython` 9 | Use it from another python script: 10 | `from py_logger import run` 11 | `run(['ipython']) 12 | """ 13 | 14 | from __future__ import unicode_literals 15 | import os 16 | import sys 17 | 18 | import pity 19 | 20 | 21 | def master_read(fd): 22 | """Read the output of a terminal writing a log in the middle. 23 | Args: 24 | fd: File descriptor being read. 25 | """ 26 | data = os.read(fd, 1024) 27 | log = open('output.log', 'a') 28 | log.write(data) 29 | return data 30 | 31 | 32 | def run(argv): 33 | """Spawn a process with master read writing a log file in between. 34 | Args: 35 | argv: Additional arguments after `python py_logger` command 36 | """ 37 | pity.spawn(argv, 38 | master_read=master_read, 39 | handle_window_size=True) 40 | 41 | if __name__ == '__main__': 42 | if len(sys.argv) == 1: 43 | print("To record what bytes ipython writes to stdout/stderr:") 44 | print("python py_logger.py ipython") 45 | else: 46 | run(sys.argv[1:]) 47 | -------------------------------------------------------------------------------- /rlundoable/Makefilelinux: -------------------------------------------------------------------------------- 1 | all: rlundo test.out rlundolibpath patched-readline rlundopatched 2 | librlundoable.so: rlundoable.c 3 | gcc -Wall -fPIC -shared -o librlundoable.so rlundoable.c -ldl 4 | rlundo: rlundo.c librlundoable.so 5 | gcc rlundo.c -o rlundo 6 | rlundolibpath: rlundolibpath.c librlundoable.so 7 | gcc rlundolibpath.c -o rlundolibpath 8 | test.out: test.c 9 | gcc -c test.c 10 | gcc -v test.o -lreadline -o test.out 11 | 12 | # creating patched readline 13 | readline-6.3: 14 | wget 'ftp://ftp.cwru.edu/pub/bash/readline-6.3.tar.gz' 15 | tar xzvf readline-6.3.tar.gz 16 | modified-readline-6.3: readline.diff readline-6.3 17 | rm -rf modified-readline-6.3 18 | cp -r readline-6.3 modified-readline-6.3 19 | patch modified-readline-6.3/readline.c readline.diff 20 | cd modified-readline-6.3; ./configure 21 | 22 | modified-readline-6.3/shlib/libreadline.so.6.3: modified-readline-6.3 23 | cd modified-readline-6.3; make 24 | # other things to call libreadline.so 25 | cd modified-readline-6.3/shlib; ln -sf libreadline.so.6.3 libreadline.5.so 26 | cd modified-readline-6.3/shlib; ln -sf libreadline.so.6.3 libreadline.so.6 27 | patched-readline: modified-readline-6.3/shlib/libreadline.so.6.3 28 | 29 | # not included in make all - rebuild the diff based on edits to modified 30 | refresh-diff: readline-6.3 31 | -diff readline-6.3/readline.c modified-readline-6.3/readline.c > readline.diff 32 | clean: 33 | rm -rf *.so modified-readline-6.3 34 | -------------------------------------------------------------------------------- /test/scenarioscript.py: -------------------------------------------------------------------------------- 1 | import sys 2 | move_up = u'\x1bM' 3 | move_right = u'\x1b[C' 4 | move_left = u'\b' 5 | clear_eol = u'\x1b[K' 6 | clear_eos = u'\x1b[J' 7 | 8 | py2 = sys.version_info[0] == 2 9 | if py2: 10 | input = raw_input 11 | 12 | 13 | def make_blank_line_below(n): 14 | "Move cursor back to prev spot after hitting return" 15 | sys.stdout.write(move_up) 16 | for _ in range(20): 17 | sys.stdout.write(move_left) 18 | for _ in range(n): 19 | sys.stdout.write(move_right) 20 | sys.stdout.write(clear_eos) 21 | sys.stdout.flush() 22 | 23 | 24 | def move_cursor_back_up(): 25 | sys.stdout.write(move_up) 26 | sys.stdout.write(move_up) 27 | sys.stdout.flush() 28 | 29 | 30 | def move_cursor_up_and_over_and_clear(n): 31 | sys.stdout.write(move_up) 32 | sys.stdout.write(move_up) 33 | for _ in range(20): 34 | sys.stdout.write(move_left) 35 | for _ in range(n): 36 | sys.stdout.write(move_right) 37 | sys.stdout.write(clear_eos) 38 | sys.stdout.flush() 39 | 40 | 41 | def dispatch(prompt=None): 42 | if prompt: 43 | inp = input(prompt) 44 | else: 45 | inp = input() 46 | if inp.startswith('1c'): 47 | make_blank_line_below(int(inp[2:])) 48 | elif inp == 'up2': 49 | move_cursor_back_up() 50 | elif inp.startswith('uc'): 51 | move_cursor_up_and_over_and_clear(int(inp[2:])) 52 | 53 | 54 | if __name__ == '__main__': 55 | dispatch(">") 56 | while True: 57 | dispatch() 58 | -------------------------------------------------------------------------------- /rlundo/__main__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | rlundo 5 | 6 | Start a repl with undo. 7 | """ 8 | 9 | from __future__ import unicode_literals 10 | import sys 11 | import os 12 | import argparse 13 | 14 | sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) 15 | import rlundo 16 | 17 | from rlundo.rlundoable import modify_env_with_modified_rl 18 | from rlundo.termrewrite import run_with_listeners 19 | 20 | from rlundo import interps 21 | 22 | 23 | def start_undoable_rl(interpreter, interparg): 24 | """Run an interpreter either with an undo script or with a generic method. 25 | 26 | If an interpreter matches the first argument, run script like that. 27 | Otherwise run that command in an environment where a modified readline will 28 | be used instead of the standard one.""" 29 | for command, predicate in interps.interpreters: 30 | if predicate(interpreter): 31 | return run_with_listeners(command + interparg) 32 | else: 33 | modify_env_with_modified_rl() 34 | return run_with_listeners([interpreter] + interparg) 35 | 36 | 37 | if __name__ == "__main__": 38 | parser = argparse.ArgumentParser(description='accepting an interpreter and any interpreter arguments into rlundo') 39 | parser.add_argument('interpreter', metavar='I', help='command to call the interpreter') 40 | parser.add_argument('interparg', nargs=argparse.REMAINDER, help='any arguments you can feed into the interpreter') 41 | argobject = parser.parse_args() 42 | start_undoable_rl(argobject.interpreter, argobject.interparg) 43 | -------------------------------------------------------------------------------- /test/test_terminal_dsl.py: -------------------------------------------------------------------------------- 1 | from .terminal_dsl import TerminalState, parse_term_state, horzcat 2 | import unittest 3 | 4 | 5 | class TestTerminalDiagramParsing(unittest.TestCase): 6 | def test_parse_term_state(self): 7 | s = """ 8 | +------+ 9 | +------+ 10 | |a-a-a-| 11 | |b-b-b-| 12 | |@ | 13 | +------+""" 14 | self.assertEqual(parse_term_state(s), ('', TerminalState( 15 | lines=['a-a-a-', 'b-b-b-', ''], 16 | cursor_line=2, 17 | cursor_offset=0, 18 | width=6, 19 | height=3, 20 | history_height=0))) 21 | 22 | def test_parse_term_state_with_empty_lines(self): 23 | s = """ 24 | +------+ 25 | +------+ 26 | |a-a-a-| 27 | |b-b-b-| 28 | |@ | 29 | ~ ~ 30 | ~ ~ 31 | +------+""" 32 | self.assertEqual(parse_term_state(s), ('', TerminalState( 33 | lines=['a-a-a-', 'b-b-b-', ''], 34 | cursor_line=2, 35 | cursor_offset=0, 36 | width=6, 37 | height=5, 38 | history_height=0))) 39 | 40 | def test_parse_term_state_input_rejection(self): 41 | s = """ 42 | +------+ 43 | ~ ~ 44 | +------+ 45 | |1-1-1-| 46 | |2-2-2-| 47 | |@-3-3-| 48 | +------+""" 49 | self.assertRaises(ValueError, parse_term_state, s) 50 | s = """ 51 | +------+ 52 | |a-a-a-| 53 | |b-b-b-| 54 | |@-c-c-| 55 | +------+""" 56 | self.assertRaises(ValueError, parse_term_state, s) 57 | s = """ 58 | +------+ 59 | +------+ 60 | |a-a-a-| 61 | |b-b-b-| 62 | |c-c-c-| 63 | +------+""" 64 | self.assertRaises(ValueError, parse_term_state, s) 65 | 66 | 67 | class TestHelpers(unittest.TestCase): 68 | def test_horzcat(self): 69 | self.assertEqual(horzcat('a\nbc\ndef\ng', 70 | 'z\nx\nywvut'), 71 | 'a z\nbc x\ndefywvut\ng ') 72 | -------------------------------------------------------------------------------- /memory_monitor.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """ 3 | memory_monitor 4 | 5 | Tool to generate and graph real time records of the memory usage. 6 | 7 | From the command line: 8 | `python memory_monitor.py` 9 | 10 | To exit: 11 | Ctrl+c 12 | """ 13 | 14 | from __future__ import unicode_literals 15 | import matplotlib.pyplot as plt 16 | import numpy as np 17 | from subprocess import Popen, PIPE 18 | 19 | 20 | def generate_memory_stats(interval=0.1, count=None): 21 | """Create a generator of the current free memory in the system. 22 | 23 | Args: 24 | interval: Interval in seconds to generate new memory records. 25 | count: Optional maximum records to generate. 26 | """ 27 | 28 | # generate a maximum of 'count' records 29 | if count: 30 | p = Popen(['vm_stat', '-c', unicode(count), unicode(interval)], 31 | stdout=PIPE) 32 | 33 | # generate infinite records 34 | else: 35 | p = Popen(['vm_stat', unicode(interval)], stdout=PIPE) 36 | 37 | # yield records until keybord interruption or 'count' reached 38 | try: 39 | while True: 40 | line = p.stdout.readline() 41 | if 'Mach' not in line and 'free' not in line: 42 | yield unicode(line.split()[0]) 43 | 44 | except KeyboardInterrupt: 45 | print("\nMemory usage monitor exited!\n") 46 | 47 | 48 | def graph_memory_usage(): 49 | """Create a real time graph of the free memory in the system.""" 50 | 51 | x = np.linspace(0, 50, 1000) 52 | y = np.linspace(0, 1, 1000) 53 | 54 | # TODO: scale should be the total memory in the system 55 | scale = int(generate_memory_stats().next()) 56 | if scale < 1600000: 57 | scale = 1600000 58 | y[0] = scale 59 | 60 | plt.ion() 61 | 62 | fig = plt.figure() 63 | ax = fig.add_subplot(111) 64 | line1, = ax.plot(x, y, 'r-') # Returns a tuple of line objects 65 | 66 | xmin, xmax = 0, 50 67 | ymin, ymax = 0, scale * 1.1 # give some room to appreciate better the line 68 | plt.axis([xmin, xmax, ymin, ymax]) 69 | 70 | try: 71 | for record in generate_memory_stats(): 72 | y = np.append(y[1:], record) 73 | line1.set_ydata(y) 74 | fig.canvas.draw() 75 | 76 | except KeyboardInterrupt: 77 | print("Memory graph exited") 78 | 79 | 80 | if __name__ == '__main__': 81 | graph_memory_usage() 82 | -------------------------------------------------------------------------------- /rlundo/findcursor.py: -------------------------------------------------------------------------------- 1 | import termios 2 | import tty 3 | import re 4 | 5 | 6 | class Cbreak(object): 7 | 8 | def __init__(self, stream): 9 | self.stream = stream 10 | 11 | def __enter__(self): 12 | self.original_stty = termios.tcgetattr(self.stream) 13 | tty.setcbreak(self.stream, termios.TCSANOW) 14 | return Termmode(self.stream, self.original_stty) 15 | 16 | def __exit__(self, *args): 17 | termios.tcsetattr(self.stream, termios.TCSANOW, self.original_stty) 18 | 19 | 20 | class Termmode(object): 21 | 22 | def __init__(self, stream, attrs): 23 | self.stream = stream 24 | self.attrs = attrs 25 | 26 | def __enter__(self): 27 | self.original_stty = termios.tcgetattr(self.stream) 28 | termios.tcsetattr(self.stream, termios.TCSANOW, self.attrs) 29 | 30 | def __exit__(self, *args): 31 | termios.tcsetattr(self.stream, termios.TCSANOW, self.original_stty) 32 | 33 | 34 | def get_cursor_position(to_terminal, from_terminal): 35 | with Cbreak(from_terminal): 36 | return _inner_get_cursor_position(to_terminal, from_terminal) 37 | 38 | 39 | def _inner_get_cursor_position(to_terminal, from_terminal): 40 | query_cursor_position = u"\x1b[6n" 41 | to_terminal.write(query_cursor_position) 42 | to_terminal.flush() 43 | 44 | def retrying_read(): 45 | while True: 46 | try: 47 | c = from_terminal.read(1) 48 | if c == '': 49 | raise ValueError("Stream should be blocking - should't" 50 | " return ''. Returned %r so far", (resp,)) 51 | return c 52 | except IOError as e: 53 | raise ValueError('cursor get pos response read interrupted: %r' % (resp, )) 54 | 55 | resp = '' 56 | while True: 57 | c = retrying_read() 58 | resp += c 59 | m = re.search('(?P.*)' 60 | '(?P\x1b\[|\x9b)' 61 | '(?P\\d+);(?P\\d+)R', resp, re.DOTALL) 62 | if m: 63 | row = int(m.groupdict()['row']) 64 | col = int(m.groupdict()['column']) 65 | extra = m.groupdict()['extra'] 66 | if extra: # TODO send these to child process 67 | raise ValueError(("Bytes preceding cursor position " 68 | "query response thrown out:\n%r\n" 69 | "Pass an extra_bytes_callback to " 70 | "CursorAwareWindow to prevent this") 71 | % (extra,)) 72 | return (row - 1, col - 1) 73 | 74 | -------------------------------------------------------------------------------- /test/test_termrewrite.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | import ast 4 | import os 5 | import unittest 6 | 7 | from .context import rlundo 8 | from rlundo import termrewrite 9 | 10 | class TestRewriteHelpers(unittest.TestCase): 11 | def test_history(self): 12 | self.assertEqual(termrewrite.history( 13 | [b'>>> print("hello\\n"*3)\nhello\nhello\nhello\n', 14 | b'>>> 1 + 1\n2\n', 15 | b'>>> ']), 16 | [b'>>> print("hello\\n"*3)', 17 | b'hello', 18 | b'hello', 19 | b'hello', 20 | b'>>> 1 + 1', 21 | b'2', 22 | b'>>> ']) 23 | 24 | def test_count_lines(self): 25 | self.assertEqual(termrewrite.count_lines("1234\n123456", 4), 2) 26 | self.assertEqual(termrewrite.count_lines("1234\n123456", 10), 1) 27 | self.assertEqual(termrewrite.count_lines("> undo\r\n> 1\r\n1\r\n", 10), 3) 28 | self.assertEqual(termrewrite.count_lines("\x01\x1b[0;32m\x02In [\x01\x1b[1;32m\x021\x01\x1b[0;32m\x02]: 1\n\x01\x1b[0m\x02\x1b[0;31mOut[\x1b[1;31m1\x1b[0;31m]: \x1b[0m1\n\n", 40), 3) 29 | self.assertEqual(termrewrite.count_lines('\r\n\x1b[0;32mIn [\x1b[1;32m2\x1b[0;32m]: \x1b[0mundo\x1b[0;32m ...: \x1b[0m \r\n\r\n', 40), 3) 30 | 31 | with open(os.path.join(os.path.dirname(__file__), "input_with_colours.txt"), "r") as f: 32 | data = ast.literal_eval('u"""'+f.read()+'"""') 33 | self.assertEqual(termrewrite.count_lines(data, 80), 6) 34 | 35 | def test_linesplit(self): 36 | self.assertEqual(termrewrite.linesplit(["1234", "123456"], 4), 37 | ["1234", "1234", "56"]) 38 | self.assertEqual(termrewrite.linesplit(["1234", "123456"], 10), 39 | ["1234", "123456"]) 40 | 41 | def test_visible_characters(self): 42 | self.assertEqual(termrewrite._visible_characters(u"1234"), 4) 43 | self.assertEqual(termrewrite._visible_characters(u"1234\n"), 4) 44 | self.assertEqual(termrewrite._visible_characters(u"\x1b[0;32m1234"), 4) 45 | self.assertEqual(termrewrite._visible_characters(u"\x1b[0m1234"), 4) 46 | 47 | def test_rows_required(self): 48 | self.assertEqual(termrewrite._rows_required(u"1234", 4), 1) 49 | self.assertEqual(termrewrite._rows_required(u"1234", 3), 2) 50 | self.assertEqual(termrewrite._rows_required(u"1234", 2), 2) 51 | self.assertEqual(termrewrite._rows_required(u"1234", 1), 4) 52 | self.assertEqual(termrewrite._rows_required(u"\x1b[0;32m1234", 2), 2) 53 | -------------------------------------------------------------------------------- /rlundo/undoreadline.py: -------------------------------------------------------------------------------- 1 | import code 2 | import socket 3 | import os 4 | import sys 5 | from functools import partial 6 | 7 | # if this process is a child, this will change from None 8 | WRITE_TO_PARENT_FD = None 9 | 10 | 11 | py2 = False 12 | if sys.version_info[0] == 2: 13 | py2 = True 14 | input = raw_input 15 | 16 | # sometimes readline will be swapped out for builtins.input 17 | orig_input = input 18 | 19 | 20 | def monkeypatch_input_no_rewrite(): 21 | import builtins 22 | builtins.input = readline_no_rewrite 23 | 24 | 25 | def monkeypatch_readline(): 26 | import builtins 27 | builtins.input = readline_no_rewrite 28 | init_terminal_rewriting() 29 | 30 | 31 | def readline_no_rewrite(prompt): 32 | global WRITE_TO_PARENT_FD 33 | 34 | while True: 35 | save() 36 | try: 37 | s = orig_input(prompt) 38 | except EOFError: 39 | die_and_tell_parent(b'exit') 40 | 41 | if s == 'undo': 42 | restore() 43 | die_and_tell_parent(b'undo') 44 | read_fd, write_fd = os.pipe() 45 | pid = os.fork() 46 | is_child = pid == 0 47 | 48 | if is_child: 49 | WRITE_TO_PARENT_FD = write_fd 50 | return s 51 | else: 52 | from_child = os.read(read_fd, 1) 53 | if from_child == b'e': 54 | die_and_tell_parent(b'exit') 55 | continue 56 | 57 | 58 | def save(): 59 | pass 60 | 61 | 62 | def restore(): 63 | pass 64 | 65 | 66 | def init_terminal_rewriting(): 67 | global save 68 | global restore 69 | try: 70 | save = partial(connect_and_wait_for_close, addr=os.environ['RLUNDO_SAVE']) 71 | restore = partial(connect_and_wait_for_close, addr=os.environ['RLUNDO_RESTORE']) 72 | except KeyError: 73 | print(sorted(os.environ.keys())) 74 | raise 75 | 76 | 77 | def connect_and_wait_for_close(addr): 78 | s = socket.socket(family=socket.AF_UNIX) 79 | try: 80 | s.connect(addr) 81 | except ConnectionRefusedError: 82 | pass 83 | else: 84 | assert b'' == s.recv(1024) 85 | 86 | 87 | def die_and_tell_parent(msg): 88 | if WRITE_TO_PARENT_FD is not None: 89 | os.write(WRITE_TO_PARENT_FD, msg+b'\n') 90 | sys.exit() 91 | 92 | 93 | class ForkUndoConsole(code.InteractiveConsole): 94 | def raw_input(self, prompt=""): 95 | return readline_no_rewrite(prompt) 96 | 97 | def input(self, prompt=""): 98 | return readline_no_rewrite(prompt) 99 | 100 | 101 | if __name__ == '__main__': 102 | console = ForkUndoConsole() 103 | console.interact() 104 | -------------------------------------------------------------------------------- /rlundoable/rlundoable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * A version of readline function that intercepts 3 | * calls to it and forks. 4 | */ 5 | 6 | #define _GNU_SOURCE 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #ifdef __APPLE__ 15 | #include 16 | #else 17 | #include 18 | #endif 19 | 20 | #include 21 | #include 22 | 23 | static char *top_undo_message = "with no command, so exiting"; 24 | static char *last_command; 25 | static char *(*original_readline)(const char*) = NULL; 26 | 27 | static char* (*get_original_readline())(const char*){ 28 | char *(*orig_readline)(const char*) = NULL; 29 | #ifdef __APPLE__ 30 | void* readline_lib = dlopen("/usr/local/opt/readline/lib/libreadline.6.dylib", RTLD_LOCAL); 31 | if (dlerror()){ 32 | printf("dl error: %s\n", dlerror()); 33 | exit(0); 34 | } 35 | orig_readline = dlsym(readline_lib, "readline"); 36 | if (dlerror()){ 37 | printf("dl error: %s\n", dlerror()); 38 | exit(0); 39 | } 40 | #else 41 | orig_readline = dlsym(RTLD_NEXT, "readline"); 42 | #endif 43 | return orig_readline; 44 | } 45 | 46 | /* Read a line of input. Prompt with PROMPT. An empty PROMPT means 47 | none. A return value of NULL means that EOF was encountered. */ 48 | char* readline(const char *prompt){ 49 | 50 | char *value; 51 | 52 | if (!last_command){ 53 | last_command = (char *) malloc(strlen(top_undo_message) + 1); 54 | strcpy(last_command, top_undo_message); 55 | } 56 | 57 | if (!original_readline){ 58 | original_readline = get_original_readline(); 59 | } 60 | 61 | system("nc -z localhost 4242"); 62 | value = (*original_readline)(prompt); 63 | if(!value){ 64 | free(last_command); 65 | printf("\n"); 66 | exit(0); 67 | } 68 | if(!strcmp(value, "undo")){ 69 | printf("undoing '%s'\n", last_command); 70 | 71 | free(last_command); 72 | exit(42); 73 | } 74 | pid_t pid = fork(); 75 | 76 | if (pid == 0) { // child 77 | last_command = strdup(value); 78 | return value; 79 | } else { // parent 80 | int status; 81 | waitpid(pid, &status, 0); 82 | int exitstatus = -1; 83 | if (WIFEXITED(status)){ 84 | exitstatus = WEXITSTATUS(status); 85 | } else { 86 | exitstatus = 1; // didn't terminate normally 87 | } 88 | 89 | if(exitstatus == 42){ 90 | system("nc -z localhost 4243"); 91 | system("nc -z localhost 4243"); 92 | return readline(prompt); 93 | } else { 94 | exit(exitstatus); 95 | } 96 | } 97 | } 98 | 99 | int main(){ 100 | char* line; 101 | int depth = 0; 102 | 103 | while (1){ 104 | depth++; 105 | line = readline("enter a string: "); // readline allocates space for returned string 106 | if(line != NULL) { 107 | printf("You entered: %s\n", line); 108 | printf("depth: %d\n", depth); 109 | free(line); // but you are responsible for freeing the space 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /rlundo/interps/undoableipython.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | undoableipython 5 | ---------------------------------- 6 | 7 | This module contains a replacement for the 8 | TerminalInteractiveShell.raw_input_original method that can be found in 9 | IPython source code. The replacement implements undoable feature for ipython 10 | without rewriting the terminal (you will see the history of written statements 11 | but the last statement will be undone). 12 | 13 | Use it from the command line: `python undoableipython.py` 14 | """ 15 | 16 | from __future__ import unicode_literals 17 | import os 18 | import sys 19 | import time 20 | from IPython.utils import py3compat 21 | from IPython.terminal.interactiveshell import TerminalInteractiveShell 22 | from IPython import start_ipython 23 | 24 | 25 | def raw_input_original(prompt): 26 | """Replace raw_input_original property in TerminalInteractiveShell. 27 | 28 | Add code to implement undo feature in IPython terminal. The original 29 | IPython code is wrapped into comments, the rest is part of the hack. 30 | 31 | Args: 32 | prompt: Prompt input from the user. 33 | 34 | Returns: 35 | The input from the user processed. 36 | """ 37 | 38 | from functools import partial 39 | import socket 40 | 41 | if sys.version_info.major == 2: 42 | ConnectionRefusedError = socket.error 43 | 44 | def connect_and_wait_for_close(addr): 45 | s = socket.socket(family=socket.AF_UNIX) 46 | try: 47 | s.connect(addr) 48 | except ConnectionRefusedError: 49 | pass 50 | else: 51 | assert b'' == s.recv(1024) 52 | 53 | save = partial(connect_and_wait_for_close, addr=os.environ['RLUNDO_SAVE']) 54 | restore = partial(connect_and_wait_for_close, addr=os.environ['RLUNDO_RESTORE']) 55 | 56 | while True: 57 | save() 58 | try: 59 | # ********************************************** 60 | # --------BEGIN of Original IPython code-------- 61 | # ********************************************** 62 | if py3compat.PY3: 63 | line = input(prompt) 64 | else: 65 | line = raw_input(prompt) 66 | # ********************************************** 67 | # --------END of Original IPython code-------- 68 | # ********************************************** 69 | except KeyboardInterrupt: 70 | line = "undo" 71 | 72 | if line == "undo": 73 | time.sleep(.001) # race condition, see issue #29 74 | restore() 75 | os._exit(42) 76 | 77 | pid = os.fork() 78 | is_child = pid == 0 79 | 80 | # if the process is not the parent, just carry on 81 | if is_child: 82 | break 83 | 84 | else: 85 | while True: 86 | try: 87 | status = os.waitpid(pid, 0) 88 | break 89 | except KeyboardInterrupt: 90 | pass 91 | exit_code = status[1] // 256 92 | if not exit_code == 42: 93 | os._exit(exit_code) 94 | 95 | return line 96 | 97 | 98 | def patch_ipython(): 99 | """Replace raw_input_original as a property.""" 100 | TerminalInteractiveShell.raw_input_original = property( 101 | lambda self: raw_input_original, lambda self, x: None) 102 | 103 | 104 | def start_undoable_ipython(args=None): 105 | """Start an undoable instance of ipython. 106 | 107 | Args: 108 | args: Arguments passed to the undoable instance to be started. 109 | """ 110 | patch_ipython() 111 | 112 | if args: 113 | sys.argv = args 114 | 115 | start_ipython() 116 | 117 | 118 | if __name__ == '__main__': 119 | start_undoable_ipython() 120 | -------------------------------------------------------------------------------- /rlundo/interps/undoablepython.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import code 4 | import logging 5 | import os 6 | import socket 7 | import sys 8 | from functools import partial 9 | 10 | # read about copy-on-write for Python processes - I feel like I've heard 11 | # this doesn't work well 12 | 13 | py2 = False 14 | if sys.version_info.major == 2: 15 | input = raw_input 16 | ConnectionRefusedError = socket.error 17 | py2 = True 18 | 19 | logger = logging.getLogger(__name__) 20 | 21 | DEBUG = False 22 | 23 | def connect_and_wait_for_close(addr): 24 | s = socket.socket(family=socket.AF_UNIX) 25 | try: 26 | s.connect(addr) 27 | except ConnectionRefusedError: 28 | pass 29 | else: 30 | assert b'' == s.recv(1024) 31 | 32 | 33 | save = None 34 | restore = None 35 | 36 | 37 | def log(msg): 38 | if DEBUG: 39 | logger.debug(str(os.getpid()) + ': ' + str(msg) + '\n') 40 | 41 | 42 | def readline(prompt): 43 | """Get input from user, fork or exit 44 | 45 | readline needs function attributes: 46 | .on_close() should notify parent process we're undoing 47 | .on_exit() should notify parent that we're exiting""" 48 | 49 | log('pid %r initial call to readline' % (os.getpid())) 50 | while True: 51 | save() 52 | try: 53 | s = input(prompt) 54 | except EOFError: 55 | readline.on_exit() 56 | except KeyboardInterrupt: 57 | s = 'undo' 58 | if s == 'undo': 59 | restore() 60 | readline.on_undo() 61 | read_fd, write_fd = os.pipe() 62 | pid = os.fork() 63 | is_child = pid == 0 64 | log('created read/write fd pair: %r %r' % (read_fd, write_fd)) 65 | 66 | if is_child: 67 | 68 | def on_undo(): 69 | log('undoing command') 70 | log('writing line to say done on fd %r' % (write_fd,)) 71 | os.write(write_fd, b'done\n') 72 | log('wrote, exiting') 73 | sys.exit() 74 | 75 | def on_exit(): 76 | log('exiting!') 77 | os.write(write_fd, b'exit\n') 78 | sys.exit() 79 | 80 | readline.on_undo = on_undo 81 | readline.on_exit = on_exit 82 | 83 | log('child returning to loop') 84 | return s 85 | else: 86 | log('Waiting for child %r by reading on fd %r' % (pid, read_fd)) 87 | from_child = os.read(read_fd, 1) 88 | if from_child == b'e': 89 | readline.on_exit() 90 | log('parent %r received response from child %r: %r' % 91 | (os.getpid(), pid, from_child)) 92 | continue 93 | 94 | 95 | readline.on_undo = sys.exit 96 | readline.on_exit = sys.exit 97 | 98 | 99 | class ForkUndoConsole(code.InteractiveConsole): 100 | 101 | def raw_input(self, prompt=""): 102 | """Write a prompt and read a line. 103 | 104 | The returned line does not include the trailing newline. 105 | When the user enters the EOF key sequence, EOFError is raised. 106 | 107 | The base implementation uses the built-in function 108 | raw_input(); a subclass may replace this with a different 109 | implementation. 110 | 111 | """ 112 | return readline(prompt) 113 | 114 | 115 | def start_undoable_python(args=None): 116 | print('running with pid %r' % (os.getpid(),)) 117 | 118 | console = ForkUndoConsole() 119 | global save 120 | global restore 121 | try: 122 | save = partial(connect_and_wait_for_close, addr=os.environ['RLUNDO_SAVE']) 123 | restore = partial(connect_and_wait_for_close, addr=os.environ['RLUNDO_RESTORE']) 124 | except KeyError: 125 | print(sorted(os.environ.keys())) 126 | raise 127 | 128 | 129 | if args: 130 | sys.argv = args 131 | 132 | sys.path.insert(0, '') 133 | console.interact() 134 | 135 | 136 | if __name__ == '__main__': 137 | start_undoable_python() 138 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/thomasballinger/rlundo.svg?branch=master)](https://travis-ci.org/thomasballinger/rlundo) 2 | 3 | # rlundo 4 | 5 | rlundo grants interactive interpreters magical undo powers! 6 | 7 | ![rlundo preview example](http://ballingt.com/assets/rlundopreview.gif) 8 | 9 | For a long read about the motivation for such a tool, see [this blog 10 | post](http://ballingt.com/interactive-interpreter-undo) 11 | 12 | A patched version of readline is used to fork an interpreter 13 | at each prompt. If the user enters `undo` then that child process dies 14 | and execution is resume. 15 | rlundo also removes the terminal output that occurred in the recently-deceased 16 | child process, restoring the terminal to its previous state. 17 | 18 | The goal is for this to work with any interpreter: 19 | 20 | $ python rlundo irb 21 | 22 | The name rlundo is modeled off of 23 | [rlwrap](https://github.com/hanslub42/rlwrap), which wraps interactive 24 | command line interfaces with the readline editing interface. Like that 25 | command, rlundo wraps other interactive interfaces. 26 | To make the analogy work better it probably should have been called undowrap, or 27 | rlundowrap to suggest that the way undo is implemented uses readline. 28 | 29 | --- 30 | 31 | Using a patched version of the readline library only works for interactive 32 | interpreters that dynamically link readline. To address this, this project 33 | includes shims for various interpreters that implement undo via fork in a 34 | less general way in /rlundo/interps/. Compiling the patched readline library 35 | is not required for interpreters implemented this way. Add your favorite! 36 | 37 | $ python rlundo python 38 | 39 | (python seems to usually statically link readline) 40 | 41 | ## Modified Readline library 42 | 43 | rlundoable is a patched version of the gnu readline library with the following 44 | modifications: 45 | 46 | * calling readline causes the process to fork 47 | * the user entering "undo" causes the process to die 48 | * tcp socket connections are made when the process forks or dies to notify 49 | a listener that might be recording terminal state 50 | 51 | To build this patched readline library: 52 | 53 | cd rlundoable 54 | make -f Makefileosx 55 | 56 | Read more about the patched readline library in that [readme](rlundoable/readme.md). 57 | 58 | The library substitution works more reliably for me on linux right now. Maybe 59 | this is because homebrew more often links readline statically? That's just 60 | speculation. Writing workarounds for common interpreters or digging into how 61 | to make readline hijacking more reliable would both be really helpful! 62 | 63 | ## Rewriting terminal state 64 | 65 | In order to restore prior terminal state on undo, interpreters are run 66 | in a pseudo terminal that takes snapshots of terminal state when the 67 | interpreter forks and restores previous terminal state when an interpreter 68 | process dies. 69 | 70 | try it with 71 | 72 | $ python rewrite.py 73 | 74 | and then in another terminal run 75 | 76 | nc -U path/to/tmp/unix/socket/*save 77 | 78 | to save terminal states, and 79 | 80 | nc -U path/tmp/tmp/unix/socket/*save; nc -U path/to/tmp/unix/socket/*restore; 81 | 82 | to restore previous terminal states. Restore always goes back two state, so it 83 | is necessary to call save before restore as shown above to restore the previous 84 | save. Ordinarily these signals are sent by the modified interpreter (or the 85 | patched readline it calls) after printing the prompt but before the user types 86 | anything. Since you'll be sending the commands manually in the above demo, the 87 | `>` prompt will not reappear after undo. 88 | 89 | # Running the tests 90 | 91 | * clone the repo, create a virtual environment 92 | * pip install nose 93 | * install [tmux](https://github.com/tmux/tmux) 1.9a or 2.1 or later. 1.8 is 94 | too early, 2.0 has a regression. 95 | * `nosetests test` in the root directory 96 | * or try `RLUNDO_USE_EXISTING_TMUX_SESSION=1 nosetests test` while you have a tmux 97 | session open to watch the tests which use tmux run 98 | 99 | 100 | --- 101 | 102 | Thanks to 103 | 104 | * John Hergenroeder for help with fixing race conditions with terminal 105 | rewriting 106 | * John Connor for discussion and Python 3 fixes 107 | * Agustín Benassi for ipython support, improved terminal rewriting, memory 108 | monitoring work and much more 109 | * Joe Jean for work on Travis tests 110 | * Madelyn Freed for work on the executable rlundo script 111 | 112 | --- 113 | 114 | ## License 115 | 116 | Copyright 2015 Thomas Ballinger 117 | 118 | Released under GPL3, like GNU readline. 119 | 120 | This program is free software: you can redistribute it and/or modify 121 | it under the terms of the GNU General Public License as published by 122 | the Free Software Foundation, either version 3 of the License, or 123 | (at your option) any later version. 124 | 125 | This program is distributed in the hope that it will be useful, 126 | but WITHOUT ANY WARRANTY; without even the implied warranty of 127 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128 | GNU General Public License for more details. 129 | 130 | You should have received a copy of the GNU General Public License 131 | along with this program. If not, see . 132 | 133 | --- 134 | 135 | $ python rlundo ipython 136 | 137 | ![undo with ctrl+c](http://ballingt.com/assets/undoable_ipython.gif) 138 | -------------------------------------------------------------------------------- /test/test_tmux.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | import unittest 4 | 5 | from . import tmux 6 | from . import terminal_dsl 7 | 8 | 9 | class TestTmuxPaneInfo(unittest.TestCase): 10 | def test_contents(self): 11 | with tmux.TmuxPane(20, 5) as t: 12 | tmux.send_command(t, 'echo 01234567890123456789') 13 | tmux.send_command(t, 'echo 01234567890123456789') 14 | self.assertEqual(tmux.all_contents(t), 15 | ['$echo 01234567890123', 16 | '456789', 17 | '01234567890123456789', 18 | '$echo 01234567890123', 19 | '456789', 20 | '01234567890123456789', 21 | '$']) 22 | self.assertEqual(tmux.all_lines(t), 23 | ['$echo 01234567890123' 24 | '456789', 25 | '01234567890123456789', 26 | '$echo 01234567890123' 27 | '456789', 28 | '01234567890123456789', 29 | '$']) 30 | 31 | 32 | class TestTerminalDSL(unittest.TestCase): 33 | def test_simple(self): 34 | with tmux.TmuxPane(10, 10) as t: 35 | tmux.send_command(t, 'true 1') 36 | self.assertEqual(tmux.visible(t), ['$true 1', 37 | '$']) 38 | tmux.send_command(t, 'true 2') 39 | termstate = terminal_dsl.TerminalState.from_tmux_pane(t) 40 | expected = terminal_dsl.TerminalState( 41 | lines=['$true 1', '$true 2', '$'], 42 | cursor_line=2, cursor_offset=1, width=10, height=10, 43 | history_height=0) 44 | self.assertEqual(expected, termstate, expected.visible_diff(termstate)) 45 | 46 | def test_wrapped_lines(self): 47 | with tmux.TmuxPane(10, 10) as t: 48 | tmux.send_command(t, 'true 12345') 49 | self.assertEqual(tmux.visible(t), ['$true 1234', 50 | '5', 51 | '$']) 52 | tmux.send_command(t, 'true 1234') 53 | termstate = terminal_dsl.TerminalState.from_tmux_pane(t) 54 | expected = terminal_dsl.TerminalState( 55 | lines=['$true 12345', '$true 1234', '$'], 56 | cursor_line=2, cursor_offset=1, width=10, height=10, 57 | history_height=0) 58 | self.assertEqual(expected, termstate, expected.visible_diff(termstate)) 59 | 60 | 61 | class TestTmux(unittest.TestCase): 62 | def test_send_command(self): 63 | with tmux.TmuxPane(20, 20) as t: 64 | tmux.send_command(t, 'sleep .1; echo hi') 65 | self.assertEqual(tmux.visible(t), ['$sleep .1; echo hi', 66 | 'hi', 67 | '$']) 68 | 69 | def test_simple(self): 70 | with tmux.TmuxPane(10, 10) as t: 71 | t.send_keys('true 1') 72 | self.assertEqual(tmux.visible(t), ['$ true 1', 73 | '$']) 74 | self.assertEqual(tmux.scrollback(t), []) 75 | t.send_keys('true 2') 76 | self.assertEqual(tmux.visible(t), ['$ true 1', 77 | '$ true 2', 78 | '$']) 79 | self.assertEqual(tmux.scrollback(t), []) 80 | 81 | def test_lines_wrap(self): 82 | """lines and cursor position wrap 83 | 84 | This is the reason we're using tmux and not vt100 emulator""" 85 | with tmux.TmuxPane(10, 10) as t: 86 | self.assertEqual(tmux.cursor_pos(t), (0, 1)) 87 | t.send_keys('true 678') 88 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 89 | t.clear() 90 | tmux.wait_for_prompt(t, '$') 91 | self.assertEqual(tmux.cursor_pos(t), (0, 1)) 92 | t.send_keys('true 6789') 93 | self.assertEqual(tmux.cursor_pos(t), (2, 1)) 94 | 95 | def test_cursor_position_updated_immediately_after_send_keys(self): 96 | """No need to wait for prompt etc. """ 97 | with tmux.TmuxPane(10, 10) as t: 98 | self.assertEqual(tmux.cursor_pos(t), (0, 1)) 99 | t.send_keys('true 678') 100 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 101 | 102 | def test_resize(self): 103 | """lines and cursor pos wrap, reported by line 104 | 105 | The front of the current line retains its position!""" 106 | with tmux.TmuxPane(10, 10) as t: 107 | self.assertEqual(tmux.cursor_pos(t), (0, 1)) 108 | tmux.send_command(t, 'true 123456789') 109 | self.assertEqual(tmux.visible(t), 110 | ['$true 1234', '56789', '$']) 111 | self.assertEqual(tmux.cursor_pos(t), (2, 1)) 112 | t.set_width(5) 113 | self.assertEqual(tmux.cursor_pos(t), (2, 1)) 114 | self.assertEqual(tmux.visible_after_prompt(t), 115 | [' 1234', '56789', '$']) 116 | tmux.stepwise_resize_width(t, 20) 117 | tmux.stepwise_resize_height(t, 20) 118 | 119 | def test_initial_size(self): 120 | tmux.assert_terminal_wide_enough(70) 121 | with tmux.TmuxPane(70, 3) as t: 122 | self.assertEqual(tmux.width(t), 70) 123 | self.assertEqual(tmux.height(t), 3) 124 | -------------------------------------------------------------------------------- /rlundoable/readme.md: -------------------------------------------------------------------------------- 1 | To test, try 2 | 3 | $ DYLD_LIBRARY_PATH=modified-readline-6.3/shlib ./test.out 4 | 5 | or 6 | 7 | $ DYLD_LIBRARY_PATH=modified-readline-6.3/shlib /usr/bin/irb 8 | 9 | 10 | 11 | #Implementation Notes 12 | 13 | Adds undo to interactive command line programs that use readline. 14 | Undo is implemented by forking the process every time readline() 15 | is called to save previous states, and returning to these saved 16 | states by exiting the child process which continued execution of 17 | the program if "undo" is entered. 18 | 19 | I'm trying out two different techniques for modifying readline: function interposition 20 | and patching readline. 21 | I'm also trying to figure out how how to get an executable to load the modified 22 | readline function instead of the system one. 23 | 24 | $ ./rlundo /usr/bin/irb 25 | $ ./rlundopatched /usr/bin/racket -il readline 26 | $ ./rlundo /usr/bin/lua 27 | 28 | ##Recipes 29 | 30 | irb (interactive Ruby interpreter) 31 | ---------------------------------- 32 | 33 | I can get `irb` to work on linux and OSX with 34 | 35 | $ make -f Makefilelinux rlundo 36 | $ ./rlundo /usr/bin/irb 37 | irb(main):001:0> a = 1 38 | => 1 39 | irb(main):002:0> undo 40 | undoing 'a = 1' 41 | irb(main):001:0> a 42 | NameError: undefined local variable or method `a' for main:Object 43 | 44 | racket 45 | ------ 46 | 47 | Racket is working for me on linux, but not osx where weird things seem to happen. 48 | 49 | $ make -f Makefilelinux rlundopatched 50 | gcc rlundopatched.c -o rlundopatched 51 | $ ./rlundopatched /usr/bin/racket -il readline 52 | Welcome to Racket v5.3.6. 53 | > (define a 1) 54 | > undo 55 | undoing '(define a 1)' 56 | > a 57 | a: undefined; 58 | cannot reference undefined identifier 59 | context...: 60 | /usr/share/racket/collects/racket/private/misc.rkt:87:7 61 | 62 | Racket bugs: 63 | * not quite working on osx - racket crashes in many cases 64 | 65 | lua 66 | --- 67 | 68 | lua works either way on linux: 69 | 70 | $ make -f Makefilelinx rlundo 71 | $ ./rlundo /usr/bin/lua 72 | Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio 73 | > a = 1 74 | > undo 75 | undoing 'a = 1' 76 | > print(a) 77 | nil 78 | 79 | but on osx it requires changing the library path: 80 | 81 | $ make -f Makefileosx rlundolibpath patched-readline 82 | $ cd modified-libreadline-6.3/shlib 83 | $ ./rlundo /usr/bin/lua 84 | Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio 85 | 86 | python 87 | ------ 88 | 89 | Python and IPython don't work yet! The readline linking story seems more 90 | complicated. I'd appreciate help with this! Right now python and ipython 91 | use wrappers written in Python that take the place of the patched readline. 92 | 93 | ##Techniques for creating the substitute readline function 94 | 95 | ###Proxying readline calls to your system readline (function interposition) 96 | 97 | rlundoable.c contains a function called readline which when called by a program with 98 | find your call out to the readline function the program would have used. 99 | 100 | `irb` is known to work with this technique on linux and OSX. 101 | 102 | $ make -f Makefilelinux rlundo 103 | $ ./rlundo ./test.out 104 | enter a string: undo 105 | undoing 'with no command, so exiting' 106 | 107 | OSX support for this is flaky right now. 108 | Based on http://wwwold.cs.umd.edu/Library/TRs/CS-TR-4585/CS-TR-4585.pdf 109 | I think the more elegant solution of dynamically locating the 110 | original readline with (`dlsym(RTLD_NEXT, "readline")`) won't work 111 | (I tried for a bit and then read that and decided to give up) 112 | so we assume an exact location for the readline library. 113 | 114 | It's currently hardcoded to try to load readline from 115 | 116 | /usr/local/opt/readline/lib/libreadline.6.dylib 117 | 118 | which happens to be where brew puts it on my computer. 119 | 120 | ###Using a patched readline library 121 | 122 | Instead of proxying calls between a program and the real readline library, 123 | we can download the real readline library and patch it with our undo behavior. 124 | 125 | $ make -f Makefileosx libreadline.6.3.dylib 126 | ... (lots of compiling output) 127 | $ make -f Makefileosx libedit.3.dylib 128 | ln -s libreadline.6.3.dylib libedit.3.dylib 129 | $ DYLD_LIBRARY_PATH=. ./test.out 130 | enter a string: undo 131 | undoing 'with no command, so exiting' 132 | 133 | ##Techniques for getting programs to load our readline function 134 | 135 | ###LD_PRELOAD 136 | 137 | LD_PRELOAD is the method I've found referenced most often: add the name of 138 | a dynamic library (`lib*.so` or `lib*.dylib`) to an environmental variable 139 | that causing that file to be loaded before any others. 140 | 141 | rlundo uses this technique, but you can also test it by setting these environmental 142 | variables directly: 143 | 144 | linux: 145 | 146 | $ LD_PRELOAD=./librlundoable.dylib ./test.out 147 | 148 | osx: 149 | 150 | $ DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=./librlundoable.dylib ./test.out 151 | 152 | ###Library Path 153 | 154 | Some programs seem to implement loading behavior themselves and don't respect LD_PRELOAD. 155 | rlundolibpath runs programs in an environment with LD_LIBRARY_PATH set to the current directory, 156 | but you can also try this directly: 157 | 158 | $ rlundolibpath ./test.out 159 | $ LD_LIBRARY_PATH=. ./test.out 160 | 161 | For this to work, the name of the dynamic library needs to be the same as the one linked against, 162 | hence the aliasing in the makefile: 163 | 164 | $ make -f Makefileosx libedit.3.dylib 165 | ln -s libreadline.6.3.dylib libedit.3.dylib 166 | $ DYLD_LIBRARY_PATH=. ./test.out 167 | 168 | racket seems to work with this approach (but right now is segfaulting on osx) 169 | -------------------------------------------------------------------------------- /rlundo/pity.py: -------------------------------------------------------------------------------- 1 | import errno 2 | import fcntl 3 | import os 4 | import pty 5 | import select 6 | import signal 7 | import sys 8 | import termios 9 | import threading 10 | import tty 11 | import logging 12 | logging.basicConfig(filename='debug.log', level=logging.INFO) 13 | from .termhelpers import Nonblocking 14 | 15 | class TerminalLock(object): 16 | """Lock that can be over-released 17 | 18 | Every master write to the pty should be followed 19 | by a pty write to the terminal, but not every pty write 20 | is initiated by a master write.""" 21 | 22 | def __init__(self): 23 | self.rlock = threading.RLock() 24 | self.lock_count = 0 25 | 26 | def acquire(self): 27 | self.rlock.acquire() 28 | self.lock_count += 1 29 | 30 | def release(self): 31 | if self.lock_count > 0: 32 | self.rlock.release() 33 | self.lock_count -= 1 34 | 35 | def __enter__(self): 36 | return self.rlock.__enter__() 37 | 38 | def __exit__(self, *args, **kwargs): 39 | return self.rlock.__exit__(*args, **kwargs) 40 | 41 | 42 | 43 | CHILD = pty.CHILD 44 | STDIN_FILENO = pty.STDIN_FILENO 45 | STDOUT_FILENO = pty.STDOUT_FILENO 46 | STDERR_FILENO = pty.STDERR_FILENO 47 | 48 | def fork(handle_window_size=False): 49 | # copied from pty.py, with modifications 50 | master_fd, slave_fd = openpty() 51 | slave_name = os.ttyname(slave_fd) 52 | pid = os.fork() 53 | if pid == CHILD: 54 | # Establish a new session. 55 | os.setsid() 56 | os.close(master_fd) 57 | 58 | if handle_window_size: 59 | clone_window_size_from(slave_name, STDIN_FILENO) 60 | 61 | # Slave becomes stdin/stdout/stderr of child. 62 | os.dup2(slave_fd, STDIN_FILENO) 63 | os.dup2(slave_fd, STDOUT_FILENO) 64 | os.dup2(slave_fd, STDERR_FILENO) 65 | if (slave_fd > STDERR_FILENO): 66 | os.close (slave_fd) 67 | 68 | # Explicitly open the tty to make it become a controlling tty. 69 | tmp_fd = os.open(os.ttyname(STDOUT_FILENO), os.O_RDWR) 70 | os.close(tmp_fd) 71 | else: 72 | os.close(slave_fd) 73 | 74 | # Parent and child process. 75 | return pid, master_fd, slave_name 76 | 77 | 78 | def openpty(): 79 | return pty.openpty() 80 | 81 | def _copy(master_fd, master_read=pty._read, stdin_read=pty._read, 82 | terminal_output_lock=None): 83 | """Parent copy loop. 84 | Copies 85 | pty master -> standard output (master_read) 86 | standard input -> pty master (stdin_read)""" 87 | logging.debug('starting _copy loop') 88 | fds = [master_fd, STDIN_FILENO] 89 | while True: 90 | logging.debug('calling select in copy') 91 | rfds, wfds, xfds = select.select(fds, [], []) 92 | logging.debug('select call in copy finished! %r %r %r' % (rfds, wfds, xfds, )) 93 | if master_fd in rfds: 94 | logging.debug('master_fd is ready, so calling read') 95 | data = master_read(master_fd) 96 | logging.debug('master_fd master_read call done, got data: %r' % (data, )) 97 | if not data: # Reached EOF. 98 | fds.remove(master_fd) 99 | else: 100 | os.write(STDOUT_FILENO, data) 101 | if terminal_output_lock is not None: 102 | terminal_output_lock.release() 103 | 104 | if STDIN_FILENO in rfds: 105 | logging.debug('stdin is ready, dealing...') 106 | 107 | with Nonblocking(STDIN_FILENO): 108 | try: 109 | data = stdin_read(STDIN_FILENO) 110 | except OSError as e: 111 | if e.errno != errno.EAGAIN: 112 | raise 113 | else: 114 | if not data: 115 | fds.remove(STDIN_FILENO) 116 | else: 117 | if terminal_output_lock is not None: 118 | terminal_output_lock.acquire() 119 | pty._writen(master_fd, data) 120 | logging.debug('done dealing with stdin') 121 | 122 | def spawn(argv, master_read=pty._read, stdin_read=pty._read, handle_window_size=False, 123 | terminal_output_lock=None): 124 | # copied from pty.py, with modifications 125 | # note that it references a few private functions - would be nice to not 126 | # do that, but you know 127 | if type(argv) == type(''): 128 | argv = (argv,) 129 | pid, master_fd, slave_name = fork(handle_window_size) 130 | if pid == CHILD: 131 | os.execlp(argv[0], *argv) 132 | try: 133 | mode = tty.tcgetattr(STDIN_FILENO) 134 | tty.setraw(STDIN_FILENO) 135 | restore = 1 136 | except tty.error: # This is the same as termios.error 137 | restore = 0 138 | 139 | if handle_window_size: 140 | signal.signal( 141 | signal.SIGWINCH, 142 | lambda signum, frame: _winch(slave_name, pid) 143 | ) 144 | 145 | while True: 146 | try: 147 | _copy(master_fd, master_read, stdin_read, terminal_output_lock) 148 | except OSError as e: 149 | if e.errno == errno.EINTR: 150 | continue 151 | if restore: 152 | tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) 153 | except select.error as e: 154 | if not sys.version_info.major == 2: # in Python 2 EINTR is a select.error 155 | raise 156 | if e[0] == errno.EINTR: 157 | continue 158 | raise 159 | break 160 | 161 | os.close(master_fd) 162 | return os.waitpid(pid, 0)[1] 163 | 164 | def clone_window_size_from(slave_name, from_fd): 165 | slave_fd = os.open(slave_name, os.O_RDWR) 166 | try: 167 | fcntl.ioctl( 168 | slave_fd, 169 | termios.TIOCSWINSZ, 170 | fcntl.ioctl(from_fd, termios.TIOCGWINSZ, " " * 1024) 171 | ) 172 | finally: 173 | os.close(slave_fd) 174 | 175 | def _winch(slave_name, child_pid): 176 | clone_window_size_from(slave_name, STDIN_FILENO) 177 | os.kill(child_pid, signal.SIGWINCH) 178 | -------------------------------------------------------------------------------- /rlundo/termrewrite.py: -------------------------------------------------------------------------------- 1 | """ 2 | rewrite runs a command as a subprocess, and has an api for saving 3 | terminal state. 4 | """ 5 | 6 | import locale 7 | import logging 8 | import os 9 | import re 10 | import socket 11 | import sys 12 | import tempfile 13 | import threading 14 | import time 15 | 16 | import blessings 17 | 18 | from . import pity 19 | from .findcursor import get_cursor_position 20 | 21 | # version 1: record sequences, guess how many lines to go back up 22 | terminal = blessings.Terminal() 23 | encoding = locale.getdefaultlocale()[1] 24 | 25 | outputs = [b''] 26 | terminal_output_lock = pity.TerminalLock() 27 | stdin_lock = threading.Lock() 28 | 29 | 30 | logger = logging.getLogger(__name__) 31 | logging.basicConfig(filename='example.log', level=logging.INFO) 32 | 33 | 34 | def temp_name(s): 35 | name = os.path.join(tempfile.gettempdir(), 'rlundo' + str(os.getpid()) + s) 36 | if os.path.exists(name): 37 | os.remove(name) 38 | if ' ' in name: 39 | raise ValueError('Things will fall apart if temp files have spaces in them: %r' % name) 40 | return name 41 | 42 | 43 | class UnlinkWrapper(object): 44 | def __init__(self, filename): 45 | self.filename = filename 46 | def __enter__(self): 47 | pass 48 | def __exit__(self, *args): 49 | os.remove(self.filename) 50 | 51 | 52 | def write(data): 53 | sys.stdout.write(data) 54 | sys.stdout.flush() 55 | 56 | 57 | def save(): 58 | time.sleep(.001) 59 | # correct way to do this would be to 60 | # wait until there's nothing to read on the pty 61 | 62 | outputs.append(b'') 63 | logger.info('full output stack: %r' % (outputs, )) 64 | 65 | 66 | def count_lines(msg, width): 67 | """Number of lines msg would move cursor down at a terminal width""" 68 | resized_lines = [_rows_required(line, width) for line in msg.split(u'\n')] 69 | num_lines = sum(resized_lines) - 1 70 | return num_lines 71 | 72 | 73 | def _visible_characters(line): 74 | """Number of characters in string without color escape characters.""" 75 | line_without_colours = re.sub(u"\x1b[[]0(;\d\d)?m", u"", line) 76 | line_without_colours = line_without_colours.strip(u"\n") 77 | return len(line_without_colours) 78 | 79 | 80 | def _rows_required(line, width): 81 | """Calculate how many rows a line will need to be printed""" 82 | return max(0, (_visible_characters(line) - 1) // width) + 1 83 | 84 | 85 | def linesplit(lines, width): 86 | rows = [] 87 | for line in lines: 88 | rows.extend(line[i:i + width] for i in range(0, len(line), width)) 89 | return rows 90 | 91 | 92 | def history(sequences): 93 | full = b''.join(sequences) 94 | return full.split(b'\n') 95 | 96 | HISTORY_BROKEN_MSG = '#<---History contiguity broken by rewind--->' 97 | 98 | 99 | def restore(): 100 | with terminal_output_lock: 101 | _restore() 102 | 103 | 104 | def _restore(): 105 | """Clears the current line and as many lines above as needed?""" 106 | logger.debug('full output stack: %r' % (outputs, )) 107 | lines_between_saves = outputs.pop() if outputs else '' 108 | lines_after_save = outputs.pop() if outputs else '' 109 | lines = lines_between_saves + lines_after_save 110 | logger.info('lines to rewind: %r' % (lines, )) 111 | n = count_lines(lines.decode(encoding), terminal.width) 112 | logger.info('number of lines to rewind %d' % (n, )) 113 | with stdin_lock: 114 | lines_available, _ = get_cursor_position(sys.stdout, sys.stdin) 115 | logger.debug('lines move: %d lines_available: %d' % (n, lines_available)) 116 | if n > lines_available: 117 | for _ in range(200): 118 | write(terminal.move_left) 119 | write(terminal.clear_eol) 120 | for _ in range(lines_available): 121 | write(terminal.move_up) 122 | write(terminal.clear_eol) 123 | write(HISTORY_BROKEN_MSG[:terminal.width]) 124 | write('\n') 125 | for _ in range(terminal.height - 2): 126 | write(terminal.move_down) 127 | for _ in range(200): 128 | write(terminal.move_left) 129 | write('\n') 130 | for _ in range(terminal.height - 1): 131 | write(terminal.move_up) 132 | middle = terminal.height // 2 133 | 134 | for line in history(outputs)[:-1][-middle:]: 135 | write((line + b'\r\n').decode('utf8')) 136 | 137 | else: 138 | logger.debug('moving cursor %d lines up for %r' % (n, lines)) 139 | for _ in range(n): 140 | write(terminal.move_up) 141 | for _ in range(200): 142 | write(terminal.move_left) 143 | write(terminal.clear_eos) 144 | 145 | 146 | def set_up_listener(handler, addr): 147 | def forever(): 148 | while True: 149 | conn, addr = sock.accept() 150 | handler() 151 | conn.close() 152 | 153 | sock = socket.socket(family=socket.AF_UNIX) 154 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 155 | sock.bind(addr) 156 | sock.listen(1) 157 | t = threading.Thread(target=forever) 158 | t.daemon = True 159 | t.start() 160 | return sock, t 161 | 162 | 163 | def master_read(fd): 164 | data = os.read(fd, 1024) 165 | logger.info('read data: %r' % data) 166 | if outputs: 167 | outputs[-1] += data 168 | return data 169 | 170 | 171 | def stdin_read(fd): 172 | with stdin_lock: 173 | data = os.read(fd, 1024) 174 | logger.info('read from stdin: %r' % data) 175 | return data 176 | 177 | 178 | def run(argv): 179 | pity.spawn(argv, 180 | master_read=master_read, 181 | stdin_read=stdin_read, 182 | handle_window_size=True, 183 | terminal_output_lock=terminal_output_lock) 184 | 185 | 186 | def run_with_listeners(args, save_addr=None, restore_addr=None, print_addrs=False): 187 | if save_addr is None: 188 | save_addr = temp_name('save') 189 | if restore_addr is None: 190 | restore_addr = temp_name('restore') 191 | if print_addrs: 192 | print("save: {}\nrestore: {}".format(save_addr, restore_addr)) 193 | print("trigger these with nc -U ") 194 | print("they have been saved in this process's envvars as") 195 | print("RLUNDO_SAVE and RLUNDO_RESTORE") 196 | listeners = [set_up_listener(save, save_addr), 197 | set_up_listener(restore, restore_addr)] 198 | os.environ["RLUNDO_SAVE"] = save_addr 199 | os.environ["RLUNDO_RESTORE"] = restore_addr 200 | with UnlinkWrapper(save_addr): 201 | with UnlinkWrapper(restore_addr): 202 | run(args) 203 | -------------------------------------------------------------------------------- /test/tmux.py: -------------------------------------------------------------------------------- 1 | from functools import partial 2 | import os 3 | import sys 4 | import tempfile 5 | import time 6 | 7 | import tmuxp 8 | import blessings 9 | 10 | py2 = sys.version_info.major == 2 11 | 12 | 13 | def assert_terminal_wide_enough(width=70): 14 | term = blessings.Terminal() 15 | required_width = width + 3 16 | msg = ("Terminal is too narrow. " 17 | "Please make it %s columns or wider and rerun tests." % width) 18 | assert term.width >= required_width, msg 19 | 20 | 21 | def all_contents(pane): 22 | return pane.cmd('capture-pane', '-epS', '-10000').stdout 23 | 24 | 25 | def all_lines(pane): 26 | return pane.cmd('capture-pane', '-epJS', '-10000').stdout 27 | 28 | 29 | def scrollback(pane): 30 | all = all_contents(pane) 31 | return all[:len(all)-len(visible(pane))] 32 | 33 | 34 | def visible(pane): 35 | return pane.cmd('capture-pane', '-ep').stdout 36 | 37 | 38 | def visible_without_formatting(pane): 39 | return pane.cmd('capture-pane', '-p').stdout 40 | 41 | 42 | def visible_after_prompt(pane, expected=u'$', interval=.1, max=1): 43 | """Return the visible region once expected is found on last line""" 44 | t0 = time.time() 45 | while True: 46 | if time.time() > t0 + max: 47 | raise ValueError("prompt %r didn't appear within max time: %r" % (expected, screen)) 48 | screen = visible(pane) 49 | if screen and screen[-1] == expected: 50 | return screen 51 | time.sleep(interval) 52 | 53 | 54 | def wait_for_prompt(pane, expected=u'$', interval=.01, max=2): 55 | visible_after_prompt(pane, expected=expected, interval=interval, max=max) 56 | 57 | 58 | def wait_for_condition(pane, final, query, condition=lambda x, y: x == y, 59 | interval=0.01, max=1): 60 | """Poll for a condition to be true, returning once it is. 61 | 62 | condition takes a response to the query and a final value, returns True if done waiting 63 | query is a function which returns a value when called on pane 64 | final is the value we're looking for""" 65 | t0 = time.time() 66 | while True: 67 | if time.time() > t0 + max: 68 | raise ValueError("condition was never true within max time: cond(%r, %r)" % (last, final)) 69 | last = query(pane) 70 | if condition(last, final): 71 | break 72 | time.sleep(interval) 73 | 74 | 75 | def cursor_pos(pane): 76 | """Returns zero-indexed cursor position""" 77 | process = pane.cmd('list-panes', '-F', '#{pane_height} #{pane_width} #{cursor_x} #{cursor_y}') 78 | height, width, x, y = [int(x) for x in process.stdout[0].split()] 79 | return y, x 80 | 81 | 82 | def width(pane): 83 | process = pane.cmd('list-panes', '-F', '#{pane_height} #{pane_width} #{cursor_x} #{cursor_y}') 84 | height, width, x, y = [int(x) for x in process.stdout[0].split()] 85 | return width 86 | 87 | 88 | def height(pane): 89 | process = pane.cmd('list-panes', '-F', '#{pane_height} #{pane_width} #{cursor_x} #{cursor_y}') 90 | height, width, x, y = [int(x) for x in process.stdout[0].split()] 91 | return height 92 | 93 | 94 | wait_for_width = partial(wait_for_condition, query=width) 95 | wait_for_height = partial(wait_for_condition, query=height) 96 | 97 | 98 | def stepwise_resize_width(pane, final_width): 99 | 100 | initial = width(pane) 101 | if initial < final_width: 102 | for _ in range(final_width - initial): 103 | pane.cmd('resize-pane', '-R', str(1)) 104 | elif initial > final_width: 105 | for _ in range(initial - final_width): 106 | pane.cmd('resize-pane', '-L', str(1)) 107 | else: 108 | return 109 | wait_for_width(pane, final_width) 110 | 111 | 112 | def stepwise_resize_height(pane, final_height): 113 | 114 | initial = height(pane) 115 | if initial < final_height: 116 | for _ in range(final_height - initial): 117 | pane.cmd('resize-pane', '-D', str(1)) 118 | elif initial > final_height: 119 | for _ in range(initial - final_height): 120 | pane.cmd('resize-pane', '-U', str(1)) 121 | else: 122 | return 123 | wait_for_height(pane, final_height) 124 | 125 | 126 | def window_name(pane): 127 | """Returns zero-indexed cursor position""" 128 | process = pane.cmd('list-panes', '-F', '#{window_name}') 129 | (name, ) = process.stdout[0].split() 130 | return name 131 | 132 | 133 | def wait_until_cursor_moves(pane, row, col, interval=.02, max=1): 134 | """if cursor_row and cursor""" 135 | t0 = time.time() 136 | while True: 137 | if time.time() > t0 + max: 138 | raise ValueError("cursor row didn't move from initial %r within max time: %r" % ((row, col), visible(pane))) 139 | if (row, col) != cursor_pos(pane): 140 | return 141 | time.sleep(interval) 142 | 143 | 144 | def send_command(pane, cmd, enter=True, prompt=u'$', maxtime=2): 145 | if not isinstance(enter, bool): 146 | raise ValueError("enter should be a bool, got %r" % (enter, )) 147 | row, col = cursor_pos(pane) 148 | pane.cmd('send-keys', cmd) 149 | wait_until_cursor_moves(pane, row, col) 150 | if enter: 151 | pane.enter() 152 | wait_for_prompt(pane, expected=prompt, max=maxtime) 153 | 154 | 155 | class TmuxPane(object): 156 | def __init__(self, width=None, height=None, use_existing_session=None): 157 | self.width = width 158 | self.height = height 159 | assert_terminal_wide_enough(width) 160 | self.tempfiles_to_close = [] 161 | if use_existing_session is not None: 162 | self.use_existing_session = use_existing_session 163 | elif 'RLUNDO_USE_EXISTING_TMUX_SESSION' in os.environ: 164 | self.use_existing_session = True 165 | else: 166 | self.use_existing_session = False 167 | 168 | def tmux_config_contents(self): 169 | return """""" 170 | 171 | def bash_config_contents(self): 172 | return """export PS1='$'""" 173 | 174 | def tempfile(self, contents, suffix=''): 175 | tmp = tempfile.NamedTemporaryFile(suffix=suffix) 176 | self.tempfiles_to_close.append(tmp) 177 | if py2: 178 | tmp.write(contents) 179 | else: 180 | tmp.write(contents.encode('utf8')) 181 | tmp.flush() 182 | return tmp 183 | 184 | def __enter__(self): 185 | self.tmux_config = self.tempfile(self.tmux_config_contents()) 186 | self.bash_config = self.tempfile(self.bash_config_contents()) 187 | 188 | self.server = tmuxp.Server() 189 | if self.use_existing_session: 190 | try: 191 | session_dict = self.server.attached_sessions() # until tmuxp bug is fixed 192 | if session_dict is None: 193 | raise tmuxp.exc.TmuxpException 194 | self.session = tmuxp.Session(self.server, **session_dict[0]) 195 | except tmuxp.exc.TmuxpException: 196 | self.session = self.server.new_session(session_name='rlundotesting') 197 | else: 198 | self.session = self.server.new_session(session_name='rlundotesting', kill_session=True) 199 | 200 | self.window = self.session.new_window(attach=False) 201 | try: 202 | output = self.window.panes[0].cmd('respawn-pane', '-k', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) 203 | if output.stderr: 204 | raise ValueError(repr(output.stderr) + " " + repr(self.window.panes)) 205 | (pane, ) = self.window.panes 206 | output = pane.cmd('respawn-pane', '-k', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) 207 | if output.stderr: 208 | raise ValueError(repr(output.stderr) + " " + repr(self.window.panes)) 209 | pane.cmd('split-window', '-h', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) 210 | pane.cmd('split-window', '-v', 'bash --rcfile %s --noprofile' % (self.bash_config.name, )) 211 | if self.width is not None: 212 | pane.set_width(self.width) 213 | wait_for_width(pane, self.width) 214 | if self.height is not None: 215 | pane.set_height(self.height) 216 | wait_for_height(pane, self.height) 217 | wait_for_prompt(pane) 218 | return pane 219 | except: 220 | self.window.kill_window() 221 | raise 222 | 223 | def __exit__(self, type, value, tb): 224 | for file in self.tempfiles_to_close: 225 | file.close() 226 | self.window.kill_window() 227 | 228 | if __name__ == '__main__': 229 | with TmuxPane(10, 10) as t: 230 | print(visible(t)) 231 | print(cursor_pos(t)) 232 | t.send_keys('true 1234') 233 | t.send_keys('true 123456789', False) 234 | print(visible(t)) 235 | t.set_width(5) 236 | print('change width to 5') 237 | print(visible(t)) 238 | print(cursor_pos(t)) 239 | t.send_keys(' ') 240 | print(visible_after_prompt(t)) 241 | print(visible(t)) 242 | print(cursor_pos(t)) 243 | -------------------------------------------------------------------------------- /test/test_rlundo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | test_rlundo 5 | 6 | Integration test for undo features implemented in rlundo. It sends commands 7 | to an open tmux terminal using 'undo' and checks that the output is the 8 | expected one. 9 | 10 | To create new tests you may one to use py_logger to capture decoded bytes from 11 | a python terminal and see how they look: 12 | `python py_logger.py ipython` or 13 | `python py_logger.py python` 14 | 15 | For some reason, the formatting is not handled properly by tmux.visible() 16 | method and thus the decode logging captured may differ, it has to be used only 17 | as a guide. 18 | """ 19 | 20 | from __future__ import unicode_literals 21 | import unittest 22 | import nose 23 | import sys 24 | 25 | from . import tmux 26 | 27 | 28 | class ActualUndo(tmux.TmuxPane): 29 | 30 | """ 31 | Run a tmux pane instance to start a repl, send commands, use undo and check 32 | the expected output. 33 | 34 | ActualUndo is better used within the context manager and tmux module can be 35 | used to send commands: 36 | 37 | with ActualUndo(80, 30) as t: 38 | tmux.send_command(t, "print 'hi'", prompt=expected_prompt) 39 | output = tmux.visible(t) 40 | 41 | If you want to see what's going on while sending commands from a repl, you 42 | can do that by oppening a session with the __enter__() method and 43 | attaching a tmux session to it. 44 | 45 | (in ipython) 46 | from test_rlundo import ActualUndo 47 | w = ActualUndo(50, 30).__enter__() 48 | 49 | (in another terminal) 50 | tmux attach 51 | 52 | (in ipython) 53 | w.send_keys("a = 1") 54 | w.send_keys("a") 55 | 56 | In the other terminal you will see the session inputs and outputs. 57 | """ 58 | 59 | def bash_config_contents(self): 60 | python_env = sys.executable 61 | return """ 62 | export PS1='$' 63 | alias irb="python rlundo /usr/bin/irb" 64 | alias ipy="{} rlundo ipython -i {}" 65 | """.format(python_env, self.ipython_startup.name) 66 | 67 | def ipython_config_contents(self): 68 | return """%colors linux""" 69 | 70 | def __enter__(self): 71 | """Initialize a pane with an undo scenario. 72 | 73 | undo schenarios always start by calling python rewrite.py 74 | 75 | >>> with ActualUndo(30, 10) as t: 76 | ... tmux.send_command(t, 'echo hello') 77 | ... tmux.visible_after_prompt(t, expected=u'$') == ['$echo hello', 'hello', '$'] 78 | True 79 | """ 80 | self.ipython_startup = self.tempfile(self.ipython_config_contents(), suffix='.ipy') 81 | return tmux.TmuxPane.__enter__(self) 82 | 83 | 84 | class IPyPrompt(object): 85 | 86 | @classmethod 87 | def in_formatted(cls, num): 88 | """Build ipython "In" prompt with formatting. 89 | 90 | For some reason tmux doesn't deal with formatting in the same way that 91 | normal ipython does, the proper ipython line for prompt is included as 92 | a comment below.""" 93 | # return u'\x1b[34mIn [\x1b[1m{}\x1b[0;34m]:'.format(num) 94 | return u'\x1b[32mIn [\x1b[1m{}\x1b[0m]:'.format(num) 95 | 96 | @classmethod 97 | def out_formatted(cls, num): 98 | """Build ipython "Out" prompt with formatting.""" 99 | 100 | # For some reason tmux doesn't deal with formatting in the same way that 101 | # normal ipython does, the proper ipython line for prompt is included as 102 | # a comment below. 103 | # return u'\x1b[31mOut[\x1b[1m{}\x1b[0;34m]:'.format(num) 104 | return u'\x1b[31mOut[\x1b[1m{}\x1b[0m]:'.format(num) 105 | 106 | @classmethod 107 | def new_l_formatted(cls): 108 | """Build ipython new line in the same "In" prompt with formatting.""" 109 | # For some reason tmux doesn't deal with formatting in the same way that 110 | # normal ipython does, the proper ipython line for prompt is included as 111 | # a comment below.""" 112 | # return u'\x1b[34m ...: \x1b[0m' 113 | return u'\x1b[32m ...: \x1b[39m' 114 | 115 | @classmethod 116 | def in_prompt(cls, num): 117 | """Build ipython "In" prompt without formatting.""" 118 | return u'In [{}]:'.format(num) 119 | 120 | @classmethod 121 | def out_prompt(cls, num): 122 | """Build ipython "Out" prompt without formatting.""" 123 | return u'Out[{}]:'.format(num) 124 | 125 | @classmethod 126 | def new_l_prompt(cls): 127 | """Build ipython new line inside an "In" prompt without formatting.""" 128 | return u' ...:' 129 | 130 | 131 | @unittest.skipIf(sys.version_info[0] == 3, "IPython interpreter doesn't work with Ipython 3") 132 | class TestUndoableIpythonWithTmux(unittest.TestCase): 133 | 134 | """Use ActualUndo and tmux to send commands to an IPython repl and check 135 | undo feature rewrite the terminal as expected. 136 | """ 137 | 138 | # @unittest.skip("skip") 139 | def test_simple(self): 140 | """Test sending commands and reading formatted output with tmux.""" 141 | with ActualUndo(70, 20) as t: 142 | tmux.send_command(t, 'ipy', prompt=IPyPrompt.in_formatted(1), maxtime=4) 143 | tmux.send_command(t, 'a = 1', prompt=IPyPrompt.in_formatted(2)) 144 | tmux.send_command(t, 'a', prompt=IPyPrompt.in_formatted(3)) 145 | tmux.send_command(t, 'def foo():', 146 | prompt=IPyPrompt.new_l_formatted()) 147 | tmux.send_command(t, 'print "hi"', 148 | prompt=IPyPrompt.new_l_formatted()) 149 | tmux.send_command(t, ' ', prompt=IPyPrompt.in_formatted(4)) 150 | output = tmux.visible(t) 151 | lines = [IPyPrompt.in_formatted(1) + ' \x1b[39ma = 1', 152 | IPyPrompt.in_formatted(2) + ' \x1b[39ma', 153 | IPyPrompt.out_formatted(2) + ' \x1b[39m1', 154 | IPyPrompt.in_formatted(3) + ' \x1b[39mdef foo():', 155 | IPyPrompt.new_l_formatted() + ' print "hi"', 156 | IPyPrompt.new_l_formatted() + '', 157 | IPyPrompt.in_formatted(4)] 158 | self.assertEqual(output[-7:], lines) 159 | 160 | # @unittest.skip("skip") 161 | def test_undo_simple(self): 162 | """Test undoing one liner command in ipython.""" 163 | with ActualUndo(70, 20) as t: 164 | 165 | # type some commands 166 | tmux.send_command(t, 'ipy', prompt=IPyPrompt.in_formatted(1), maxtime=4) 167 | tmux.send_command(t, 'a = 1', prompt=IPyPrompt.in_formatted(2)) 168 | tmux.send_command(t, 'a', prompt=IPyPrompt.in_formatted(3)) 169 | output = tmux.visible_without_formatting(t) 170 | lines = [IPyPrompt.in_prompt(1) + ' a = 1', 171 | IPyPrompt.in_prompt(2) + ' a', 172 | IPyPrompt.out_prompt(2) + ' 1', 173 | IPyPrompt.in_prompt(3)] 174 | self.assertEqual(output[-4:], lines) 175 | 176 | # undo 177 | tmux.send_command(t, 'undo', prompt=IPyPrompt.in_formatted(2)) 178 | output = tmux.visible_without_formatting(t) 179 | self.assertEqual(output[-2:], 180 | lines[:1] + [IPyPrompt.in_prompt(2)]) 181 | 182 | # @unittest.skip("skip") 183 | def test_undo_multiple_input_lines(self): 184 | """Test undoing a line of a multiple lines command in ipython.""" 185 | with ActualUndo(70, 20) as t: 186 | 187 | # type some commands 188 | tmux.send_command(t, 'ipy', prompt=IPyPrompt.in_formatted(1), maxtime=4) 189 | tmux.send_command(t, 'def foo():', 190 | prompt=IPyPrompt.new_l_formatted()) 191 | tmux.send_command(t, 'print "hi"', 192 | prompt=IPyPrompt.new_l_formatted()) 193 | tmux.send_command(t, ' ', prompt=IPyPrompt.in_formatted(2)) 194 | tmux.send_command(t, 'foo()', prompt=IPyPrompt.in_formatted(3)) 195 | output = tmux.visible_without_formatting(t) 196 | lines = [IPyPrompt.in_prompt(1) + ' def foo():', 197 | IPyPrompt.new_l_prompt() + ' print "hi"', 198 | IPyPrompt.new_l_prompt() + '', 199 | IPyPrompt.in_prompt(2) + ' foo()', 200 | 'hi', 201 | IPyPrompt.in_prompt(3)] 202 | self.assertEqual(output[-6:], lines) 203 | 204 | # undo 205 | tmux.send_command(t, 'undo', prompt=IPyPrompt.in_formatted(2)) 206 | output = tmux.visible_without_formatting(t) 207 | self.assertEqual(output[-4:], lines[:3] + [IPyPrompt.in_prompt(2)]) 208 | 209 | # undo again 210 | tmux.send_command(t, 'undo', prompt=IPyPrompt.new_l_formatted()) 211 | import time 212 | time.sleep(1) 213 | output = tmux.visible_without_formatting(t) 214 | self.assertEqual(output[-3:], lines[:2] + [IPyPrompt.new_l_prompt()]) 215 | 216 | 217 | if __name__ == '__main__': 218 | nose.run(defaultTest=__name__) 219 | # unittest.main() 220 | -------------------------------------------------------------------------------- /test/terminal_dsl.py: -------------------------------------------------------------------------------- 1 | """ 2 | Terminal diagrams for describing how text is moved when the window of a 3 | terminal emulator is resized. 4 | 5 | # @ is cursor 6 | # + means this is a continued line 7 | # . means a space character (spaces are empty) 8 | # ~ means this line is empty (no content on line, no newline on prev line) 9 | # everything else is content 10 | 11 | A diagram looks like this: 12 | +-----+ 13 | |ABC | 14 | +-----+ 15 | |BC | 16 | |abc@ | 17 | | | 18 | +-----+ 19 | 20 | """ 21 | from __future__ import print_function 22 | 23 | import re 24 | from collections import namedtuple 25 | 26 | def divide_term_states(s): 27 | """Return a list of vertically divided terminal diagrams from one string 28 | 29 | >>> len(divide_term_states(''' 30 | ... +-----+ +-------+ +--+ 31 | ... |ABC | |ABC | +--+ 32 | ... +-----+ +-------+ |@ | 33 | ... |BC | -> |BC | +--+ 34 | ... |abc@ | |abc@ | 35 | ... |~ | |~ | 36 | ... +-----+ +-------+ 37 | ... ''')) 38 | 3 39 | """ 40 | # TODO allow the first line to have content on it (has to be blank right now) 41 | lines = s.split('\n') 42 | if lines[0].strip(): 43 | raise ValueError('Top line needs to be blank') 44 | max_length = max(len(line) for line in lines) 45 | spaces_by_line = [set(m.start() for m in re.finditer(r' ', line)).union( 46 | set(range(len(line), max_length))) 47 | for line in s.split('\n') if s.strip()] 48 | empty_columns = set.intersection(*spaces_by_line) 49 | empty_columns.add(max_length) 50 | 51 | sections = [] 52 | last = 0 53 | for index in sorted(empty_columns): 54 | vertical_strip = [] 55 | for line in s.split('\n'): 56 | vertical_strip.append(line[last:index]) 57 | sections.append(vertical_strip) 58 | last = index 59 | candidates = ['\n'.join(section) for section in sections] 60 | diagrams = [x for x in candidates if '|' in x and '-' in x and '+' in x] 61 | return diagrams 62 | 63 | _TerminalStateBase = namedtuple('TerminalState', [ 64 | 'lines', # logical lines of history and content 65 | 'cursor_line', # 0-indexed logical line of cursor 66 | 'cursor_offset', # position in logical line of first character 67 | 'width', # number of columns 68 | 'height', # number of visible rows 69 | 'history_height' # number of history rows 70 | ]) 71 | 72 | 73 | def split_line(line, width): 74 | """ 75 | >>> split_line('abcdefg', 3) 76 | ['abc', 'def', 'g'] 77 | """ 78 | if line == '': 79 | return [''] 80 | splits = list(range(0, len(line), width)) + [len(line)] 81 | return [line[start:end] for start, end in zip(splits[:-1], splits[1:])] 82 | 83 | 84 | def split_lines(lines, width): 85 | """ 86 | >>> split_lines(['abcd', 'e', 'fgh', ''], 3) 87 | ['abc', 'd', 'e', 'fgh', ''] 88 | """ 89 | rows = [] 90 | for line in lines: 91 | rows.extend(split_line(line, width)) 92 | return rows 93 | 94 | 95 | def horzcat(a, b): 96 | a_lines = a.split('\n') 97 | b_lines = b.split('\n') 98 | width = max(len(line) for line in a_lines) 99 | full_lines = [line + ' '*(width - len(line)) for line in a_lines] 100 | full_left = full_lines + [' '*width] * max(0, len(b_lines) - len(a_lines)) 101 | full_right = b_lines + [''] * max(0, len(a_lines) - len(b_lines)) 102 | assert len(full_left) == len(full_right) 103 | return '\n'.join(l+r for l, r in zip(full_left, full_right)) 104 | 105 | 106 | class TerminalState(_TerminalStateBase): 107 | 108 | @classmethod 109 | def from_tmux_pane(cls, pane): 110 | from . import tmux 111 | lines = tmux.all_lines(pane) 112 | history_height = len(tmux.scrollback(pane)) 113 | print('tmux.all_contents:', tmux.all_contents(pane)) 114 | print('tmux.visible:', tmux.visible(pane)) 115 | print('history_height:', history_height) 116 | print('tmux.scrollback:', tmux.scrollback(pane)) 117 | width, height = tmux.width(pane), tmux.height(pane) 118 | 119 | cursor_row, cursor_col = tmux.cursor_pos(pane) 120 | 121 | #TODO deal with cursors not at the bottom 122 | 123 | termstate = TerminalState( 124 | lines=lines, 125 | cursor_line=len(lines) - 1, 126 | cursor_offset=len(lines[-1]), 127 | width=width, 128 | height=height, 129 | history_height=history_height) 130 | print(termstate) 131 | 132 | #assert termstate.cursor_row == cursor_row 133 | #assert termstate.cursor_column == cursor_col 134 | 135 | return termstate 136 | 137 | def visible_diff(self, other): 138 | if self != other: 139 | s1, s2 = self.render(), other.render() 140 | display = horzcat(s1, s2) 141 | if len(self.lines) != len(other.lines): 142 | error = ('Terminal states have different number of lines:' 143 | '%d and %d' % (len(self.lines), len(other.lines))) 144 | elif self.lines != other.lines: 145 | for i, (a, b) in enumerate(zip(self.lines, other.lines)): 146 | if a != b: 147 | error = "line %d is the first line to differ:" % (i, ) 148 | break 149 | else: 150 | error = "Terminal states differ somehow:" 151 | return error + '\n' + display + '\n' + repr(self) + '\n' + repr(other) 152 | return 'TerminalStates do not differ' 153 | 154 | def render(self): 155 | horz_border = '+' + '-'*self.width + '+' 156 | output = [] 157 | output.append(horz_border) 158 | row_num = -1 159 | in_history = True 160 | cursor_row, cursor_col = self.cursor_row, self.cursor_column 161 | cursor_row = self.cursor_row 162 | for line in self.lines: 163 | line_rows = [] 164 | for row in split_line(line, self.width): 165 | row_num += 1 166 | if in_history and row_num == self.history_height: 167 | output.append(horz_border) 168 | row_num = 0 169 | in_history = False 170 | if not in_history and row_num == cursor_row: 171 | row = row[:cursor_row] + '@' + row[cursor_col+1:] 172 | line_rows.append('|' + row + ' '*(self.width - len(row)) + '|') 173 | line_rows = ([r[:-1]+'+' for r in line_rows[:len(line_rows)-1]] + 174 | [line_rows[-1]]) 175 | 176 | output.extend(line_rows) 177 | while row_num < self.height - 1: 178 | output.append('~' + ' '*self.width + '~') 179 | row_num += 1 180 | output.append(horz_border) 181 | return '\n'.join(output) 182 | 183 | @property 184 | def visible_rows(self): 185 | return split_lines(self.lines, self.width)[self.history_height:] 186 | 187 | @property 188 | def history_rows(self): 189 | return split_lines(self.lines, self.width)[:self.history_height] 190 | 191 | @property 192 | def cursor_row(self): 193 | """one-indexed""" 194 | above = len(split_lines(self.lines[:self.cursor_line], self.width)) 195 | rows, _ = divmod(len(self.lines[self.cursor_line][:self.cursor_offset+1]), self.width) 196 | return above - self.history_height + rows + 1 197 | 198 | @property 199 | def cursor_column(self): 200 | """one-indexed""" 201 | _, offset = divmod(len(self.lines[self.cursor_line][:self.cursor_offset+1]), self.width) 202 | return offset + 1 203 | 204 | 205 | def parse_term_state(s): 206 | """Returns TerminalState tuple given a terminal state diagram 207 | 208 | >>> label, state = parse_term_state(''' 209 | ... label 210 | ... +-----------+ 211 | ... |12345678901+ 212 | ... |23456789 | 213 | ... |$ echo hi | 214 | ... +-----------+ 215 | ... |hi | 216 | ... |$ python | 217 | ... |>>> 1 + 1 | 218 | ... |2 | 219 | ... |@ | 220 | ... ~ ~ 221 | ... +-----------+ 222 | ... ''') 223 | >>> label 224 | 'label' 225 | >>> state.width 226 | 11 227 | >>> state.lines 228 | ['1234567890123456789', '$ echo hi', 'hi', '$ python', '>>> 1 + 1', '2', ''] 229 | >>> state.history_rows 230 | ['12345678901', '23456789', '$ echo hi'] 231 | >>> state.visible_rows 232 | ['hi', '$ python', '>>> 1 + 1', '2', ''] 233 | >>> (state.cursor_line, state.cursor_offset) 234 | (6, 0) 235 | >>> (state.cursor_row, state.cursor_column) 236 | (5, 1) 237 | """ 238 | 239 | top_border_match = re.search(r'(?<=\n)\s*([+][-]+[+])\s*(?=\n)', s) 240 | label = ' '.join(line.strip() 241 | for line in s[:top_border_match.start()].split('\n') 242 | if line.strip()) 243 | top_border = top_border_match.group(1) 244 | width = len(top_border) - 2 245 | 246 | sections = ('before', 'history', 'visible', 'after') 247 | section = sections[0] 248 | lines = [] 249 | unfinished_line = None 250 | section_heights = dict(zip(sections, [0] * len(sections))) 251 | 252 | input_rows = re.findall(r'(?<=\n)\s*([+|~].*[+|~])\s*(?=\n|\Z)', s) 253 | if not all(len(input_rows[0]) == len(r) for r in input_rows): 254 | raise ValueError('row differs in width from first') 255 | for input_row in input_rows: 256 | inner = input_row[1:-1] 257 | if inner == '-'*width: 258 | section = sections[sections.index(section) + 1] 259 | if section == 'after': 260 | break 261 | continue 262 | else: 263 | section_heights[section] += 1 264 | 265 | if input_row[0] == input_row[-1] == '~': 266 | if not section == 'visible': 267 | raise ValueError('~ in non-visible section') 268 | continue 269 | 270 | if input_row[-1] == '+': 271 | if unfinished_line is None: 272 | unfinished_line = '' 273 | unfinished_line += inner 274 | else: 275 | if unfinished_line is None: 276 | lines.append(inner.rstrip()) 277 | else: 278 | unfinished_line += inner.rstrip() 279 | lines.append(unfinished_line) 280 | unfinished_line = None 281 | else: 282 | raise ValueError("not enough sections, didn't finish parsing") 283 | if unfinished_line is not None: 284 | lines.append(unfinished_line) 285 | 286 | for cursor_line, line in enumerate(lines): 287 | if '@' in line: 288 | cursor_offset = line.index('@') 289 | lines[cursor_line] = line.replace('@', '') 290 | break 291 | else: 292 | raise ValueError("No cursor (@) found") 293 | 294 | return label, TerminalState( 295 | lines=lines, 296 | cursor_line=cursor_line, 297 | cursor_offset=cursor_offset, 298 | width=width, 299 | height=section_heights['visible'], 300 | history_height=section_heights['history'], 301 | ) 302 | 303 | 304 | 305 | # should eventually test xterm, gnome-terminal, iterm, terminal.app, tmux 306 | 307 | #terminal questions: 308 | # * does xterm have a difference between spaces and nothing? 309 | # * does xterm do cursor at bottom scroll up differently? 310 | # * can xterm ever have a clear space at bottom but history 311 | -------------------------------------------------------------------------------- /test/test_rewrite.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | import os 4 | import re 5 | import socket 6 | import time 7 | import unittest 8 | 9 | import nose 10 | from flaky import flaky 11 | 12 | from . import terminal_dsl 13 | from . import tmux 14 | from . import scenarioscript 15 | 16 | from .context import rlundo 17 | from rlundo import termrewrite 18 | 19 | 20 | def save(): 21 | s = socket.socket(family=socket.AF_UNIX) 22 | s.connect(os.environ['RLUNDO_SAVE']) 23 | assert b'' == s.recv(100) 24 | 25 | 26 | def restore(t): 27 | """Pretend to be the program we're undoing and print prompt""" 28 | s = socket.socket(family=socket.AF_UNIX) 29 | s.connect(os.environ['RLUNDO_RESTORE']) 30 | assert b'' == s.recv(100) 31 | t.cmd('send-keys', '>') 32 | time.sleep(.1) 33 | 34 | 35 | class DiagramsWithTmux(object): 36 | maxDiff = 10000 37 | 38 | def assert_undo(self, diagram, slow=False): 39 | states = [terminal_dsl.parse_term_state(x)[1] 40 | for x in terminal_dsl.divide_term_states(diagram)] 41 | if len(states) < 2: 42 | raise ValueError("Diagram has only one state") 43 | with UndoScenario(states[0]) as t: 44 | UndoScenario.initialize(t, states[0]) 45 | if slow: time.sleep(1) 46 | for before, after in zip(states[:-1], states[1:]): 47 | self.resize(before, after, t) 48 | if slow: time.sleep(1) 49 | if self.should_undo(before, after): 50 | restore(t) 51 | if slow: time.sleep(1) 52 | actual = terminal_dsl.TerminalState.from_tmux_pane(t) 53 | self.assertEqual(after, actual, after.visible_diff(actual)) 54 | self.assertEqual(tmux.all_contents(t), 55 | termrewrite.linesplit(after.lines, after.width)) 56 | 57 | def resize(self, before, after, t): 58 | tmux.stepwise_resize_width(t, after.width) 59 | tmux.stepwise_resize_height(t, after.height) 60 | 61 | def should_undo(self, s1, s2): 62 | return (len(s1.lines) > len(s2.lines) or 63 | s1.lines.count('>undo') > s2.lines.count('>undo')) 64 | 65 | @flaky 66 | class TestDiagramsWithTmux(unittest.TestCase, DiagramsWithTmux): 67 | def test_simple_undo(self): 68 | self.assert_undo(''' 69 | before after 70 | +-----------+ +-----------+ 71 | |$rw | |$rw | 72 | |>1 + 1 | |>1 + 1 | 73 | |usually 2 | |usually 2 | 74 | +-----------+ +-----------+ 75 | |>2 + 2 | |>2 + 2 | 76 | |notquite 4 | |notquite 4 | 77 | |>3 | |>@ | 78 | |3 | ~ ~ 79 | |>@ | ~ ~ 80 | ~ ~ ~ ~ 81 | +-----------+ +-----------+ 82 | ''') 83 | 84 | def test_resizing_window(self): 85 | lines = [u'$rw', 86 | u'>1 + 1', 87 | u'usually 2', 88 | u'>2 + 2', 89 | u'notquite 4', 90 | u'>3', 91 | u'3', 92 | u'>'] 93 | termstate = terminal_dsl.TerminalState( 94 | lines=lines, cursor_line=7, cursor_offset=1, width=11, 95 | height=6, history_height=3) 96 | with UndoScenario(termstate) as t: 97 | UndoScenario.initialize(t, termstate) 98 | tmux.stepwise_resize_width(t, 11) 99 | self.assertEqual(tmux.all_contents(t), lines) 100 | 101 | def test_simple_resize(self): 102 | self.assert_undo(''' 103 | before after 104 | +-----------+ +--------------+ 105 | |$rw | |$rw | 106 | |>1 + 1 | |>1 + 1 | 107 | |usually 2 | |usually 2 | 108 | +-----------+ +--------------+ 109 | |>2 + 2 | |>2 + 2 | 110 | |notquite 4 | |notquite 4 | 111 | |>3 | |>3 | 112 | |3 | |3 | 113 | |>@ | |>@ | 114 | ~ ~ ~ ~ 115 | +-----------+ +--------------+ 116 | ''') 117 | 118 | def test_multistep(self): 119 | self.assert_undo(''' 120 | initial widen narrow widen and undo 121 | +-----------+ +--------------+ +----------+ +--------------+ 122 | |$rw | |$rw | |$rw | |$rw | 123 | |>1 + 1 | |>1 + 1 | |>1 + 1 | |>1 + 1 | 124 | |usually 2 | |usually 2 | |usually 2 | |usually 2 | 125 | +-----------+ +--------------+ +----------+ +--------------+ 126 | |>2 + 2 | |>2 + 2 | |>2 + 2 | |>2 + 2 | 127 | |notquite 4 | |notquite 4 | |notquite 4| |notquite 4 | 128 | |>3 | |>3 | |>3 | |>@ | 129 | |3 | |3 | |3 | ~ ~ 130 | |>@ | |>@ | |>@ | ~ ~ 131 | ~ ~ ~ ~ ~ ~ ~ ~ 132 | +-----------+ +--------------+ +----------+ +--------------+ 133 | ''') 134 | 135 | 136 | class TmuxPaneWithAddrsInEnv(tmux.TmuxPane): 137 | def bash_config_contents(self): 138 | self.save_addr = termrewrite.temp_name('save') 139 | self.restore_addr = termrewrite.temp_name('restore') 140 | os.environ['RLUNDO_SAVE'] = self.save_addr 141 | os.environ['RLUNDO_RESTORE'] = self.restore_addr 142 | return """ 143 | export PS1='$' 144 | export SAVE={} 145 | export RESTORE={} 146 | """.format(self.save_addr, self.restore_addr) 147 | 148 | 149 | class TestRunWithTmux(unittest.TestCase): 150 | 151 | def test_cursor_query(self): 152 | with tmux.TmuxPane(40, 10) as t: 153 | tmux.send_command(t, 'true 1234') 154 | tmux.send_command(t, 'true 1234') 155 | program = "from rlundo import findcursor; import sys; print(findcursor.get_cursor_position(sys.stdout, sys.stdin))" 156 | tmux.send_command(t, 'python -c "%s"' % (program, )) 157 | lines = tmux.visible(t) 158 | while lines[-1] == u'$': 159 | lines.pop() 160 | line = lines[-1] 161 | self.assertTrue(len(line) > 1, repr(line)) 162 | row, col = [int(x) for x in re.search( 163 | r'[(](\d+), (\d+)[)]', line).groups()] 164 | self.assertEqual(tmux.cursor_pos(t), (row+1, col+1)) 165 | 166 | def test_running_rewrite(self): 167 | with tmux.TmuxPane(40, 10) as t: 168 | t.send_keys('python rewrite.py') 169 | self.assertEqual(tmux.visible_after_prompt(t, '>', max=4), 170 | ['$ python rewrite.py', '>']) 171 | 172 | def test_simple_save_and_restore(self): 173 | with TmuxPaneWithAddrsInEnv(70, 10) as t: 174 | tmux.send_command(t, 'python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', prompt=u'>') 175 | self.assertEqual(tmux.visible(t), ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', '>']) 176 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 177 | save() 178 | 179 | tmux.send_command(t, 'hello!', prompt=u'>') 180 | self.assertEqual(tmux.visible(t), 181 | ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', '>hello!', '>']) 182 | restore(t) 183 | 184 | self.assertEqual(tmux.visible_after_prompt(t, '>'), 185 | ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', '>']) 186 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 187 | 188 | def test_scroll_down(self): 189 | with TmuxPaneWithAddrsInEnv(70, 8) as t: 190 | tmux.send_command(t, 'true') 191 | tmux.send_command(t, 'true') 192 | tmux.send_command(t, 'true') 193 | tmux.send_command(t, 'true') 194 | tmux.send_command(t, 'true') 195 | tmux.send_command(t, 'python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', prompt=u'>') 196 | self.assertEqual(tmux.visible(t), 197 | (['$true']*5 + 198 | ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', 199 | '>'])) 200 | self.assertEqual(tmux.cursor_pos(t), (6, 1)) 201 | save() 202 | tmux.send_command(t, 'hello!', prompt=u'>') 203 | save() 204 | 205 | tmux.send_command(t, 'hi again!', prompt=u'>') 206 | save() 207 | self.assertEqual(tmux.visible(t), 208 | ['$true']*4 + ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', 209 | '>hello!', 210 | '>hi again!', 211 | '>']) 212 | restore(t) 213 | 214 | self.assertEqual(tmux.visible_after_prompt(t, '>'), 215 | ['$true']*4 + ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', 216 | '>hello!', 217 | '>']) 218 | self.assertEqual(tmux.cursor_pos(t), (6, 1)) 219 | 220 | def test_scroll_off(self): 221 | """Scroll down causing recorded output to scroll off the top.""" 222 | with TmuxPaneWithAddrsInEnv(60, 3) as t: 223 | tmux.send_command(t, 'python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', prompt=u'>') 224 | save() 225 | self.assertEqual(tmux.visible(t), ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', '>']) 226 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 227 | 228 | tmux.send_command(t, 'hello!', prompt=u'>') 229 | save() 230 | 231 | tmux.send_command(t, 'hi again!', prompt=u'>') 232 | save() 233 | self.assertEqual(tmux.visible(t), 234 | ['>hello!', '>hi again!', '>']) 235 | restore(t) 236 | self.assertEqual(tmux.visible_after_prompt(t, '>'), 237 | ['>hello!', '>']) 238 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 239 | 240 | def test_rewind_to_scrolled_off_prompt(self): 241 | """Recreating history in visible area because undo goes offscreen 242 | 243 | For this we need to track history, do math to place 244 | this history in the visible window, and track scrolling 245 | or cursor position to know that we've run out of space. 246 | I think we don't need an emulator yet - just cursor querying should do. 247 | """ 248 | self.maxDiff = None 249 | with TmuxPaneWithAddrsInEnv(70, 3) as t: 250 | tmux.send_command(t, 'python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', prompt=u'>') 251 | save() 252 | self.assertEqual(tmux.visible(t), ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', '>']) 253 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 254 | 255 | tmux.send_command(t, 'hi there!', prompt=u'>') 256 | save() 257 | 258 | tmux.send_command(t, 'hello!', prompt=u'>') 259 | tmux.send_command(t, 'hi again!', prompt=u'>') 260 | tmux.send_command(t, 'hey!', prompt=u'>') 261 | save() 262 | self.assertEqual(tmux.scrollback(t), 263 | ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', 264 | '>hi there!', 265 | '>hello!']) 266 | self.assertEqual(tmux.visible(t), 267 | ['>hi again!', 268 | '>hey!', 269 | '>']) 270 | restore(t) 271 | self.assertEqual(tmux.scrollback(t), 272 | ['$python rewrite.py --save-addr $SAVE --restore-addr $RESTORE', 273 | '>hi there!', 274 | '>hello!', 275 | '#<---History contiguity broken by rewind--->']) 276 | self.assertEqual(tmux.visible_after_prompt(t, '>'), 277 | ['>hi there!', 278 | '>']) 279 | self.assertEqual(tmux.cursor_pos(t), (1, 1)) 280 | 281 | 282 | class TestWrappedLines(unittest.TestCase, DiagramsWithTmux): 283 | maxDiff = 10000 284 | 285 | def test_wrapped_undo(self): 286 | self.assert_undo(""" 287 | +------+ +------+ 288 | +------+ +------+ 289 | |$rw | |$rw | 290 | |>1 | |>1 | 291 | |>stuff| |>@ | 292 | |abcdef+ ~ ~ 293 | |ghijk | ~ ~ 294 | |>@ | ~ ~ 295 | ~ ~ ~ ~ 296 | +------+ +------+ 297 | """) 298 | 299 | def test_wrapped_undo_after_narrow(self): 300 | self.assert_undo(""" 301 | +------+ +------+ 302 | +------+ +-----------+ |$rw | |$rw | 303 | +------+ +-----------+ +------+ +------+ 304 | |$rw | |$rw | |>1 | |>1 | 305 | |>1 | |>1 | |>stuff| |>@ | 306 | |>stuff| |>stuff | |abcdef+ ~ ~ 307 | |abcdef+ |abcdefghijk+ |ghijkl+ ~ ~ 308 | |ghijkl+ |lmnopq | |mnopq | ~ ~ 309 | |mnopq | |>@ | |>@ | ~ ~ 310 | |>@ ~ ~ ~ ~ ~ ~ ~ 311 | +------+ +-----------+ +------+ +------+ 312 | """) 313 | 314 | def test_irb_style_undo(self): 315 | self.assert_undo(""" 316 | +----------+ +----------+ 317 | +----------+ +----------+ 318 | |$rw | |$rw | 319 | |>1 + 1 | |>@ | 320 | |2 | ~ ~ 321 | |>undo | ~ ~ 322 | |@ | ~ ~ 323 | +----------+ +----------+ 324 | """) 325 | 326 | def test_undo_back_into_history(self): 327 | self.assert_undo(""" 328 | +----------+ 329 | +----------+ |$rw | 330 | |$rw | |>10.to.11 | 331 | |>10.to.11 | |10 | 332 | |10 | |12 | 333 | |12 | |13 | 334 | |13 | |>1.to.1 | 335 | |>1.to.1 | |1 | 336 | |1 | |2 | 337 | |2 | |3 | 338 | |3 | |#<---Histo| 339 | +----------+ +----------+ 340 | |4 | |12 | 341 | |5 | |13 | 342 | |6 | |>@ | 343 | |>undo | ~ ~ 344 | |@ ~ ~ ~ 345 | +----------+ +----------+ 346 | """) 347 | 348 | 349 | class UndoScenario(tmux.TmuxPane): 350 | """ 351 | A series of prompts, inputs, and associated outputs. 352 | Final line must have a cursor on it. 353 | """ 354 | def bash_config_contents(self): 355 | save_addr = termrewrite.temp_name('save') 356 | restore_addr = termrewrite.temp_name('restore') 357 | os.environ['RLUNDO_SAVE'] = save_addr 358 | os.environ['RLUNDO_RESTORE'] = restore_addr 359 | return """ 360 | export PS1='$' 361 | alias rw="python rewrite.py --save-addr %s --restore-addr %s python %s" 362 | """ % (save_addr, restore_addr, self.python_script.name) 363 | 364 | def python_script_contents(self): 365 | return open(scenarioscript.__file__).read() 366 | 367 | def __init__(self, termstate): 368 | self.validate_termstate(termstate) 369 | self.termstate = termstate 370 | tmux.TmuxPane.__init__(self, termstate.width, termstate.height) 371 | 372 | @classmethod 373 | def validate_termstate(cls, termstate): 374 | """Check that termstate represents a valid undo scenario 375 | 376 | Undo scenarios must start with "$rw" and then consist 377 | of pairs of > prompts with commands followed by zero 378 | or more lines of output. 379 | 380 | >>> termstate = terminal_dsl.TerminalState( 381 | ... ['>a', 'b', 'c', '>d', 'e', '>'], cursor_line=5, 382 | ... cursor_offset=1, width=10, height=10, history_height=0) 383 | >>> UndoScenario.validate_termstate(termstate) 384 | Traceback (most recent call last): 385 | ... 386 | ValueError: termstate doesn't start with a call to rw: '>a' 387 | """ 388 | uless_repr = lambda x: repr(x)[1:] if repr(x).startswith('u') else repr(x) 389 | if not termstate.lines[0] == '$rw': 390 | raise ValueError("termstate doesn't start with a call to rw: %s" % (uless_repr(termstate.lines[0]), )) 391 | if not termstate.cursor_line == len(termstate.lines) - 1: 392 | raise ValueError("cursor not on last line!") 393 | 394 | def __enter__(self): 395 | """Initialize a pane with an undo scenario. 396 | 397 | undo schenarios always start by calling python rewrite.py 398 | 399 | >>> termstate = terminal_dsl.TerminalState( 400 | ... ['$rw', '>a', 'b', 'c', '>d', 'e', '>'], cursor_line=6, 401 | ... cursor_offset=1, width=10, height=10, history_height=0) 402 | >>> with UndoScenario(termstate) as t: 403 | ... UndoScenario.initialize(t, termstate) 404 | ... tmux.visible_after_prompt(t, expected=u'>') == [u'$rw', u'>a', u'b', u'c', u'>d', u'e', u'>'] 405 | True 406 | """ 407 | self.python_script = self.tempfile(self.python_script_contents()) 408 | return tmux.TmuxPane.__enter__(self) 409 | 410 | @classmethod 411 | def initialize(cls, pane, termstate): 412 | 413 | lines = termstate.lines[:] 414 | assert lines.pop(0) == '$rw' 415 | tmux.send_command(pane, 'rw', prompt=u'>') 416 | save() 417 | first_line = lines.pop(0) 418 | assert first_line.startswith('>') 419 | pane.cmd('send-keys', first_line[1:]) 420 | pane.enter() 421 | tmux.wait_until_cursor_moves(pane, 1, 1) 422 | for i, line in enumerate(lines): 423 | if i == termstate.cursor_line: 424 | assert len(lines) == i - 1 425 | pane.cmd('send-keys', line) 426 | elif line.startswith('>'): 427 | tmux.send_command(pane, '>', enter=False, prompt=u'>') 428 | save() 429 | pane.cmd('send-keys', line[1:]) 430 | if i != len(lines) - 1: 431 | pane.enter() 432 | else: 433 | if line != '': 434 | row, col = tmux.cursor_pos(pane) 435 | pane.cmd('send-keys', line) 436 | tmux.wait_until_cursor_moves(pane, row, col) 437 | if i != len(lines) - 1: 438 | pane.enter() 439 | row, col = tmux.cursor_pos(pane) 440 | 441 | additional_required_blank_rows = ( 442 | termstate.history_height - len(tmux.scrollback(pane)) + 443 | termstate.height - row - 1) 444 | assert additional_required_blank_rows >= 0 445 | assert col == len(line) % termstate.width, 'col: %r len(line): %r termstate.width: %r' % (col, len(line), termstate.width) # TODO allow other columns 446 | if additional_required_blank_rows == 1: 447 | pane.cmd('1c'+str(col)) 448 | pane.enter() 449 | elif additional_required_blank_rows > 1: 450 | for _ in range(additional_required_blank_rows - 1): 451 | pane.enter() 452 | for _ in range(additional_required_blank_rows - 2): 453 | pane.cmd('send-keys', 'up2') 454 | pane.enter() 455 | pane.cmd('send-keys', 'uc'+str(col)) 456 | pane.enter() 457 | 458 | 459 | class TestUndoScenario(unittest.TestCase): 460 | def test_initialize(self): 461 | lines = ['$rw', '>a', 'b', 'c', '>d', 'e', '>'] 462 | termstate = terminal_dsl.TerminalState( 463 | lines, cursor_line=6, cursor_offset=1, 464 | width=10, height=10, history_height=0) 465 | with UndoScenario(termstate) as t: 466 | UndoScenario.initialize(t, termstate) 467 | output = tmux.visible(t) 468 | self.assertEqual(output, lines) 469 | 470 | def assertRoundtrip(self, diagram): 471 | (before, ) = [terminal_dsl.parse_term_state(x)[1] 472 | for x in terminal_dsl.divide_term_states(diagram)] 473 | with UndoScenario(before) as t: 474 | UndoScenario.initialize(t, before) 475 | after = terminal_dsl.TerminalState.from_tmux_pane(t) 476 | 477 | self.assertEqual(before, after, before.visible_diff(after)) 478 | 479 | def test_wrapped(self): 480 | self.assertRoundtrip(""" 481 | +------+ 482 | |$rw | 483 | +------+ 484 | |>1 | 485 | |>stuff| 486 | |abcdef+ 487 | |ghijkl+ 488 | |mnopq | 489 | |>@ | 490 | +------+ 491 | """) 492 | 493 | def test_blank_lines(self): 494 | self.assertRoundtrip(""" 495 | +------+ 496 | |$rw | 497 | +------+ 498 | |>1 | 499 | |>x | 500 | |abcde | 501 | |>@ | 502 | ~ ~ 503 | ~ ~ 504 | +------+ 505 | """) 506 | 507 | if __name__ == '__main__': 508 | nose.run(defaultTest=__name__) 509 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------