Skip to content

Commit

Permalink
Automatic backup 2024-11-09
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Nov 10, 2024
1 parent 69ab91a commit e77ef66
Show file tree
Hide file tree
Showing 10 changed files with 837 additions and 876 deletions.
6 changes: 6 additions & 0 deletions rich_tables/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ def _list(data: list) -> RenderableType:
return flexitable(tuple(data))


@flexitable.register
@debug
def _list_list(data: List[List[Any]]) -> RenderableType:
return list_table(flexitable(i) for i in data)


@flexitable.register
@debug
def _str_list(data: Sequence[str]) -> RenderableType:
Expand Down
2 changes: 1 addition & 1 deletion rich_tables/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _draw_data_list(data: list[JSONDict], **kwargs) -> Iterator[RenderableType]:
def main() -> None:
args = get_args()
if args.command == "diff":
console.print(pretty_diff(args.before, args.after).markup, markup=False)
console.print(pretty_diff(args.before, args.after), markup=False)
else:
data = load_data("/dev/stdin")
if args.json:
Expand Down
75 changes: 33 additions & 42 deletions rich_tables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from functools import lru_cache
from itertools import groupby, islice, starmap, zip_longest
from math import copysign
from pprint import pformat
from pprint import pformat, pprint
from string import ascii_lowercase, ascii_uppercase, printable, punctuation
from typing import (
Any,
Expand Down Expand Up @@ -118,21 +118,14 @@ def format_space(string: str) -> str:


def format_new(string: str) -> str:
# if string == "\n":
# string = "⮠ \n"
string = re.sub("^\n", lambda m: m[0].replace("\n", "⮠ \n"), string)

# return f"{{+{string}+}}"
string = re.sub("^\n+", lambda m: m[0].replace("\n", "⮠\n"), string)
return wrap(format_space(string), BOLD_GREEN)


def format_old(string: str) -> str:
# if string == "\n":
# string = "⮠ "
string = re.sub("\n+$", lambda m: m[0].replace("\n", "⮠ \n"), string)

# return f"{{-{string}-}}"
return wrap(wrap(string, BOLD_RED), "s")
string = re.sub("^\n|\n$", lambda m: m[0].replace("\n", "⮠ "), string)
# string = re.sub("(?<!^)\n(?!$)", lambda m: m[0].replace("\n", ""), string)
return wrap(string, f"{BOLD_RED}")


def fmtdiff(change: str, before: str, after: str) -> str:
Expand All @@ -141,7 +134,10 @@ def fmtdiff(change: str, before: str, after: str) -> str:
if change == "delete":
return format_old(before)
if change == "replace":
return format_old(before) + format_new(after)
return "".join(
(format_old(a) + format_new(b)) if a != b else a
for a, b in zip(before.partition("\n"), after.rpartition("\n"))
)

return wrap(before, "dim")

Expand Down Expand Up @@ -169,43 +165,29 @@ def make_difftext(
after: str,
junk: str = "".join(sorted((set(punctuation) - {"_", "-", ":"}) | {"\n"})),
) -> str:
# matcher = SequenceMatcher(lambda x: x == "\n", autojunk=False, a=before, b=after)
matcher = SequenceMatcher(None, autojunk=False, a=before, b=after)
matcher = SequenceMatcher(lambda x: x in " \n", autojunk=False, a=before, b=after)
ops = matcher.get_opcodes()
print(ops)
to_remove_ids = [
i
for i, (op, a, b, *_) in enumerate(ops)
for i, (op, a, b, c, d) in enumerate(ops)
if 0 < i < len(ops) - 1
and op == "equal"
and b - a < 3
# and before[a:b].replace("\n", "")
# and not before[:a].endswith("\n")
and b - a < 5
and before[a:b].strip()
and after[c:d].strip()
]
for i in reversed(to_remove_ids):
a, b = ops[i - 1], ops[i + 1]
if "replace" in {a[0], b[0]}:
action = "replace"
else:
action = a[0]
print(
action,
a[0],
b[0],
before[ops[i][1] : ops[i][2]].encode(),
before[a[1] : b[2]].encode(),
after[a[3] : b[4]].encode(),
)
action = "replace"

ops[i] = (action, a[1], b[2], a[3], b[4])

del ops[i + 1]
del ops[i - 1]
print(ops)

text = "".join(
return "".join(
fmtdiff(code, before[a1:a2], after[b1:b2]) or "" for code, a1, a2, b1, b2 in ops
)
# text = re.sub(r"([^\n]+)\\n", format_added_line, text)
return text


def duration2human(duration: SupportsFloat) -> str:
Expand Down Expand Up @@ -235,6 +217,13 @@ def get_theme() -> Optional[Theme]:


class SafeConsole(Console):
def render_str(self, text: str, **kwargs) -> Text:
try:
return super().render_str(text, **kwargs)
except MarkupError:
kwargs["markup"] = False
return super().render_str(text, **kwargs)

def print(self, *args, **kwargs):
try:
super().print(*args, **kwargs)
Expand All @@ -248,7 +237,7 @@ def make_console(**kwargs) -> SafeConsole:
kwargs.setdefault("force_terminal", True)
kwargs.setdefault("force_interactive", False)
kwargs.setdefault("emoji", True)
return Console(**kwargs)
return SafeConsole(**kwargs)


console = make_console()
Expand Down Expand Up @@ -276,7 +265,9 @@ def colnames(self) -> List[str]:
return [str(c.header) for c in self.columns]


def new_table(*headers: str, **kwargs) -> NewTable:
def new_table(
*headers: str, rows: Iterable[Iterable[RenderableType]] | None = None, **kwargs
) -> NewTable:
kwargs.setdefault("show_edge", False)
kwargs.setdefault("show_header", False)
kwargs.setdefault("pad_edge", False)
Expand All @@ -289,10 +280,10 @@ def new_table(*headers: str, **kwargs) -> NewTable:
kwargs.setdefault("box", box.ROUNDED)

if headers:
kwargs.update(
header_style="bold misty_rose1", box=box.SIMPLE_HEAVY, show_header=True
)
rows = kwargs.pop("rows", [])
kwargs.setdefault("header_style", "bold misty_rose1")
kwargs.setdefault("show_header", True)
kwargs.setdefault("box", box.SIMPLE_HEAVY)

table = NewTable(*headers, **kwargs)
if rows:
table.add_rows(rows)
Expand Down
Loading

0 comments on commit e77ef66

Please sign in to comment.