Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #713 from raccube/kjk/console-stdin
Browse files Browse the repository at this point in the history
Kjk/console stdin
  • Loading branch information
trickeydan authored Jun 18, 2021
2 parents 3a9a8a3 + e95ed44 commit 8dc5ff3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions j5/backends/console/console.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Console helper classes."""

import sys
from typing import Callable, Dict, Optional, Type, TypeVar

T = TypeVar("T")
Expand Down Expand Up @@ -29,9 +29,12 @@ def read( # type: ignore
self,
prompt: str,
return_type: Optional[Type[T]] = str, # type: ignore
check_stdin: bool = True,
) -> T:
"""Get a value of type 'return_type' from the user."""
if return_type is not None:
if check_stdin and return_type is bool and not sys.stdin.isatty():
return False # type: ignore
elif return_type is not None:
while True:
response = self._input(f"{self._descriptor}: {prompt}: ")
try:
Expand Down
6 changes: 3 additions & 3 deletions tests/backends/console/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ def is_finished(self) -> bool:
)

for _ in MockConsoleState.true_cases:
val = console.read("I want an bool", bool)
val = console.read("I want an bool", bool, check_stdin=False)
assert isinstance(val, bool)
assert val

for _ in MockConsoleState.false_cases:
val = console.read("I want an bool", bool)
val = console.read("I want an bool", bool, check_stdin=False)
assert isinstance(val, bool)
assert not val

# Test if false inputs are skipped.
val = console.read("I want an bool", bool)
val = console.read("I want an bool", bool, check_stdin=False)
assert isinstance(val, bool)
assert val

Expand Down

0 comments on commit 8dc5ff3

Please sign in to comment.