Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
erlend-aasland merged 16 commits intopython:mainfromStanFromIreland:color_step1
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
0a33388
Add
StanFromIrelandMay 5, 2025
e680c35
Import cleanup
StanFromIrelandMay 5, 2025
9bf179c
Merge branch 'main' into color_step1
StanFromIrelandMay 5, 2025
b456133
Highlight error messages
StanFromIrelandMay 5, 2025
8ef9217
Merge branch 'main' into color_step1
StanFromIrelandMay 6, 2025
ca015a1
Now with themes!
StanFromIrelandMay 6, 2025
2c06076
Simpler usage
StanFromIrelandMay 6, 2025
198399e
Fixup
StanFromIrelandMay 6, 2025
2a8c729
Simplify
ambvMay 6, 2025
a220442
Don't skip coloring errors on single-statement execution
ambvMay 6, 2025
e65d724
Also ignore color in the CLI test case
ambvMay 6, 2025
66711a0
Clean up
StanFromIrelandMay 6, 2025
62a0164
Fix line removal
StanFromIrelandMay 7, 2025
52ee84c
Merge branch 'main' into color_step1
StanFromIrelandMay 9, 2025
4ed5815
Lint
StanFromIrelandMay 9, 2025
4c0b52c
fixup! Lint
StanFromIrelandMay 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Highlight error messages
  • Loading branch information
@StanFromIreland
StanFromIreland committedMay 5, 2025
commitb4561334eb4a9dbcabed1d16bf302635ed2123e9
31 changes: 20 additions & 11 deletionsLib/sqlite3/__main__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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 execute(c, sql, suppress_errors=True):
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.
Expand All@@ -28,20 +36,24 @@ def execute(c, sql, suppress_errors=True):
except sqlite3.Error as e:
tp = type(e).__name__
try:
print(f"{tp} ({e.sqlite_errorname}): {e}", file=sys.stderr)
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"{tp}: {e}", file=sys.stderr)
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):
def __init__(self, connection, use_color=False):
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.
Expand All@@ -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)
execute(self._cur, source, use_color=self._use_color)
return False


Expand DownExpand Up@@ -109,11 +121,8 @@ def main(*args):

use_color = colorize.can_colorize()

bold_magenta = colorize.ANSIColors.BOLD_MAGENTA if use_color else ""
reset = colorize.ANSIColors.RESET if use_color else ""

sys.ps1 = f"{bold_magenta}sqlite> {reset}"
sys.ps2 = f"{bold_magenta} ... {reset}"
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:
Expand All@@ -122,7 +131,7 @@ def main(*args):
execute(con, args.sql, suppress_errors=False)
else:
# No SQL provided; start the REPL.
console = SqliteInteractiveConsole(con)
console = SqliteInteractiveConsole(con, use_color)
try:
import readline # noqa: F401
except ImportError:
Expand Down
3 changes: 3 additions & 0 deletionsLib/test/test_sqlite3/test_cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -165,6 +165,9 @@ def test_color(self):
out, err = self.run_cli(commands="\n")
self.assertIn("\x1b[1;35msqlite> \x1b[0m", out)
self.assertIn("\x1b[1;35m ... \x1b[0m\x1b", out)
out, err = self.run_cli(commands=("sel;",))
self.assertIn('\x1b[1;35mOperationalError (SQLITE_ERROR)\x1b[0m: '
'\x1b[35mnear "sel": syntax error\x1b[0m', err)

if __name__ == "__main__":
unittest.main()
Loading

[8]ページ先頭

©2009-2025 Movatter.jp