summary refs log tree commit diff stats
diff options
context:
space:
mode:
authortoonn <toonn@toonn.io>2019-12-31 15:05:28 +0100
committertoonn <toonn@toonn.io>2019-12-31 15:05:28 +0100
commitcfe92cf906fb41522353d38e83ab02a786351b35 (patch)
tree9f381c85225c7620744078206f775ccb55eb88cb
parent4283046b3bcb95f7cdbacb9d9fb2ccb0d7046f49 (diff)
parentfc45daadf794a55f5ccd0eacbc9c686b70b0ad4f (diff)
downloadranger-cfe92cf906fb41522353d38e83ab02a786351b35.tar.gz
Merge branch 'aRkedos-tmux-fix'
-rw-r--r--doc/ranger.14
-rw-r--r--doc/ranger.pod2
-rw-r--r--ranger/config/rc.conf2
-rw-r--r--ranger/gui/ui.py85
4 files changed, 65 insertions, 28 deletions
diff --git a/doc/ranger.1 b/doc/ranger.1
index 5324ccc3..ceb44098 100644
--- a/doc/ranger.1
+++ b/doc/ranger.1
@@ -133,7 +133,7 @@
 .\" ========================================================================
 .\"
 .IX Title "RANGER 1"
-.TH RANGER 1 "ranger-1.9.2" "2019-12-30" "ranger manual"
+.TH RANGER 1 "ranger-1.9.2" "2019-12-31" "ranger manual"
 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
 .\" way too many mistakes in technical documents.
 .if n .ad l
@@ -1176,7 +1176,7 @@ Requires the python-bidi pip package.
 Set a window title? Updates both the \fI\s-1WM_NAME\s0\fR and \fI\s-1WM_ICON_NAME\s0\fR properties.
 .IP "update_tmux_title [bool]" 4
 .IX Item "update_tmux_title [bool]"
