fix: update for newer versions of rich

This commit is contained in:
Daylin Morgan 2023-05-07 23:36:16 -05:00
parent a9b77d7825
commit 1361fd8074
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
5 changed files with 27 additions and 17 deletions

View file

@ -1,18 +1,20 @@
CONSOLE_SVG_FORMAT = """\ CONSOLE_SVG_FORMAT = """\
<svg class="rich-terminal shadow" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg"> <svg class="rich-terminal shadow" viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">
<!-- Generated with Rich https://www.textualize.io --> <!-- Generated with Rich https://www.textualize.io & yartsu https://github.com/daylinmorgan/yartsu -->
<style> <style>
@font-face {{ @font-face {{
font-family: "Fira Code"; font-family: "Fira Code";
src: local("FiraCode-Regular"), src: local("FiraCode-Regular"),
url("https://cdn.jsdelivr.net/gh/ryanoasis/nerd-fonts@2.1.0/patched-fonts/FiraCode/Regular/complete/Fira%20Code%20Regular%20Nerd%20Font%20Complete.ttf") format("truetype"); url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Regular.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Regular.woff") format("woff");
font-style: normal; font-style: normal;
font-weight: 400; font-weight: 400;
}} }}
@font-face {{ @font-face {{
font-family: "Fira Code"; font-family: "Fira Code";
src: local("FiraCode-Bold"), src: local("FiraCode-Bold"),
url("https://cdn.jsdelivr.net/gh/ryanoasis/nerd-fonts@2.1.0/patched-fonts/FiraCode/Bold/complete/Fira%20Code%20Bold%20Nerd%20Font%20Complete.ttf") format("truetype"); url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff2/FiraCode-Bold.woff2") format("woff2"),
url("https://cdnjs.cloudflare.com/ajax/libs/firacode/6.2.0/woff/FiraCode-Bold.woff") format("woff");
font-style: bold; font-style: bold;
font-weight: 700; font-weight: 700;
}} }}

View file

@ -68,7 +68,10 @@ class CustomFormatter(StdHelpFormatter):
# short action name; start on the same line and pad two spaces # short action name; start on the same line and pad two spaces
elif action_header_len <= action_width: elif action_header_len <= action_width:
tup = self._current_indent, "", action_width, action_header tup = self._current_indent, "", action_width, action_header
action_header = f"{' '*self._current_indent}{action_header}{' '*(action_width+2 - action_header_len)}" action_header = (
f"{' '*self._current_indent}{action_header}"
f"{' '*(action_width+2 - action_header_len)}"
)
indent_first = 0 indent_first = 0
# long action name; start on the next line # long action name; start on the next line
@ -114,7 +117,6 @@ class CustomFormatter(StdHelpFormatter):
def add_argument(self, action: Action) -> None: def add_argument(self, action: Action) -> None:
if action.help is not SUPPRESS: if action.help is not SUPPRESS:
# find all invocations # find all invocations
get_invocation = self._format_action_invocation get_invocation = self._format_action_invocation
invocations = [get_invocation(action)] invocations = [get_invocation(action)]

View file

@ -19,7 +19,6 @@ DEFAULT_THEME = os.getenv("YARTSU_THEME", "cat-mocha")
def get_parser() -> ArgumentParser: def get_parser() -> ArgumentParser:
parser = ArgumentParser( parser = ArgumentParser(
usage=SUPPRESS, usage=SUPPRESS,
description=textwrap.dedent( description=textwrap.dedent(

View file

@ -20,18 +20,24 @@ class Console(RichConsole):
theme: Optional[TerminalTheme] = None, theme: Optional[TerminalTheme] = None,
clear: bool = True, clear: bool = True,
code_format: str = CONSOLE_SVG_FORMAT, code_format: str = CONSOLE_SVG_FORMAT,
font_aspect_ratio: float = 0.61,
unique_id: Optional[str] = None,
) -> str: ) -> str:
""" """
Generate an SVG from the console contents (requires record=True in Console constructor). Generate an SVG from the console contents (requires record=True in Console constructor).
Args: Args:
path (str): The path to write the SVG to. title (str, optional): The title of the tab in the output image
title (str): The title of the tab in the output image
theme (TerminalTheme, optional): The ``TerminalTheme`` object to use to style the terminal theme (TerminalTheme, optional): The ``TerminalTheme`` object to use to style the terminal
clear (bool, optional): Clear record buffer after exporting. Defaults to ``True`` clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``
code_format (str): Format string used to generate the SVG. Rich will inject a number of variables code_format (str, optional): Format string used to generate the SVG. Rich will inject a number of variables
into the string in order to form the final SVG output. The default template used and the variables into the string in order to form the final SVG output. The default template used and the variables
injected by Rich can be found by inspecting the ``console.CONSOLE_SVG_FORMAT`` variable. injected by Rich can be found by inspecting the ``console.CONSOLE_SVG_FORMAT`` variable.
font_aspect_ratio (float, optional): The width to height ratio of the font used in the ``code_format``
string. Defaults to 0.61, which is the width to height ratio of Fira Code (the default font).
If you aren't specifying a different font inside ``code_format``, you probably don't need this.
unique_id (str, optional): unique id that is used as the prefix for various elements (CSS styles, node
ids). If not set, this defaults to a computed value based on the recorded content.
""" """
from rich.cells import cell_len from rich.cells import cell_len
@ -75,7 +81,7 @@ class Console(RichConsole):
width = self.width width = self.width
char_height = 20 char_height = 20
char_width = char_height * 0.61 char_width = char_height * font_aspect_ratio
line_height = char_height * 1.22 line_height = char_height * 1.22
margin_top = 1 margin_top = 1
@ -127,10 +133,12 @@ class Console(RichConsole):
if clear: if clear:
self._record_buffer.clear() self._record_buffer.clear()
if unique_id is None:
unique_id = "terminal-" + str( unique_id = "terminal-" + str(
zlib.adler32( zlib.adler32(
("".join(segment.text for segment in segments)).encode( ("".join(repr(segment) for segment in segments)).encode(
"utf-8", "ignore" "utf-8",
"ignore",
) )
+ title.encode("utf-8", "ignore") + title.encode("utf-8", "ignore")
) )

View file

@ -27,7 +27,6 @@ theme = Theme({"header": "bold cyan", "option": "yellow", "metavar": "green"})
class Term: class Term:
def __init__(self, width: int) -> None: def __init__(self, width: int) -> None:
self.console = Console(highlight=False, theme=theme, width=width) self.console = Console(highlight=False, theme=theme, width=width)
self.err_console = Console( self.err_console = Console(
theme=Theme({"error": "bold red"}, inherit=True), theme=Theme({"error": "bold red"}, inherit=True),