From 7e36a088444e3f55237622c79e76efe85cac0965 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Wed, 10 Jun 2009 16:07:03 -0400 Subject: [PATCH] start of the color overhaul --HG-- branch : pmacs2 --- application.py | 6 +- buffer/about.py | 2 +- buffer/{color.py => colors.py} | 34 ++----- buffer/console.py | 2 +- color.py | 169 ++++++++++++++++++--------------- method/__init__.py | 4 +- method/svn.py | 2 +- method/vc.py | 4 +- mode/colortest.py | 4 +- mode/python.py | 3 +- 10 files changed, 114 insertions(+), 116 deletions(-) rename buffer/{color.py => colors.py} (82%) diff --git a/application.py b/application.py index 404873e..9ef1c21 100755 --- a/application.py +++ b/application.py @@ -5,7 +5,7 @@ import math import traceback from subprocess import Popen, PIPE, STDOUT -import buffer, buffer.about, buffer.color, buffer.console, buffer.data +import buffer, buffer.about, buffer.colors, buffer.console, buffer.data import buffer.fs, buffer.aes import bufferlist, color, completer, ispell, keyinput, method, minibuffer import mode, util, window @@ -484,7 +484,7 @@ class Application(object): if self.has_buffer_name(name): b = self.bufferlist.buffer_names[name] self.remove_buffer(b) - b = buffer.color.ColorDataBuffer(name, data) + b = buffer.colors.ColorDataBuffer(name, data) if modename is not None: b.modename = modename window.Window(b, self, height=0, width=0) @@ -920,7 +920,7 @@ class Application(object): def _draw_slot(self, i): slot = self.bufferlist.slots[i] w = slot.window - redattr = color.build_attr(color.pairs('red', 'default')) + redattr = color.build('red', 'default') x, y = w.first.xy() lm, rm = w.mode.lmargin, w.mode.rmargin lines = w.buffer.lines diff --git a/buffer/about.py b/buffer/about.py index b4b5035..7126483 100644 --- a/buffer/about.py +++ b/buffer/about.py @@ -1,4 +1,4 @@ -from buffer.color import ColorDataBuffer +from buffer.colors import ColorDataBuffer _data = ''' [r:d:*]=============================================================================== diff --git a/buffer/color.py b/buffer/colors.py similarity index 82% rename from buffer/color.py rename to buffer/colors.py index b600b24..4ef9717 100644 --- a/buffer/color.py +++ b/buffer/colors.py @@ -1,37 +1,14 @@ import re import lex + from buffer import Buffer from buffer.data import DataBuffer +import color from highlight import Highlighter -color_map = { - 'B': 'black', - 'r': 'red', - 'g': 'green', - 'y': 'yellow', - 'b': 'blue', - 'm': 'magenta', - 'c': 'cyan', - 'w': 'white', - 'd': 'default', - '*': 'bold', -} - -reverse_map = { - 'black': 'B', - 'blue': 'b', - 'cyan': 'c', - 'default': 'd', - 'green': 'g', - 'magenta': 'm', - 'red': 'r', - 'white': 'w', - 'yellow': 'y', - 'bold': '*', -} - def get_cbuf_code(*args): - return '[' + ':'.join([reverse_map[x] for x in args]) + ']' + #return '[' + ':'.join([reverse_map.get(x, 'd') for x in args]) + ']' + return '[' + ':'.join([color.rev_ascii_map[x] for x in args]) + ']' # NOTE: this highlighter will not reprocess the data given. it is intended to # be used with read-only buffers like DataBuffer and ColorBuffer @@ -64,7 +41,8 @@ class ColorHighlighter(Highlighter): if j > i: offset += self.append_token(y, i - offset, line[i:j], c) fields = m.group(1).split(':') - c = [color_map.get(x, x) for x in fields] + #c = [color_map.get(x, x) for x in fields] + c = [color.ascii_map.get(x, x) for x in fields] offset += k - j i = k else: diff --git a/buffer/console.py b/buffer/console.py index 792e836..54a6aae 100644 --- a/buffer/console.py +++ b/buffer/console.py @@ -1,5 +1,5 @@ from buffer import Buffer, ACT_NORM -from buffer.color import ColorDataBuffer +from buffer.colors import ColorDataBuffer # console is another singleton console = None diff --git a/color.py b/color.py index 06c8adb..8253ceb 100644 --- a/color.py +++ b/color.py @@ -1,91 +1,110 @@ import curses +colors = {} +_colors = [] +_pairs = {} +index = 1 +attributes = { + 'bold': curses.A_BOLD, + 'reverse': curses.A_REVERSE, + 'normal': curses.A_NORMAL, + 'underline': curses.A_UNDERLINE, + 'dim': curses.A_DIM, + 'standout': curses.A_STANDOUT, +} + inited = False -#default_color = False default_color = True -def init(): - global colors, _colors, _pairs, attributes, inited, index - if not inited: - index = 1 - - colors = { - 'cyan': curses.COLOR_CYAN, - 'green': curses.COLOR_GREEN, - 'red': curses.COLOR_RED, - 'yellow': curses.COLOR_YELLOW, - 'blue': curses.COLOR_BLUE, - 'magenta': curses.COLOR_MAGENTA, - 'black': curses.COLOR_BLACK, - 'white': curses.COLOR_WHITE, - } +ascii_map = { + '*': 'bold', +} +rev_ascii_map = { + 'bold': '*', +} - for i in range(0, curses.COLORS): - if curses.COLORS == 256: - colors['f%02x' % i] = i - else: - colors['f%02x' % i] = curses.COLOR_WHITE +def add_color(name, value, abbrev): + colors[name] = value + ascii_map[abbrev] = name + rev_ascii_map[name] = abbrev - if default_color: - colors["default"] = -1 - - _colors = [] - _pairs = {} - - for key in _pairs: - fg, bg = key - curses.init_pair(index, colors[fg], colors[bg]) - _pairs[key] = curses.color_pair(index) - _colors.append(key) - index = len(_colors) + 1 - - attributes = { - 'bold': curses.A_BOLD, - 'reverse': curses.A_REVERSE, - 'normal': curses.A_NORMAL, - 'underline': curses.A_UNDERLINE, - 'dim': curses.A_DIM, - 'standout': curses.A_STANDOUT, - } - - inited = True - -def pairs(fg, bg): - if not curses.has_colors(): - return curses.color_pair(0) - - global colors, _colors, _pairs, index - key = (fg, bg) - if key not in _pairs: - assert index < curses.COLOR_PAIRS - if not default_color: - if fg == "default": - fg = "white" - if bg == "default": - bg = "black" - curses.init_pair(index, colors[fg], colors[bg]) - _pairs[key] = curses.color_pair(index) - _colors.append(key) - index = len(_colors) + 1 - return _pairs[key] - -def get_pairs(index): - if index == 0: - return ('white', 'black') +def color256(name, fallback, abbrev, r, g, b): + name2 = '%s%d%d%d' % (name, r, g, b) + abbrev2 = '%s%d%d%d' % (abbrev, r, g, b) + if curses.COLORS == 256: + value = 16 + r * 36 + g * 6 + b else: - return _colors[index-1] + value = fallback + add_color(name2, value, abbrev2) -def reverse_colors(attr): - return attr ^ curses.A_REVERSE +# PUBLIC METHODS +def init(): + global _pairs, inited + if inited: + return + + add_color('cyan', curses.COLOR_CYAN, 'c') + add_color('blue', curses.COLOR_BLUE, 'b') + add_color('green', curses.COLOR_GREEN, 'g') + add_color('red', curses.COLOR_RED, 'r') + add_color('yellow', curses.COLOR_YELLOW, 'y') + add_color('magenta', curses.COLOR_MAGENTA, 'm') + add_color('black', curses.COLOR_BLACK, 'B') + add_color('white', curses.COLOR_WHITE, 'w') + + inited = True + + for i in range(0, 256): + name = 'f%02x' % i + abbrev = 'f%02x' % i + add_color(name, i, abbrev) + + for i in range(0, 24): + color256('grey', curses.COLOR_WHITE, 'G', i, i, i) + + for i in range(1, 6): + for j in range(0, i): + for k in range(0, i): + color256('red', curses.COLOR_RED, 'r', i, j, k) + color256('green', curses.COLOR_GREEN, 'g', j, i, k) + color256('blue', curses.COLOR_BLUE, 'b', j, k, i) + + for i in range(1, 6): + for j in range(0, i): + color256('yellow', curses.COLOR_YELLOW, 'y', i, i, j) + color256('cyan', curses.COLOR_CYAN, 'c', j, i, i) + color256('magenta', curses.COLOR_MAGENTA, 'm', i, j, i) + + if default_color: + colors['default'] = -1 + ascii_map['d'] = 'default' + rev_ascii_map['default'] = 'd' def build(fg, bg, *attr): - cattr = pairs(fg, bg) - return build_attr(cattr, *attr) - -def build_attr(*attr): - v = curses.A_NORMAL + v = curses.A_NORMAL | pairs(fg, bg) for x in attr: if type(x) == type(''): x = attributes[x] v = v | x return v + + return build_attr(cattr, *attr) + +# PRIVATE METHODS +def pairs(fg, bg): + if not curses.has_colors(): + return curses.color_pair(0) + + global index + fgi = colors.get(fg, colors['white']) + bgi = colors.get(bg, colors['black']) + key = (fgi, bgi) + if key not in _pairs: + assert index < curses.COLOR_PAIRS, "too many colors" + assert type(fgi) == type(0), "illegal fgi: %r" % fgi + assert type(bgi) == type(0), "illegal bgi: %r" % bgi + curses.init_pair(index, fgi, bgi) + _pairs[key] = curses.color_pair(index) + _colors.append(key) + index = len(_colors) + 1 + return _pairs[key] diff --git a/method/__init__.py b/method/__init__.py index 85a32d7..7471d28 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -2,7 +2,7 @@ import os, commands, re, tempfile from subprocess import Popen, PIPE, STDOUT import buffer, completer, default, dirutil, regex, util, window -import buffer.color +import buffer.colors from point import Point class MethodError(Exception): @@ -1114,7 +1114,7 @@ class ViewTokenColors(Method): l = max(l, len(key)) lines = ['Color information for %d tokens:\n' % len(keys), '\n'] for key in keys: - c = buffer.color.get_cbuf_code(*a.token_colors[key]) + c = buffer.colors.get_cbuf_code(*a.token_colors[key]) lines.append('%s%-*s %r\n' % (c, l, key, a.token_colors[key])) a.color_data_buffer("*Token-Colors*", ''.join(lines), switch_to=True) diff --git a/method/svn.py b/method/svn.py index 402ec59..b2ec8f5 100644 --- a/method/svn.py +++ b/method/svn.py @@ -3,7 +3,7 @@ from subprocess import Popen, PIPE, STDOUT import buffer, default, dirutil, lex, regex, util, window from point import Point -import buffer.color +import buffer.colors from method.vc import VcBlame from method import Method, Argument diff --git a/method/vc.py b/method/vc.py index 7062fe3..9cdec33 100644 --- a/method/vc.py +++ b/method/vc.py @@ -1,5 +1,5 @@ from method import Method -import buffer.color +import buffer.colors import util import lex @@ -46,7 +46,7 @@ class VcBlame(Method): if d['tokens']: suffix = '' for t in d['tokens']: - code = buffer.color.get_cbuf_code(*t.color) + code = buffer.colors.get_cbuf_code(*t.color) suffix += code + util.cbuf_escape(t.string) else: suffix = d['content'] + '\n' diff --git a/mode/colortest.py b/mode/colortest.py index 7862d46..ce7ded7 100644 --- a/mode/colortest.py +++ b/mode/colortest.py @@ -5,7 +5,7 @@ class ColortestGrammar(Grammar): rules = [] for i in range(0, 256): c = '%02x' % i - rules.append(PatternRule(c, c)) + rules.append(PatternRule('z' + c, c)) class Colortest(Fundamental): name = 'Colortest' @@ -13,6 +13,6 @@ class Colortest(Fundamental): colors = {} for i in range(0, 256): c = '%02x' % i - colors[c] = ('default', 'f' + c) + colors['z' + c] = ('default', 'f' + c) install = Colortest.install diff --git a/mode/python.py b/mode/python.py index 7bb02bf..7ebfb19 100644 --- a/mode/python.py +++ b/mode/python.py @@ -558,7 +558,8 @@ class Python(mode.Fundamental): closetags = {')': '(', ']': '[', '}': '{'} commentc = '#' colors = { - 'python.def': ('blue', 'default', 'bold'), + #'python.def': ('blue', 'default', 'bold'), + 'python.def': ('red532', 'default', 'bold'), 'python.class': ('yellow', 'default', 'bold'), 'python.decorator': ('magenta', 'default'), 'python.reserved': ('magenta', 'default', 'bold'),