-Set the tmux \fIwindow-name\fR to \*(L"ranger\*(R"?
+Set the tmux/screen \fIwindow-name\fR to \*(L"ranger\*(R"?
 .IP "use_preview_script [bool] <zv>" 4
 .IX Item "use_preview_script [bool] <zv>"
 Use the preview script defined in the setting \fIpreview_script\fR?
diff --git a/doc/ranger.pod b/doc/ranger.pod
index bae332e9..b2ce21e1 100644
--- a/doc/ranger.pod
+++ b/doc/ranger.pod
@@ -1266,7 +1266,7 @@ Set a window title? Updates both the I<WM_NAME> and I<WM_ICON_NAME> properties.
 
 =item update_tmux_title [bool]
 
-Set the tmux I<window-name> to "ranger"?
+Set the tmux/screen I<window-name> to "ranger"?
 
 =item use_preview_script [bool] <zv>
 
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index 66a5fbbc..9d08a6a7 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -185,7 +185,7 @@ set display_tags_in_all_columns true
 # Set a title for the window? Updates both `WM_NAME` and `WM_ICON_NAME`
 set update_title false
 
-# Set the tmux window-name to "ranger"?
+# Set the tmux/screen window-name to "ranger"?
 set update_tmux_title true
 
 # Shorten the title if it gets long?  The number defines how many
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index b6ea0886..d2dbb759 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -70,8 +70,11 @@ class UI(  # pylint: disable=too-many-instance-attributes,too-many-public-method
         self.status = None
         self.console = None
         self.pager = None
+        self.multiplexer = None
         self._draw_title = None
         self._tmux_automatic_rename = None
+        self._tmux_title = None
+        self._screen_title = None
         self.browser = None
 
         if fm is not None:
@@ -118,20 +121,10 @@ class UI(  # pylint: disable=too-many-instance-attributes,too-many-public-method
             self.win.refresh()
             self._draw_title = curses.tigetflag('hs')  # has_status_line
 
-            # Save tmux setting `automatic-rename`
-            if self.settings.update_tmux_title and 'TMUX' in os.environ:
-                try:
-                    self._tmux_automatic_rename = check_output(
-                        ['tmux', 'show-window-options', '-v', 'automatic-rename']).strip()
-                except CalledProcessError:
-                    self._tmux_automatic_rename = None
-
         self.update_size()
         self.is_on = True
 
-        if self.settings.update_tmux_title and 'TMUX' in os.environ:
-            sys.stdout.write("\033kranger\033\\")
-            sys.stdout.flush()
+        self.handle_multiplexer()
 
         if 'vcsthread' in self.__dict__:
             self.vcsthread.unpause()
@@ -180,19 +173,7 @@ class UI(  # pylint: disable=too-many-instance-attributes,too-many-public-method
             del self.__dict__['vcsthread']
         DisplayableContainer.destroy(self)
 
-        # Restore tmux setting `automatic-rename`
-        if self.settings.update_tmux_title and 'TMUX' in os.environ:
-            if self._tmux_automatic_rename:
-                try:
-                    check_output(['tmux', 'set-window-option',
-                                  'automatic-rename', self._tmux_automatic_rename])
-                except CalledProcessError:
-                    pass
-            else:
-                try:
-                    check_output(['tmux', 'set-window-option', '-u', 'automatic-rename'])
-                except CalledProcessError:
-                    pass
+        self.restore_multiplexer_name()
 
         self.suspend()
 
@@ -485,6 +466,62 @@ class UI(  # pylint: disable=too-many-instance-attributes,too-many-public-method
         else:
             self.titlebar.throbber = string
 
+    # Handles window renaming behaviour of the terminal multiplexers
+    # GNU Screen and Tmux
+    def handle_multiplexer(self):
+        if self.settings.update_tmux_title:
+            if 'TMUX' in os.environ:
+                # Stores the automatic-rename setting
+                # prints out a warning if the allow-rename in tmux is not set
+                tmux_allow_rename = check_output(
+                    ['tmux', 'show-window-options', '-v',
+                     'allow-rename']).strip()
+                if tmux_allow_rename == 'off':
+                    self.fm.notify('Warning: allow-rename not set in Tmux!',
+                                   bad=True)
+                elif self._tmux_title is None:
+                    self._tmux_title = check_output(
+                        ['tmux', 'display-message', '-p', '#W']).strip()
+                else:
+                    try:
+                        self._tmux_automatic_rename = check_output(
+                            ['tmux', 'show-window-options', '-v',
+                             'automatic-rename']).strip()
+                        if self._tmux_automatic_rename == 'on':
+                            check_output(['tmux', 'set-window-option',
+                                          'automatic-rename', 'off'])
+                    except CalledProcessError:
+                        pass
+            elif 'screen' in os.environ['TERM'] and self._screen_title is None:
+                # Stores the screen window name before renaming it
+                # gives out a warning if $TERM is not "screen"
+                try:
+                    self._screen_title = check_output(
+                        ['screen', '-Q', 'title']).strip()
+                except CalledProcessError:
+                    self._screen_title = None
+
+            sys.stdout.write("\033kranger\033\\")
+            sys.stdout.flush()
+
+    # Restore window name
+    def restore_multiplexer_name(self):
+        try:
+            if 'TMUX' in os.environ:
+                if self._tmux_automatic_rename:
+                    check_output(['tmux', 'set-window-option',
+                                  'automatic-rename',
+                                  self._tmux_automatic_rename])
+                else:
+                    check_output(['tmux', 'set-window-option', '-u',
+                                  'automatic-rename'])
+                if self._tmux_title:
+                    check_output(['tmux', 'rename-window', self._tmux_title])
+            elif 'screen' in os.environ['TERM'] and self._screen_title:
+                check_output(['screen', '-X', 'title', self._screen_title])
+        except CalledProcessError:
+            self.fm.notify("Could not restore window-name!", bad=True)
+
     def hint(self, text=None):
         self.status.hint = text