Skip to content

Commit

Permalink
Use prompt_toolkit.token everywhere. (Make pygments more optional.)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanslenders committed Jan 20, 2016
1 parent 640c29a commit b0eea7b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion prompt_toolkit/contrib/regular_languages/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
the input using a regular grammar with token annotations.
"""
from __future__ import unicode_literals
from pygments.token import Token
from prompt_toolkit.layout.lexers import Lexer
from prompt_toolkit.token import Token

from .compiler import _CompiledGrammar

Expand Down
5 changes: 1 addition & 4 deletions prompt_toolkit/layout/toolbars.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import unicode_literals

from pygments.lexers import BashLexer

from ..enums import IncrementalSearchDirection

from .processors import BeforeInput

from .lexers import PygmentsLexer, SimpleLexer
from .lexers import SimpleLexer
from .dimension import LayoutDimension
from .controls import BufferControl, TokenListControl, UIControl
from .containers import Window, ConditionalContainer
Expand Down Expand Up @@ -38,7 +36,6 @@ def __init__(self, get_tokens, filter=Always(), **kw):
class SystemToolbarControl(BufferControl):
def __init__(self):
super(SystemToolbarControl, self).__init__(
lexer=PygmentsLexer(BashLexer),
buffer_name=SYSTEM_BUFFER,
input_processors=[BeforeInput.static('Shell command: ', Token.Toolbar.System)],)

Expand Down
8 changes: 6 additions & 2 deletions prompt_toolkit/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@
from six import text_type, exec_, PY2

import os
import pygments.lexer
import sys
import textwrap

try:
from pygments.lexer import Lexer as pygments_Lexer
except ImportError:
pygments_Lexer = None

if is_windows():
from .terminal.win32_output import Win32Output
from .terminal.conemu_output import ConEmuOutput
Expand Down Expand Up @@ -209,7 +213,7 @@ def create_prompt_layout(message='', lexer=None, is_password=False,
# class is given, turn it into a PygmentsLexer. (Important for
# backwards-compatibility.)
try:
if issubclass(lexer, pygments.lexer.Lexer):
if pygments_Lexer and issubclass(lexer, pygments.lexer.Lexer):
lexer = PygmentsLexer(lexer)
except TypeError: # Happens when lexer is `None` or an instance of something else.
pass
Expand Down
23 changes: 16 additions & 7 deletions prompt_toolkit/styles.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
"""
Styling for prompt_toolkit applications.
Pygments needs to be installed for usage of ``PygmentsStyle``.
"""
from __future__ import unicode_literals
from abc import ABCMeta, abstractmethod
from collections import namedtuple
from pygments.token import Token
from six import with_metaclass

import pygments.style
import pygments.styles.default
from .token import Token

# Following imports are only needed when a ``PygmentsStyle`` class is used.
try:
from pygments.style import Style as pygments_Style
from pygments.styles.default import DefaultStyle as pygments_DefaultStyle
except ImportError:
pygments_Style = None
pygments_DefaultStyle = None


__all__ = (
'Style',
Expand Down Expand Up @@ -97,7 +106,7 @@ class PygmentsStyle(Style):
:param pygments_style_cls: Pygments ``Style`` class.
"""
def __init__(self, pygments_style_cls):
assert issubclass(pygments_style_cls, pygments.style.Style)
assert issubclass(pygments_style_cls, pygments_Style)
self.pygments_style_cls = pygments_style_cls
self._token_to_attrs_dict = None

Expand All @@ -120,7 +129,7 @@ def invalidation_hash(self):

@classmethod
def from_defaults(cls, style_dict=None,
pygments_style_cls=pygments.styles.default.DefaultStyle,
pygments_style_cls=pygments_DefaultStyle,
include_extensions=True):
"""
Shortcut to create a :class:`.PygmentsStyle` instance from a Pygments
Expand All @@ -131,9 +140,9 @@ def from_defaults(cls, style_dict=None,
:param include_extensions: (`bool`) Include prompt_toolkit extensions.
"""
assert style_dict is None or isinstance(style_dict, dict)
assert pygments_style_cls is None or issubclass(pygments_style_cls, pygments.style.Style)
assert pygments_style_cls is None or issubclass(pygments_style_cls, pygments_Style)

class _CustomStyle(pygments.styles.default.DefaultStyle):
class _CustomStyle(pygments_DefaultStyle):
background_color = None
styles = {}

Expand Down
7 changes: 6 additions & 1 deletion prompt_toolkit/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ def __repr__(self):
return 'Token' + (self and '.' or '') + '.'.join(self)


Token = _TokenType()
# Prefer the Token class from Pygments. If Pygments is not installed, use our
# minimalistic Token class.
try:
from pygments.token import Token
except ImportError:
Token = _TokenType()
4 changes: 2 additions & 2 deletions tools/debug_vt100_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
Parse vt100 input and print keys.
For testing terminal input.
"""

from __future__ import unicode_literals
import sys

from prompt_toolkit.terminal.vt100_input import InputStream, raw_mode
from prompt_toolkit.keys import Keys
import sys


def callback(key_press):
Expand Down

0 comments on commit b0eea7b

Please sign in to comment.