-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
51 lines (34 loc) · 1014 Bytes
/
state.py
File metadata and controls
51 lines (34 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""State machine and cancel sentinel for prompts."""
from enum import Enum, auto
from typing import Any
class State(Enum):
"""Prompt states."""
INITIAL = auto()
ACTIVE = auto()
SUBMIT = auto()
CANCEL = auto()
ERROR = auto()
class _CancelType:
"""Sentinel for cancelled prompts."""
_instance: "_CancelType | None" = None
def __new__(cls) -> "_CancelType":
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __repr__(self) -> str:
return "CANCEL"
def __bool__(self) -> bool:
return False
CANCEL = _CancelType()
def is_cancel(value: Any) -> bool:
"""Check if a value represents a cancelled prompt.
Args:
value: The value to check.
Returns:
True if the value is the CANCEL sentinel.
Example:
>>> result = text(message="Name?")
>>> if is_cancel(result):
... print("User cancelled")
"""
return value is CANCEL