summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-28 03:39:32 +0100
committerhut <hut@lavabit.com>2009-12-28 03:39:32 +0100
commit06aefcf5068e733f22251894306c2818860bf255 (patch)
tree37d1bc24b72a358e146f6246d8c8a303c2c80df6 /ranger
parentadcdb2d3fdda337efd85750c965da2992debfe4e (diff)
downloadranger-06aefcf5068e733f22251894306c2818860bf255.tar.gz
display the contents of the file in the pager
Diffstat (limited to 'ranger')
-rw-r--r--ranger/actions.py12
-rw-r--r--ranger/defaults/keys.py27
-rw-r--r--ranger/ext/move.py8
-rw-r--r--ranger/gui/defaultui.py24
-rw-r--r--ranger/gui/displayable.py9
-rw-r--r--ranger/gui/widgets/filelistcontainer.py31
-rw-r--r--ranger/gui/widgets/pager.py78
7 files changed, 177 insertions, 12 deletions
diff --git a/ranger/actions.py b/ranger/actions.py
index 968aae4f..3e5ebdb7 100644
--- a/ranger/actions.py
+++ b/ranger/actions.py
@@ -151,6 +151,18 @@ class Actions(EnvironmentAware, SettingsAware):
 	def handle_mouse(self):
 		"""Handle mouse-buttons if one was pressed"""
 		self.ui.handle_mouse()
+	
+	def display_file(self):
+		if not hasattr(self.ui, 'open_embedded_pager'):
+			return
+
+		try:
+			f = open(self.env.cf.path, 'r')
+		except:
+			pass
+		else:
+			pager = self.ui.open_embedded_pager()
+			pager.set_source(f)
 
 	def execute_file(self, files, app='', flags='', mode=0):
 		"""Execute a file.
diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py
index cfbcc0e9..5e764ddd 100644
--- a/ranger/defaults/keys.py
+++ b/ranger/defaults/keys.py
@@ -44,8 +44,8 @@ def initialize_commands(command_list):
 	bind(KEY_NPAGE, ctrl('f'), fm.move_pointer_by_pages(1))
 	bind(KEY_PPAGE, ctrl('b'), fm.move_pointer_by_pages(-1))
 	bind('E', fm.edit_file())
-	bind('i', fm.tag_toggle())
-	bind('I', fm.tag_remove())
+	bind('b', fm.tag_toggle())
+	bind('B', fm.tag_remove())
 
 	bind(' ', fm.mark(toggle=True))
 	bind('v', fm.mark(all=True, toggle=True))
@@ -91,6 +91,8 @@ def initialize_commands(command_list):
 		if cf:
 			fm.open_console(cmode.COMMAND, 'rename ' + cf.basename)
 
+	bind('i', fm.display_file())
+
 	bind('A', edit_name)
 	bind('cw', fm.open_console(cmode.COMMAND, 'rename '))
 	bind('cd', fm.open_console(cmode.COMMAND, 'cd '))
@@ -219,6 +221,25 @@ def initialize_process_manager_commands(command_list):
 
 	bind('dd', wdg.process_remove())
 	bind('w', ESC, ctrl('d'), ctrl('c'),
-			lambda arg: arg.wdg.fm.ui.close_pman())
+			lambda arg: arg.fm.ui.close_pman())
+
+	command_list.rebuild_paths()
+
+def initialize_pager_commands(command_list):
+	initialize_embedded_pager_commands(command_list)
+
+def initialize_embedded_pager_commands(command_list):
+	system_functions(command_list)
+	bind, hint = make_abbreviations(command_list)
+
+	bind('j', KEY_DOWN, nwrap.move(relative=1))
+	bind('k', KEY_DOWN, nwrap.move(relative=-1))
+	bind('gg', KEY_DOWN, nwrap.move(absolute=0))
+	bind('G', KEY_DOWN, nwrap.move(absolute=-1))
+	
+	bind('q', 'i', lambda arg: arg.fm.ui.close_embedded_pager())
+	bind('h', wdg.move_horizontal(relative=-4))
+	bind('l', wdg.move_horizontal(relative=4))
+	bind('Q', 'ZZ', fm.exit())
 
 	command_list.rebuild_paths()
diff --git a/ranger/ext/move.py b/ranger/ext/move.py
new file mode 100644
index 00000000..5909a876
--- /dev/null
+++ b/ranger/ext/move.py
@@ -0,0 +1,8 @@
+def move_between(current, minimum, maximum, relative=0, absolute=None):
+	i = current
+	if isinstance(absolute, int):
+		i = absolute
+	if isinstance(relative, int):
+		i += relative
+	i = max(minimum, min(maximum - 1, i))
+	return i
diff --git a/ranger/gui/defaultui.py b/ranger/gui/defaultui.py
index 31049178..1bb62d72 100644
--- a/ranger/gui/defaultui.py
+++ b/ranger/gui/defaultui.py
@@ -12,6 +12,8 @@ class DefaultUI(UI):
 		from ranger.gui.widgets.statusbar import StatusBar
 		from ranger.gui.widgets.process_manager import ProcessManager
 		from ranger.gui.widgets.notify import Notify
+		from ranger.gui.widgets.pager import Pager
+
 		self.titlebar = TitleBar(self.win)
 		self.add_obj(self.titlebar)
 
@@ -32,16 +34,19 @@ class DefaultUI(UI):
 		self.notify = Notify(self.win)
 		self.add_obj(self.notify)
 
+		self.pager = Pager(self.win)
+		self.add_obj(self.pager)
+
 	def update_size(self):
 		"""resize all widgets"""
 		UI.update_size(self)
 		y, x = self.env.termsize
 
 		notify_hei = self.notify.requested_height
-#		log(notify_hei)
 
 		self.filelist_container.resize(1, 0, y - 2 - notify_hei, x)
 		self.pman.resize(1, 0, y - 2 - notify_hei, x)
+		self.pager.resize(1, 0, y - 2 - notify_hei, x)
 		self.notify.resize(y - 1 - notify_hei, 0, notify_hei, x)
 		self.titlebar.resize(0, 0, 1, x)
 		self.status.resize(y - 1, 0, 1, x)
@@ -55,6 +60,23 @@ class DefaultUI(UI):
 	def display(self, *a, **k):
 		return self.notify.display(*a, **k)
 
+	def close_pager(self):
+		self.pager.visible = False
+		self.pager.focused = False
+		self.filelist_container.visible = True
+	
+	def open_pager(self):
+		self.pager.visible = True
+		self.pager.focused = True
+		self.filelist_container.visible = False
+
+	def open_embedded_pager(self):
+		self.filelist_container.open_pager()
+		return self.filelist_container.pager
+
+	def close_embedded_pager(self):
+		self.filelist_container.close_pager()
+	
 	def open_console(self, mode, string=''):
 		if self.console.open(mode, string):
 			self.console.on_close = self.close_console
diff --git a/ranger/gui/displayable.py b/ranger/gui/displayable.py
index e3191cdb..62ff41f9 100644
--- a/ranger/gui/displayable.py
+++ b/ranger/gui/displayable.py
@@ -83,6 +83,15 @@ class Displayable(EnvironmentAware, FileManagerAware, SettingsAware):
 		"""
 		pass
 
