Skip to content

Commit

Permalink
Improve menu
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 22, 2025
1 parent e619825 commit 3b21cdb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
42 changes: 14 additions & 28 deletions src/nanosaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,40 @@
from inquirer.themes import GreenPassion
from jtop import jtop, JtopException

from nanosaur import __version__
from nanosaur.utilities import Params, get_nanosaur_home
from nanosaur import workspace
from nanosaur.workspace import workspaces_info, parser_workspace_menu, create_developer_workspace, create_maintainer_workspace, get_workspaces_path
from nanosaur.robot import parser_robot_menu, robot_start, robot_stop
from nanosaur.simulation import parser_simulation_menu
from nanosaur.swarm import parser_swarm_menu
from nanosaur.prompt_colors import TerminalFormatter
from nanosaur.utilities import RobotList
from nanosaur.utilities import Params, RobotList, package_info


NANOSAUR_INSTALL_OPTIONS = ['Simple', 'Developer', 'Maintainer']

# Define default parameters
DEFAULT_PARAMS = {
'nanosaur_branch': 'nanosaur2',
'nanosaur_raw_github_repo': 'https://raw.githubusercontent.com/rnanosaur/nanosaur',
}
DEFAULT_PARAMS = {}


def info(platform, params: Params, args):
"""Print version information."""
# Print version information
package_info(params, args.verbose)
# Print mode if it exists in params
if 'mode' in params:
if params['mode'] == 'Developer':
mode_string = TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='green', bold=True)
print(f"{mode_string}\n")
print(f"\n{mode_string}")
elif params['mode'] == 'Maintainer':
mode_string = TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='red', bold=True)
print(f"{mode_string}\n")
print(f"\n{mode_string}")
elif params['mode'] == 'Raffo':
mode_string = TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='cyan', bold=True)
print(f"{mode_string}\n")
print(f"\n{mode_string}")

robot_list = RobotList.load(params)
# Print current robot configuration
robot_data = RobotList.get_robot(params)
print()
robot_data.verbose()
# Print other robots if they exist
if len(robot_list.robots) > 1 or args.verbose:
Expand All @@ -76,25 +74,13 @@ def info(platform, params: Params, args):
if 'simulation_tool' in params:
print(f"\n{TerminalFormatter.color_text('Simulation Tool:', bold=True)} {params['simulation_tool']}")
# Print installed workspaces
workspaces = workspace.get_workspaces_path(params)
if workspaces or args.verbose:
print(TerminalFormatter.color_text("\nInstalled Workspaces:", bold=True))
for ws_name, ws_path in workspaces.items():
# Get the workspace path if it exists
print(f" {TerminalFormatter.color_text(ws_name, bold=True)}: {TerminalFormatter.clickable_path(ws_path)}")
workspaces_info(params, args.verbose)
# Print all robot configurations
if args.verbose:
# Print device information
print(TerminalFormatter.color_text("\nPlatform Information:", bold=True))
for key, value in platform.items():
print(f" {key}: {value}")
# Print version information
print(TerminalFormatter.color_text("\nVersion Information:", bold=True))
print(f" {TerminalFormatter.color_text('Nanosaur package:', bold=True)} {__version__}")
print(f" {TerminalFormatter.color_text('Nanosaur version (branch):', bold=True)} {params['nanosaur_branch']}")
print(f" {TerminalFormatter.color_text('Nanosaur home:', bold=True)} {TerminalFormatter.clickable_path(get_nanosaur_home())}")
config_file_path = Params.get_params_file()
print(f" {TerminalFormatter.color_text('Nanosaur config file:', bold=True)} {TerminalFormatter.clickable_path(config_file_path)}")


def install(platform, params: Params, args):
Expand Down Expand Up @@ -127,10 +113,10 @@ def install(platform, params: Params, args):
if install_type == 'Simple':
print(TerminalFormatter.color_text(f"Not implemented yet {device_type}", color='red'))
elif install_type == 'Developer':
if not workspace.create_developer_workspace(platform, params, args):
if not create_developer_workspace(platform, params, args):
return False
elif install_type == 'Maintainer':
if not workspace.create_maintainer_workspace(platform, params, args):
if not create_maintainer_workspace(platform, params, args):
return False
# Set params in maintainer mode
current_mode = params.get('mode', 'Simple')
Expand Down Expand Up @@ -189,9 +175,9 @@ def main():
parser_install.set_defaults(func=install)

# Subcommand: workspace (with a sub-menu for workspace operations)
if workspace.get_workspaces_path(params):
if get_workspaces_path(params):
# Add workspace subcommand
parser_workspace = workspace.parser_workspace_menu(subparsers)
parser_workspace = parser_workspace_menu(subparsers)

# Subcommand: simulation (with a sub-menu for simulation types)
if device_type == 'desktop' and 'mode' in params:
Expand Down
10 changes: 6 additions & 4 deletions src/nanosaur/prompt_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def clickable_text(text, url):
return f"\033]8;;{url}\033\\{text}\033]8;;\033\\"

