about summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-11-29 18:58:13 +0100
committerhut <hut@lavabit.com>2009-11-29 18:58:13 +0100
commitf027adc08ce0d15717c7694956f23ff637553543 (patch)
tree59ccf6cf540da9b34d32f19ebb7d5abdae1ce5a7 /ranger
parent9dc6fe07975ef2d489e5071f4e6d7223a33cb3b3 (diff)
downloadranger-f027adc08ce0d15717c7694956f23ff637553543.tar.gz
implemented colorschemes.
Diffstat (limited to 'ranger')
-rw-r--r--ranger/conf/colorschemes/snow.py35
-rw-r--r--ranger/conf/options.py2
-rw-r--r--ranger/gui/color.py33
-rw-r--r--ranger/gui/colorscheme.py64
-rw-r--r--ranger/gui/ui.py2
-rw-r--r--ranger/gui/wdisplay.py53
-rw-r--r--ranger/gui/widget.py27
7 files changed, 141 insertions, 75 deletions
diff --git a/ranger/conf/colorschemes/snow.py b/ranger/conf/colorschemes/snow.py
index 52f5dbb4..40909c9d 100644
--- a/ranger/conf/colorschemes/snow.py
+++ b/ranger/conf/colorschemes/snow.py
@@ -1,5 +1,38 @@
 from ranger.gui.colorscheme import ColorScheme
+from ranger.gui.color import *
 
 class Snow(ColorScheme):
 	def use(self, context):
-		return 1, 0, 0
+		if context.reset:
+			return default_colors
+
+		if context.wdisplay:
+			fg, bg = default, default
+
+			if context.selected:
+				attr = reverse
+			else:
+				attr = normal
+
+			if context.empty:
+				bg = red
+
+			if context.directory:
+				fg = blue
+
+			if context.executable:
+				fg = green
+
+			if context.media:
+				fg = pink
+
+			if context.link:
+				fg = cyan
+
+			if context.maindisplay and context.selected:
+				attr = attr | bold
+				fg = yellow
+
+			return fg, bg, attr
+
+		return default_colors
diff --git a/ranger/conf/options.py b/ranger/conf/options.py
index 92e38c8d..11ed80cc 100644
--- a/ranger/conf/options.py
+++ b/ranger/conf/options.py
@@ -4,4 +4,4 @@ def get():
 
 def dummy():
 	""" provide a way of getting options until get() is implemented """
-	return { 'show_hidden': False, 'scroll_offset': 2, 'directories_first': True, 'preview_files' : True }
+	return { 'show_hidden': False, 'scroll_offset': 2, 'directories_first': True, 'preview_files' : False }
diff --git a/ranger/gui/color.py b/ranger/gui/color.py
new file mode 100644
index 00000000..214383ee
--- /dev/null
+++ b/ranger/gui/color.py
@@ -0,0 +1,33 @@
+import curses
+
+COLOR_PAIRS = {10: 0}
+
+def get_color(fg, bg):
+	import curses
+
+	c = bg+2 + 9*(fg + 2)
+
+	if c not in COLOR_PAIRS:
+		size = len(COLOR_PAIRS)
+		curses.init_pair(size, fg, bg)
+		COLOR_PAIRS[c] = size
+
+	return COLOR_PAIRS[c]
+
+black   = curses.COLOR_BLACK
+blue    = curses.COLOR_BLUE
+cyan    = curses.COLOR_CYAN
+green   = curses.COLOR_GREEN
+magenta = curses.COLOR_MAGENTA
+red     = curses.COLOR_RED
+white   = curses.COLOR_WHITE
+yellow  = curses.COLOR_YELLOW
+default = -1
+
+normal     = curses.A_NORMAL
+bold       = curses.A_BOLD
+reverse    = curses.A_REVERSE
+underline  = curses.A_UNDERLINE
+invisible  = curses.A_INVIS
+
+default_colors = (default, default, normal)
diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py
index f410983f..ff3f9243 100644
--- a/ranger/gui/colorscheme.py
+++ b/ranger/gui/colorscheme.py
@@ -1,54 +1,34 @@
+CONTEXT_KEYS = ['reset', 'wdisplay', 'wstatusbar', 'wtitlebar', 'wconsole', 'directory', 'file', 'maindisplay', 'executable', 'media', 'link', 'broken', 'selected', 'empty']
 
-TUPLE_KEYS = ['wdisplay', 'wstatusbar', 'wtitlebar', 'wconsole', 'folder', 'executable', 'media', 'link', 'broken', 'selected']
+class ColorSchemeContext():
+	pass
 
-COLOR_PAIRS = {10: 0}
-
-def get_color(fg, bg):
-	import curses
-
-	c = bg+2 + 9*(fg + 2)
-
-	if c not in COLOR_PAIRS:
-		size = len(COLOR_PAIRS)
-		curses.init_pair(size, fg, bg)
-		COLOR_PAIRS[c] = size
-
-	return COLOR_PAIRS[c]
+class ColorScheme():
+	def __init__(self):
+		self.cache = {}
 
+	def get(self, *keys):
+		try:
+			return self.cache[keys]
 
