|
1 | | -from __future__ import unicode_literals |
2 | | -from __future__ import print_function |
| 1 | +from __future__ import print_function, unicode_literals |
3 | 2 |
|
| 3 | +import itertools |
| 4 | +import logging |
4 | 5 | import os |
| 6 | +import re |
| 7 | +import shutil |
5 | 8 | import sys |
6 | | -import traceback |
7 | | -import logging |
8 | 9 | import threading |
9 | | -from time import time |
| 10 | +import traceback |
| 11 | +from collections import namedtuple |
10 | 12 | from datetime import datetime |
11 | 13 | from io import open |
12 | | -from collections import namedtuple |
13 | 14 | from sqlite3 import OperationalError, sqlite_version |
14 | | -import shutil |
| 15 | +from time import time |
15 | 16 |
|
16 | | -from cli_helpers.tabular_output import TabularOutputFormatter |
17 | | -from cli_helpers.tabular_output import preprocessors |
18 | 17 | import click |
19 | 18 | import sqlparse |
| 19 | +from cli_helpers.tabular_output import TabularOutputFormatter, preprocessors |
| 20 | +from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
20 | 21 | from prompt_toolkit.completion import DynamicCompleter |
21 | | -from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode |
22 | | -from prompt_toolkit.shortcuts import PromptSession, CompleteStyle |
23 | 22 | from prompt_toolkit.document import Document |
| 23 | +from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode |
24 | 24 | from prompt_toolkit.filters import HasFocus, IsDone |
25 | 25 | from prompt_toolkit.formatted_text import ANSI |
| 26 | +from prompt_toolkit.history import FileHistory |
26 | 27 | from prompt_toolkit.layout.processors import ( |
27 | | - HighlightMatchingBracketProcessor, |
28 | 28 | ConditionalProcessor, |
| 29 | + HighlightMatchingBracketProcessor, |
29 | 30 | ) |
30 | 31 | from prompt_toolkit.lexers import PygmentsLexer |
31 | | -from prompt_toolkit.history import FileHistory |
32 | | -from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
| 32 | +from prompt_toolkit.shortcuts import CompleteStyle, PromptSession |
33 | 33 |
|
34 | | -from .packages.special.main import NO_QUERY |
35 | | -from .packages.prompt_utils import confirm, confirm_destructive_query |
36 | | -from .packages import special |
37 | | -from .sqlcompleter import SQLCompleter |
38 | | -from .clitoolbar import create_toolbar_tokens_func |
39 | | -from .clistyle import style_factory, style_factory_output |
40 | | -from .sqlexecute import SQLExecute |
| 34 | +from .__init__ import __version__ |
41 | 35 | from .clibuffer import cli_is_multiline |
| 36 | +from .clistyle import style_factory, style_factory_output |
| 37 | +from .clitoolbar import create_toolbar_tokens_func |
42 | 38 | from .completion_refresher import CompletionRefresher |
43 | 39 | from .config import config_location, ensure_dir_exists, get_config |
44 | 40 | from .key_bindings import cli_bindings |
45 | 41 | from .lexer import LiteCliLexer |
46 | | -from .__init__ import __version__ |
| 42 | +from .packages import special |
47 | 43 | from .packages.filepaths import dir_path_exists |
48 | | - |
49 | | -import itertools |
| 44 | +from .packages.prompt_utils import confirm, confirm_destructive_query |
| 45 | +from .packages.special.main import NO_QUERY |
| 46 | +from .sqlcompleter import SQLCompleter |
| 47 | +from .sqlexecute import SQLExecute |
50 | 48 |
|
51 | 49 | click.disable_unicode_literals_warning = True |
52 | 50 |
|
@@ -758,20 +756,32 @@ def get_completions(self, text, cursor_positition): |
758 | 756 | return self.completer.get_completions(Document(text=text, cursor_position=cursor_positition), None) |
759 | 757 |
|
760 | 758 | def get_prompt(self, string): |
761 | | - self.logger.debug("Getting prompt") |
| 759 | + self.logger.debug("Getting prompt %r", string) |
762 | 760 | sqlexecute = self.sqlexecute |
763 | 761 | now = datetime.now() |
764 | | - string = string.replace("\\d", sqlexecute.dbname or "(none)") |
765 | | - string = string.replace("\\f", os.path.basename(sqlexecute.dbname or "(none)")) |
766 | | - string = string.replace("\\n", "\n") |
767 | | - string = string.replace("\\D", now.strftime("%a %b %d %H:%M:%S %Y")) |
768 | | - string = string.replace("\\m", now.strftime("%M")) |
769 | | - string = string.replace("\\P", now.strftime("%p")) |
770 | | - string = string.replace("\\R", now.strftime("%H")) |
771 | | - string = string.replace("\\r", now.strftime("%I")) |
772 | | - string = string.replace("\\s", now.strftime("%S")) |
773 | | - string = string.replace("\\_", " ") |
774 | | - return string |
| 762 | + |
| 763 | + # Prepare the replacements dictionary |
| 764 | + replacements = { |
| 765 | + r"\d": sqlexecute.dbname or "(none)", |
| 766 | + r"\f": os.path.basename(sqlexecute.dbname or "(none)"), |
| 767 | + r"\n": "\n", |
| 768 | + r"\D": now.strftime("%a %b %d %H:%M:%S %Y"), |
| 769 | + r"\m": now.strftime("%M"), |
| 770 | + r"\P": now.strftime("%p"), |
| 771 | + r"\R": now.strftime("%H"), |
| 772 | + r"\r": now.strftime("%I"), |
| 773 | + r"\s": now.strftime("%S"), |
| 774 | + r"\_": " ", |
| 775 | + } |
| 776 | + # Compile a regex pattern that matches any of the keys in replacements |
| 777 | + pattern = re.compile("|".join(re.escape(key) for key in replacements.keys())) |
| 778 | + |
| 779 | + # Define the replacement function |
| 780 | + def replacer(match): |
| 781 | + return replacements[match.group(0)] |
| 782 | + |
| 783 | + # Perform the substitution |
| 784 | + return pattern.sub(replacer, string) |
775 | 785 |
|
776 | 786 | def run_query(self, query, new_line=True): |
777 | 787 | """Runs *query*.""" |
|
0 commit comments