Skip to content

Commit

Permalink
Improve commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 22, 2025
1 parent ef80c26 commit c0930d0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
50 changes: 28 additions & 22 deletions src/nanosaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,15 @@ def info(platform, params: Params, args):
"""Print version information."""
# Print mode if it exists in params
if 'mode' in params:
print(TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='red', bold=True))
print()
if params['mode'] == 'Developer':
mode_string = TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='green', bold=True)
print(f"{mode_string}\n")
elif params['mode'] == 'Maintainer':
mode_string = TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='red', bold=True)
print(f"{mode_string}\n")
elif params['mode'] == 'Raffo':
mode_string = TerminalFormatter.color_text(f"Mode: {params['mode']}", bg_color='cyan', bold=True)
print(f"{mode_string}\n")

robot_list = RobotList.load(params)
# Print current robot configuration
Expand All @@ -78,13 +85,11 @@ def info(platform, params: Params, args):

# Print all workspaces installed
print(TerminalFormatter.color_text("\nInstalled Workspaces:", bold=True))
for ws_name in ['ws_perception_name', 'ws_robot_name', 'ws_simulation_name']:
for ws_name in ['ws_developer_name', 'ws_perception_name', 'ws_robot_name', 'ws_simulation_name']:
# Get the workspace path if it exists
if ws_path := workspace.get_workspace_path(
params, params.get(ws_name)
):
if ws_path := workspace.get_workspace_path(params, params.get(ws_name)):
ws_name_split = ws_name.split('_')[1] # Split and get the middle part
print(f" {TerminalFormatter.color_text(ws_name_split, bold=True)}: {ws_path}")
print(f" {TerminalFormatter.color_text(ws_name_split, bold=True)}: {TerminalFormatter.clickable_path(ws_path)}")

# Print all robot configurations
if args.verbose:
Expand All @@ -96,19 +101,12 @@ def info(platform, params: Params, args):
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)} {get_nanosaur_home(params['nanosaur_home'])}")
print(f" {TerminalFormatter.color_text('Nanosaur home:', bold=True)} {TerminalFormatter.clickable_path(get_nanosaur_home(params['nanosaur_home']))}")
config_file_path = Params.get_params_file(params['nanosaur_home'], NANOSAUR_CONFIG_FILE_NAME)
print(f" {TerminalFormatter.color_text('Nanosaur config file:', bold=True)} {config_file_path}")


def install_old(platform, params: Params, args, password=None):
if args.developer:
workspace.create_developer_workspace(platform, params, args)
else:
print(TerminalFormatter.color_text("Not implemented yet", color='red'))
print(f" {TerminalFormatter.color_text('Nanosaur config file:', bold=True)} {TerminalFormatter.clickable_path(config_file_path)}")


def install(platform, params: Params, args, password=None):
def install(platform, params: Params, args):
# Determine the device type
device_type = "robot" if platform['Machine'] == 'jetson' else "desktop"
# Questions to ask the user
Expand All @@ -135,12 +133,20 @@ def install(platform, params: Params, args, password=None):
# Get the selected install type
install_type = answers['choice']
print(f"Installing {install_type} workspace...")
if install_type == 'Developer':
workspace.create_developer_workspace(platform, params, args)
elif install_type == 'Maintainer':
workspace.create_maintainer_workspace(platform, params, args)
elif install_type == 'Simple':
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):
return False
elif install_type == 'Maintainer':
if not workspace.create_maintainer_workspace(platform, params, args):
return False
# Set params in maintainer mode
current_mode = params.get('mode', 'Simple')
if (install_type == 'Developer' and current_mode == 'Simple') or \
(install_type == 'Maintainer' and current_mode in ['Simple', 'Developer']) or \
(install_type == 'Raffo' and current_mode in NANOSAUR_INSTALL_OPTIONS):
params['mode'] = install_type


def main():
Expand Down
31 changes: 31 additions & 0 deletions src/nanosaur/prompt_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os

class TerminalFormatter:
# Define ANSI color codes
Expand Down Expand Up @@ -79,4 +80,34 @@ def color_text(text, color=None, bg_color=None, bold=False, italic=False):

# Return the styled text
return f"{style_prefix}{text}{reset_code}"

@staticmethod
def clickable_text(text, url):
"""
Create a clickable link in the terminal.
:param text: The display text for the link.
:param url: The URL the link points to.
:return: A string formatted as a clickable link.
"""
return f"\033]8;;{url}\033\\{text}\033]8;;\033\\"

@staticmethod
def clickable_path(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.
:param path: The file path to be used as both the display text and the URL.
:return: A string formatted as a clickable link.
"""
if os.path.isfile(path) or os.path.isdir(path):
url = f"file://{os.path.abspath(path)}"
elif os.path.islink(path):
url = f"file://{os.path.abspath(os.readlink(path))}"
else:
url = path # Assume it's a URL if it's neither a file, directory, nor link

return TerminalFormatter.clickable_text(path, url)

# EOF
7 changes: 3 additions & 4 deletions src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,14 @@ def build_workspace(nanosaur_raw_github_repo, branch, workspace_path, rosinstall
return True


def create_developer_workspace(platform, params: Params, args, password=None):
def create_developer_workspace(platform, params: Params, args, password=None) -> bool:
# Get the Nanosaur home folder and branch
nanosaur_home = params['nanosaur_home']
# Create the Nanosaur home folder
nanosaur_home_path = create_nanosaur_home(nanosaur_home)
# Create developer workspace
create_workspace(nanosaur_home_path, params['ws_developer_name'], skip_create_colcon_setting=True)
return True


@require_sudo_password
Expand Down Expand Up @@ -264,9 +265,7 @@ def create_maintainer_workspace(platform, params: Params, args, password=None):

# Make the perception workspace
ws_name_path = create_workspace(nanosaur_home_path, params['ws_perception_name'])
build_workspace(nanosaur_raw_github_repo, branch, ws_name_path, 'perception', password, skip_rosdep=True, skip_build=True)
# Set params in maintainer mode
params['mode'] = 'maintainer'
return build_workspace(nanosaur_raw_github_repo, branch, ws_name_path, 'perception', password, skip_rosdep=True, skip_build=True)


@require_sudo_password
Expand Down

0 comments on commit c0930d0

Please sign in to comment.