summary refs log tree commit diff stats
path: root/ranger/gui
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-11-28 17:29:57 +0100
committerhut <hut@lavabit.com>2009-11-28 17:29:57 +0100
commit3d566884280b1db24cb9fa13adde3985f0e7c5a2 (patch)
tree4e84f707b965d1a7bbebf84858a15df6d8c3ef92 /ranger/gui
parent419e4aa53fc99c29dce66cd46b7f894efd2d57a8 (diff)
downloadranger-3d566884280b1db24cb9fa13adde3985f0e7c5a2.tar.gz
restructuration
Diffstat (limited to 'ranger/gui')
-rw-r--r--ranger/gui/__init__.py0
-rw-r--r--ranger/gui/color.py26
-rw-r--r--ranger/gui/defaultui.py39
-rw-r--r--ranger/gui/ui.py74
-rw-r--r--ranger/gui/wdisplay.py146
-rw-r--r--ranger/gui/widget.py57
-rw-r--r--ranger/gui/wtitlebar.py12
7 files changed, 354 insertions, 0 deletions
diff --git a/ranger/gui/__init__.py b/ranger/gui/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/ranger/gui/__init__.py
diff --git a/ranger/gui/color.py b/ranger/gui/color.py
new file mode 100644
index 00000000..5701c0ee
--- /dev/null
+++ b/ranger/gui/color.py
@@ -0,0 +1,26 @@
+#import curses
+
+color_pairs = {10: 0}
+
+
+#class ColorScheme():
+#	def isdir
+
+
+#def get_color(fg, bg):
+#	c = bg+2 + 9*(fg + 2)
+#	try:
+#		return color_pairs[c]
+#	except KeyError:
+#		size = len(color_pairs)
+#		curses.init_pair(size, curses.COLOR_RED, curses.COLOR_WHITE)
+#		color_pairs[c] = size
+#		return color_pairs[c]
+#
+#def color(fg = -1, bg = -1, attribute = 0):
+#	pass
+##	prin
+#	curses.attrset(attribute | curses.color_pair(get_color(fg, bg)))
+
+#color(-1, -1)
+#print(color_pairs)
diff --git a/ranger/gui/defaultui.py b/ranger/gui/defaultui.py
new file mode 100644
index 00000000..4d4f6d44
--- /dev/null
+++ b/ranger/gui/defaultui.py
@@ -0,0 +1,39 @@
+from ranger.gui.ui import UI as SuperClass
+from ranger.gui.wdisplay import WDisplay
+from ranger.gui.wtitlebar import WTitleBar
+
+RATIO = ( 0.15, 0.15, 0.4, 0.3 )
+
+class DefaultUI(SuperClass):
+	def setup(self):
+		self.titlebar = WTitleBar(self.win)
+		self.add_widget(self.titlebar)
+
+		self.displays = [
+				WDisplay(self.win, -2),
+				WDisplay(self.win, -1),
+				WDisplay(self.win, 0),
+				WDisplay(self.win, 1) ]
+		self.displays[2].display_infostring = True
+		self.displays[2].main_display = True
+		for disp in self.displays:
+			self.add_widget(disp)
+	
+	def resize(self):
+		SuperClass.resize(self)
+		y, x = self.win.getmaxyx()
+
+		leftborder = 0
+
+		i = 0
+		for ratio in RATIO:
+			wid = int(ratio * x)
+			try:
+				self.displays[i].setdim(1, leftborder, y-1, wid - 1)
+			except KeyError:
+				pass
+			leftborder += wid
+			i += 1
+
+		self.titlebar.setdim(0, 0, 1, x)
+
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
new file mode 100644
index 00000000..2abf8939
--- /dev/null
+++ b/ranger/gui/ui.py
@@ -0,0 +1,74 @@
+import curses
+from ranger.api import log
+class UI():
+	def __init__(self, env, commandlist):
+		self.env = env
+		self.commandlist = commandlist
+
+		self.widgets = []
+		self.win = curses.initscr()
+		self.win.leaveok(1)
+
+		self.initialize()
+
+		self.setup()
+		self.resize()
+
+	def initialize(self):
+		curses.noecho()
+		curses.halfdelay(10)
+		curses.curs_set(0)
+		curses.start_color()
+		curses.use_default_colors()
+
+	def setup(self):
+		pass
+
+	def resize(self):
+		self.env.termsize = self.win.getmaxyx()
+
+	def redraw(self):
+		self.win.redrawwin()
+		self.win.refresh()
+		self.win.redrawwin()
+
+	def add_widget(self, widg):
+		self.widgets.append(widg)
+
+	def feed_env(self, env):
+		self.env = env
+
+	def press(self, key, fm):
+		self.env.key_append(key)
+		log(self.env.keybuffer)
+
+		try:
+			cmd = self.commandlist.paths[self.env.keybuffer]
+		except KeyError:
+			self.env.key_clear()
+			return
+
+		if cmd == self.commandlist.dummy_object:
+			return
+
+		cmd.execute(fm)
+		self.env.key_clear()
+
+	def exit(self):
+		curses.nocbreak()
+		curses.echo()
+		curses.endwin()
+
+	def draw(self):
+		self.win.erase()
+		for widg in self.widgets:
+			widg.feed_env(self.env)
+			widg.draw()
+		self.win.refresh()
+#		log(self.env.cf)
+
+	def get_next_key(self):
+		key = self.win.getch()
+		curses.flushinp()
+		return key
+
diff --git a/ranger/gui/wdisplay.py b/ranger/gui/wdisplay.py
new file mode 100644
index 00000000..95014d51
--- /dev/null
+++ b/ranger/gui/wdisplay.py
@@ -0,0 +1,146 @@
+from ranger.gui.color import color_pairs
+from ranger.gui.widget import Widget as SuperClass
+import curses
+#from ranger.color import color
+
+class WDisplay(SuperClass):
+	def __init__(self, win, level):
+		SuperClass.__init__(self,win)
+		self.level = level
+		self.main_display = False
+		self.display_infostring = False
+		self.scroll_begin = 0
+
+	def feed_env(self, env):
+		self.target = env.at_level(self.level)
+		self.show_hidden = env.opt['show_hidden']
+		self.scroll_offset = env.opt['scroll_offset']
+		self.directories_first = env.opt['directories_first']
+		self.preview_files = env.opt['preview_files']
+
+	def draw(self):
+		from ranger.file import File
+		from ranger.directory import Directory
+
+		if self.target is None:
+			pass
+		elif type(self.target) == File:
+			self.draw_file()
+		elif type(self.target) == Directory:
+			self.draw_directory()
+		else:
+			self.win.addnstr(self.y, self.x, "unknown type.", self.wid)
+
+	def draw_file(self):
+		if not self.target.accessible:
+			self.win.addnstr(self.y, self.x, "not accessible", self.wid)
+			return
+		
+		if self.preview_files:
+			try:
+				if self.target.size < 1024 * 20:
+					f = open(self.target.path, 'r')
+					for line in range(self.hei):
+						read = f.readline().expandtabs()
+						self.win.addnstr(self.y + line, self.x, read, self.wid)
+			except:
+				pass
+
+		else:
+			self.win.addnstr(self.y, self.x, "this is a file.", self.wid)
+
+	def draw_directory(self):
+		from ranger.directory import Directory
+		self.target.show_hidden = self.show_hidden
+		self.target.load_content_if_outdated()
+		self.target.directories_first = self.directories_first
+		self.target.sort_if_outdated()
+		main_display = self.main_display
+
+		if self.target.empty():
+			self.color(bg=1)
+			self.win.addnstr(self.y, self.x, "empty", self.wid)
+			self.color()
+			return
+
+		if not self.target.accessible:
+			self.win.addnstr(self.y, self.x, "not accessible", self.wid)
+			return
+
+		self.set_scroll_begin()
+
+		selected_i = self.target.pointed_index
+		for line in range(self.hei):
+			i = line + self.scroll_begin
+			# last file reached?
+			try: drawed = self.target[i]
+			except IndexError: break
+
+			if isinstance(drawed, Directory):
+				self.color(fg = 4)
+			else:
+				self.color()
+
+			invert = i == selected_i
+			if invert:
+				self.win.attron(curses.A_REVERSE)
+
+			if self.main_display:
+				self.win.addnstr(
+						self.y + line,
+						self.x + 1,
+						' ' + drawed.basename + ' ',
+						self.wid - 2)
+			else:
+				self.win.addnstr(
+						self.y + line,
+						self.x,
+						drawed.basename,
+						self.wid)
+
+			if self.display_infostring and drawed.infostring:
+				info = drawed.infostring
+				x = self.x + self.wid - 1 - len(info)
+				if x > self.x:
+					self.win.addstr(self.y + line, x, str(info) + ' ')
+			self.win.attrset(0)
+
+	def get_scroll_begin(self):
+		offset = self.scroll_offset
+		dirsize = len(self.target)
+		winsize = self.hei
+		halfwinsize = winsize // 2
+		index = self.target.pointed_index or 0
+		original = self.target.scroll_begin
+		projected = index - original
+
+		upper_limit = winsize - 1 - offset
+		lower_limit = offset
+
+		if dirsize < winsize:
+			return 0
+
+		if halfwinsize < offset:
+			return min( dirsize - winsize, max( 0, index - halfwinsize ))
+
+		if original > dirsize - winsize:
+			self.target.scroll_begin = dirsize - winsize
+			return self.get_scroll_begin()
+
+		if projected < upper_limit and projected > lower_limit:
+			return original
+
+		if projected > upper_limit:
+			return min( dirsize - winsize,
+					original + (projected - upper_limit))
+
+		if projected < upper_limit:
+			return max( 0,
+					original - (lower_limit - projected))
+		
+		return original
+
+	def set_scroll_begin(self):
+		self.scroll_begin = self.get_scroll_begin()
+		self.target.scroll_begin = self.scroll_begin
+
diff --git a/ranger/gui/widget.py b/ranger/gui/widget.py
new file mode 100644
index 00000000..cb5fdc1f
--- /dev/null
+++ b/ranger/gui/widget.py
@@ -0,0 +1,57 @@
+import curses
+from ranger.gui.color import color_pairs
+
+class OutOfBoundsException(Exception): pass
+
+class Widget():
+	def __init__(self, win):
+		self.win = win
+		self.setdim(0, 0, 0, 0)
+
+	def get_color(self, fg, bg):
+		c = bg+2 + 9*(fg + 2)
+		try:
+			return color_pairs[c]
+		except KeyError:
+			size = len(color_pairs)
+			curses.init_pair(size, fg, bg)
+			color_pairs[c] = size
+			return color_pairs[c]
+
+	def color(self, fg = -1, bg = -1, attr = 0):
+		self.win.attrset(attr | curses.color_pair(self.get_color(fg, bg)))
+
+	def setdim(self, y, x, hei=None, wid=None):
+		maxy, maxx = self.win.getmaxyx()
+		wid = wid or maxx - x
+		hei = hei or maxy - y
+		if x + wid > maxx and y + hei > maxy:
+			raise OutOfBoundsException("X and Y out of bounds!")
+		if x + wid > maxx:
+			raise OutOfBoundsException("X out of bounds!")
+		if y + hei > maxy:
+			raise OutOfBoundsException("Y out of bounds!")
+
+		self.x = x
+		self.y = y
+		self.wid = wid
+		self.hei = hei
+	
+	def contains_point(self, y, x):
+		return (x >= self.x and x < self.x + self.wid) and \
+				(y >= self.y and y < self.y + self.hei)
+
+	def feed_env(self):
+		pass
+
+	def feed(self):
+		pass
+
+	def click(self):
+		pass
+	
+	def draw(self):
+		pass
+
+	def destroy(self):
+		pass
diff --git a/ranger/gui/wtitlebar.py b/ranger/gui/wtitlebar.py
new file mode 100644
index 00000000..2ac8ae65
--- /dev/null
+++ b/ranger/gui/wtitlebar.py
@@ -0,0 +1,12 @@
+import curses
+from ranger.gui.widget import Widget as SuperClass
+
+class WTitleBar(SuperClass):
+	def feed_env(self, env):
+		self.pathway = env.pathway
+
+	def draw(self):
+		self.win.move(self.y, self.x)
+		for path in self.pathway:
+			currentx = self.win.getyx()[1]
+			self.win.addnstr(path.basename + ' / ', (self.wid - currentx))