@staticmethod
def clickable_path(path):
def clickable_link(path):
"""
Create a clickable link in the terminal where the path is the URL and the text.
Detect if the path is a file, folder, or link and format the URL accordingly.
Expand All @@ -104,11 +104,13 @@ def clickable_path(path):
"""
if os.path.isfile(path) or os.path.isdir(path):
url = f"file://{os.path.abspath(path)}"
return TerminalFormatter.clickable_text(path, url)
elif os.path.islink(path):
url = f"file://{os.path.abspath(os.readlink(path))}"
return TerminalFormatter.clickable_text(path, url)
elif path.startswith("http://") or path.startswith("https://"):
return TerminalFormatter.clickable_text(path, path)
else:
url = path # Assume it's a URL if it's neither a file, directory, nor link

return TerminalFormatter.clickable_text(path, url)
return path # Return the same text if it's neither a file, directory, link, nor URL

# EOF
26 changes: 22 additions & 4 deletions src/nanosaur/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import yaml
import pexpect
import getpass
from nanosaur import __version__
from nanosaur.prompt_colors import TerminalFormatter

DEFAULT_ROBOT_CONFIG = {
Expand All @@ -46,6 +47,7 @@

NANOSAUR_CONFIG_FILE_NAME = 'nanosaur.yaml'
NANOSAUR_HOME_NAME = 'nanosaur'
NANOSAUR_WEBSITE_URL = 'https://nanosaur.ai'
NANOSAUR_MAIN_GITHUB_URL = 'https://github.com/rnanosaur/nanosaur.git'
NANOSAUR_MAIN_BRANCH = 'nanosaur2'

Expand Down Expand Up @@ -292,6 +294,20 @@ def items(self):
return self._params_dict.items()


def package_info(params: Params, verbose: bool):
# Print version information
nanosaur_website = TerminalFormatter.clickable_link(NANOSAUR_WEBSITE_URL)
print(f"{TerminalFormatter.color_text('Nanosaur website:', bold=True)} {nanosaur_website}")
nanosaur_home_folder = TerminalFormatter.clickable_link(get_nanosaur_home())
print(f"{TerminalFormatter.color_text('Nanosaur home:', bold=True)} {nanosaur_home_folder}")
if verbose:
print(f"{TerminalFormatter.color_text('Nanosaur package:', bold=True)} {__version__}")
nanosaur_branch = params.get('nanosaur_branch', NANOSAUR_MAIN_BRANCH)
print(f"{TerminalFormatter.color_text('Nanosaur version (branch):', bold=True)} {nanosaur_branch}")
config_file_path = TerminalFormatter.clickable_link(Params.get_params_file())
print(f"{TerminalFormatter.color_text('Nanosaur config file:', bold=True)} {config_file_path}")


def get_nanosaur_raw_github_url(params: Params) -> str:
nanosaur_github_url = params.get('nanosaur_github', NANOSAUR_MAIN_GITHUB_URL)
nanosaur_branch = params.get('nanosaur_branch', NANOSAUR_MAIN_BRANCH)
Expand All @@ -316,10 +332,12 @@ def create_nanosaur_home() -> str:


def get_nanosaur_home() -> str:
# Get the current user's home directory
nanosaur_home = os.getenv('NANOSAUR_HOME', NANOSAUR_HOME_NAME)
user_home_dir = os.path.expanduser("~")
return os.path.join(user_home_dir, nanosaur_home)
""" Get the nanosaur home directory. """
# Check if the environment variable is set
if 'NANOSAUR_HOME' in os.environ:
return os.environ['NANOSAUR_HOME']
# Get the current nanosaur's home directory
return os.path.join(os.path.expanduser("~"), NANOSAUR_HOME_NAME)


def require_sudo(func):
Expand Down
17 changes: 15 additions & 2 deletions src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,23 @@

DEFAULT_WORKSPACE_PERCEPTION = 'perception_ws'
DEFAULT_WORKSPACE_SIMULATION = 'simulation_ws'
DEFAULT_WORKSPACE_ROBOT = 'ros_ws'
DEFAULT_WORKSPACE_DEVELOPER = 'developer_ws'
DEFAULT_WORKSPACE_ROBOT = 'robot_ws'
DEFAULT_WORKSPACE_DEVELOPER = 'ros_ws'


def workspaces_info(params: Params, verbose: bool):
"""Print information about the workspaces."""
# Print installed workspaces
workspaces = get_workspaces_path(params)
print()
if workspaces:
print(TerminalFormatter.color_text("Installed Workspaces:", bold=True))
for ws_name, ws_path in workspaces.items():
# Get the workspace path if it exists
print(f" {TerminalFormatter.color_text(ws_name, bold=True)}: {TerminalFormatter.clickable_link(ws_path)}")
elif verbose:
print(TerminalFormatter.color_text("No workspaces installed", bold=True))

def parser_workspace_menu(subparsers: argparse._SubParsersAction) -> argparse.ArgumentParser:
parser_workspace = subparsers.add_parser(
'workspace', aliases=["ws"], help="Manage the Nanosaur workspace")
Expand Down

0 comments on commit 3b21cdb

Please sign in to comment.