about summary refs log tree commit diff stats
path: root/ranger/api/commands.py
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2016-04-03 23:08:10 +0200
committerhut <hut@lepus.uberspace.de>2016-04-03 23:59:54 +0200
commit3ae25fa5b14c224c699e572c31cd8c95dc3d9040 (patch)
tree78f0297e1218f8901c248d99507e53d48e99473d /ranger/api/commands.py
parent46537efbf1959dcde3f0ad5fb29e0436b324aca2 (diff)
downloadranger-3ae25fa5b14c224c699e572c31cd8c95dc3d9040.tar.gz
allow toggling options with `:set <option>!`
Diffstat (limited to 'ranger/api/commands.py')
-rw-r--r--ranger/api/commands.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/ranger/api/commands.py b/ranger/api/commands.py
index a20adecb..c3e0a59a 100644
--- a/ranger/api/commands.py
+++ b/ranger/api/commands.py
@@ -152,6 +152,27 @@ class Command(FileManagerAware):
         return ''.join([self._tabinsert_left, word, self._tabinsert_right])
 
     def parse_setting_line(self):
+        """
+        Parses the command line argument that is passed to the `:set` command.
+        Returns [option, value, name_complete].
+
+        Can parse incomplete lines too, and `name_complete` is a boolean
+        indicating whether the option name looks like it's completed or
+        unfinished.  This is useful for generating tab completions.
+
+        >>> Command("set foo=bar").parse_setting_line()
+        ['foo', 'bar', True]
+        >>> Command("set foo").parse_setting_line()
+        ['foo', '', False]
+        >>> Command("set foo=").parse_setting_line()
+        ['foo', '', True]
+        >>> Command("set foo ").parse_setting_line()
+        ['foo', '', True]
+        >>> Command("set myoption myvalue").parse_setting_line()
+        ['myoption', 'myvalue', True]
+        >>> Command("set").parse_setting_line()
+        ['', '', False]
+        """
         if self._setting_line is not None:
             return self._setting_line
         match = _SETTINGS_RE.match(self.rest(1))
@@ -163,6 +184,25 @@ class Command(FileManagerAware):
         self._setting_line = result
         return result
 
+    def parse_setting_line_v2(self):
+        """
+        Parses the command line argument that is passed to the `:set` command.
+        Returns [option, value, name_complete, toggle].
+
+        >>> Command("set foo=bar").parse_setting_line_v2()
+        ['foo', 'bar', True, False]
+        >>> Command("set foo!").parse_setting_line_v2()
+        ['foo', '', True, True]
+        """
+        option, value, name_complete = self.parse_setting_line()
+        if len(option) >= 2 and option[-1] == '!':
+            toggle = True
+            option = option[:-1]
+            name_complete = True
+        else:
+            toggle = False
+        return [option, value, name_complete, toggle]
+
     def parse_flags(self):
         """Finds and returns flags in the command