Skip to content

Commit

Permalink
feat(ux): show syntax-highlighted SQL if pygments is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Oct 20, 2023
1 parent 5200c2f commit 09881b0
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ibis/expr/sql.py
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import operator
from functools import singledispatch
from typing import IO
Expand Down Expand Up @@ -336,6 +337,25 @@ def __repr__(self) -> str:
def _repr_markdown_(self) -> str:
return f"```sql\n{self!s}\n```"

def _repr_pretty_(self, p, cycle) -> str:
output = str(self)
try:
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import SqlLexer
except ImportError:
pass
else:
with contextlib.suppress(Exception):
output = highlight(
code=output,
lexer=SqlLexer(),
formatter=TerminalFormatter(),
)

# strip trailing newline
p.text(output.strip())


@public
def to_sql(expr: ir.Expr, dialect: str | None = None, **kwargs) -> SQLString:
Expand Down

0 comments on commit 09881b0

Please sign in to comment.