+	def activate(self, boolean):
+		boolean = bool(boolean)
+		self.visible = boolean
+		self.focused = boolean
+	
+	def show(self, boolean):
+		boolean = bool(boolean)
+		self.visible = boolean
+
 	def poke(self):
 		"""Called before drawing, even if invisible"""
 	
diff --git a/ranger/gui/widgets/filelistcontainer.py b/ranger/gui/widgets/filelistcontainer.py
index 6e925f83..53cbf064 100644
--- a/ranger/gui/widgets/filelistcontainer.py
+++ b/ranger/gui/widgets/filelistcontainer.py
@@ -1,6 +1,7 @@
 """The FileListContainer manages a set of FileLists."""
 from . import Widget
 from .filelist import FileList
+from .pager import Pager
 from ..displayable import DisplayableContainer
 from ranger import log
 
@@ -39,6 +40,9 @@ class FileListContainer(Widget, DisplayableContainer):
 		else:
 			self.main_filelist.display_infostring = True
 			self.main_filelist.main_display = True
+
+		self.pager = Pager(win, embedded=True)
+		self.add_obj(self.pager)
 	
 	def resize(self, y, x, hei, wid):
 		"""Resize all the filelists according to the given ratio"""
@@ -53,12 +57,17 @@ class FileListContainer(Widget, DisplayableContainer):
 		else:
 			generator = zip(self.ratios, range(len(self.ratios)))
 
+		last_i = len(self.ratios) - 1
+
 		for ratio, i in generator:
 			wid = int(ratio * self.wid)
 