-class ColorSchemeContext(object):
-	def __init__(self, dictionary):
-		object.__init__(self)
+		except KeyError:
+			context = ColorSchemeContext()
 
-		self.__tuple__ = None
-		for key in TUPLE_KEYS:
-			if key in dictionary:
-				self.__dict__[key] = dictionary[key]
-			else:
-				self.__dict__[key] = False
+			for key in CONTEXT_KEYS:
+				context.__dict__[key] = (key in keys)
 
-	def to_tuple(self):
-		d = self.__dict__
+			color = self.use(context)
+			self.cache[keys] = color
+			return color
 
-		if '__tuple__' is None:
-			self.__tuple__ = tuple(d[key] for key in TUPLE_KEYS)
+	def get_attr(self, *keys):
+		from ranger.gui.color import get_color
+		import curses
 
-		return self.__tuple__
+		fg, bg, attr = self.get(*keys)
+		return attr | curses.color_pair(get_color(fg, bg))
 
-class ColorScheme():
-	def __init__(self):
-		self.cache = {}
 
-	def get(self, **keywords):
-		con = ColorSchemeContext(keywords)
-		tup = con.to_tuple()
-		try:
-			return self.cache[tup]
-		except KeyError:
-			color = self.use(con)
-			self.cache[tup] = color
-			return color
-	
 	def use(self, context):
 		return -1, -1, 0
-		
+
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index 6bcfc7fe..a54003dc 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -17,7 +17,7 @@ class UI():
 
 	def initialize(self):
 		curses.noecho()
-		curses.halfdelay(10)
+		curses.halfdelay(20)
 		curses.curs_set(0)
 		curses.start_color()
 		curses.use_default_colors()
diff --git a/ranger/gui/wdisplay.py b/ranger/gui/wdisplay.py
index d8d70bf3..f021279b 100644
--- a/ranger/gui/wdisplay.py
+++ b/ranger/gui/wdisplay.py
@@ -1,6 +1,4 @@
-from ranger.gui.color import color_pairs
 from ranger.gui.widget import Widget as SuperClass
-import curses
 
 class WDisplay(SuperClass):
 	def __init__(self, win, colorscheme, level):
@@ -45,64 +43,69 @@ class WDisplay(SuperClass):
 			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
+		import curses
+
 		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
+
+		base_color = ['wdisplay']
+
+		if self.main_display:
+			base_color.append('maindisplay')
 
 		if self.target.empty():
-			self.color(bg=1)
+			self.color(base_color, 'empty')
 			self.win.addnstr(self.y, self.x, "empty", self.wid)
-			self.color()
+			self.color_reset()
 			return
 
 		if not self.target.accessible:
+			self.color(base_color, 'error')
 			self.win.addnstr(self.y, self.x, "not accessible", self.wid)
+			self.color_reset()
 			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?
+			i = line + self.scroll_begin
 			try: drawed = self.target[i]
 			except IndexError: break
 
+			this_color = base_color[:]
+
+			if i == selected_i:
+				this_color.append('selected')
+
 			if isinstance(drawed, Directory):
-				self.color(fg = 4)
+				this_color.append('directory')
 			else:
-				self.color()
+				this_color.append('file')
 
-			invert = i == selected_i
-			if invert:
-				self.win.attron(curses.A_REVERSE)
+			if drawed.islink:
+				this_color.append('link')
 
+			string = drawed.basename
 			if self.main_display:
-				self.win.addnstr(
-						self.y + line,
-						self.x + 1,
-						' ' + drawed.basename + ' ',
-						self.wid - 2)
+				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)
+				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)
+
+			self.color_at(self.y + line, self.x, self.wid, this_color)
+
+			self.color_reset()
 
 	def get_scroll_begin(self):
 		offset = self.scroll_offset
diff --git a/ranger/gui/widget.py b/ranger/gui/widget.py
index 9c64cc1a..cb78062b 100644
--- a/ranger/gui/widget.py
+++ b/ranger/gui/widget.py
@@ -1,7 +1,12 @@
-import curses
-from ranger.gui.colorscheme import get_color
 
-class OutOfBoundsException(Exception): pass
+class OutOfBoundsException(Exception):
+	pass
+
+def combine(keylist, keys):
+	if type(keylist) in (list, tuple):
+		return tuple(tuple(keylist) + keys)
+	else:
+		return tuple((keylist, ) + keys)
 
 class Widget():
 	def __init__(self, win, colorscheme):
@@ -9,17 +14,29 @@ class Widget():
 		self.colorscheme = colorscheme
 		self.setdim(0, 0, 0, 0)
 
-	def color(self, fg = -1, bg = -1, attr = 0):
-		self.win.attrset(attr | curses.color_pair(get_color(fg, bg)))
+	def color(self, keylist = None, *keys):
+		keys = combine(keylist, keys)
+		self.win.attrset(self.colorscheme.get_attr(*keys))
+
+	def color_at(self, y, x, wid, keylist = None, *keys):
+		keys = combine(keylist, keys)
+		self.win.chgat(y, x, wid, self.colorscheme.get_attr(*keys))
+	
+	def color_reset(self):
+		Widget.color(self, 'reset')
 
 	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!")