ui.ui_helper¶
DefaultUIHelper ¶
DefaultUIHelper(
*args,
print_func: Optional[_PrintFunc] = None,
input_func: Optional[_PrintFunc] = None,
**kwargs,
)
Bases: _UiHelperBase
Default implementation of the UI helper for user interaction.
This class provides a concrete implementation of the UI helper interface using standard console input/output for user interactions.
Example
helper = DefaultUIHelper()
# Get user choice from list
options = ["Option A", "Option B", "Option C"]
choice = helper.prompt_pick_from_list(options, "Choose an option:")
# Ask yes/no question
proceed = helper.prompt_yes_no_question("Continue with operation?")
# Get text input
name = helper.prompt_text("Enter your name: ")
# Get numeric input
value = helper.prompt_float("Enter a number: ")
Initializes the UI helper with optional custom print and input functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
print_func
|
Optional[_PrintFunc]
|
Custom function for printing messages |
None
|
input_func
|
Optional[_PrintFunc]
|
Custom function for receiving input |
None
|
Example
# Default UI helper
helper = DefaultUIHelper()
# Custom print/input functions
def custom_print(msg):
logging.info(f"UI: {msg}")
def custom_input(prompt):
return input(f"[CUSTOM] {prompt}")
helper = DefaultUIHelper(
print_func=custom_print,
input_func=custom_input
)
Source code in src/clabe/ui/ui_helper.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
prompt_pick_from_list ¶
prompt_pick_from_list(
value: List[str],
prompt: str,
allow_0_as_none: bool = True,
**kwargs,
) -> Optional[str]
Prompts the user to pick an item from a list.
Displays a numbered list of options and prompts the user to select one by entering the corresponding number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
List[str]
|
The list of items to choose from |
required |
prompt
|
str
|
The prompt message |
required |
allow_0_as_none
|
bool
|
Whether to allow 0 as a choice for None |
True
|
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The selected item or None |
Example
helper = DefaultUIHelper()
files = ["file1.txt", "file2.txt", "file3.txt"]
selected = helper.prompt_pick_from_list(files, "Choose a file:")
# With None option disabled
selected = helper.prompt_pick_from_list(
files, "Must choose a file:", allow_0_as_none=False
)
Source code in src/clabe/ui/ui_helper.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
prompt_yes_no_question ¶
Prompts the user with a yes/no question.
Continues prompting until a valid yes/no response is received.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The question to ask |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True for yes, False for no |
Example
helper = DefaultUIHelper()
if helper.prompt_yes_no_question("Save changes?"):
save_data()
proceed = helper.prompt_yes_no_question("Delete all files?")
if proceed:
delete_files()
Source code in src/clabe/ui/ui_helper.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
prompt_text ¶
Prompts the user for text input.
Simple text input prompt that returns the user's input as a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt message |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The user input |
Example
helper = DefaultUIHelper()
name = helper.prompt_text("Enter your name: ")
description = helper.prompt_text("Enter description: ")
path = helper.prompt_text("Enter file path: ")
Source code in src/clabe/ui/ui_helper.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
|
prompt_float ¶
Prompts the user for a float input.
Continues prompting until a valid float value is entered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt message |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The parsed user input |
Example
helper = DefaultUIHelper()
temperature = helper.prompt_float("Enter temperature: ")
weight = helper.prompt_float("Enter weight in kg: ")
price = helper.prompt_float("Enter price: $")
Source code in src/clabe/ui/ui_helper.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
print ¶
Prints a message using the configured print function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
str
|
The message to print |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The result of the print function |
Example
helper = DefaultUIHelper()
helper.print("Hello, world!")
helper.print("Status: Processing...")
Source code in src/clabe/ui/ui_helper.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
input ¶
Prompts the user for input using the configured input function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt message |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The user input |
Example
helper = DefaultUIHelper()
name = helper.input("Enter your name: ")
password = helper.input("Enter password: ")
Source code in src/clabe/ui/ui_helper.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
prompt_field_from_input ¶
prompt_field_from_input(
model: Type[_TModel],
field_name: str,
default: Optional[_T] = None,
) -> Optional[_T]
Prompts the user to input a value for a specific field in a model.
Uses the model's field information to prompt for input and validates the entered value against the field's type annotation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Type[_TModel]
|
The model containing the field |
required |
field_name
|
str
|
The name of the field |
required |
default
|
Optional[_T]
|
The default value if no input is provided |
None
|
Returns:
Type | Description |
---|---|
Optional[_T]
|
Optional[_T]: The validated input value or the default value |
Example
from pydantic import BaseModel, Field
class UserModel(BaseModel):
name: str = Field(description="User's full name")
age: int = Field(description="User's age in years")
# Prompt for name field
name = prompt_field_from_input(UserModel, "name", "Anonymous")
# Prompt for age field
age = prompt_field_from_input(UserModel, "age", 18)
Source code in src/clabe/ui/ui_helper.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|