|
10 | 10 | import time |
11 | 11 | from typing import TYPE_CHECKING |
12 | 12 |
|
13 | | -# try to import readline, unix specific enhancement |
14 | | -try: |
15 | | - import readline |
16 | | - |
17 | | - if TYPE_CHECKING and sys.platform == 'win32': # always false, for type checking |
18 | | - raise ImportError # NoQA: TRY301 |
19 | | - READLINE_AVAILABLE = True |
20 | | - if readline.__doc__ and 'libedit' in readline.__doc__: |
21 | | - readline.parse_and_bind('bind ^I rl_complete') |
22 | | - USE_LIBEDIT = True |
23 | | - else: |
24 | | - readline.parse_and_bind('tab: complete') |
25 | | - USE_LIBEDIT = False |
26 | | -except ImportError: |
27 | | - READLINE_AVAILABLE = False |
28 | | - USE_LIBEDIT = False |
29 | | - |
30 | 13 | from docutils.utils import column_width |
31 | 14 |
|
32 | 15 | import sphinx.locale |
33 | 16 | from sphinx import __display_version__, package_dir |
34 | 17 | from sphinx._cli.util.colour import ( |
| 18 | + _create_input_mode_colour_func, |
35 | 19 | bold, |
36 | 20 | disable_colour, |
37 | 21 | red, |
38 | 22 | terminal_supports_colour, |
39 | 23 | ) |
40 | 24 | from sphinx.locale import __ |
41 | | -from sphinx.util.console import colorize |
42 | 25 | from sphinx.util.osutil import ensuredir |
43 | 26 | from sphinx.util.template import SphinxRenderer |
44 | 27 |
|
45 | 28 | if TYPE_CHECKING: |
46 | 29 | from collections.abc import Callable, Sequence |
47 | 30 | from typing import Any |
48 | 31 |
|
| 32 | +# try to import readline, unix specific enhancement |
| 33 | +try: |
| 34 | + import readline |
| 35 | + |
| 36 | + if TYPE_CHECKING and sys.platform == 'win32': |
| 37 | + # MyPy doesn't realise that this raises a ModuleNotFoundError |
| 38 | + # on Windows, and complains that 'parse_and_bind' is not defined. |
| 39 | + # This condition is always False at runtime, but tricks type checkers. |
| 40 | + raise ImportError # NoQA: TRY301 |
| 41 | +except ImportError: |
| 42 | + READLINE_AVAILABLE = USE_LIBEDIT = False |
| 43 | +else: |
| 44 | + READLINE_AVAILABLE = True |
| 45 | + USE_LIBEDIT = 'libedit' in getattr(readline, '__doc__', '') |
| 46 | + if USE_LIBEDIT: |
| 47 | + readline.parse_and_bind('bind ^I rl_complete') |
| 48 | + else: |
| 49 | + readline.parse_and_bind('tab: complete') |
| 50 | + |
49 | 51 | EXTENSIONS = { |
50 | 52 | 'autodoc': __('automatically insert docstrings from modules'), |
51 | 53 | 'doctest': __('automatically test code snippets in doctest blocks'), |
|
73 | 75 | PROMPT_PREFIX = '> ' |
74 | 76 |
|
75 | 77 | if sys.platform == 'win32': |
76 | | - # On Windows, show questions as bold because of color scheme of PowerShell (refs: #5294). |
77 | | - COLOR_QUESTION = 'bold' |
| 78 | + # On Windows, show questions as bold because of PowerShell's colour scheme |
| 79 | + # (xref: https://github.com/sphinx-doc/sphinx/issues/5294). |
| 80 | + from sphinx._cli.util.colour import bold as _question_colour |
78 | 81 | else: |
79 | | - COLOR_QUESTION = 'purple' |
| 82 | + from sphinx._cli.util.colour import purple as _question_colour |
| 83 | + |
| 84 | + if READLINE_AVAILABLE: |
| 85 | + # Use an input-mode colour function if readline is available |
| 86 | + if escape_code := getattr(_question_colour, '__escape_code', ''): |
| 87 | + _question_colour = _create_input_mode_colour_func(escape_code) |
| 88 | + del escape_code |
80 | 89 |
|
81 | 90 |
|
82 | 91 | # function to get input from terminal -- overridden by the test suite |
@@ -158,11 +167,8 @@ def do_prompt( |
158 | 167 | # sequence (see #5335). To avoid the problem, all prompts are not colored |
159 | 168 | # on libedit. |
160 | 169 | pass |
161 | | - elif READLINE_AVAILABLE: |
162 | | - # pass input_mode=True if readline available |
163 | | - prompt = colorize(COLOR_QUESTION, prompt, input_mode=True) |
164 | 170 | else: |
165 | | - prompt = colorize(COLOR_QUESTION, prompt, input_mode=False) |
| 171 | + prompt = _question_colour(prompt) |
166 | 172 | x = term_input(prompt).strip() |
167 | 173 | if default and not x: |
168 | 174 | x = default |
|
0 commit comments