Permalink
Browse files

Support carriage return ('\r') characters in the qtconsole. Closes #629.

  • Loading branch information...
1 parent 4b8920a commit 7d05f6cee2c88c846c9e5005f8e17f287dbb7249 @mdboom committed Dec 2, 2011
Showing with 29 additions and 17 deletions.
  1. +25 −17 IPython/frontend/qt/console/ansi_code_processor.py
  2. +4 −0 IPython/frontend/qt/console/console_widget.py
@@ -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])')
#-----------------------------------------------------------------------------
@@ -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:
@@ -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)
@@ -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:

0 comments on commit 7d05f6c

Please sign in to comment.