Skip to content

Commit

Permalink
Merge pull request #179 from Bishoy-at-pieces/feat-1.6.1
Browse files Browse the repository at this point in the history
feat 1.6.1
  • Loading branch information
bishoy-at-pieces authored Sep 2, 2024
2 parents 50575c1 + 82fc469 commit 597a9f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/pieces/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from .version_command import version
from .signout_command import sign_out
from .config_command import ConfigCommands
from .execute_command import ExecuteCommand

__all__ = ['loop',
'version',
'search',
'change_model',
'sign_out',
'ListCommand',
'ConfigCommands']
'ConfigCommands',
"ExecuteCommand"]

Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
from pieces.settings import Settings
from pieces.assets import check_assets_existence, AssetsCommandsApi
from typing import List, Tuple, Callable
from ..assets import check_assets_existence, AssetsCommandsApi
import subprocess
from enum import Enum
from list_command import PiecesSelectMenu
from pieces.assets.assets_command import AssetsCommands
from .list_command import PiecesSelectMenu
from ..assets.assets_command import AssetsCommands

class AssetClassification(Enum):
SHELL = "sh"
BASH = "bat" # Added BASH classification

class ExecuteCommand:
@classmethod
@check_assets_existence
def execute_command(cls, max_assets: int = 10):
def execute_command(cls, max_assets: int = 10,**kwargs):
if max_assets < 1:
print("Max assets must be greater than 0")
max_assets = 10
Expand All @@ -23,8 +17,8 @@ def execute_command(cls, max_assets: int = 10):
for i, uuid in enumerate(list(assets_snapshot.keys())[:max_assets], start=1):
asset = AssetsCommandsApi.get_asset_snapshot(uuid)
classification = cls.get_asset_classification(asset)
if classification in [AssetClassification.SHELL.value, AssetClassification.BASH.value]:
assets.append((f"{i}: {asset.name}", {"ITEM_INDEX": i, "UUID": uuid, "CLASSIFICATION": classification}))
if classification in ["bat","sh"]:
assets.append((f"{i}: {asset.name}", {"ITEM_INDEX": i, "UUID": uuid, "CLASSIFICATION": classification,"show_warning":False}))

if not assets:
print("No shell or bash assets found")
Expand Down Expand Up @@ -56,15 +50,15 @@ def execute_asset(cls, **kwargs):
@staticmethod
def execute_command_in_subprocess(command: str, classification: str):
try:
if classification == AssetClassification.BASH.value:
if classification == "bat":
result = subprocess.run(['bash', '-c', command], capture_output=True, text=True)
elif classification == AssetClassification.SHELL.value:
elif classification == "sh":
result = subprocess.run(command, shell=True, capture_output=True, text=True)
else:
print(f"Unsupported asset classification: {classification}")
return

print(f"\nExecuting {classification} command:")
print(f"Executing {classification} command:")
print(result.stdout)
if result.stderr:
print("Errors:")
Expand All @@ -74,5 +68,3 @@ def execute_command_in_subprocess(command: str, classification: str):
except Exception as e:
print(f"An error occurred: {e}")

if __name__ == "__main__":
ExecuteCommand.execute_command()
20 changes: 13 additions & 7 deletions src/pieces/commands/list_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
from prompt_toolkit.layout.containers import HSplit, Window
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.styles import Style
from typing import List, Tuple, Callable
from typing import List, Tuple, Callable,Optional
from pieces.assets.assets_command import AssetsCommands
import time
from .change_model import change_model

class PiecesSelectMenu:
def __init__(self, menu_options:List[Tuple],on_enter_callback:Callable):
def __init__(self, menu_options: List[Tuple], on_enter_callback: Callable, footer_text: Optional[str] = None):
self.menu_options = menu_options
self.on_enter_callback = on_enter_callback
self.current_selection = 0
self.footer_text = footer_text

def get_menu_text(self):
result = []
Expand Down Expand Up @@ -51,7 +51,14 @@ def select_option(event):
menu_control = FormattedTextControl(text=self.get_menu_text)
self.menu_window = Window(content=menu_control, always_hide_cursor=True)

layout = Layout(HSplit([self.menu_window]))
layout_items = [self.menu_window]

if self.footer_text:
footer_control = FormattedTextControl(text=self.footer_text)
footer_window = Window(content=footer_control, height=1, always_hide_cursor=True)
layout_items.append(footer_window)

layout = Layout(HSplit(layout_items))

style = Style.from_dict({
'selected': 'reverse',
Expand All @@ -68,6 +75,7 @@ def select_option(event):
elif isinstance(args, dict):
self.on_enter_callback(**args)


class ListCommand:
@classmethod
def list_command(cls, **kwargs):
Expand Down Expand Up @@ -106,9 +114,7 @@ def list_models(cls):
return

models = [(f"{idx}: {model_name}", {"MODEL_INDEX":idx,"show_warning":False}) for idx, model_name in enumerate(Settings.models.keys(), start=1)]
# models.append((f"Currently using: {Settings.model_name} with uuid {Settings.model_id}")) # TODO
# TODO: add a normal print message
select_menu = PiecesSelectMenu(models, change_model)
select_menu = PiecesSelectMenu(models, change_model,f"Currently using: {Settings.model_name} with uuid {Settings.model_id}")
select_menu.run()

@classmethod
Expand Down

0 comments on commit 597a9f5

Please sign in to comment.