Page through text by feeding it to another program.
(text, cmd)
| 1679 | return re.sub('.\b', '', text) |
| 1680 | |
| 1681 | def pipepager(text, cmd): |
| 1682 | """Page through text by feeding it to another program.""" |
| 1683 | import subprocess |
| 1684 | proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, |
| 1685 | errors='backslashreplace') |
| 1686 | try: |
| 1687 | with proc.stdin as pipe: |
| 1688 | try: |
| 1689 | pipe.write(text) |
| 1690 | except KeyboardInterrupt: |
| 1691 | # We've hereby abandoned whatever text hasn't been written, |
| 1692 | # but the pager is still in control of the terminal. |
| 1693 | pass |
| 1694 | except OSError: |
| 1695 | pass # Ignore broken pipes caused by quitting the pager program. |
| 1696 | while True: |
| 1697 | try: |
| 1698 | proc.wait() |
| 1699 | break |
| 1700 | except KeyboardInterrupt: |
| 1701 | # Ignore ctl-c like the pager itself does. Otherwise the pager is |
| 1702 | # left running and the terminal is in raw mode and unusable. |
| 1703 | pass |
| 1704 | |
| 1705 | def tempfilepager(text, cmd): |
| 1706 | """Page through text by invoking a program on a temporary file.""" |