ui.ui_helper¶
DefaultUIHelper ¶
DefaultUIHelper(
print_func: _PrintFunc = _DEFAULT_PRINT_FUNC,
input_func: _InputFunc = _DEFAULT_INPUT_FUNC,
)
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 DefaultUIHelper with custom print and input functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
print_func
|
_PrintFunc
|
Custom function for printing messages |
_DEFAULT_PRINT_FUNC
|
input_func
|
_InputFunc
|
Custom function for receiving user input |
_DEFAULT_INPUT_FUNC
|
Source code in src/clabe/ui/ui_helper.py
162 163 164 165 166 167 168 169 170 171 172 173 |
|
print ¶
print(message: str) -> None
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 |
None
|
The result of the print function (usually None) |
Source code in src/clabe/ui/ui_helper.py
175 176 177 178 179 180 181 182 183 184 185 |
|
input ¶
Prompts the user for input using the configured input function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt message to display |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The user input received from the input function |
Source code in src/clabe/ui/ui_helper.py
187 188 189 190 191 192 193 194 195 196 197 |
|
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
199 200 201 202 203 204 205 206 207 208 209 210 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 |
|
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
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
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
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
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
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
|