summary refs log blame commit diff stats
path: root/doc/pydoc/ranger.defaults.html
blob: 22015dc05745e38c8d7b07fb4e225e6e6d60e3b5 (plain) (tree)























                                                                                                                                                                                                                       
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: package ranger.defaults</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.defaults</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/defaults/__init__.py">/home/hut/work/ranger/ranger/defaults/__init__.py</a></font></td></tr></table>
    <p><tt>Default&nbsp;options&nbsp;and&nbsp;configration&nbsp;files</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Package Contents</strong></big></font></td></tr>
    
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="ranger.defaults.apps.html">apps</a><br>
</td><td width="25%" valign=top><a href="ranger.defaults.keys.html">keys</a><br>
</td><td width="25%" valign=top><a href="ranger.defaults.options.html">options</a><br>
</td><td width="25%" valign=top></td></tr></table></td></tr></table>
</body></html>
                                          
                                        
                                     

                               
 















                                                                                      


                                                
                                                                             

                                                              
                                               
        








                                                       



                                                       






























                                                                        




                                                         
                                                     










                                                     
 























                                                                                  
"""
The pager displays text and allows you to scroll inside it.
"""
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, embedded=False):
		Widget.__init__(self, win)
		self.embedded = embedded
		self.scroll_begin = 0
		self.startx = 0
		self.lines = []

		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=self.startx)

		for line, i in zip(line_gen, range(self.hei)):
			self.addstr(i, 0, line)
	
	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
		if i >= length:
			self._get_line(i+self.hei)

		length = len(self.lines) - self.hei - 1
		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()

		if hasattr(source, '__getitem__'):
			self.source_is_stream = False
			self.lines = source
		elif hasattr(source, 'readline'):
			self.source_is_stream = True
			self.lines = []
		else:
			self.source = None
			self.source_is_stream = False
			return False

		self.source = source
		return True

	def _get_line(self, n, attempt_to_read=True):
		try:
			return self.lines[n]
		except (KeyError, IndexError):
			if attempt_to_read and self.source_is_stream:
				for l in self.source:
					self.lines.append(l)
					if len(self.lines) > n:
						break
				return self._get_line(n, attempt_to_read=False)
			return ""
	
	def _generate_lines(self, starty, startx):
		i = starty
		if not self.source:
			raise StopIteration
		while True:
			try:
				line = self._get_line(i).expandtabs(4)
				line = line[startx:self.wid - 1 + startx].rstrip()
				yield line
			except IndexError:
				raise StopIteration
			i += 1