summary refs log tree commit diff stats
path: root/ranger/gui/colorscheme.py
blob: 4fdcaad27b2e049e5f4e59699ceb6a719eaf96e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
CONTEXT_KEYS = [ 'reset', 'error',
		'in_display', 'in_statusbar', 'in_titlebar', 'in_console',
		'in_notify',
		'directory', 'file', 'hostname',
		'executable', 'media', 'link',
		'video', 'audio', 'image', 'media', 'document', 'container',
		'selected', 'empty', 'maindisplay', 'message', 'background',
		'good', 'bad',
		'space', 'permissions', 'owner', 'group', 'mtime', 'nlink',
		'scroll', 'all', 'bot', 'top', 'percentage',
		'marked',
		'text', 'highlight',
		'keybuffer']

# colorscheme specification:
#
# A colorscheme must...
#
# 1. be inside either of these directories:
# ~/.ranger/colorschemes/
# path/to/ranger/colorschemes/
#
# 2. be a subclass ofranger.gui.colorscheme.ColorScheme
# 
# 3. have a use(self, context) method which returns a tuple of 3 integers.
# the first integer is the foreground color, the second is the background
# color, the third is the attribute, as specified by the curses module.
#
#
# define which colorscheme to use by having this to your options.py:
# from ranger import colorschemes
# colorscheme = colorschemes.filename
# 
# If your colorscheme-file contains more than one colorscheme, specify it with:
# colorscheme = colorschemes.filename.classname

from ranger.ext.openstruct import OpenStruct

class ColorScheme(object):
	def __init__(self):
		self.cache = {}

	def get(self, *keys):
		"""Determine the (fg, bg, attr) tuple or get it from cache"""
		try:
			return self.cache[keys]

		except KeyError:
			context = OpenStruct()

			for key in CONTEXT_KEYS:
				context[key] = (key in keys)

			# add custom error messages for broken colorschemes
			color = self.use(context)
			self.cache[keys] = color
			return color

	def get_attr(self, *keys):
		"""Returns the curses attr integer for the specified keys"""
		from ranger.gui.color import get_color
		import curses

		fg, bg, attr = self.get(*keys)
		return attr | curses.color_pair(get_color(fg, bg))


	def use(self, context):
		"""Use the colorscheme to determine the (fg, bg, attr) tuple.
      This is a dummy function which always returns default_colors.
      Override this in your custom colorscheme!
      """
		from ranger.gui.color import default_colors
		return default_colors