Skip to content

Commit

Permalink
Support carriage return ('\r') characters in the qtconsole. Closes ip…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Dec 2, 2011
1 parent 4b8920a commit 7d05f6c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
42 changes: 25 additions & 17 deletions IPython/frontend/qt/console/ansi_code_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
# An action for scroll requests (SU and ST) and form feeds.
ScrollAction = namedtuple('ScrollAction', ['action', 'dir', 'unit', 'count'])

# An action for the carriage return character
CarriageReturnAction = namedtuple('CarriageReturnAction', ['action'])

# Regular expressions.
CSI_COMMANDS = 'ABCDEFGHJKSTfmnsu'
CSI_SUBPATTERN = '\[(.*?)([%s])' % CSI_COMMANDS
OSC_SUBPATTERN = '\](.*?)[\x07\x1b]'
ANSI_PATTERN = re.compile('\x01?\x1b(%s|%s)\x02?' % \
(CSI_SUBPATTERN, OSC_SUBPATTERN))
ANSI_PATTERN = ('\x01?\x1b(%s|%s)\x02?' % \
(CSI_SUBPATTERN, OSC_SUBPATTERN))
ANSI_OR_CR_PATTERN = re.compile('(\r)|(?:%s)' % ANSI_PATTERN)
SPECIAL_PATTERN = re.compile('([\f])')

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -76,7 +80,7 @@ def split_string(self, string):
self.actions = []
start = 0

for match in ANSI_PATTERN.finditer(string):
for match in ANSI_OR_CR_PATTERN.finditer(string):
raw = string[start:match.start()]
substring = SPECIAL_PATTERN.sub(self._replace_special, raw)
if substring or self.actions:
Expand All @@ -85,20 +89,24 @@ def split_string(self, string):

self.actions = []
groups = filter(lambda x: x is not None, match.groups())
params = [ param for param in groups[1].split(';') if param ]
if groups[0].startswith('['):
# Case 1: CSI code.
try:
params = map(int, params)
except ValueError:
# Silently discard badly formed codes.
pass
else:
self.set_csi_code(groups[2], params)

elif groups[0].startswith(']'):
# Case 2: OSC code.
self.set_osc_code(params)
if groups[0] == '\r':
self.actions.append(CarriageReturnAction('carriage-return'))
yield ''
else:
params = [ param for param in groups[1].split(';') if param ]
if groups[0].startswith('['):
# Case 1: CSI code.
try:
params = map(int, params)
except ValueError:
# Silently discard badly formed codes.
pass
else:
self.set_csi_code(groups[2], params)

elif groups[0].startswith(']'):
# Case 2: OSC code.
self.set_osc_code(params)

raw = string[start:]
substring = SPECIAL_PATTERN.sub(self._replace_special, raw)
Expand Down
4 changes: 4 additions & 0 deletions IPython/frontend/qt/console/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,10 @@ def _insert_plain_text(self, cursor, text):
cursor.joinPreviousEditBlock()
cursor.deletePreviousChar()

elif act.action == 'carriage-return':
cursor.movePosition(
cursor.StartOfLine, cursor.KeepAnchor)

format = self._ansi_processor.get_format()
cursor.insertText(substring, format)
else:
Expand Down

0 comments on commit 7d05f6c

Please sign in to comment.