Styling cleanups

This commit is contained in:
Kovid Goyal 2010-09-21 21:05:23 -06:00
parent fd9164f768
commit 7f472f742e

View File

@ -8,18 +8,20 @@ __docformat__ = 'restructuredtext en'
from PyQt4.Qt import QTextCharFormat, QFont, QBrush, QColor
from pygments.formatter import Formatter as PF
from pygments.token import Token, Generic
from pygments.token import Token, Generic, string_to_tokentype
class Formatter(object):
def __init__(self, prompt, continuation, **options):
def __init__(self, prompt, continuation, style='default'):
if len(prompt) != len(continuation):
raise ValueError('%r does not have the same length as %r' %
(prompt, continuation))
self.prompt, self.continuation = prompt, continuation
self.set_style(style)
pf = PF(**options)
def set_style(self, style):
pf = PF(style=style)
self.styles = {}
self.normal = self.base_fmt()
self.background_color = pf.style.background_color
@ -27,6 +29,7 @@ class Formatter(object):
for ttype, ndef in pf.style:
fmt = self.base_fmt()
fmt.setProperty(fmt.UserProperty, str(ttype))
if ndef['color']:
fmt.setForeground(QBrush(QColor('#%s'%ndef['color'])))
fmt.setUnderlineColor(QColor('#%s'%ndef['color']))
@ -49,6 +52,14 @@ class Formatter(object):
QTextEdit { color: %s; background-color: %s }
'''%(self.color, self.background_color)
def get_fmt(self, token):
if type(token) != type(Token.Generic):
token = string_to_tokentype(token)
fmt = self.styles.get(token, None)
if fmt is None:
fmt = self.base_fmt()
fmt.setProperty(fmt.UserProperty, str(token))
return fmt
def base_fmt(self):
fmt = QTextCharFormat()
@ -59,7 +70,7 @@ class Formatter(object):
cursor.insertText(raw, self.normal)
def render_syntax_error(self, tb, cursor):
fmt = self.styles[Token.Error]
fmt = self.get_fmt(Token.Error)
cursor.insertText(tb, fmt)
def render(self, tokens, cursor):
@ -84,7 +95,9 @@ class Formatter(object):
def render_prompt(self, is_continuation, cursor):
pr = self.continuation if is_continuation else self.prompt
fmt = self.styles[Generic.Prompt]
fmt = self.get_fmt(Generic.Prompt)
if fmt is None:
fmt = self.base_fmt()
cursor.insertText(pr, fmt)