Skip to content

Commit e1733c5

Browse files
rybarczykjthomasballinger
authored andcommitted
Use global Enum class to avoid typing strings
- all_logical_lines uses tuples where the second val is "input" or "output". Using LineTypeTranslator avoids tying strings
1 parent 8839681 commit e1733c5

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import bpython
3333
from bpython.repl import Repl as BpythonRepl, SourceNotFound
34+
from bpython.repl import LineTypeTranslator as LineType
3435
from bpython.config import (
3536
Struct,
3637
loadini,
@@ -396,7 +397,6 @@ def __init__(
396397
# current line of output - stdout and stdin go here
397398
self.current_stdouterr_line = ""
398399

399-
400400
# this is every line that's been displayed (input and output)
401401
# as with formatting applied. Logical lines that exceeded the terminal width
402402
# at the time of output are split across multiple entries in this list.
@@ -408,8 +408,9 @@ def __init__(
408408
# This is every logical line that's been displayed, both input and output.
409409
# Like self.history, lines are unwrapped, uncolored, and without prompt.
410410
# 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)
413414
self.all_logical_lines = []
414415

415416
# 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):
881882
self.rl_history.reset()
882883

883884
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))
885886
self.push(self.current_line, insert_into_history=new_code)
886887

887-
888888
def on_tab(self, back=False):
889889
"""Do something on tab key
890890
taken from bpython.cli
@@ -1016,7 +1016,7 @@ def send_session_to_external_editor(self, filename=None):
10161016
"""
10171017
for_editor = EDIT_SESSION_HEADER
10181018
for_editor += "\n".join(
1019-
line[0] if line[1] == "input"
1019+
line[0] if line[1] == INPUT
10201020
else "### " + line[0]
10211021
for line in self.all_logical_lines
10221022
)
@@ -1189,9 +1189,7 @@ def push(self, line, insert_into_history=True):
11891189
if c:
11901190
logger.debug("finished - buffer cleared")
11911191
self.cursor_offset = 0
1192-
11931192
self.display_lines.extend(self.display_buffer_lines)
1194-
11951193
self.display_buffer = []
11961194
self.buffer = []
11971195

@@ -1296,14 +1294,12 @@ def send_to_stdouterr(self, output):
12961294
[],
12971295
)
12981296
)
1299-
13001297
# These can be FmtStrs, but self.all_logical_lines only wants strings
13011298
for line in [self.current_stdouterr_line] + lines[1:-1]:
13021299
if isinstance(line, FmtStr):
1303-
self.all_logical_lines.append((line.s, "output"))
1300+
self.all_logical_lines.append((line.s, LineType.OUTPUT))
13041301
else:
1305-
self.all_logical_lines.append((line, "output"))
1306-
1302+
self.all_logical_lines.append((line, LineType.OUTPUT))
13071303

13081304
self.current_stdouterr_line = lines[-1]
13091305
logger.debug("display_lines: %r", self.display_lines)
@@ -1846,8 +1842,6 @@ def take_back_empty_line(self):
18461842
self.display_lines.pop()
18471843
self.all_logical_lines.pop()
18481844

1849-
1850-
18511845
def prompt_undo(self):
18521846
if self.buffer:
18531847
return self.take_back_buffer_line()
@@ -1865,7 +1859,7 @@ def redo(self):
18651859
if (self.redo_stack):
18661860
temp = self.redo_stack.pop()
18671861
self.history.append(temp)
1868-
self.all_logical_lines.append((temp, "input"))
1862+
self.all_logical_lines.append((temp, LineType.INPUT))
18691863
self.push(temp)
18701864
else:
18711865
self.status_bar.message("Nothing to redo.")
@@ -1954,7 +1948,6 @@ def getstdout(self):
19541948
)
19551949
return s
19561950

1957-
19581951
def focus_on_subprocess(self, args):
19591952
prev_sigwinch_handler = signal.getsignal(signal.SIGWINCH)
19601953
try:

bpython/repl.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from itertools import takewhile
4242
from six import itervalues
4343
from types import ModuleType
44+
from enum import Enum
4445

4546
from pygments.token import Token
4647

@@ -387,6 +388,11 @@ def file_prompt(self, s):
387388
class SourceNotFound(Exception):
388389
"""Exception raised when the requested source could not be found."""
389390

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"
390396

391397
class Repl(object):
392398
"""Implements the necessary guff for a Python-repl-alike interface
@@ -821,11 +827,9 @@ def formatforfile(self, session_ouput):
821827
output lines."""
822828

823829
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
829833
elif line.rstrip():
830834
yield "# OUT: %s" % (line,)
831835

bpython/test/test_curtsies_repl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from bpython.curtsiesfrontend import repl as curtsiesrepl
1414
from bpython.curtsiesfrontend import interpreter
1515
from bpython.curtsiesfrontend import events as bpythonevents
16+
from bpython.repl import LineTypeTranslator as LineType
1617
from bpython import autocomplete
1718
from bpython import config
1819
from bpython import args
@@ -79,7 +80,7 @@ def test_external_communication_encoding(self):
7980
with captured_output():
8081
self.repl.display_lines.append('>>> "åß∂ƒ"')
8182
self.repl.history.append('"åß∂ƒ"')
82-
self.repl.all_logical_lines.append(('"åß∂ƒ"',"input"))
83+
self.repl.all_logical_lines.append(('"åß∂ƒ"', LineType.INPUT))
8384
self.repl.send_session_to_external_editor()
8485

8586
def test_get_last_word(self):

0 commit comments

Comments
 (0)