1from typing import Dict 2 3from prompt_toolkit.styles import BaseStyle, Style, merge_styles 4from prompt_toolkit.styles.pygments import style_from_pygments_cls 5from prompt_toolkit.utils import is_conemu_ansi, is_windows, is_windows_vt100_supported 6from pygments.styles import get_all_styles, get_style_by_name 7 8__all__ = ["get_all_code_styles", "get_all_ui_styles", "generate_style"] 9 10 11def get_all_code_styles() -> Dict[str, BaseStyle]: 12 """ 13 Return a mapping from style names to their classes. 14 """ 15 result: Dict[str, BaseStyle] = { 16 name: style_from_pygments_cls(get_style_by_name(name)) 17 for name in get_all_styles() 18 } 19 result["win32"] = Style.from_dict(win32_code_style) 20 return result 21 22 23def get_all_ui_styles() -> Dict[str, BaseStyle]: 24 """ 25 Return a dict mapping {ui_style_name -> style_dict}. 26 """ 27 return { 28 "default": Style.from_dict(default_ui_style), 29 "blue": Style.from_dict(blue_ui_style), 30 } 31 32 33def generate_style(python_style: BaseStyle, ui_style: BaseStyle) -> BaseStyle: 34 """ 35 Generate Pygments Style class from two dictionaries 36 containing style rules. 37 """ 38 return merge_styles([python_style, ui_style]) 39 40 41# Code style for Windows consoles. They support only 16 colors, 42# so we choose a combination that displays nicely. 43win32_code_style = { 44 "pygments.comment": "#00ff00", 45 "pygments.keyword": "#44ff44", 46 "pygments.number": "", 47 "pygments.operator": "", 48 "pygments.string": "#ff44ff", 49 "pygments.name": "", 50 "pygments.name.decorator": "#ff4444", 51 "pygments.name.class": "#ff4444", 52 "pygments.name.function": "#ff4444", 53 "pygments.name.builtin": "#ff4444", 54 "pygments.name.attribute": "", 55 "pygments.name.constant": "", 56 "pygments.name.entity": "", 57 "pygments.name.exception": "", 58 "pygments.name.label": "", 59 "pygments.name.namespace": "", 60 "pygments.name.tag": "", 61 "pygments.name.variable": "", 62} 63 64 65default_ui_style = { 66 "control-character": "ansiblue", 67 # Classic prompt. 68 "prompt": "bold", 69 "prompt.dots": "noinherit", 70 # (IPython <5.0) Prompt: "In [1]:" 71 "in": "bold #008800", 72 "in.number": "", 73 # Return value. 74 "out": "#ff0000", 75 "out.number": "#ff0000", 76 # Completions. 77 "completion.builtin": "", 78 "completion.param": "#006666 italic", 79 "completion.keyword": "fg:#008800", 80 "completion.keyword fuzzymatch.inside": "fg:#008800", 81 "completion.keyword fuzzymatch.outside": "fg:#44aa44", 82 # Separator between windows. (Used above docstring.) 83 "separator": "#bbbbbb", 84 # System toolbar 85 "system-toolbar": "#22aaaa noinherit", 86 # "arg" toolbar. 87 "arg-toolbar": "#22aaaa noinherit", 88 "arg-toolbar.text": "noinherit", 89 # Signature toolbar. 90 "signature-toolbar": "bg:#44bbbb #000000", 91 "signature-toolbar current-name": "bg:#008888 #ffffff bold", 92 "signature-toolbar operator": "#000000 bold", 93 "docstring": "#888888", 94 # Validation toolbar. 95 "validation-toolbar": "bg:#440000 #aaaaaa", 96 # Status toolbar. 97 "status-toolbar": "bg:#222222 #aaaaaa", 98 "status-toolbar.title": "underline", 99 "status-toolbar.inputmode": "bg:#222222 #ffffaa", 100 "status-toolbar.key": "bg:#000000 #888888", 101 "status-toolbar key": "bg:#000000 #888888", 102 "status-toolbar.pastemodeon": "bg:#aa4444 #ffffff", 103 "status-toolbar.pythonversion": "bg:#222222 #ffffff bold", 104 "status-toolbar paste-mode-on": "bg:#aa4444 #ffffff", 105 "record": "bg:#884444 white", 106 "status-toolbar more": "#ffff44", 107 "status-toolbar.input-mode": "#ffff44", 108 # The options sidebar. 109 "sidebar": "bg:#bbbbbb #000000", 110 "sidebar.title": "bg:#668866 #ffffff", 111 "sidebar.label": "bg:#bbbbbb #222222", 112 "sidebar.status": "bg:#dddddd #000011", 113 "sidebar.label selected": "bg:#222222 #eeeeee", 114 "sidebar.status selected": "bg:#444444 #ffffff bold", 115 "sidebar.separator": "underline", 116 "sidebar.key": "bg:#bbddbb #000000 bold", 117 "sidebar.key.description": "bg:#bbbbbb #000000", 118 "sidebar.helptext": "bg:#fdf6e3 #000011", 119 # # Styling for the history layout. 120 # history.line: '', 121 # history.line.selected: 'bg:#008800 #000000', 122 # history.line.current: 'bg:#ffffff #000000', 123 # history.line.selected.current: 'bg:#88ff88 #000000', 124 # history.existinginput: '#888888', 125 # Help Window. 126 "window-border": "#aaaaaa", 127 "window-title": "bg:#bbbbbb #000000", 128 # Meta-enter message. 129 "accept-message": "bg:#ffff88 #444444", 130 # Exit confirmation. 131 "exit-confirmation": "bg:#884444 #ffffff", 132} 133 134 135# Some changes to get a bit more contrast on Windows consoles. 136# (They only support 16 colors.) 137if is_windows() and not is_conemu_ansi() and not is_windows_vt100_supported(): 138 default_ui_style.update( 139 { 140 "sidebar.title": "bg:#00ff00 #ffffff", 141 "exitconfirmation": "bg:#ff4444 #ffffff", 142 "toolbar.validation": "bg:#ff4444 #ffffff", 143 "menu.completions.completion": "bg:#ffffff #000000", 144 "menu.completions.completion.current": "bg:#aaaaaa #000000", 145 } 146 ) 147 148 149blue_ui_style = {} 150blue_ui_style.update(default_ui_style) 151# blue_ui_style.update({ 152# # Line numbers. 153# Token.LineNumber: '#aa6666', 154# 155# # Highlighting of search matches in document. 156# Token.SearchMatch: '#ffffff bg:#4444aa', 157# Token.SearchMatch.Current: '#ffffff bg:#44aa44', 158# 159# # Highlighting of select text in document. 160# Token.SelectedText: '#ffffff bg:#6666aa', 161# 162# # Completer toolbar. 163# Token.Toolbar.Completions: 'bg:#44bbbb #000000', 164# Token.Toolbar.Completions.Arrow: 'bg:#44bbbb #000000 bold', 165# Token.Toolbar.Completions.Completion: 'bg:#44bbbb #000000', 166# Token.Toolbar.Completions.Completion.Current: 'bg:#008888 #ffffff', 167# 168# # Completer menu. 169# Token.Menu.Completions.Completion: 'bg:#44bbbb #000000', 170# Token.Menu.Completions.Completion.Current: 'bg:#008888 #ffffff', 171# Token.Menu.Completions.Meta: 'bg:#449999 #000000', 172# Token.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000', 173# Token.Menu.Completions.ProgressBar: 'bg:#aaaaaa', 174# Token.Menu.Completions.ProgressButton: 'bg:#000000', 175# }) 176