| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8839681 commit e1733c5
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,6 +31,7 @@ | |||
| 31 | 31 | ||
| 32 | 32 | import bpython | |
| 33 | 33 | from bpython.repl import Repl as BpythonRepl, SourceNotFound | |
| 34 | + from bpython.repl import LineTypeTranslator as LineType | ||
| 34 | 35 | from bpython.config import ( | |
| 35 | 36 | Struct, | |
| 36 | 37 | loadini, | |
@@ -396,7 +397,6 @@ def __init__( | |||
| 396 | 397 | # current line of output - stdout and stdin go here | |
| 397 | 398 | self.current_stdouterr_line = "" | |
| 398 | 399 | ||
| 399 | - | ||
| 400 | 400 | # this is every line that's been displayed (input and output) | |
| 401 | 401 | # as with formatting applied. Logical lines that exceeded the terminal width | |
| 402 | 402 | # at the time of output are split across multiple entries in this list. | |
@@ -408,8 +408,9 @@ def __init__( | |||
| 408 | 408 | # This is every logical line that's been displayed, both input and output. | |
| 409 | 409 | # Like self.history, lines are unwrapped, uncolored, and without prompt. | |
| 410 | 410 | # Entries are tuples, where | |
| 411 | - # the first element a string of the line | ||
| 412 | - # the second element is one of 2 strings: "input" or "output". | ||
| 411 | + # - the first element the line (string, not fmtsr) | ||
| 412 | + # - the second element is one of 2 global constants: "input" or "output" | ||
| 413 | + # (use LineType.INPUT or LineType.OUTPUT to avoid typing these strings) | ||
| 413 | 414 | self.all_logical_lines = [] | |
| 414 | 415 | ||
| 415 | 416 | # formatted version of lines in the buffer kept around so we can | |
@@ -881,10 +882,9 @@ def on_enter(self, new_code=True, reset_rl_history=True): | |||
| 881 | 882 | self.rl_history.reset() | |
| 882 | 883 | ||
| 883 | 884 | self.history.append(self.current_line) | |
| 884 | - self.all_logical_lines.append((self.current_line, "input")) | ||
| 885 | + self.all_logical_lines.append((self.current_line, LineType.INPUT)) | ||
| 885 | 886 | self.push(self.current_line, insert_into_history=new_code) | |
| 886 | 887 | ||
| 887 | - | ||
| 888 | 888 | def on_tab(self, back=False): | |
| 889 | 889 | """Do something on tab key | |
| 890 | 890 | taken from bpython.cli | |
@@ -1016,7 +1016,7 @@ def send_session_to_external_editor(self, filename=None): | |||
| 1016 | 1016 | """ | |
| 1017 | 1017 | for_editor = EDIT_SESSION_HEADER | |
| 1018 | 1018 | for_editor += "\n".join( | |
| 1019 | - line[0] if line[1] == "input" | ||
| 1019 | + line[0] if line[1] == INPUT | ||
| 1020 | 1020 | else "### " + line[0] | |
| 1021 | 1021 | for line in self.all_logical_lines | |
| 1022 | 1022 | ) | |
@@ -1189,9 +1189,7 @@ def push(self, line, insert_into_history=True): | |||
| 1189 | 1189 | if c: | |
| 1190 | 1190 | logger.debug("finished - buffer cleared") | |
| 1191 | 1191 | self.cursor_offset = 0 | |
| 1192 | - | ||
| 1193 | 1192 | self.display_lines.extend(self.display_buffer_lines) | |
| 1194 | - | ||
| 1195 | 1193 | self.display_buffer = [] | |
| 1196 | 1194 | self.buffer = [] | |
| 1197 | 1195 | ||
@@ -1296,14 +1294,12 @@ def send_to_stdouterr(self, output): | |||
| 1296 | 1294 | [], | |
| 1297 | 1295 | ) | |
| 1298 | 1296 | ) | |
| 1299 | - | ||
| 1300 | 1297 | # These can be FmtStrs, but self.all_logical_lines only wants strings | |
| 1301 | 1298 | for line in [self.current_stdouterr_line] + lines[1:-1]: | |
| 1302 | 1299 | if isinstance(line, FmtStr): | |
| 1303 | - self.all_logical_lines.append((line.s, "output")) | ||
| 1300 | + self.all_logical_lines.append((line.s, LineType.OUTPUT)) | ||
| 1304 | 1301 | else: | |
| 1305 | - self.all_logical_lines.append((line, "output")) | ||
| 1306 | - | ||
| 1302 | + self.all_logical_lines.append((line, LineType.OUTPUT)) | ||
| 1307 | 1303 | ||
| 1308 | 1304 | self.current_stdouterr_line = lines[-1] | |
| 1309 | 1305 | logger.debug("display_lines: %r", self.display_lines) | |
@@ -1846,8 +1842,6 @@ def take_back_empty_line(self): | |||
| 1846 | 1842 | self.display_lines.pop() | |
| 1847 | 1843 | self.all_logical_lines.pop() | |
| 1848 | 1844 | ||
| 1849 | - | ||
| 1850 | - | ||
| 1851 | 1845 | def prompt_undo(self): | |
| 1852 | 1846 | if self.buffer: | |
| 1853 | 1847 | return self.take_back_buffer_line() | |
@@ -1865,7 +1859,7 @@ def redo(self): | |||
| 1865 | 1859 | if (self.redo_stack): | |
| 1866 | 1860 | temp = self.redo_stack.pop() | |
| 1867 | 1861 | self.history.append(temp) | |
| 1868 | - self.all_logical_lines.append((temp, "input")) | ||
| 1862 | + self.all_logical_lines.append((temp, LineType.INPUT)) | ||
| 1869 | 1863 | self.push(temp) | |
| 1870 | 1864 | else: | |
| 1871 | 1865 | self.status_bar.message("Nothing to redo.") | |
@@ -1954,7 +1948,6 @@ def getstdout(self): | |||
| 1954 | 1948 | ) | |
| 1955 | 1949 | return s | |
| 1956 | 1950 | ||
| 1957 | - | ||
| 1958 | 1951 | def focus_on_subprocess(self, args): | |
| 1959 | 1952 | prev_sigwinch_handler = signal.getsignal(signal.SIGWINCH) | |
| 1960 | 1953 | try: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ | |||
| 41 | 41 | from itertools import takewhile | |
| 42 | 42 | from six import itervalues | |
| 43 | 43 | from types import ModuleType | |
| 44 | + from enum import Enum | ||
| 44 | 45 | ||
| 45 | 46 | from pygments.token import Token | |
| 46 | 47 | ||
@@ -387,6 +388,11 @@ def file_prompt(self, s): | |||
| 387 | 388 | class SourceNotFound(Exception): | |
| 388 | 389 | """Exception raised when the requested source could not be found.""" | |
| 389 | 390 | ||
| 391 | + class LineTypeTranslator(Enum): | ||
| 392 | + """ Used when adding a tuple to all_logical_lines, to get input / output values | ||
| 393 | + having to actually type/know the strings """ | ||
| 394 | + INPUT = "input" | ||
| 395 | + OUTPUT = "output" | ||
| 390 | 396 | ||
| 391 | 397 | class Repl(object): | |
| 392 | 398 | """Implements the necessary guff for a Python-repl-alike interface | |
@@ -821,11 +827,9 @@ def formatforfile(self, session_ouput): | |||
| 821 | 827 | output lines.""" | |
| 822 | 828 | ||
| 823 | 829 | def process(): | |
| 824 | - for line in session_ouput.split("\n"): | ||
| 825 | - if line.startswith(self.ps1): | ||
| 826 | - yield line[len(self.ps1) :] | ||
| 827 | - elif line.startswith(self.ps2): | ||
| 828 | - yield line[len(self.ps2) :] | ||
| 830 | + for line, lineType in self.all_logical_lines: | ||
| 831 | + if lineType == LineTypeTranslator.INPUT: | ||
| 832 | + yield line | ||
| 829 | 833 | elif line.rstrip(): | |
| 830 | 834 | yield "# OUT: %s" % (line,) | |
| 831 | 835 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ | |||
| 13 | 13 | from bpython.curtsiesfrontend import repl as curtsiesrepl | |
| 14 | 14 | from bpython.curtsiesfrontend import interpreter | |
| 15 | 15 | from bpython.curtsiesfrontend import events as bpythonevents | |
| 16 | + from bpython.repl import LineTypeTranslator as LineType | ||
| 16 | 17 | from bpython import autocomplete | |
| 17 | 18 | from bpython import config | |
| 18 | 19 | from bpython import args | |
@@ -79,7 +80,7 @@ def test_external_communication_encoding(self): | |||
| 79 | 80 | with captured_output(): | |
| 80 | 81 | self.repl.display_lines.append('>>> "åß∂ƒ"') | |
| 81 | 82 | self.repl.history.append('"åß∂ƒ"') | |
| 82 | - self.repl.all_logical_lines.append(('"åß∂ƒ"',"input")) | ||
| 83 | + self.repl.all_logical_lines.append(('"åß∂ƒ"', LineType.INPUT)) | ||
| 83 | 84 | self.repl.send_session_to_external_editor() | |
| 84 | 85 | ||
| 85 | 86 | def test_get_last_word(self): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments