summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-25 07:53:53 +0100
committerhut <hut@lavabit.com>2009-12-25 07:53:53 +0100
commit5599636da56d7948c1918778f771217778d4129e (patch)
tree4b3d1efeaa9c6d97af4cef3f436af22587f3e4a2
parentfc972ea21dc9240a4b7147cc24674cdfa2aec11b (diff)
downloadranger-5599636da56d7948c1918778f771217778d4129e.tar.gz
made directories use the accumulator "interface"
-rw-r--r--ranger/actions.py15
-rw-r--r--ranger/commands.py6
-rw-r--r--ranger/container/environment.py5
-rw-r--r--ranger/ext/accumulator.py1
-rw-r--r--ranger/fsobject/directory.py99
-rw-r--r--ranger/gui/widgets/filelist.py8
-rw-r--r--ranger/gui/widgets/statusbar.py4
7 files changed, 49 insertions, 89 deletions
diff --git a/ranger/actions.py b/ranger/actions.py
index 7a1215df..ebd5ac02 100644
--- a/ranger/actions.py
+++ b/ranger/actions.py
@@ -8,14 +8,12 @@ class Actions(EnvironmentAware, SettingsAware):
 	def search_forward(self):
 		"""Search forward for the regexp in self.env.last_search"""
 		if self.env.pwd:
-			if self.env.pwd.search(self.env.last_search):
-				self.env.cf = self.env.pwd.pointed_file
+			self.env.pwd.search(self.env.last_search)
 
 	def search_backward(self):
 		"""Search backward for the regexp in self.env.last_search"""
 		if self.env.pwd:
