Skip to content
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

Look for $SHLVL when determining whether in a terminal #3

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/mads/environ/in_out.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ class InOut(BaseSettings):

term: str | None = None
force_terminal: bool | None = None
shlvl: int | None = None

@computed_field
def is_terminal(self) -> bool:
if self.force_terminal is not None:
return self.force_terminal
return self.is_atty
return bool(self.is_atty or self.is_subshell)

@computed_field
def is_atty(self) -> bool:
return sys.stdout.isatty()

@computed_field
def is_subshell(self) -> bool:
return self.shlvl is not None and self.shlvl > 1

@computed_field
def is_interactive(self) -> bool:
return self.is_atty and not self.is_dumb
Expand All @@ -40,6 +45,7 @@ def __rich_repr__(self):
yield "is_terminal", self.is_terminal
yield "force_terminal", self.force_terminal, None
yield "is_atty", self.is_atty, True
yield "is_subshell", self.is_subshell
yield "is_interactive", self.is_interactive

@classmethod
Expand Down