about summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authortoonn <toonn@toonn.io>2020-07-07 20:41:53 +0200
committertoonn <toonn@toonn.io>2020-07-07 20:41:53 +0200
commit1fdc647b519693aca0805024755005b39685aa95 (patch)
tree5a95879592a1c5cf753563656cf30d57039604a7 /ranger
parent6dc6a4c9b1aaaedff7c74c811dfd48e76f87a10a (diff)
downloadranger-1fdc647b519693aca0805024755005b39685aa95.tar.gz
Refactor _add_character as function
The `_add_character` method no longer has any `self` references because
of the change in the version check so pylint insists on it being a
function instead.
Diffstat (limited to 'ranger')
-rw-r--r--ranger/gui/widgets/console.py79
1 files changed, 40 insertions, 39 deletions
diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py
index f5d54cd0..1325c42f 100644
--- a/ranger/gui/widgets/console.py
+++ b/ranger/gui/widgets/console.py
@@ -18,6 +18,45 @@ 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
@@ -203,7 +242,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 = self._add_character(key, self.unicode_buffer, line, self.pos)
+        result = _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.
@@ -217,44 +256,6 @@ class Console(Widget):  # pylint: disable=too-many-instance-attributes,too-many-
             self.unicode_buffer, self.line, self.pos = result
             self.on_line_change()
 
-    def _add_character(self, 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()