about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--examples/bash_automatic_cd.sh21
-rw-r--r--examples/bash_subshell_notice.sh7
-rw-r--r--examples/shell_automatic_cd.sh23
-rw-r--r--examples/shell_subshell_notice.sh10
-rwxr-xr-xranger.py19
5 files changed, 43 insertions, 37 deletions
diff --git a/examples/bash_automatic_cd.sh b/examples/bash_automatic_cd.sh
deleted file mode 100644
index 04b58e24..00000000
--- a/examples/bash_automatic_cd.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-# Compatible with ranger 1.4.2 through 1.7.*
-#
-# Automatically change the directory in bash after closing ranger
-#
-# This is a bash function for .bashrc to automatically change the directory to
-# the last visited one after ranger quits.
-# To undo the effect of this function, you can type "cd -" to return to the
-# original directory.
-
-function ranger-cd {
-    tempfile="$(mktemp -t tmp.XXXXXX)"
-    ranger --choosedir="$tempfile" "${@:-$(pwd)}"
-    test -f "$tempfile" &&
-    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
-        cd -- "$(cat "$tempfile")"
-    fi
-    rm -f -- "$tempfile"
-}
-
-# This binds Ctrl-O to ranger-cd:
-bind '"\C-o":"ranger-cd\C-m"'
diff --git a/examples/bash_subshell_notice.sh b/examples/bash_subshell_notice.sh
deleted file mode 100644
index 4c9269c4..00000000
--- a/examples/bash_subshell_notice.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-# Compatible with ranger 1.5.3 through 1.7.*
-#
-# Change the prompt when you open a shell from inside ranger
-#
-# Add this line to your .bashrc for it to work.
-
-[ -n "$RANGER_LEVEL" ] && PS1="$PS1"'(in ranger) '
diff --git a/examples/shell_automatic_cd.sh b/examples/shell_automatic_cd.sh
new file mode 100644
index 00000000..391946c7
--- /dev/null
+++ b/examples/shell_automatic_cd.sh
@@ -0,0 +1,23 @@
+# shellcheck shell=sh
+
+# Compatible with ranger 1.4.2 through 1.9.*
+#
+# Automatically change the current working directory after closing ranger
+#
+# This is a shell function to automatically change the current working
+# directory to the last visited one after ranger quits. Either put it into your
+# .zshrc/.bashrc/etc or source this file from your shell configuration.
+# To undo the effect of this function, you can type "cd -" to return to the
+# original directory.
+
+ranger_cd() {
+    temp_file="$(mktemp -t "ranger_cd.XXXXXXXXXX")"
+    ranger --choosedir="$temp_file" -- "${@:-$PWD}"
+    if chosen_dir="$(cat -- "$temp_file")" && [ -n "$chosen_dir" ] && [ "$chosen_dir" != "$PWD" ]; then
+        cd -- "$chosen_dir"
+    fi
+    rm -f -- "$temp_file"
+}
+
+# This binds Ctrl-O to ranger-cd:
+bind '"\C-o":"ranger-cd\C-m"'
diff --git a/examples/shell_subshell_notice.sh b/examples/shell_subshell_notice.sh
new file mode 100644
index 00000000..0d8d2431
--- /dev/null
+++ b/examples/shell_subshell_notice.sh
@@ -0,0 +1,10 @@
+# shellcheck shell=sh
+
+# Compatible with ranger 1.5.3 through 1.9.*
+#
+# Change the prompt when you open a shell from inside ranger
+#
+# Source this file from your shell startup file (.bashrc, .zshrc etc) for it to
+# work.
+
+[ -n "$RANGER_LEVEL" ] && PS1="$PS1"'(in ranger) '
diff --git a/ranger.py b/ranger.py
index 3c4b0f01..7160160f 100755
--- a/ranger.py
+++ b/ranger.py
@@ -9,17 +9,18 @@
 # default is simply "ranger". (Not this file itself!)
 # The other arguments are passed to ranger.
 """":
-tempfile="$(mktemp -t tmp.XXXXXX)"
+temp_file="$(mktemp -t "ranger_cd.XXXXXXXXXX")"
 ranger="${1:-ranger}"
-test -z "$1" || shift
-"$ranger" --choosedir="$tempfile" "${@:-$(pwd)}"
-returnvalue=$?
-test -f "$tempfile" &&
-if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
-    cd "$(cat "$tempfile")"
+if [ -n "$1" ]; then
+    shift
 fi
-rm -f -- "$tempfile"
-return $returnvalue
+"$ranger" --choosedir="$temp_file" -- "${@:-$PWD}"
+return_value="$?"
+if chosen_dir="$(cat -- "$temp_file")" && [ -n "$chosen_dir" ] && [ "$chosen_dir" != "$PWD" ]; then
+    cd -- "$chosen_dir"
+fi
+rm -f -- "$temp_file"
+return "$return_value"
 """
 
 from __future__ import (absolute_import, division, print_function)