Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement the DevEnv Settings Window with Textual. #231

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions dem/cli/command/create_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ def open_dev_env_settings_panel(all_tool_images: dict[str, ToolImage]) -> list[s
Returns:
the selected Tool Image names
"""
dev_env_settings_panel = DevEnvSettingsWindow(convert_to_printable_tool_images(all_tool_images))
dev_env_settings_panel.wait_for_user()
dev_env_settings_window = DevEnvSettingsWindow(convert_to_printable_tool_images(all_tool_images))
# This will block the main thread until the window is closed
dev_env_settings_window.run()

if "cancel" in dev_env_settings_panel.cancel_save_menu.get_selection():
if dev_env_settings_window.last_button_pressed is None or \
dev_env_settings_window.last_button_pressed is dev_env_settings_window.cancel_button_id:
raise typer.Abort()

return dev_env_settings_panel.tool_image_menu.get_selected_tool_images()
return dev_env_settings_window.selected_tool_images

def create_new_dev_env_descriptor(dev_env_name: str, selected_tool_images: list[str]) -> dict:
""" Create a new Development Environment descriptor.
Expand Down
81 changes: 28 additions & 53 deletions dem/cli/command/modify_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,9 @@
from dem.core.platform import Platform
from dem.core.exceptions import PlatformError
from dem.cli.console import stderr, stdout
from dem.cli.tui.renderable.menu import SelectMenu
from dem.cli.tui.window.dev_env_settings_window import DevEnvSettingsWindow
from dem.cli.tui.printable_tool_image import PrintableToolImage, convert_to_printable_tool_images

def get_confirm_from_user() -> str:
""" Get the confirmation from the user.

Returns:
the confirmation option
"""
confirm_menu_items = ["confirm", "save as", "cancel"]
select_menu = SelectMenu(confirm_menu_items)
select_menu.set_title("Are you sure to overwrite the Development Environment?")
select_menu.wait_for_user()
return select_menu.get_selected()

def handle_user_confirm(confirmation: str, dev_env_local: DevEnv, platform: Platform) -> None:
""" Handle the user confirmation.

Args:
confirmation -- the confirmation option
dev_env_local -- the local Development Environment
platform -- the platform

Exceptions:
typer.Abort -- when the user tries to save as the Dev Env with a taken name or cancels
the operation
"""
if confirmation == "cancel":
raise typer.Abort()

if confirmation == "save as":
new_dev_env = copy.deepcopy(dev_env_local)
new_dev_env.name = typer.prompt("Name of the new Development Environment")

check_for_new_dev_env = platform.get_dev_env_by_name(new_dev_env.name)

if check_for_new_dev_env is None:
platform.local_dev_envs.append(new_dev_env)
else:
stderr.print("[red]The Development Environment already exist.")
raise typer.Abort()

platform.flush_dev_env_properties()

def get_already_selected_tool_images(dev_env: DevEnv) -> set[str]:
""" Get the already selected Tool Images.

Expand Down Expand Up @@ -92,27 +50,28 @@ def remove_missing_tool_images(all_tool_images: dict[str, ToolImage],
typer.confirm("By continuing, the missing tool images will be removed from the Development Environment.",
abort=True)

def open_dev_env_settings_panel(already_selected_tool_images: list[str],
printable_tool_images: list[PrintableToolImage]) -> list[str]:
def open_dev_env_settings_window(already_selected_tool_images: list[str],
printable_tool_images: list[PrintableToolImage]) -> DevEnvSettingsWindow:
""" Open the Development Environment settings panel.

Args:
already_selected_tool_images -- the already selected Tool Images
printable_tool_images -- the printable Tool Images

Returns:
the selected Tool Images
The closed Development Environment settings window.

Exceptions:
typer.Abort -- if the user cancels the operation
"""
dev_env_settings_panel = DevEnvSettingsWindow(printable_tool_images, already_selected_tool_images)
dev_env_settings_panel.wait_for_user()
dev_env_settings_window = DevEnvSettingsWindow(printable_tool_images, already_selected_tool_images)
dev_env_settings_window.run()

if "cancel" in dev_env_settings_panel.cancel_save_menu.get_selection():
if dev_env_settings_window.last_button_pressed is None or \
dev_env_settings_window.last_button_pressed is dev_env_settings_window.cancel_button_id:
raise typer.Abort()

return dev_env_settings_panel.tool_image_menu.get_selected_tool_images()
return dev_env_settings_window

def update_dev_env(dev_env: DevEnv, selected_tool_images: list[str]) -> None:
""" Update the Development Environment.
Expand Down Expand Up @@ -149,11 +108,27 @@ def modify_with_tui(platform: Platform, dev_env: DevEnv) -> None:
already_selected_tool_images = get_already_selected_tool_images(dev_env)
remove_missing_tool_images(platform.tool_images.all_tool_images, already_selected_tool_images)
printable_tool_images = convert_to_printable_tool_images(platform.tool_images.all_tool_images)
selected_tool_images = open_dev_env_settings_panel(already_selected_tool_images,
dev_env_settings_window = open_dev_env_settings_window(already_selected_tool_images,
printable_tool_images)
update_dev_env(dev_env, selected_tool_images)
confirmation = get_confirm_from_user()
handle_user_confirm(confirmation, dev_env, platform)

if dev_env_settings_window.last_button_pressed == dev_env_settings_window.cancel_button_id:
raise typer.Abort()
elif dev_env_settings_window.last_button_pressed == dev_env_settings_window.confirm_screen_save_as_button_id:
new_dev_env = copy.deepcopy(dev_env)
update_dev_env(new_dev_env, dev_env_settings_window.selected_tool_images)
new_dev_env.name = typer.prompt("Name of the new Development Environment")

check_for_new_dev_env = platform.get_dev_env_by_name(new_dev_env.name)

if check_for_new_dev_env is None:
platform.local_dev_envs.append(new_dev_env)
else:
stderr.print("[red]The Development Environment already exist.")
raise typer.Abort()
elif dev_env_settings_window.last_button_pressed == dev_env_settings_window.confirm_screen_confirm_button_id:
update_dev_env(dev_env, dev_env_settings_window.selected_tool_images)

platform.flush_dev_env_properties()

def execute(platform: Platform, dev_env_name: str) -> None:
""" Modify the Development Environment.
Expand Down
Loading
Loading