diff options
author | toonn <toonn@toonn.io> | 2020-07-07 22:17:58 +0200 |
---|---|---|
committer | toonn <toonn@toonn.io> | 2020-07-07 22:17:58 +0200 |
commit | 46d083e7b91c48dcbb96922232e9b4571a0e8d7c (patch) | |
tree | 07a425fdfd08662e1c53d342a8d40c0ed48a5b76 /ranger | |
parent | 1fdc647b519693aca0805024755005b39685aa95 (diff) | |
download | ranger-46d083e7b91c48dcbb96922232e9b4571a0e8d7c.tar.gz |
Refactor _add_character as static method
Diffstat (limited to 'ranger')
-rw-r--r-- | ranger/gui/widgets/console.py | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py index 1325c42f..88a59136 100644 --- a/ranger/gui/widgets/console.py +++ b/ranger/gui/widgets/console.py @@ -18,45 +18,6 @@ from ranger.container.history import History, HistoryEmptyException import ranger -def _add_character(key, unicode_buffer, line, pos): - # Takes the pressed key, a string "unicode_buffer" containing a - # potentially incomplete unicode character, the current line and the - # position of the cursor inside the line. - # This function returns the new unicode buffer, the modified line and - # position. - if isinstance(key, int): - try: - key = chr(key) - except ValueError: - return unicode_buffer, line, pos - - if PY3: - if len(unicode_buffer) >= 4: - unicode_buffer = "" - if ord(key) in range(0, 256): - unicode_buffer += key - try: - decoded = unicode_buffer.encode("latin-1").decode("utf-8") - except UnicodeDecodeError: - return unicode_buffer, line, pos - except UnicodeEncodeError: - return unicode_buffer, line, pos - else: - unicode_buffer = "" - if pos == len(line): - line += decoded - else: - line = line[:pos] + decoded + line[pos:] - pos += len(decoded) - else: - if pos == len(line): - line += key - else: - line = line[:pos] + key + line[pos:] - pos += len(key) - return unicode_buffer, line, pos - - class Console(Widget): # pylint: disable=too-many-instance-attributes,too-many-public-methods visible = False last_cursor_mode = None @@ -242,7 +203,7 @@ class Console(Widget): # pylint: disable=too-many-instance-attributes,too-many- self.tab_deque = None line = "" if self.question_queue else self.line - result = _add_character(key, self.unicode_buffer, line, self.pos) + result = self._add_character(key, self.unicode_buffer, line, self.pos) if result[1] == line: # line didn't change, so we don't need to do anything, just update # the unicode _buffer. @@ -256,6 +217,45 @@ class Console(Widget): # pylint: disable=too-many-instance-attributes,too-many- self.unicode_buffer, self.line, self.pos = result self.on_line_change() + @staticmethod + def _add_character(key, unicode_buffer, line, pos): + # Takes the pressed key, a string "unicode_buffer" containing a + # potentially incomplete unicode character, the current line and the + # position of the cursor inside the line. + # This function returns the new unicode buffer, the modified line and + # position. + if isinstance(key, int): + try: + key = chr(key) + except ValueError: + return unicode_buffer, line, pos + + if PY3: + if len(unicode_buffer) >= 4: + unicode_buffer = "" + if ord(key) in range(0, 256): + unicode_buffer += key + try: + decoded = unicode_buffer.encode("latin-1").decode("utf-8") + except UnicodeDecodeError: + return unicode_buffer, line, pos + except UnicodeEncodeError: + return unicode_buffer, line, pos + else: + unicode_buffer = "" + if pos == len(line): + line += decoded + else: + line = line[:pos] + decoded + line[pos:] + pos += len(decoded) + else: + if pos == len(line): + line += key + else: + line = line[:pos] + key + line[pos:] + pos += len(key) + return unicode_buffer, line, pos + def history_move(self, n): try: current = self.history.current() |