-			if i == len(self.ratios) - 1:
+			if i == last_i:
 				wid = int(self.wid - left + 1)
 
+			if i == last_i - 1:
+				self.pager.resize(self.y, left, hei, max(1, self.wid - left))
+
 			try:
 				self.container[i].resize(self.y, left, hei, max(1, wid-1))
 			except KeyError:
@@ -66,10 +75,28 @@ class FileListContainer(Widget, DisplayableContainer):
 
 			left += wid
 	
+	def open_pager(self):
+		self.pager.activate(True)
+		self.pager.open()
+		try:
+			self.container[-2].show(False)
+			self.container[-3].show(False)
+		except IndexError:
+			pass
+	
+	def close_pager(self):
+		self.pager.activate(False)
+		self.pager.close()
+		try:
+			self.container[-2].show(True)
+			self.container[-3].show(True)
+		except IndexError:
+			pass
+	
 	def poke(self):
 		DisplayableContainer.poke(self)
 		if self.settings.collapse_preview and self.preview:
-			has_preview = self.container[-1].has_preview()
+			has_preview = self.container[-2].has_preview()
 			if self.preview_available != has_preview:
 				self.preview_available = has_preview
 				self.resize(self.y, self.x, self.hei, self.wid)
diff --git a/ranger/gui/widgets/pager.py b/ranger/gui/widgets/pager.py
index 7a93a2d6..6a147766 100644
--- a/ranger/gui/widgets/pager.py
+++ b/ranger/gui/widgets/pager.py
@@ -3,21 +3,39 @@ The pager displays text and allows you to scroll inside it.
 """
 from ranger import log
 from . import Widget
+from ranger.container.commandlist import CommandList
+from ranger.ext.move import move_between
 
 class Pager(Widget):
 	source = None
 	source_is_stream = False
-	def __init__(self, win):
+	def __init__(self, win, embedded=False):
 		Widget.__init__(self, win)
+		self.embedded = embedded
 		self.scroll_begin = 0
+		self.startx = 0
+		self.lines = []
 
-#		self.commandlist = CommandList()
-#		self.settings.keys.initialize_pager_commands( \
-#				self.commandlist)
+		self.commandlist = CommandList()
+
+		if embedded:
+			keyfnc = self.settings.keys.initialize_embedded_pager_commands
+		else:
+			keyfnc = self.settings.keys.initialize_pager_commands
+
+		keyfnc(self.commandlist)
+	
+	def open(self):
+		self.scroll_begin = 0
+		self.startx = 0
+	
+	def close(self):
+		if self.source and self.source_is_stream:
+			self.source.close()
 	
 	def draw(self):
 		line_gen = self._generate_lines(
-				starty=self.scroll_begin, startx=0)
+				starty=self.scroll_begin, startx=self.startx)
 
 		for line, i in zip(line_gen, range(self.hei)):
 			y, x = self.y + i, self.x
@@ -27,6 +45,54 @@ class Pager(Widget):
 			except:
 				pass
 	
+	def move(self, relative=0, absolute=None):
+		i = self.scroll_begin
+		if isinstance(absolute, int):
+			i = absolute
+
+		if isinstance(relative, int):
+			i += relative
+
+		length = len(self.lines) - self.hei - 1
+		log('before: ' + str(length))
+
+		if i >= length:
+			self._get_line(i+self.hei)
+
+		length = len(self.lines) - self.hei - 1
+		log('after: ' + str(length))
+
+		if i >= length:
+			i = length
+
+		if i < 0:
+			i = 0
+
+		self.scroll_begin = i
+	
+	def move_horizontal(self, relative=0, absolute=None):
+		self.startx = move_between(
+				current=self.startx,
+				minimum=0,
+				maximum=999,
+				relative=relative,
+				absolute=absolute)
+
+	def press(self, key):
+		try:
+			tup = self.env.keybuffer.tuple_without_numbers()
+			if tup:
+				cmd = self.commandlist[tup]
+			else:
+				return
+				
+		except KeyError:
+			self.env.key_clear()
+		else:
+			if hasattr(cmd, 'execute'):
+				cmd.execute_wrap(self)
+				self.env.key_clear()
+	
 	def set_source(self, source):
 		if self.source and self.source_is_stream:
 			self.source.close()
@@ -44,7 +110,7 @@ class Pager(Widget):
 
 		self.source = source
 		return True
-	
+
 	def _get_line(self, n, attempt_to_read=True):
 		try:
 			return self.lines[n]