mirror of
https://github.com/caelestia-dots/cli.git
synced 2026-06-16 05:49:59 -05:00
feat: allow disabling input in io module
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
import sys
|
import sys
|
||||||
from typing import Never
|
from typing import Never
|
||||||
|
|
||||||
|
_disable_input: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
def disable_input() -> None:
|
||||||
|
global _disable_input
|
||||||
|
_disable_input = True
|
||||||
|
|
||||||
|
|
||||||
def log_exception(func):
|
def log_exception(func):
|
||||||
"""Log exceptions to stdout instead of raising
|
"""Log exceptions to stdout instead of raising
|
||||||
@@ -44,6 +51,10 @@ def fatal(err: str | Exception) -> Never:
|
|||||||
|
|
||||||
|
|
||||||
def _input(prompt: str) -> str:
|
def _input(prompt: str) -> str:
|
||||||
|
if _disable_input:
|
||||||
|
print(prompt, end="")
|
||||||
|
return ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return input(prompt)
|
return input(prompt)
|
||||||
except (KeyboardInterrupt, EOFError):
|
except (KeyboardInterrupt, EOFError):
|
||||||
@@ -55,6 +66,17 @@ def prompt(msg: str) -> str:
|
|||||||
return _input(_format_msg(36, msg) + " ")
|
return _input(_format_msg(36, msg) + " ")
|
||||||
|
|
||||||
|
|
||||||
|
def confirm(msg: str, default: bool = True) -> bool:
|
||||||
|
suffix = " [Y/n]" if default else " [y/N]"
|
||||||
|
answer = prompt(msg + suffix).strip().lower()
|
||||||
|
if not answer:
|
||||||
|
return default
|
||||||
|
return answer in ("y", "yes")
|
||||||
|
|
||||||
|
|
||||||
def pause() -> None:
|
def pause() -> None:
|
||||||
|
if _disable_input:
|
||||||
|
return
|
||||||
|
|
||||||
_input("\n\033[2m\033[3m(Ctrl+C to exit, enter to continue)\033[0m")
|
_input("\n\033[2m\033[3m(Ctrl+C to exit, enter to continue)\033[0m")
|
||||||
print("\033[1A\r\033[2K\033[1A\r\033[2K", end="") # Clear pause prompt
|
print("\033[1A\r\033[2K\033[1A\r\033[2K", end="") # Clear pause prompt
|
||||||
|
|||||||
Reference in New Issue
Block a user