Skip to content

Commit e82e441

Browse files
committed
update 1.8.2
1 parent c2167c6 commit e82e441

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
## ... `v1.8.2`
1818
* the client command `xulbux-help` now tells you that there's a newer version of the library available, if you're not using the latest version
19+
* added two new params to `Console.input()`:
20+
- <code>default_val: *Optional[T]* = None</code> the default value to return if the input is empty
21+
- <code>output_type: *T* = str</code> the type (*class*) to convert the input to before returning it
1922

2023
## 20.08.2025 `v1.8.1`
2124
* **fixed a critical bug which caused the package to not install properly and make the whole library not work**

src/xulbux/console.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def _(event):
730730
return input_string
731731

732732
@staticmethod
733-
def input(
733+
def input[T](
734734
prompt: object = "",
735735
start="",
736736
end="",
@@ -742,7 +742,9 @@ def input(
742742
allowed_chars: str = CHARS.ALL, #type: ignore[assignment]
743743
allow_paste: bool = True,
744744
validator: Optional[Callable[[str], Optional[str]]] = None,
745-
) -> str:
745+
default_val: Optional[T] = None,
746+
output_type: type[T] = str, # type: ignore[assignment]
747+
) -> T:
746748
"""Acts like a standard Python `input()` a bunch of cool extra features.\n
747749
------------------------------------------------------------------------------------
748750
- `prompt` -⠀the input prompt
@@ -758,12 +760,15 @@ def input(
758760
- `allow_paste` -⠀whether to allow pasting text into the input or not
759761
- `validator` -⠀a function that takes the input string and returns a string error
760762
message if invalid, or nothing if valid
763+
- `default_val` -⠀the default value to return if the input is empty
764+
- `output_type` -⠀the type (class) to convert the input to before returning it\n
761765
------------------------------------------------------------------------------------
762766
The input prompt can be formatted with special formatting codes. For more detailed
763767
information about formatting codes, see the `format_codes` module documentation."""
764768
result_text = ""
765769
tried_pasting = False
766770
filtered_chars = set()
771+
has_default = default_val is not None
767772

768773
class InputValidator(Validator):
769774

@@ -905,4 +910,16 @@ def _(event: KeyPressEvent) -> None:
905910
FormatCodes.print(start, end="")
906911
session.prompt()
907912
FormatCodes.print(end, end="")
908-
return result_text
913+
914+
if result_text in ("", None):
915+
if has_default: return default_val
916+
result_text = ""
917+
918+
if output_type == str:
919+
return result_text # type: ignore[return-value]
920+
else:
921+
try:
922+
return output_type(result_text) # type: ignore[call-arg]
923+
except (ValueError, TypeError):
924+
if has_default: return default_val
925+
raise

0 commit comments

Comments
 (0)