summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-15 23:44:49 +0100
committerhut <hut@lavabit.com>2009-12-15 23:44:49 +0100
commite48e3c5c5046bf992a563d112629f10a3adc8168 (patch)
treeeb1a41180d2918dbe78da52b3730fa53fc6bf6a2
parent7ec262f86c81c4df4360643b05dfb788d294a8ae (diff)
downloadranger-e48e3c5c5046bf992a563d112629f10a3adc8168.tar.gz
fixed CD command / tab completion
-rw-r--r--ranger/commands.py75
-rw-r--r--ranger/container/environment.py5
-rw-r--r--ranger/gui/widgets/console.py17
3 files changed, 48 insertions, 49 deletions
diff --git a/ranger/commands.py b/ranger/commands.py
index d016a822..e61e9fd8 100644
--- a/ranger/commands.py
+++ b/ranger/commands.py
@@ -47,51 +47,43 @@ class cd(Command):
 			self.fm.enter_dir(destination)
 
 	def tab(self):
+		from os.path import dirname, basename, expanduser, join, isdir
+
 		line = parse(self.line)
+		pwd = self.fm.env.pwd.path
+
 		try:
-			dest = line.chunks[1]
+			rel_dest = line.chunks[1]
 		except IndexError:
-			dest = ''
-
-		if dest.startswith('~'):
-			return line + os.path.expanduser(dest) + '/'
-
-		absolute = lambda path: os.path.join(self.fm.env.pwd.path, path)
-		absdest = absolute(dest)
-
-#		if dest == '':
-#			return sorted(os.listdir(dest))
+			rel_dest = ''
 
-		if dest.endswith('/') or dest == '':
-			if os.path.isdir(dest):
-				walker = os.walk(absdest)
-				_, dirnames, _ = walker.next()
-				dirnames.sort()
-				return (line.line + dirname for dirname in dirnames)
+		if rel_dest.startswith('~'):
+			return line + expanduser(rel_dest) + '/'
 
+		abs_dest = join(pwd, rel_dest)
+		abs_dirname = dirname(abs_dest)
+		rel_basename = basename(rel_dest)
+		rel_dirname = dirname(rel_dest)
+		
 		try:
-			original_dirname = os.path.dirname(absdest)
-			basename = os.path.basename(absdest)
-
-			walker = os.walk(original_dirname)
-			_, dirnames, _ = walker.next()
-			dirnames = [dn for dn in dirnames if dn.startswith(basename)]
-
+			if rel_dest.endswith('/') or rel_dest == '':
+				_, dirnames, _ = os.walk(abs_dest).next()
+			else:
+				_, dirnames, _ = os.walk(abs_dirname).next()
+				dirnames = [dn for dn in dirnames \
+						if dn.startswith(rel_basename)]
+		except (OSError, StopIteration):
+			pass
+		else:
 			dirnames.sort()
 
-			start = line + os.path.dirname(dest) + '/'
 			if len(dirnames) == 0:
 				return
-			elif len(dirnames) == 1:
-				if os.path.isdir(os.path.join(absdest, dirnames[0])):
-					return start + dirnames[0] + '/'
-				else:
-					return start + dirnames[0]
-			else:
-				return (start + dirname for dirname in dirnames)
-		except OSError:
-			pass
 
+			if len(dirnames) == 1:
+				return line + join(rel_dirname, dirnames[0]) + '/'
+
+			return (line + join(rel_dirname, dirname) for dirname in dirnames)
 
 # -------------------------------- rest
 
@@ -104,7 +96,18 @@ for varname, var in vars().copy().items():
 		pass
 
 def execute(name, line):
-	return by_name[name](line).execute()
+	try:
+		command = by_name[name](line)
+	except KeyError:
+		pass
+	else:
+		command.execute()
 
 def tab(name, line):
-	return by_name[name](line).tab()
+	try:
+		command = by_name[name](line)
+	except KeyError:
+		pass
+	else:
+		return command.tab()
+
diff --git a/ranger/container/environment.py b/ranger/container/environment.py
index a6d291cf..11c0082b 100644
--- a/ranger/container/environment.py
+++ b/ranger/container/environment.py
@@ -1,4 +1,4 @@
-from os.path import abspath, normpath, join, expanduser
+from os.path import abspath, normpath, join, expanduser, isdir
 from ranger.fsobject.directory import Directory, NoDirectoryGiven
 from ranger.container import KeyBuffer, History
 from ranger.shared import SettingsAware
@@ -91,6 +91,9 @@ level <0 => parent directories"""
 		# get the absolute path
 		path = normpath(join(self.path, expanduser(path)))
 
+		if not isdir(path):
+			return
+
 		try:
 			new_pwd = self.get_directory(path)
 		except NoDirectoryGiven:
diff --git a/ranger/gui/widgets/console.py b/ranger/gui/widgets/console.py
index 25341e00..7421db98 100644
--- a/ranger/gui/widgets/console.py
+++ b/ranger/gui/widgets/console.py
@@ -140,7 +140,7 @@ class CommandConsole(Console):
 	prompt = ':'
 
 	def execute(self):
-		self._exec_cmd()
+		commands.execute(self._get_cmd(), self.line)
 		Console.execute(self)
 	
 	def tab(self, n=1):
@@ -163,26 +163,19 @@ class CommandConsole(Console):
 			self.line = self.tab_deque[0]
 			self.pos = len(self.line)
 
-	def _get_tab(self):
-		cmd = self._get_cmd()
-		try:
-			return commands.tab(cmd, self.line)
-		except KeyError:
-			return None
-	
 	def _get_cmd(self):
 		try:
 			return self.line.split()[0]
 		except:
 			return ''
 	
-	def _exec_cmd(self):
+	def _get_tab(self):
 		cmd = self._get_cmd()
 		try:
-			commands.execute(cmd, self.line)
+			return commands.tab(cmd, self.line)
 		except KeyError:
-			pass # command not found!
-			
+			return commands.tab(None, self.line)
+
 
 class QuickCommandConsole(Console):
 	prompt = '>'
' href='#n388'>388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450