Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-133447: Add basic color tosqlite3 CLI#133461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
0a33388e680c359bf179cb4561338ef9217ca015a12c06076198399e2a8c729a220442e65d72466711a062a016452ee84c4ed58154c0b52cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -12,8 +12,16 @@ | ||
| from textwrap import dedent | ||
| import _colorize as colorize | ||
| BOLD_MAGENTA = colorize.ANSIColors.BOLD_MAGENTA | ||
| MAGENTA =colorize.ANSIColors.MAGENTA | ||
| RESET = colorize.ANSIColors.RESET | ||
| def color(color, use_color): | ||
| if use_color: | ||
| return color | ||
| return '' | ||
| def execute(c, sql, suppress_errors=True, use_color=False): | ||
| """Helper that wraps execution of SQL code. | ||
| This is used both by the REPL and by direct execution from the CLI. | ||
| @@ -28,20 +36,24 @@ def execute(c, sql, suppress_errors=True): | ||
| except sqlite3.Error as e: | ||
| tp = type(e).__name__ | ||
| try: | ||
| print(f"{color(BOLD_MAGENTA, use_color)}{tp} ({e.sqlite_errorname})" | ||
| f"{color(RESET, use_color)}: " | ||
| f"{color(MAGENTA, use_color)}{e}{color(RESET, use_color)}", file=sys.stderr) | ||
| except AttributeError: | ||
| print(f"{color(BOLD_MAGENTA, use_color)}{tp}{color(RESET, use_color)}: " | ||
| f"{color(MAGENTA, use_color)}{e}{color(RESET, use_color)}", file=sys.stderr) | ||
| if not suppress_errors: | ||
| sys.exit(1) | ||
| class SqliteInteractiveConsole(InteractiveConsole): | ||
| """A simple SQLite REPL.""" | ||
| def __init__(self, connection, use_color=False): | ||
StanFromIreland marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| super().__init__() | ||
| self._con = connection | ||
| self._cur = connection.cursor() | ||
| self._use_color = use_color | ||
| def runsource(self, source, filename="<input>", symbol="single"): | ||
| """Override runsource, the core of the InteractiveConsole REPL. | ||
| @@ -59,7 +71,7 @@ def runsource(self, source, filename="<input>", symbol="single"): | ||
| case _: | ||
| if not sqlite3.complete_statement(source): | ||
| return True | ||
| execute(self._cur, source, use_color=self._use_color) | ||
| return False | ||
| @@ -109,11 +121,8 @@ def main(*args): | ||
| use_color = colorize.can_colorize() | ||
StanFromIreland marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| sys.ps1 = f"{color(BOLD_MAGENTA, use_color)}sqlite> {color(RESET, use_color)}" | ||
| sys.ps2 = f"{color(BOLD_MAGENTA, use_color)} ... {color(RESET, use_color)}" | ||
| con = sqlite3.connect(args.filename, isolation_level=None) | ||
| try: | ||
| @@ -122,7 +131,7 @@ def main(*args): | ||
| execute(con, args.sql, suppress_errors=False) | ||
| else: | ||
| # No SQL provided; start the REPL. | ||
| console = SqliteInteractiveConsole(con, use_color) | ||
| try: | ||
| import readline # noqa: F401 | ||
| except ImportError: | ||
Uh oh!
There was an error while loading.Please reload this page.