-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update I/O handling for better CI output (#2)
Rich is a really great library for use with CLI tools, but internally it's structured around a terminal. Specifically, the issue is that it requires a width. In the absence of one, it picks 80, which is normally fine, but it makes wide log outputs look really bad and take up more space than they should. This PR tries to determine if pretty terminal output makes sense, and if it doesn't, it foregoes the use of Rich altogether.
- Loading branch information
Showing
8 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sys | ||
from pydantic import computed_field | ||
from pydantic_settings import BaseSettings | ||
from .runners import Runner | ||
|
||
|
||
def runner_settings() -> dict: | ||
"""Override the detected settings with runner information""" | ||
|
||
runner = Runner.current | ||
return runner.io_settings | ||
|
||
|
||
class InOut(BaseSettings): | ||
"""The input/output we're running with. IO is a reserved name in some contexts.""" | ||
|
||
term: str | None = None | ||
force_terminal: bool | None = None | ||
|
||
@computed_field | ||
def is_terminal(self) -> bool: | ||
if self.force_terminal is not None: | ||
return self.force_terminal | ||
return self.is_atty | ||
|
||
@computed_field | ||
def is_atty(self) -> bool: | ||
return sys.stdout.isatty() | ||
|
||
@computed_field | ||
def is_interactive(self) -> bool: | ||
return self.is_atty and not self.is_dumb | ||
|
||
@property | ||
def is_dumb(self) -> bool: | ||
return self.term is not None and self.term.lower() in ("dumb", "unknown") | ||
|
||
def __rich_repr__(self): | ||
yield "term", self.term | ||
yield "is_terminal", self.is_terminal | ||
yield "force_terminal", self.force_terminal, None | ||
yield "is_atty", self.is_atty, True | ||
yield "is_interactive", self.is_interactive | ||
|
||
@classmethod | ||
def settings_customise_sources(cls, *args, env_settings, **kwargs) -> tuple: | ||
return ( | ||
runner_settings, | ||
env_settings, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import time | ||
from pydantic import Field | ||
|
||
|
||
from mads.lib.path import current_repo | ||
from .runner import Runner | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
"""Test the CLI interface""" | ||
|
||
from mads import cli | ||
from mads.environ import Git | ||
|
||
|
||
def test_determine_tag(capsys): | ||
"""Test that the help message works""" | ||
|
||
cli.main(["docker", "tag"]) | ||
assert capsys.readouterr().out == "dev\n" | ||
|
||
git = Git() | ||
expected = { | ||
"main": "latest", | ||
"master": "latest", | ||
"beta": "beta", | ||
None: "dev", | ||
}.get(git.branch, "dev") | ||
assert capsys.readouterr().out == f"{expected}\n" | ||
|
||
|
||
def test_determine_tag_default(capsys): | ||
"""Test that the help message works""" | ||
|
||
cli.main(["docker", "tag", "--default", "beta"]) | ||
assert capsys.readouterr().out == "beta\n" | ||
|
||
git = Git() | ||
expected = { | ||
"main": "latest", | ||
"master": "latest", | ||
"beta": "beta", | ||
None: "beta", | ||
}.get(git.branch, "beta") | ||
assert capsys.readouterr().out == f"{expected}\n" |