-			if self.env.pwd.search(self.env.last_search, -1):
-				self.env.cf = self.env.pwd.pointed_file
+			self.env.pwd.search(self.env.last_search, -1)
 
 	def interrupt(self):
 		"""
@@ -122,12 +120,11 @@ class Actions(EnvironmentAware, SettingsAware):
 
 	def move_pointer(self, relative = 0, absolute = None):
 		"""Move the pointer down by <relative> or to <absolute>"""
-		self.env.cf = self.env.pwd.move_pointer(relative, absolute)
+		self.env.pwd.move(relative, absolute)
 
 	def move_pointer_by_pages(self, relative):
 		"""Move the pointer down by <relative> pages"""
-		self.env.cf = self.env.pwd.move_pointer(
-				relative = int(relative * self.env.termsize[0]))
+		self.env.pwd.move(relative=int(relative * self.env.termsize[0]))
 
 	def move_pointer_by_percentage(self, relative=0, absolute=None):
 		"""Move the pointer down by <relative>% or to <absolute>%"""
@@ -135,7 +132,7 @@ class Actions(EnvironmentAware, SettingsAware):
 			factor = len(self.env.pwd) / 100.0
 		except:
 			return
-		self.env.cf = self.env.pwd.move_pointer( \
+		self.env.cf = self.env.pwd.move( \
 				relative=int(relative * factor), \
 				absolute=int(absolute * factor) )
 
@@ -143,7 +140,7 @@ class Actions(EnvironmentAware, SettingsAware):
 		"""Scroll down by <relative> lines"""
 		if hasattr(self.ui, 'scroll'):
 			self.ui.scroll(relative)
-			self.env.cf = self.env.pwd.pointed_file
+			self.env.cf = self.env.pwd.pointed_obj
 
 	def redraw_window(self):
 		"""Redraw the window"""
diff --git a/ranger/commands.py b/ranger/commands.py
index 0ddd5d01..66e43064 100644
--- a/ranger/commands.py
+++ b/ranger/commands.py
@@ -152,13 +152,13 @@ class find(Command):
 		
 		length = len(pwd.files)
 		for i in range(length):
-			actual_index = (pwd.pointed_index + i) % length
+			actual_index = (pwd.pointer + i) % length
 			filename = pwd.files[actual_index].basename_lower
 			if arg in filename:
 				self.count += 1
 				if self.count == 1:
-					pwd.move_pointer(absolute=actual_index)
-					self.fm.env.cf = pwd.pointed_file
+					pwd.move(absolute=actual_index)
+					self.fm.env.cf = pwd.pointed_obj
 			if self.count > 1:
 				return False
 
diff --git a/ranger/container/environment.py b/ranger/container/environment.py
index b08b5320..8a6f6674 100644
--- a/ranger/container/environment.py
+++ b/ranger/container/environment.py
@@ -90,7 +90,7 @@ class Environment(SettingsAware):
 				last_path = path
 				continue
 
-			path.move_pointer_to_file_path(last_path)
+			path.move_to_obj(last_path)
 			last_path = path
 	
 	def history_go(self, relative):
@@ -137,10 +137,9 @@ class Environment(SettingsAware):
 		# set the current file.
 		self.pwd.directories_first = self.settings.directories_first
 		self.pwd.sort_if_outdated()
-		self.cf = self.pwd.pointed_file
+		self.cf = self.pwd.pointed_obj
 
 		if history:
 			self.history.add(new_pwd)
 
 		return True
-
diff --git a/ranger/ext/accumulator.py b/ranger/ext/accumulator.py
index 7fed72dd..7529471f 100644
--- a/ranger/ext/accumulator.py
+++ b/ranger/ext/accumulator.py
@@ -24,6 +24,7 @@ class Accumulator(object):
 
 		self.pointer = i
 		self.correct_pointer()
+		return self.pointer
 	
 	def move_to_obj(self, arg, attr=None):
 		if not arg:
diff --git a/ranger/fsobject/directory.py b/ranger/fsobject/directory.py
index 9942984a..12e687bc 100644
--- a/ranger/fsobject/directory.py
+++ b/ranger/fsobject/directory.py
@@ -1,5 +1,6 @@
 from ranger.fsobject import BAD_INFO, File, FileSystemObject
 from ranger.shared import SettingsAware
+from ranger.ext.accumulator import Accumulator
 from ranger import log
 import ranger.fsobject
 
@@ -14,7 +15,7 @@ def sort_by_directory(path):
 class NoDirectoryGiven(Exception):
 	pass
 
-class Directory(FileSystemObject, SettingsAware):
+class Directory(FileSystemObject, Accumulator, SettingsAware):
 	enterable = False
 	load_generator = None
 	loading = False
@@ -23,8 +24,6 @@ class Directory(FileSystemObject, SettingsAware):
 	files = None
 	filter = None
 	marked_items = None
-	pointed_index = None
-	pointed_file = None
 	scroll_begin = 0
 	scroll_offset = 0
 
@@ -46,6 +45,7 @@ class Directory(FileSystemObject, SettingsAware):
 		if isfile(path):
 			raise NoDirectoryGiven()
 
+		Accumulator.__init__(self)
 		FileSystemObject.__init__(self, path)
 
 		self.marked_items = set()
@@ -56,6 +56,9 @@ class Directory(FileSystemObject, SettingsAware):
 		self.old_sort = self.settings.sort
 		self.old_reverse = self.settings.reverse
 	
+	def get_list(self):
+		return self.files
+	
 	def mark_item(self, item, val):
 		item._mark(val)
 		if val:
@@ -95,8 +98,8 @@ class Directory(FileSystemObject, SettingsAware):
 		self._gc_marked_items()
 		if self.marked_items:
 			return set(self.marked_items)
-		elif self.pointed_file:
-			return set([self.pointed_file])
+		elif self.pointed_obj:
+			return set([self.pointed_obj])
 		else:
 			return set()
 	
@@ -156,8 +159,10 @@ class Directory(FileSystemObject, SettingsAware):
 			self.sort()
 
 			if len(self.files) > 0:
-				if self.pointed_file is not None:
-					self.move_pointer_to_file_path(self.pointed_file)
+				if self.pointed_obj is not None:
+					self.sync_index()
+				else:
+					self.move(absolute=0)
 		else:
 			self.filenames = None
 			self.files = None
@@ -203,7 +208,7 @@ class Directory(FileSystemObject, SettingsAware):
 		if self.files is None:
 			return
 
-		old_pointed_file = self.pointed_file
+		old_pointed_obj = self.pointed_obj
 		try:
 			sort_func = self.sort_dict[self.settings.sort]
 		except:
@@ -216,8 +221,8 @@ class Directory(FileSystemObject, SettingsAware):
 		if self.settings.directories_first:
 			self.files.sort(key = sort_by_directory)
 
-		if self.pointed_index is not None:
-			self.move_pointer_to_file_path(old_pointed_file)
+		if self.pointer is not None:
+			self.move_to_obj(old_pointed_obj)
 		else:
 			self.correct_pointer()
 
@@ -232,47 +237,17 @@ class Directory(FileSystemObject, SettingsAware):
 				or self.old_reverse != self.settings.reverse:
 			self.sort()
 
-	# Notice: fm.env.cf should always point to the current file. If you
-	# modify the current directory with this function, make sure
-	# to update fm.env.cf aswell.
-	def move_pointer(self, relative=0, absolute=None):
-		"""Move the index pointer"""
-		if self.empty(): return
-		i = self.pointed_index
-		if isinstance(absolute, int):
-			if absolute < 0:
-				absolute = len(self.files) + absolute
-			i = absolute
-
-		if isinstance(relative, int):
-			i += relative
-
-		self.pointed_index = i
-		self.correct_pointer()
-		return self.pointed_file
-
-	def move_pointer_to_file_path(self, path):
-		"""
-		Move the index pointer to the index of the file object
-		with the given path.
-		"""
-		if path is None: return
-		try: path = path.path
-		except AttributeError: pass
-
-		self.load_content_once()
-		if self.empty(): return
+	def move_to_obj(self, arg):
+		try:
+			arg = arg.path
+		except:
+			pass
+		self.load_content_once(schedule=False)
+		if self.empty():
+			return
 
-		i = 0
-		for f in self.files:
-			if f.path == path:
-				self.move_pointer(absolute = i)
-				self.correct_pointer()
-				return True
-			i += 1
+		Accumulator.move_to_obj(self, arg, attr='path')
 
-		return self.move_pointer(absolute=self.pointed_index)
-	
 	def search(self, arg, direction = 1):
 		"""Search for a regular expression"""
 		if self.empty() or arg is None:
@@ -285,38 +260,26 @@ class Directory(FileSystemObject, SettingsAware):
 		length = len(self)
 
 		if direction > 0:
-			generator = ((self.pointed_index + (x + 1)) % length for x in range(length-1))
+			generator = ((self.pointer + (x + 1)) % length for x in range(length-1))
 		else:
-			generator = ((self.pointed_index - (x + 1)) % length for x in range(length-1))
+			generator = ((self.pointer - (x + 1)) % length for x in range(length-1))
 
 		for i in generator:
 			_file = self.files[i]
 			if fnc(_file):
-				self.pointed_index = i
-				self.pointed_file = _file
+				self.pointer = i
+				self.pointed_obj = _file
+				self.correct_pointer()
 				return True
 		return False
 
 	def correct_pointer(self):
 		"""Make sure the pointer is in the valid range"""
-
-		if self.files is None or len(self.files) == 0:
-			self.pointed_index = None
-			self.pointed_file = None
-
-		else:
-			i = self.pointed_index
-
-			if i is None: i = 0
-			if i >= len(self.files): i = len(self.files) - 1
-			if i < 0: i = 0
-
-			self.pointed_index = i
-			self.pointed_file = self[i]
+		Accumulator.correct_pointer(self)
 
 		try:
 			if self == self.fm.env.pwd:
-				self.fm.env.cf = self.pointed_file
+				self.fm.env.cf = self.pointed_obj
 		except:
 			pass
 		
diff --git a/ranger/gui/widgets/filelist.py b/ranger/gui/widgets/filelist.py
index 999cfd12..4973ae4f 100644
--- a/ranger/gui/widgets/filelist.py
+++ b/ranger/gui/widgets/filelist.py
@@ -30,7 +30,7 @@ class FileList(Widget):
 					self.fm.enter_dir(self.target.path)
 
 				if index < len(self.target):
-					self.fm.move_pointer(absolute = index)
+					self.fm.move(absolute = index)
 			elif event.pressed(3):
 				try:
 					clicked_file = self.target[index]
@@ -160,7 +160,7 @@ class FileList(Widget):
 
 		self.set_scroll_begin()
 
-		selected_i = self.target.pointed_index
+		selected_i = self.target.pointer
 		for line in range(self.hei):
 			i = line + self.scroll_begin
 
@@ -213,7 +213,7 @@ class FileList(Widget):
 		dirsize = len(self.target)
 		winsize = self.hei
 		halfwinsize = winsize // 2
-		index = self.target.pointed_index or 0
+		index = self.target.pointer or 0
 		original = self.target.scroll_begin
 		projected = index - original
 
@@ -261,5 +261,5 @@ class FileList(Widget):
 		self.set_scroll_begin()
 
 		if self.target.scroll_begin == old_value:
-			self.target.move_pointer(relative = relative)
+			self.target.move(relative = relative)
 			self.target.scroll_begin += relative
diff --git a/ranger/gui/widgets/statusbar.py b/ranger/gui/widgets/statusbar.py
index 6913672d..741e80fd 100644
--- a/ranger/gui/widgets/statusbar.py
+++ b/ranger/gui/widgets/statusbar.py
@@ -76,9 +76,9 @@ class StatusBar(Widget):
 		part = []
 
 		if self.filelist is not None:
-			target = self.filelist.target.pointed_file
+			target = self.filelist.target.pointed_obj
 		else:
-			target = self.env.at_level(0).pointed_file
+			target = self.env.at_level(0).pointed_obj
 
 		if target is None:
 			return part