8000 gh-133447: Add basic color to `sqlite3` CLI by StanFromIreland · Pull Request #133461 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133447: Add basic color to sqlite3 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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Add
  • Loading branch information
StanFromIreland committed May 5, 2025
commit 0a33388f6c77c855c3c340f3a4f4dd98b323aa3b
11 changes: 9 additions & 2 deletions Lib/sqlite3/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from argparse import ArgumentParser
from code import InteractiveConsole
from textwrap import dedent
import _colorize as colorize


def execute(c, sql, suppress_errors=True):
Expand Down Expand Up @@ -104,8 +105,14 @@ def main(*args):
Each command will be run using execute() on the cursor.
Type ".help" for more information; type ".quit" or {eofkey} to quit.
""").strip()
sys.ps1 = "sqlite> "
sys.ps2 = " ... "

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}"

con = sqlite3.connect(args.filename, isolation_level=None)
try:
Expand Down
11 changes: 9 additions & 2 deletions Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import unittest

from sqlite3.__main__ import main as cli
from test.support.os_helper import TESTFN, unlink
from test.support import captured_stdout, captured_stderr, captured_stdin
from test.support.os_helper import TESTFN, unlink, EnvironmentVarGuard
from test.support import (captured_stdout, captured_stderr, captured_stdin,
force_not_colorized_test_class)


class CommandLineInterface(unittest.TestCase):
Expand Down Expand Up @@ -63,6 +64,7 @@ def test_cli_on_disk_db(self):
self.assertIn("(0,)", out)


@force_not_colorized_test_class
class InteractiveSession(unittest.TestCase):
MEMORY_DB_MSG = "Connected to a transient in-memory database"
PS1 = "sqlite> "
Expand Down Expand Up @@ -152,6 +154,11 @@ def test_interact_on_disk_file(self):
out, _ = self.run_cli(TESTFN, commands=("SELECT count(t) FROM t;",))
self.assertIn("(0,)\n", out)

def test_color(self):
with unittest.mock.patch("_colorize.can_colorize", return_value=True):
out, err = self.run_cli(commands="\n")
self.assertIn("\x1b[1;35msqlite> \x1b[0m", out)
self.assertIn("\x1b[1;35m ... \x1b[0m\x1b", out)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add basic color to :mod:`sqlite3` CLI interface.
0