summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorWojciech Siewierski <wojciech.siewierski@onet.pl>2016-06-29 22:29:11 +0200
committerWojciech Siewierski <wojciech.siewierski@onet.pl>2016-06-30 09:33:10 +0200
commit058a7ebe563e4c30ff9bd15b2861c1f86df7d23b (patch)
tree55875ffec9c59bce7a5f84770f951b3b80e4192e
parenta9a75dde4cb02d63c1cf07884413e4d4d0b7aa32 (diff)
downloadranger-058a7ebe563e4c30ff9bd15b2861c1f86df7d23b.tar.gz
Refactor and improve the TERMCMD handling
Now only the first token of TERMCMD needs to exist in PATH. Previously
the whole value was checked, which could contain additional parameters.

The check and term guessing were performed in two places. Now it's done
by the get_term() function to reduce the code duplication. In that
function the second check was indented one level deeper (relative to the
original code). Keeping both checks on the same level, while visually
pleasing, was conveying the wrong message. One never wants to perform
the second check if the first one fails. Nesting shouldn't be a problem:
if the nesting will need to be any deeper than this, this code will need
to be refactored anyway.

Fixes #613.
-rwxr-xr-xranger/config/commands.py10
-rw-r--r--ranger/core/runner.py8
-rw-r--r--ranger/ext/get_executables.py14
3 files changed, 18 insertions, 14 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 36c21f70..4928ba26 100755
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -493,14 +493,8 @@ class terminal(Command):
     Spawns an "x-terminal-emulator" starting in the current directory.
     """
     def execute(self):
-        import os
-        from ranger.ext.get_executables import get_executables
-        command = os.environ.get('TERMCMD', os.environ.get('TERM'))
-        if command not in get_executables():
-            command = 'x-terminal-emulator'
-        if command not in get_executables():
-            command = 'xterm'
-        self.fm.run(command, flags='f')
+        from ranger.ext.get_executables import get_term
+        self.fm.run(get_term(), flags='f')
 
 
 class delete(Command):
diff --git a/ranger/core/runner.py b/ranger/core/runner.py
index 6d03f8fb..78d52764 100644
--- a/ranger/core/runner.py
+++ b/ranger/core/runner.py
@@ -25,7 +25,7 @@ t: run application in a new terminal window
 import os
 import sys
 from subprocess import Popen, PIPE
-from ranger.ext.get_executables import get_executables
+from ranger.ext.get_executables import get_executables, get_term
 from ranger.ext.popen_forked import Popen_forked
 
 
@@ -194,11 +194,7 @@ class Runner(object):
         if 't' in context.flags:
             if 'DISPLAY' not in os.environ:
                 return self._log("Can not run with 't' flag, no display found!")
-            term = os.environ.get('TERMCMD', os.environ.get('TERM'))
-            if term not in get_executables():
-                term = 'x-terminal-emulator'
-            if term not in get_executables():
-                term = 'xterm'
+            term = get_term()
             if isinstance(action, str):
                 action = term + ' -e ' + action
             else:
diff --git a/ranger/ext/get_executables.py b/ranger/ext/get_executables.py
index ee988e49..f2a31345 100644
--- a/ranger/ext/get_executables.py
+++ b/ranger/ext/get_executables.py
@@ -4,6 +4,7 @@
 from stat import S_IXOTH, S_IFREG
 from ranger.ext.iter_tools import unique
 from os import listdir, environ, stat
+import shlex
 
 
 _cached_executables = None
@@ -44,3 +45,16 @@ def get_executables_uncached(*paths):
             if filestat.st_mode & (S_IXOTH | S_IFREG):
                 executables.add(item)
     return executables
+
+
+def get_term():
+    """Get the user terminal executable name.
+
+    Either $TERMCMD, $TERM, "x-terminal-emulator" or "xterm", in this order.
+    """
+    command = environ.get('TERMCMD', environ.get('TERM'))
+    if shlex.split(command)[0] not in get_executables():
+        command = 'x-terminal-emulator'
+        if command not in get_executables():
+            command = 'xterm'
+    return command