summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorThe Flying Rapist <admin@nosuck.org>2015-01-17 12:52:48 +0900
committerhut <hut@lepus.uberspace.de>2015-01-30 13:40:12 +0100
commit939022cfa62986696595a86b926e82740adad84f (patch)
tree368b6f3a1b8d3e740054879eef1e6bf27247a1fd /ranger
parent2655a1bc19dd4edfc6e88c38be9e960e570feb94 (diff)
downloadranger-939022cfa62986696595a86b926e82740adad84f.tar.gz
Added tab_switch fm action.
Diffstat (limited to 'ranger')
-rw-r--r--ranger/core/actions.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index d73270d5..ed3f7598 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -1066,6 +1066,48 @@ class Actions(FileManagerAware, EnvironmentAware, SettingsAware):
             if not i in self.tabs:
                 return self.tab_open(i, path)
 
+    def tab_switch(self, path, create_directory=True):
+        """Switches to tab of given path, opening a new tab as necessary.
+
+        If path does not exist, it is treated as a directory.
+        """
+        if not os.path.exists(path):
+            file_selection = None
+            if create_directory:
+                try:
+                    os.makedirs(path, exist_ok=True)
+                except OSError as err:
+                    self.fm.notify(err, bad=True)
+                    return
+                target_directory = path
+            else:
+                # Give benefit of the doubt.
+                potential_parent = os.path.dirname(path)
+                if os.path.exists(potential_parent) and os.path.isdir(potential_parent):
+                    target_directory = potential_parent
+                else:
+                    self.fm.notify("Unable to resolve given path.", bad=True)
+                    return
+        elif os.path.isdir(path):
+            file_selection = None
+            target_directory = path
+        else:
+            file_selection = path
+            target_directory = os.path.dirname(path)
+
+        for name in self.fm.tabs:
+            tab = self.fm.tabs[name]
+            # Is a tab already open?
+            if tab.path == target_directory:
+                self.fm.tab_open(name=name)
+                if file_selection:
+                    self.fm.select_file(file_selection)
+                return
+
+        self.fm.tab_new(path=target_directory)
+        if file_selection:
+            self.fm.select_file(file_selection)
+
     def _get_tab_list(self):
         assert len(self.tabs) > 0, "There must be >=1 tabs at all times"
         return sorted(self.tabs)