Giter Club home page Giter Club logo

enrich's Introduction

enrich

Enriched extends rich library functionality with a set of changes that were not accepted to rich itself.

Console with redirect support

Our Console class adds one additional option to rich.Console in order to redirect sys.stdout and sys.stderr streams using a FileProxy.

from enrich.console import Console
import sys

console = Console(
    redirect=True,  # <-- not supported by rich.console.Console
    record=True)
sys.write("foo")

# this assert would have passed without redirect=True
assert console.export_text() == "foo"

Console with implicit soft wrapping

If you want to produce fluid terminal output, one where the client terminal decides where to wrap the text instead of the application, you can now tell the Console constructor the soft_wrap preference.

from enrich.console import Console
import sys

console = Console(soft_wrap=True)
console.print(...)  # no longer need to pass soft_wrap to each print

Console.print can also deal with ANSI escapes

Extends Rich Console to detect if original text already had ANSI escapes and decodes it before processing it. This solves the case where printing output captured from other processes that contained ANSI escapes would brake. upstream-404

Soft-wrapping logger

Rich logger assumes that you always have a fixed width console and it does wrap logged output according to it. Our alternative logger does exactly the opposite: it ignores the columns of the current console and prints output using a Console with soft wrapping enabled.

The result are logged lines that can be displayed on any terminal or web page as they will allow the client to decide when to perform the wrapping.

import logging
from enrich.logging import RichHandler

FORMAT = "%(message)s"
logging.basicConfig(
    level="NOTSET", format=FORMAT, datefmt="[%X]", handlers=[RichHandler()]
)

log = logging.getLogger("rich")
log.info("Text that we do not want pre-wrapped by logger: %s", 100 * "x")

enrich's People

Contributors

dvzrv avatar pre-commit-ci[bot] avatar schwehr avatar ssbarnea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

enrich's Issues

Tests are installed

Hi! I'm packaging this project on Arch Linux.
I noticed that the test files are installed alongside the files of this project, but they are not required during runtime and should therefore end up on a user system.

New release?

Hello. Would it make sense to do a new release? There are some important changes since the last one.

Support messages with newlines

It seems the FluidLogRender doesn't work well with messages containing newlines:

import logging
from enrich.logging import RichHandler

logging.basicConfig(level="INFO", handlers=[RichHandler()])

log = logging.getLogger("rich")
log.info("Text that we\ndo not want\npre-wrapped by logger: %s", 100 * "x")

Enable soft wrapping consoles

Some users do not want to take in consideration the width of the terminal but still make use of most of the features provides by Rich library.

At this moment this is not possible and a change to add this to Rich was refused, so we will have to add it here.

This is a common use case for tools running in CI/CD pipelines, where they would still want to produce colorful output but where there is not real fixed width terminal, as the produced log would endup being visualized inside the browser, where browser will decide where to wrap the lines or if to wrap them at all.

A soft wrapped console is also useful for normal terminals output as it allows user to resize its terminal window without having too see weird wrappings. Terminals do soft-wrapping by default.

Duplicated lines in console buffer

While testing enrich in an environment with rich 12.5.1 installed, I'm seeing duplicated text in the console buffer, and I'm struggling to work out how this happens. Test failure is below:

[    4s]     def test_rich_console_ex() -> None:
[    4s]         """Validate that ConsoleEx can capture output from print() calls."""
[    4s]         console = Console(record=True, redirect=True)
[    4s]         console.print("alpha")
[    4s]         print("beta")
[    4s]         sys.stdout.write("gamma\n")
[    4s]         sys.stderr.write("delta\n")
[    4s]         # While not supposed to happen we want to be sure that this will not raise
[    4s]         # an exception. Some libraries may still sometimes send bytes to the
[    4s]         # streams, notable example being click.
[    4s]         # sys.stdout.write(b"epsilon\n")  # type: ignore
[    4s]         text = console.export_text()
[    4s] >       assert text == "alpha\nbeta\ngamma\ndelta\n"
[    4s] E       AssertionError: assert 'alpha\nbeta\...elta\ndelta\n' == 'alpha\nbeta\ngamma\ndelta\n'
[    4s] E           alpha
[    4s] E           beta
[    4s] E         + beta
[    4s] E         + gamma
[    4s] E           gamma
[    4s] E           delta
[    4s] E         + delta
[    4s] 
[    4s] src/enrich/test/test_console.py:23: AssertionError

test_rich_console_ex fails with rich 12.5.1

The following test (test_rich_console_ex) fails with:

  • enrich 1.2.7
  • rich 12.5.1
  • python 3.10

========================================================================
src/enrich/test/test_console.py::test_rich_console_ex

____________________________ test_rich_console_ex _____________________________

def test_rich_console_ex() -> None:
"""Validate that ConsoleEx can capture output from print() calls."""
console = Console(record=True, redirect=True)
console.print("alpha")
print("beta")
sys.stdout.write("gamma\n")
sys.stderr.write("delta\n")
# While not supposed to happen we want to be sure that this will not raise
# an exception. Some libraries may still sometimes send bytes to the
# streams, notable example being click.
# sys.stdout.write(b"epsilon\n") # type: ignore
text = console.export_text()

   assert text == "alpha\nbeta\ngamma\ndelta\n"

E AssertionError: assert 'alpha\nbeta...elta\ndelta\n' == 'alpha\nbeta\ngamma\ndelta\n'
E alpha
E beta
E + beta
E + gamma
E gamma
E delta
E + delta

src/enrich/test/test_console.py:23: AssertionError
----------------------------- Captured stdout call -----------------------------
alpha
beta
gamma
delta

========================================================================

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.