summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xranger.py21
-rwxr-xr-xwrapper.sh16
2 files changed, 33 insertions, 4 deletions
diff --git a/ranger.py b/ranger.py
index cfac83c3..2ab5ee1d 100755
--- a/ranger.py
+++ b/ranger.py
@@ -1,13 +1,21 @@
 #!/usr/bin/python
 # coding=utf-8
 
-# TODO: cd after exit
+import sys, os
+
+# Change the directory of the parent shell after exiting Ranger.
+# Read the comments in wrapper.sh for more info.
+try:
+	assert sys.argv[1] == '--cd-after-exit'
+	cd_after_exit = True
+	sys.stderr = sys.stdout
+	del sys.argv[1]
+except:
+	cd_after_exit = False
 
 from ranger import debug, fm, options, environment, command, keys
 from ranger.defaultui import DefaultUI as UI
 
-# TODO: find out the real name of this script and include files relative to here
-
 # TODO: Parse arguments
 
 # TODO: load config
@@ -32,7 +40,12 @@ def main():
 	except BaseException as original_error:
 		try: my_ui.exit()
 		except: pass
-
+	
 		raise original_error
 
+	finally:
+		if cd_after_exit:
+			try: sys.__stderr__.write(env.pwd.path)
+			except: pass
+
 if __name__ == "__main__": main()
diff --git a/wrapper.sh b/wrapper.sh
new file mode 100755
index 00000000..080033c2
--- /dev/null
+++ b/wrapper.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+#
+# Use this file to activate a special feature:
+# Changing the directory of the parent shell after exiting Ranger.
+# To use this, create an alias for this command in your shell config:
+#
+# source path/to/wrapper.sh path/to/ranger.py
+#
+# (This does not work with all shells, it was successfully tested
+# with bash and zsh though.)
+#
+if [ $1 ]; then
+	cd "`$1 --cd-after-exit $@ 3>&1 1>&2 2>&3 3>&-`"
+else
+	echo "use with: source path/to/wrapper.sh path/to/ranger.py"
+fi