-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add parameter to for output stream #235
base: master
Are you sure you want to change the base?
Conversation
The addition of the `stream` parameter allows the user to choose to send the yaspin spinner output to a stream or TextIO object other than sys.stdout. The most obvious use for this feature is to set the stream to stderr to support piping of output like `python main.py > some.log` and seeing the yaspin spinner in the terminal while stdout goes to `some.log`
@@ -591,24 +612,3 @@ def _set_interval(spinner: Spinner) -> float: | |||
def _set_cycle(frames: Union[str, Sequence[str]]) -> Iterator[str]: | |||
return itertools.cycle(frames) | |||
|
|||
@staticmethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of calls so isatty
these can no longer be static methods
@bpshaver please run |
Done. Does this seem like a sensible change? If so, I'll look through the failing test cases now. |
Leaving the default parameter of `stream` as `None` and using `sys.stdout` helps in cases when `sys.stdout` is patched between class definition and instance creation. This helps a few previously failing test cases pass.
…unction `test_in_out.py::test_hide_show` was failing after my changes due to the teardown function calling `.stop()` on a spinner that somehow already had its output stream closed. This may indicate a problem, but I put `.stop()` in a try/except block since it seems inappropriate for a test to fail because a teardown function raises an exception.
@@ -120,7 +120,13 @@ def test_hide_show(monkeypatch, capsys, text, request, isatty_fixture): | |||
|
|||
# Ensure that sp.stop() will be executed | |||
def teardown(): | |||
sp.stop() | |||
try: | |||
sp.stop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why sp.stop()
was raising an exception due to an already closed file.
@bpshaver thanks for your contribution! I'll try to review this thoroughly next week. |
Apparently it is possible to detect situations when different streams are not tty:
So, I would rather prefer this approach, aligning behavior with e.g. #31 (comment) to move further here. It must be much more convenient than requiring developer to define custom streams upfront. Plus, if yaspin is capable to find out which streams it is attached to, there is no need to rewrite older code. |
@pavdmyt I do not understand how this is more convenient. The idea of detecting whether the target stream is a TTY and changing behavior or issuing a warning is a good one. But whether that stream is always stdout or whether it should be configurable by the user is a separate question. If anything, #31 (comment) supports making
In fact, my proposed change keeps
Just because My proposed changes support a file like this: import sys
from time import sleep
from yaspin import yaspin
with yaspin(stream=sys.stderr):
for i in range(5):
print(f"{i} - Log some data")
sleep(1) being piped to a log file like so: $ python foo.py > foo.log
⠋ where the spinner shows during script execution and the log file ends up looking like this:
Without |
I think, I need to elaborate a bit more about my point. The idea behind capability to automatically detect streams to which yaspin is attached to allows to separate:
So that spinner's stream is always ends up in To summarize, we want to have spinner's stream and useful text clearly separated so that user can decide what to do with these streams. Having such feature, I'm not sure about the value of having an additional |
I understand now, thanks! I would call the In the issue I was trying to tackle ( mandiant/capa#1812 ) the accompanying text should go to $ python foo.py > foo.log
⠋ Analyzing data... while program output goes to I understand a sensible default may be to send accompanying text to the non-TTY stream, but this example I think illustrates a case where that is not desired. |
I'm not sure I've understood this correctly, "accompanying text should go to |
accompanying text is my name for whatever text normally goes right next to the spinner. You called it useful text, which is begging the question with respect to where it should go in the corner cases we're discussing. Call them what you like, but I'm pointing to three different types of text (not two):
I just don't see the utility in sending the spinner and any text to the right of it (determined by |
@@ -374,15 +378,11 @@ def fail(self, text: str = "FAIL") -> None: | |||
_text = text if text else "FAIL" | |||
self._freeze(_text) | |||
|
|||
def is_jupyter(self) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure that Jupyter is not the only front-end capable of intercepting the output stream.
@@ -533,6 +544,15 @@ def _set_attrs(attrs: Sequence[str]) -> set[str]: | |||
) | |||
return set(attrs) | |||
|
|||
# Static |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of this comment?
What is the status of this pull request? How can I help getting it merged and released? |
Seems like @pavdmyt disagreed with my reasoning and the discussion ended. |
The addition of the
stream
parameter allows the user to choose to send the yaspin spinner output to a stream or TextIO object other than sys.stdout. The most obvious use for this feature is to set the stream to stderr to support piping of output likepython main.py > some.log
and seeing the yaspin spinner in the terminal while stdout goes tosome.log