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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
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 @@ -105,8 +106,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
7 changes: 7 additions & 0 deletions Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
captured_stderr,
captured_stdin,
force_not_colorized,
force_not_colorized_test_class,
)


Expand Down Expand Up @@ -69,6 +70,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 @@ -158,6 +160,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.
Loading