Ensure newline before typer.secho
#661
Replies: 3 comments
-
|
Hi @WilliamDEdwards! Short version: no, there isn't a built-in flag for this — Typer and Click don't track "has anything been printed yet" for you, so you'll need a small bit of state to handle it yourself. The core idea: keep a flag that gets set the first time anything is printed, and have your generic function check it before deciding whether to add a separating blank line. Wrap your regular output calls so they just mark that something's been printed, without adding any spacing themselves: import typer
_has_output = False
def echo(message: str = "", **kwargs) -> None:
global _has_output
typer.secho(message, **kwargs)
_has_output = True
def echo_section(message: str = "", **kwargs) -> None:
global _has_output
if _has_output:
typer.echo() # blank line separator
typer.secho(message, **kwargs)
_has_output = TrueNote: Swap your regular typer.secho() calls for echo(), and use echo_section() specifically in your generic function. That way normal sequential output stays tight with no extra spacing, but your generic function automatically gets a blank line before it whenever something already printed, and skips it cleanly when it's the very first thing on screen. One quick heads-up on scaling this up: if the app grows or you start testing commands in isolation, a bare global can get awkward. The same flag works just as well stored on Typer's ctx.obj and passed through your commands instead - happy to sketch that version too if it'd help your setup. I hope this gets you unblocked! Let me know if this fits your case. (And if this clears things up, I'd really appreciate it if you could mark this as the answer! ) |
Beta Was this translation helpful? Give feedback.
-
|
@DhruvalAnandkar: are all of these answers to discussions AI generated? Please disclose 🙏 |
Beta Was this translation helpful? Give feedback.
-
|
Hi @svlandeg , Yes, I used AI to help structure this draft for the explanation because English is not my first language even not second language but I did review the logic and also verified the state-flag approach before posting to make sure it was a solid workaround for @WilliamDEdwards . I completely understand that I should have explicitly disclosed that upfront that is entirely my mistake I will make sure to clearly label any AI assistance in my future contributions. I still hope the proposed code helps unblock the issue! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
-Description
How do I tell Typer to ensure a newline before
typer.sechowhen there was output before?In my case, I do not know if there was output before
typer.secho(as it is called from a generic function). So adding a leading\ndoes not suffice, as that would cause a newline to be present when there was no output before too.For example:
... should output:
... and this:
... should output this:
Operating System
Linux, macOS
Operating System Details
No response
Typer Version
0.7.0
Python Version
3.11
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions