Skip to content

Commit

Permalink
Update sql
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Jun 16, 2024
1 parent 178cbbc commit a1fac07
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 451 deletions.
26 changes: 2 additions & 24 deletions rich_tables/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import TYPE_CHECKING, Any, Callable, Iterable, MutableMapping

from rich.bar import Bar
from rich.syntax import Syntax
from rich.text import Text

from .utils import (
Expand All @@ -33,6 +32,7 @@
progress_bar,
simple_panel,
split_with_color,
sql_syntax,
syntax,
timestamp2timestr,
wrap,
Expand Down Expand Up @@ -213,6 +213,7 @@ def counts_table(data: list[JSONDict]) -> Table:
diffHunk=lambda x: syntax(x, "diff"),
snippet=lambda x: border_panel(syntax(x, "python", indent_guides=True)),
query=lambda x: Text(x, style="bold"),
sql=lambda x: border_panel(sql_syntax(x)),
)
fields_by_func: dict[Callable[..., RenderableType], Iterable[str]] = {
format_with_color: (
Expand Down Expand Up @@ -298,29 +299,6 @@ def counts_table(data: list[JSONDict]) -> Table:
for field in fields:
FIELDS_MAP[field] = func

try:
import sqlparse
except ModuleNotFoundError:
pass
else:
FIELDS_MAP["sql"] = lambda x: border_panel(
Syntax(
sqlparse.format(
x,
indent_columns=False,
strip_whitespace=True,
strip_comments=True,
reindent=True,
reindent_aligned=False,
),
"sql",
theme="material",
background_color="#000000",
word_wrap=True,
)
)


DISPLAY_HEADER: dict[str, str] = {
"track": "#",
"bpm": "🚀",
Expand Down
18 changes: 18 additions & 0 deletions rich_tables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)

import platformdirs
import sqlparse
from multimethod import multimethod
from rich import box
from rich.align import Align
Expand Down Expand Up @@ -388,6 +389,23 @@ def syntax(*args: Any, **kwargs: Any) -> Syntax:
return Syntax(*args, **{**default, **kwargs})


def sql_syntax(sql_string: str) -> Syntax:
return Syntax(
sqlparse.format(
sql_string,
indent_columns=True,
strip_whitespace=True,
strip_comments=True,
reindent=True,
reindent_aligned=False,
),
"sql",
theme="gruvbox-dark",
background_color="black",
word_wrap=True,
)


@multimethod
def diff(before: str, after: str) -> Any:
return make_difftext(before, after)
Expand Down
Loading

0 comments on commit a1fac07

Please sign in to comment.