Merge branch 'adqm-pt_colors'

This commit is contained in:
Anthony Scopatz 2015-11-29 20:37:39 -05:00
commit 30f6b8095c
5 changed files with 86 additions and 25 deletions

View file

@ -117,6 +117,14 @@ applicable.
- ``None``
- This is a mapping of user-specified styles for prompt-toolkit. See the
prompt-toolkit documentation for more details. If None, this is skipped.
* - PROMPT_TOOLKIT_COLORS
- ``{}``
- This is a mapping of from color names to HTML color codes. Whenever
prompt-toolkit would color a word a particular color (in the prompt, or
in syntax highlighting), it will use the value specified here to
represent that color, instead of its default. If a color is not
specified here, prompt-toolkit uses the colors from
``xonsh.tools._PT_COLORS``.
* - PUSHD_MINUS
- ``False``
- Flag for directory pushing functionality. False is the normal behaviour.

View file

@ -5,21 +5,22 @@ from __future__ import unicode_literals, print_function
import nose
from nose.tools import assert_equal
import builtins
from xonsh.tools import format_prompt_for_prompt_toolkit
from xonsh.tools import TERM_COLORS
from xonsh.environ import format_prompt
from xonsh.environ import format_prompt, Env
builtins.__xonsh_env__ = Env()
builtins.__xonsh_env__['PROMPT_TOOLKIT_COLORS'] = {'WHITE': '#ffffff'}
def test_format_prompt_for_prompt_toolkit():
templ = ('>>> {BOLD_BLUE}~/xonsh {WHITE} (main){NO_COLOR}')
prompt = format_prompt(templ, TERM_COLORS)
token_names, color_styles, strings = format_prompt_for_prompt_toolkit(prompt)
assert_equal(token_names, ['NO_COLOR', 'BOLD_BLUE', 'WHITE', 'NO_COLOR'])
assert_equal(color_styles, ['', 'bold #0000FF', '#FFFFFF', ''])
assert_equal(color_styles, ['', 'bold #0000FF', '#ffffff', ''])
assert_equal(strings, ['>>> ', '~/xonsh ', ' (main)', ''])
if __name__ == '__main__':
nose.runmodule()

View file

@ -152,6 +152,7 @@ DEFAULT_VALUES = {
'PATH': (),
'PATHEXT': (),
'PROMPT': DEFAULT_PROMPT,
'PROMPT_TOOLKIT_COLORS': {},
'PROMPT_TOOLKIT_STYLES': None,
'PUSHD_MINUS': False,
'PUSHD_SILENT': False,

View file

@ -8,12 +8,13 @@ from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.layout.lexers import PygmentsLexer
from prompt_toolkit.filters import Condition
from pygments.token import Token
from pygments.style import Style
from pygments.styles.default import DefaultStyle
from pygments.token import (Keyword, Name, Comment, String, Error, Number,
Operator, Generic, Whitespace, Token)
from xonsh.base_shell import BaseShell
from xonsh.tools import format_prompt_for_prompt_toolkit
from xonsh.tools import format_prompt_for_prompt_toolkit, _make_style
from xonsh.prompt_toolkit_completer import PromptToolkitCompleter
from xonsh.prompt_toolkit_history import LimitedFileHistory
from xonsh.prompt_toolkit_key_bindings import load_xonsh_bindings
@ -110,21 +111,66 @@ class PromptToolkitShell(BaseShell):
def get_tokens(cli):
return list(zip(tokens, strings))
class CustomStyle(Style):
styles = DefaultStyle.styles.copy()
styles.update({
Token.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Completion: 'bg:#008888 #ffffff',
Token.Menu.Completions.ProgressButton: 'bg:#003333',
Token.Menu.Completions.ProgressBar: 'bg:#00aaaa',
Token.AutoSuggestion: '#666666',
Token.Aborted: '#888888',
})
# update with the prompt styles
styles.update({t: s for (t, s) in zip(tokens, cstyles)})
# Update with with any user styles
userstyle = builtins.__xonsh_env__.get('PROMPT_TOOLKIT_STYLES')
if userstyle is not None:
styles.update(userstyle)
custom_style = _xonsh_style(tokens, cstyles)
return get_tokens, CustomStyle
return get_tokens, custom_style
def _xonsh_style(tokens=tuple(), cstyles=tuple()):
class XonshStyle(Style):
styles = {
Whitespace: "GRAY",
Comment: "UNDERLINE INTENSE GRAY",
Comment.Preproc: "UNDERLINE INTENSE GRAY",
Keyword: "BOLD GREEN",
Keyword.Pseudo: "GREEN",
Keyword.Type: "MAGENTA",
Operator: "GRAY",
Operator.Word: "BOLD",
Name.Builtin: "INTENSE GREEN",
Name.Function: "BLUE",
Name.Class: "BOLD BLUE",
Name.Namespace: "BOLD BLUE",
Name.Exception: "BOLD INTENSE RED",
Name.Variable: "CYAN",
Name.Constant: "RED",
Name.Label: "YELLOW",
Name.Entity: "BOLD WHITE",
Name.Attribute: "CYAN",
Name.Tag: "BOLD GREEN",
Name.Decorator: "CYAN",
String: "MAGENTA",
String.Doc: "UNDERLINE MAGENTA",
String.Interpol: "BOLD MAGENTA",
String.Escape: "BOLD RED",
String.Regex: "MAGENTA",
String.Symbol: "BOLD GREEN",
String.Other: "GREEN",
Number: "RED",
Generic.Heading: "BOLD BLUE",
Generic.Subheading: "BOLD MAGENTA",
Generic.Deleted: "RED",
Generic.Inserted: "GREEN",
Generic.Error: "BOLD RED",
Generic.Emph: "UNDERLINE",
Generic.Prompt: "BOLD BLUE",
Generic.Output: "GRAY",
Generic.Traceback: "RED",
Error: "RED",
}
styles = {k: _make_style(v) for k, v in styles.items()}
styles.update({
Token.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
Token.Menu.Completions.Completion: 'bg:#008888 #ffffff',
Token.Menu.Completions.ProgressButton: 'bg:#003333',
Token.Menu.Completions.ProgressBar: 'bg:#00aaaa',
Token.AutoSuggestion: '#666666',
Token.Aborted: '#888888',
})
# update with the prompt styles
styles.update({t: s for (t, s) in zip(tokens, cstyles)})
# Update with with any user styles
userstyle = builtins.__xonsh_env__.get('PROMPT_TOOLKIT_STYLES')
if userstyle is not None:
styles.update(userstyle)
return XonshStyle

View file

@ -738,7 +738,8 @@ _PT_COLORS = {'BLACK': '#000000',
'BLUE': '#0000FF',
'PURPLE': '#0000FF',
'CYAN': '#00FFFF',
'WHITE': '#FFFFFF'}
'WHITE': '#FFFFFF',
'GRAY': '#888888'}
_PT_STYLE = {'BOLD': 'bold',
'UNDERLINE': 'underline',
@ -751,9 +752,13 @@ def _make_style(color_name):
for k, v in _PT_STYLE.items():
if k in color_name:
style.append(v)
for k, v in _PT_COLORS.items():
_custom_colors = builtins.__xonsh_env__.get('PROMPT_TOOLKIT_COLORS')
for k, v in _custom_colors.items():
if k in color_name:
style.append(v)
for k, v in _PT_COLORS.items():
if k not in _custom_colors and k in color_name:
style.append(v)
return ' '.join(style)