From 725adf92d825c015a923cd85ded6c5a8d8166f87 2010-10-21 06:17:33 From: MinRK Date: 2010-10-21 06:17:33 Subject: [PATCH] added --colors flag to ipythonqt --- diff --git a/IPython/frontend/qt/console/ipython_widget.py b/IPython/frontend/qt/console/ipython_widget.py index 87919c1..278d767 100644 --- a/IPython/frontend/qt/console/ipython_widget.py +++ b/IPython/frontend/qt/console/ipython_widget.py @@ -25,8 +25,9 @@ from IPython.core.inputsplitter import IPythonInputSplitter, \ from IPython.core.usage import default_gui_banner from IPython.utils.traitlets import Bool, Str from frontend_widget import FrontendWidget -from styles import (default_light_style_sheet, default_dark_style_sheet, - default_light_syntax_style, default_dark_syntax_style) +from styles import (default_light_style_sheet, default_light_syntax_style, + default_dark_style_sheet, default_dark_syntax_style, + default_bw_style_sheet, default_bw_syntax_style) #----------------------------------------------------------------------------- # Constants @@ -329,21 +330,24 @@ class IPythonWidget(FrontendWidget): # 'IPythonWidget' interface #--------------------------------------------------------------------------- - def set_default_style(self, lightbg=True): + def set_default_style(self, colors='light'): """ Sets the widget style to the class defaults. Parameters: ----------- - lightbg : bool, optional (default True) + colors : str, optional (default light) Whether to use the default IPython light background or dark - background style. + background or B&W style. """ - if lightbg: + if colors=='light': self.style_sheet = default_light_style_sheet self.syntax_style = default_light_syntax_style - else: + elif colors=='dark': self.style_sheet = default_dark_style_sheet self.syntax_style = default_dark_syntax_style + elif colors=='bw': + self.style_sheet = default_bw_style_sheet + self.syntax_style = default_bw_syntax_style #--------------------------------------------------------------------------- # 'IPythonWidget' protected interface diff --git a/IPython/frontend/qt/console/ipythonqt.py b/IPython/frontend/qt/console/ipythonqt.py index 21e7b65..96174f7 100644 --- a/IPython/frontend/qt/console/ipythonqt.py +++ b/IPython/frontend/qt/console/ipythonqt.py @@ -150,13 +150,13 @@ def main(): wgroup.add_argument('--gui-completion', action='store_true', help='use a GUI widget for tab completion') wgroup.add_argument('--style', type=str, - help='specify a pygments style by name. \ - Valid are: %s'%(list(get_all_styles()))) + choices = list(get_all_styles()), + help='specify a pygments style for by name.') wgroup.add_argument('--stylesheet', type=str, help="path to a custom CSS stylesheet.") - wgroup.add_argument('--dark', action='store_true', - help="use the dark style template instead of lightbg.\ - If --style is not specified, the default dark style is used.") + wgroup.add_argument('--colors', type=str, + help="Set the color scheme (light, dark, or bw). This is guessed\ + based on the pygments style if not set.") args = parser.parse_args() @@ -197,20 +197,32 @@ def main(): # configure the style: if not args.pure: # only IPythonWidget supports styles + # parse the colors arg down to current known labels + if args.colors: + colors=args.colors.lower() + if colors in ('lightbg', 'light'): + colors='light' + elif colors in ('dark', 'linux'): + colors='dark' + else: + colors='nocolor' + else: + colors=None + lightbg = colors != 'linux' + if args.style: # guess whether it's a dark style: - dark = args.dark or styles.dark_style(args.style) widget.syntax_style = args.style - widget.style_sheet = styles.sheet_from_template(args.style, not dark) + widget.style_sheet = styles.sheet_from_template(args.style, lightbg) widget._syntax_style_changed() widget._style_sheet_changed() - elif args.dark: - # use default dark style - widget.set_default_style(lightbg=False) + elif colors: + # use a default style + widget.set_default_style(colors=colors) else: # this is redundant for now, but allows the widget's # defaults to change - widget.set_default_style(lightbg=True) + widget.set_default_style() if args.stylesheet: # we got an expicit stylesheet diff --git a/IPython/frontend/qt/console/styles.py b/IPython/frontend/qt/console/styles.py index 11055c2..b9e9255 100644 --- a/IPython/frontend/qt/console/styles.py +++ b/IPython/frontend/qt/console/styles.py @@ -43,6 +43,17 @@ default_dark_style_sheet = default_dark_style_template%dict( bgcolor='black', fgcolor='white', select="#555") default_dark_syntax_style = 'monokai' +# The default monochrome +default_bw_style_sheet = ''' + QPlainTextEdit, QTextEdit { background-color: white; + color: black ; + selection-background-color: #cccccc} + .in-prompt-number { font-weight: bold; } + .out-prompt-number { font-weight: bold; } +''' +default_bw_syntax_style = 'bw' + + def hex_to_rgb(color): """Convert a hex color to rgb integer tuple.""" if color.startswith('#'): @@ -76,7 +87,8 @@ def dark_style(stylename): return dark_color(get_style_by_name(stylename).background_color) def get_colors(stylename): - """Construct the keys to be used building the base stylesheet.""" + """Construct the keys to be used building the base stylesheet + from a templatee.""" style = get_style_by_name(stylename) fgcolor = style.style_for_token(Token.Text)['color'] or '' if len(fgcolor) in (3,6): @@ -94,9 +106,14 @@ def get_colors(stylename): fgcolor = fgcolor ) -def sheet_from_template(name, lightbg=True): +def sheet_from_template(name, colors='light'): """Use one of the base templates, and set bg/fg/select colors.""" - if lightbg: + colors = colors.lower() + if colors=='light': return default_light_style_template%get_colors(name) + elif colors=='dark': + return default_dark_style_template%get_colors(name) + elif colors=='nocolor': + return default_bw_style_sheet else: - return default_dark_style_template%get_colors(name) \ No newline at end of file + raise KeyError("No such color scheme: %s"%colors)