[Lldb-commits] [lldb] r141846 - /lldb/trunk/examples/customization/pwd-cd-and-system/utils.py

Johnny Chen johnny.chen at apple.com
Wed Oct 12 18:20:34 PDT 2011


Author: johnny
Date: Wed Oct 12 20:20:34 2011
New Revision: 141846

URL: http://llvm.org/viewvc/llvm-project?rev=141846&view=rev
Log:
Add 'cd -' feature to change to the previous working directory.

Modified:
    lldb/trunk/examples/customization/pwd-cd-and-system/utils.py

Modified: lldb/trunk/examples/customization/pwd-cd-and-system/utils.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/examples/customization/pwd-cd-and-system/utils.py?rev=141846&r1=141845&r2=141846&view=diff
==============================================================================
--- lldb/trunk/examples/customization/pwd-cd-and-system/utils.py (original)
+++ lldb/trunk/examples/customization/pwd-cd-and-system/utils.py Wed Oct 12 20:20:34 2011
@@ -2,13 +2,37 @@
 
 import os, shlex, subprocess
 
+# Store the previous working directory for the 'cd -' command.
+class Holder:
+    """Holds the _prev_dir_ class attribute for chdir() function."""
+    _prev_dir_ = None
+
+    @classmethod
+    def prev_dir(cls):
+        return cls._prev_dir_
+
+    @classmethod
+    def swap(cls, dir):
+        cls._prev_dir_ = dir
+
 def chdir(debugger, args, result, dict):
-    """Change the working directory, or cd to ${HOME}."""
-    dir = args.strip()
-    if dir:
-        os.chdir(args)
-    else:
-        os.chdir(os.path.expanduser('~'))
+    """
+    Change the working directory, or cd to ${HOME}.
+    You can also issue 'cd -' to change to the previous working directory.    
+    """
+    new_dir = args.strip()
+    if not new_dir:
+        new_dir = os.path.expanduser('~')
+    elif new_dir == '-':
+        if not Holder.prev_dir():
+            # Bad directory, not changing.
+            print "bad directory, not changing"
+            return
+        else:
+            new_dir = Holder.prev_dir()
+
+    Holder.swap(os.getcwd())
+    os.chdir(new_dir)
     print "Current working directory: %s" % os.getcwd()
 
 def system(debugger, command_line, result, dict):





More information about the lldb-commits mailing list