Skip to content

Commit

Permalink
Merge pull request #187 from xen0n/cli-non-wrapped-columns
Browse files Browse the repository at this point in the history
Disable CLI hard line-wrapping as much as possible
  • Loading branch information
xen0n authored Sep 1, 2024
2 parents a1cbed9 + 8106328 commit 606ed91
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
12 changes: 6 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ruyi"
version = "0.17.0-alpha.20240812"
version = "0.17.0-alpha.20240901"
description = "Package manager for RuyiSDK"
keywords = ["ruyi", "ruyisdk"]
# license = { file = "LICENSE-Apache.txt" }
Expand Down
10 changes: 5 additions & 5 deletions ruyi/log.py → ruyi/log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from rich.console import Console, ConsoleRenderable
from rich.text import Text

from . import is_debug, is_porcelain
from .utils.porcelain import PorcelainEntity, PorcelainEntityType, PorcelainOutput
from .. import is_debug, is_porcelain
from ..utils.porcelain import PorcelainEntity, PorcelainEntityType, PorcelainOutput


class PorcelainLog(PorcelainEntity):
Expand All @@ -26,9 +26,9 @@ def log_time_formatter(x: datetime.datetime) -> Text:
return Text(f"debug: [{x.isoformat()}]")


STDOUT_CONSOLE = Console(file=sys.stdout, highlight=False)
DEBUG_CONSOLE = Console(file=sys.stderr, log_time_format=log_time_formatter)
LOG_CONSOLE = Console(file=sys.stderr, highlight=False)
STDOUT_CONSOLE = Console(file=sys.stdout, highlight=False, soft_wrap=True)
DEBUG_CONSOLE = Console(file=sys.stderr, log_time_format=log_time_formatter, soft_wrap=True)
LOG_CONSOLE = Console(file=sys.stderr, highlight=False, soft_wrap=True)
PORCELAIN_SINK = PorcelainOutput(sys.stderr.buffer)

Renderable = str | ConsoleRenderable
Expand Down
4 changes: 2 additions & 2 deletions ruyi/ruyipkg/news_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rich.table import Table

from ..config import GlobalConfig
from ..utils.markdown import MarkdownWithSlimHeadings
from ..utils.markdown import RuyiStyledMarkdown
from ..utils.porcelain import PorcelainOutput
from .. import is_porcelain, log
from .news import NewsItem, NewsItemContent, NewsItemStore
Expand Down Expand Up @@ -115,6 +115,6 @@ def filter_news_items_by_specs(


def print_news(nic: NewsItemContent) -> None:
md = MarkdownWithSlimHeadings(nic.content)
md = RuyiStyledMarkdown(nic.content)
log.stdout(md)
log.stdout("")
24 changes: 22 additions & 2 deletions ruyi/utils/markdown.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rich.console import Console, ConsoleOptions, RenderResult
from rich.markdown import Heading, Markdown, MarkdownContext
from rich.markdown import CodeBlock, Heading, Markdown, MarkdownContext
from rich.syntax import Syntax
from rich.text import Text


Expand All @@ -18,6 +19,25 @@ def __rich_console__(
yield self.text


class MarkdownWithSlimHeadings(Markdown):
# inspired by https://github.com/Textualize/rich/issues/3154
class NonWrappingCodeBlock(CodeBlock):
def __rich_console__(
self,
console: Console,
options: ConsoleOptions,
) -> RenderResult:
code = str(self.text).rstrip()
syntax = Syntax(
code,
self.lexer_name,
theme=self.theme,
word_wrap=False,
padding=0,
)
return syntax.highlight(code).__rich_console__(console, options)


class RuyiStyledMarkdown(Markdown):
elements = Markdown.elements
elements["fence"] = NonWrappingCodeBlock
elements["heading_open"] = SlimHeading

0 comments on commit 606ed91

Please sign in to comment.