Skip to content

Commit

Permalink
feat(ux): render url strings as links in rich table output
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Oct 6, 2023
1 parent c26e48b commit 1c7a9b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ibis/expr/types/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
from functools import singledispatch
from math import isfinite
from urllib.parse import urlparse

import rich
from rich.align import Align
Expand Down Expand Up @@ -138,6 +139,12 @@ def _(dtype, values):
v = "".join(
f"[dim]{repr(c)[1:-1]}[/]" if not c.isprintable() else c for c in v
)
elif len(v) <= max_string:
url = urlparse(v)
# check both scheme and netloc to avoid rendering e.g.,
# `https://` as link
if url.scheme and url.netloc:
v = f"[link]{v}[/link]"
text = Text.from_markup(v, style="green")
else:
text = Text.styled("~", "dim")
Expand Down
11 changes: 10 additions & 1 deletion ibis/tests/expr/test_pretty_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import ibis
import ibis.expr.datatypes as dt
from ibis.expr.types.pretty import format_column
from ibis.expr.types.pretty import format_column, format_values

null = "NULL"

Expand Down Expand Up @@ -178,3 +178,12 @@ def test_non_interactive_column_repr():

result = capture.get()
assert "Selection" not in result


def test_format_link():
result = format_values(dt.string, ["https://ibis-project.org"])
assert result[0].spans[0].style == "link"

# not links
result = format_values(dt.string, ["https://", "https:", "https"])
assert all(not rendered.spans for rendered in result)

0 comments on commit 1c7a9b6

Please sign in to comment.