def test_rtf(self): RTFColourizer.reset_background_colour() code = Code(text=self.py_source, lexer_name='py3') listing = Listing(code) listing.set_display('rtf') self.file_compare('print_rtf_nobg.out', print_rtf, listing) self.file_compare('print_rtf_bg.out', print_rtf, listing, 'ccc')
def _create_code(self, filename: str = "", text: str = "", code: Code = None) -> Optional[Code]: if filename or text or code: return code or Code( filename=filename, text=text, lexer_name=self.__lexer_name)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates highlight chain action from purdy.actions import Append, HighlightChain from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen(starting_line_number=1) code_box = screen.code_box blob = Code('../display_code/code.py') actions = [ Append(code_box, blob), HighlightChain(code_box, ['1-2', '3-4', '5-6', '7-8', '9-12']), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Example of a multi-line string in a Python REPL session using the typewriter # animation. from purdy.actions import AppendTypewriter from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen() code_box = screen.code_box blob = Code('../display_code/mls.repl') actions = [ AppendTypewriter(code_box, blob), AppendTypewriter(code_box, blob), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates the Sleep action from purdy.actions import Append, Sleep from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen(starting_line_number=10) code_box = screen.code_box blob = Code('../display_code/simple.repl') actions = [ Append(code_box, blob), Sleep(3), Append(code_box, blob), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates movie mode and the StopMovie action interrupting it from purdy.actions import (Append, AppendTypewriter, Highlight, StopMovie, Wait) from purdy.content import Code from purdy.settings import settings from purdy.ui import SplitScreen settings['movie_mode'] = 500 py_code = Code('../display_code/code.py') con_code = Code('../display_code/simple.repl') screen = SplitScreen(settings, top_starting_line_number=-1, top_auto_scroll=False) py_box = screen.top con_box = screen.bottom actions = [ Append(py_box, py_code), Wait(), AppendTypewriter(con_box, con_code), Wait(), StopMovie(), Wait(), Highlight(py_box, 4, True), Wait(),
#!/usr/bin/env python ### Example purdy library code # # Uses the typewriter animation to display a bash console session from purdy.actions import AppendTypewriter from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen() code_box = screen.code_box blob = Code('../display_code/dollar.bash', lexer_name='dbash') actions = [ AppendTypewriter(code_box, blob), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Displays a function with a multi-line doc string (these can be tricky to # parse, want to make sure each line is colourized as a string) from purdy.actions import Append from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen() blob = Code('../display_code/docstring.py') actions = [ Append(screen.code_box, blob), ] if __name__ == '__main__': screen.run(actions)
screen = SimpleScreen() code_box = screen.code_box text = """// JavaScript Code let a = 3 let b = '4' if(a) { console.log(a) } // =============================== """ lexer = PurdyLexer('js', 'Javascript', JavascriptLexer, False, 'code') js = Code(text=text, lexer_name='custom', purdy_lexer=lexer) text = """ // Node.js Console Session > let a = 3 undefined > a 3 > let b = '4' undefined > b '4' > b == a false > b === a false
return 'Something completely different' def do_something(foo, bar=3): with open('zzz_output.txt', 'a') as f: f.write(f'Doing something {foo} {bar}\n') def undo_something(foo, bar=3): with open('zzz_output.txt', 'a') as f: f.write(f'Undoing something {foo} {bar}\n') screen = SimpleScreen(starting_line_number=10) code_box = screen.code_box first = Code(text="First things first") second = Code(text="Second things second") third = Code(text="Now read zzz_output.txt file to check it worked") actions = [ Append(code_box, first), Wait(), RunFunction(do_something, undo_something, 'FOO', bar=42), Wait(), Append(code_box, second), Wait(), Append(code_box, third), RunFunction(something_else, None), Append(code_box, Code(text='after parameterless function')), ]
# This comment should be replaced # Replace between the dollar signs $replace me$ # Fold the numbers in the following # a # 1 # 2 # b return def should_be_culled(): pass """ before = Code(text=SOURCE) after = Code(text=SOURCE).left_justify() after.python_portion('show_only_this_function') after.insert_line(1, "# After") after.remove_lines(3, 2) after.replace_line(8, ' # Was replaced') after.inline_replace(9, 41, 'done$') after.fold_lines(13, 14) after.remove_double_blanks() actions = [ Append(box, before), Wait(), Transition(box, after), ]
def test_html(self): code = Code(text=self.py_source, lexer_name='py3') listing = Listing(code) listing.set_display('html') self.file_compare('print_html_snippet.out', print_html, listing, True) self.file_compare('print_html_full.out', print_html, listing, False)
def test_ansi(self): code = Code(text=self.py_source, lexer_name='py3') listing = Listing(code) listing.set_display('ansi') self.file_compare('print_ansi.out', print_ansi, listing)
def test_tokens(self): code = Code(text=self.py_source, lexer_name='py3') listing = Listing(code) self.file_compare('print_tokens_ansi.out', print_tokens, listing, True) self.file_compare('print_tokens_bw.out', print_tokens, listing, False)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates inserting lines of code with and without typewriter animation from purdy.actions import Append, Insert, Wait, InsertTypewriter from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen(starting_line_number=10) code_box = screen.code_box blob = Code('../display_code/code.py') insert_me = """\ for x in range(1, 3): print(x)""" blob2 = Code(text=insert_me, lexer_name='py3') insert_me = """\ l1 = [5, 6, 7, 8, 9, ] l2 = [3, 1, 4, 5, ]""" blob3 = Code(text=insert_me, lexer_name='py3') insert_me = " self.stuff(1)" blob4 = Code(text=insert_me, lexer_name='py3') actions = [ Append(code_box, blob), Wait(), Insert(code_box, 8, blob2),
#!/usr/bin/env python ### Example purdy library code # # Demonstrates the use of a custom lexer from purdy.actions import Append from purdy.content import Code from purdy.parser import PurdyLexer from purdy.ui import SimpleScreen from pygments.lexers.javascript import JavascriptLexer screen = SimpleScreen() code_box = screen.code_box lexer = PurdyLexer('js', 'Javascript', JavascriptLexer, False, 'code') blob = Code('../display_code/ugly.js', lexer_name='custom', purdy_lexer=lexer) actions = [ Append(code_box, blob), ] if __name__ == '__main__': screen.run(actions)
### Example purdy library code # # Demonstrates using the XML colourizing palatte and custom lexer from purdy.actions import Append, Highlight, Wait from purdy.content import Code from purdy.parser import PurdyLexer from purdy.settings import settings from purdy.ui import SimpleScreen from pygments.lexers.html import HtmlLexer screen = SimpleScreen(settings, starting_line_number=1) code_box = screen.code_box lexer = PurdyLexer('html', 'Html', HtmlLexer, False, 'xml') #blob = Code('../display_code/base.html', lexer_name='custom', purdy_lexer=lexer) blob = Code( '/Users/ctrudeau/s/RealPython/drf/code/Fedora/templates/registration/login.html', lexer_name='custom', purdy_lexer=lexer) actions = [ Append(code_box, blob), Wait(), Highlight(code_box, range(5, 41), True), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Shows a split screen session with two code boxes on top and a third below. from purdy.actions import Append from purdy.content import Code from purdy.ui import Screen, CodeBox, TwinCodeBox py_code = Code(filename='../display_code/code.py') con_code = Code(filename='../display_code/console.repl') screen = Screen(rows=[ TwinCodeBox(left_starting_line_number=1, right_weight=2, height=14), CodeBox(auto_scroll=False) ]) actions = [ Append(screen.code_boxes[0], py_code), Append(screen.code_boxes[1], con_code), Append(screen.code_boxes[2], con_code), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates replacing the code in a code box by clearing it and appending # new code from purdy.actions import Append, Clear, Wait from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen(starting_line_number=10) code_box = screen.code_box blob = Code('../display_code/simple.repl') blob2 = Code('../display_code/traceback.repl') actions = [ Append(code_box, blob), Wait(), Clear(code_box), Append(code_box, blob2), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Displays a colourized Python REPL session to the screen from purdy.actions import Append from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen() blob = Code('../display_code/console.repl') actions = [ Append(screen.code_box, blob), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Shows a split screen session with two code boxes on top and a third below. from purdy.actions import Append, AppendTypewriter from purdy.content import Code from purdy.ui import Screen, CodeBox, TwinCodeBox py_code = Code(filename='../display_code/decorator.repl') con_code = Code(filename='../display_code/console.repl') screen = Screen(rows=[ TwinCodeBox(left_starting_line_number=1, right_weight=2, height=14), CodeBox(auto_scroll=False) ]) actions = [ AppendTypewriter(screen.code_boxes[0], py_code), Append(screen.code_boxes[1], con_code), Append(screen.code_boxes[2], con_code), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates the Shell action that runs a subprocess and returns the result from purdy.actions import Shell, AppendTypewriter from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen() code_box = screen.code_box cmd1 = 'echo "hello there"' cmd2 = 'echo "it is a nice day today"' blob = Code(text=f'$ {cmd1}') blob2 = Code(text=f'$ {cmd2}') actions = [ AppendTypewriter(code_box, blob), Shell(code_box, cmd1), AppendTypewriter(code_box, blob2), Shell(code_box, cmd2), ] if __name__ == '__main__': screen.run(actions)
from purdy.ui import SimpleScreen def do_something(foo, bar=3): with open('zzz_output.txt', 'a') as f: f.write(f'Doing something {foo} {bar}\n') def undo_something(foo, bar=3): with open('zzz_output.txt', 'a') as f: f.write(f'Undoing something {foo} {bar}\n') screen = SimpleScreen(starting_line_number=10) code_box = screen.code_box first = Code(text="First things first") second = Code(text="Second things second") third = Code(text="Now read zzz_output.txt file to check it worked") actions = [ Append(code_box, first), Wait(), RunFunction(do_something, undo_something, 'FOO', bar=42), Wait(), Append(code_box, second), Wait(), Append(code_box, third), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates a split screen with content that exceeds the window size from purdy.actions import Append from purdy.content import Code from purdy.ui import SplitScreen code1 = Code('../display_code/console.repl') code2 = Code('../display_code/console.repl') screen = SplitScreen(max_height=30) actions = [ Append(screen.code_boxes[0], code1), Append(screen.code_boxes[1], code2), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates a split screen with a large top window from purdy.actions import (Append, AppendTypewriter, Highlight, StopMovie, Wait) from purdy.content import Code from purdy.settings import settings from purdy.ui import SplitScreen settings['movie_mode'] = 2 original = Code('../display_code/code.py') code1 = original.subset(3, 18) code2 = original.subset(28, 39) screen = SplitScreen(settings, top_height=15) actions = [ Append(screen.top, code1), Append(screen.bottom, code2), ] if __name__ == '__main__': screen.run(actions)
#!/usr/bin/env python ### Example purdy library code # # Demonstrates the slide transition animation from purdy.actions import Append, Wait, Transition, Fold from purdy.content import Code from purdy.ui import SimpleScreen, VirtualCodeBox screen = SimpleScreen(starting_line_number=10) code_box = screen.code_box blob = Code('../display_code/simple.repl') blob2 = Code('../display_code/traceback.repl') blob3 = Code('../display_code/decorator.repl') vbox = VirtualCodeBox(starting_line_number=20, display_mode='urwid') # prep vbox for copy vbox.perform_actions([ Append(vbox, blob3), Fold(vbox, 2, 2), ]) actions = [ Append(code_box, blob2), Wait(), Transition(code_box), # test transition to empty Append(code_box, blob), Wait(),
#!/usr/bin/env python ### Example purdy library code # # Uses the typewriter animation to display a bash console session from purdy.actions import AppendTypewriter from purdy.content import Code from purdy.ui import SimpleScreen screen = SimpleScreen() code_box = screen.code_box blob = Code('../display_code/curl.bash') actions = [ AppendTypewriter(code_box, blob), ] if __name__ == '__main__': screen.run(actions)