Add PromptMenuAction, add cancel button to SelectionAction, make get_command async, add Action validation and defauilt nargs to None.
This commit is contained in:
@ -6,7 +6,7 @@ from falyx.action import ActionFactoryAction, ChainedAction, HTTPAction, Selecti
|
||||
# Selection of a post ID to fetch (just an example set)
|
||||
post_selector = SelectionAction(
|
||||
name="Pick Post ID",
|
||||
selections=["1", "2", "3", "4", "5"],
|
||||
selections=["15", "25", "35", "45", "55"],
|
||||
title="Choose a Post ID to submit",
|
||||
prompt_message="Post ID > ",
|
||||
show_table=True,
|
||||
@ -14,7 +14,7 @@ post_selector = SelectionAction(
|
||||
|
||||
|
||||
# Factory that builds and executes the actual HTTP POST request
|
||||
def build_post_action(post_id) -> HTTPAction:
|
||||
async def build_post_action(post_id) -> HTTPAction:
|
||||
print(f"Building HTTPAction for Post ID: {post_id}")
|
||||
return HTTPAction(
|
||||
name=f"POST to /posts (id={post_id})",
|
||||
|
@ -2,8 +2,16 @@ import asyncio
|
||||
import time
|
||||
|
||||
from falyx import Falyx
|
||||
from falyx.action import Action, ActionGroup, ChainedAction, MenuAction, ProcessAction
|
||||
from falyx.action import (
|
||||
Action,
|
||||
ActionGroup,
|
||||
ChainedAction,
|
||||
MenuAction,
|
||||
ProcessAction,
|
||||
PromptMenuAction,
|
||||
)
|
||||
from falyx.menu import MenuOption, MenuOptionMap
|
||||
from falyx.themes import OneColors
|
||||
|
||||
|
||||
# Basic coroutine for Action
|
||||
@ -77,20 +85,28 @@ parallel = ActionGroup(
|
||||
|
||||
process = ProcessAction(name="compute", action=heavy_computation)
|
||||
|
||||
menu_options = MenuOptionMap(
|
||||
{
|
||||
"A": MenuOption("Run basic Action", basic_action, style=OneColors.LIGHT_YELLOW),
|
||||
"C": MenuOption("Run ChainedAction", chained, style=OneColors.MAGENTA),
|
||||
"P": MenuOption("Run ActionGroup (parallel)", parallel, style=OneColors.CYAN),
|
||||
"H": MenuOption("Run ProcessAction (heavy task)", process, style=OneColors.GREEN),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
# Menu setup
|
||||
|
||||
menu = MenuAction(
|
||||
name="main-menu",
|
||||
title="Choose a task to run",
|
||||
menu_options=MenuOptionMap(
|
||||
{
|
||||
"1": MenuOption("Run basic Action", basic_action),
|
||||
"2": MenuOption("Run ChainedAction", chained),
|
||||
"3": MenuOption("Run ActionGroup (parallel)", parallel),
|
||||
"4": MenuOption("Run ProcessAction (heavy task)", process),
|
||||
}
|
||||
),
|
||||
menu_options=menu_options,
|
||||
)
|
||||
|
||||
|
||||
prompt_menu = PromptMenuAction(
|
||||
name="select-user",
|
||||
menu_options=menu_options,
|
||||
)
|
||||
|
||||
flx = Falyx(
|
||||
@ -108,6 +124,13 @@ flx.add_command(
|
||||
logging_hooks=True,
|
||||
)
|
||||
|
||||
flx.add_command(
|
||||
key="P",
|
||||
description="Show Prompt Menu",
|
||||
action=prompt_menu,
|
||||
logging_hooks=True,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(flx.run())
|
||||
|
@ -2,6 +2,7 @@ import asyncio
|
||||
|
||||
from falyx.action import SelectionAction
|
||||
from falyx.selection import SelectionOption
|
||||
from falyx.signals import CancelSignal
|
||||
|
||||
selections = {
|
||||
"1": SelectionOption(
|
||||
@ -23,4 +24,7 @@ select = SelectionAction(
|
||||
show_table=True,
|
||||
)
|
||||
|
||||
print(asyncio.run(select()))
|
||||
try:
|
||||
print(asyncio.run(select()))
|
||||
except CancelSignal:
|
||||
print("Selection was cancelled.")
|
||||
|
Reference in New Issue
Block a user