Skip to main content

Output API

Hexagon provides a powerful output system for displaying information to users. This page documents the output API and how to use it in your custom tools and plugins.

Printer Module

The hexagon.support.output.printer module provides functions for outputting information to the console. The main interface is the log object, which provides methods for different types of output.

from hexagon.support.output.printer import log

# Basic output
log.info("This is an informational message")

# Result output (for command results)
log.result("Command output")

# Error message
log.error("An error occurred")

# Panel for highlighted information
log.panel("Important information")

# Start and finish messages
log.start("Starting operation")
log.finish("Operation complete")

Output Methods

The log object provides several methods for different types of output:

MethodDescription
infoDisplay an informational message
resultDisplay command result output
errorDisplay an error message
panelDisplay a highlighted panel with content
exampleDisplay code examples with syntax highlighting
fileDisplay file contents with syntax highlighting
extraDisplay additional information
startDisplay a start message (typically at CLI start)
finishDisplay a finish message (typically at CLI end)
gapAdd a blank line
statusShow a progress status indicator

Themes

Hexagon supports different themes for output. The theme can be set using the HEXAGON_THEME environment variable or in the CLI configuration.

from hexagon.support.output.printer import log

# Load a theme
log.load_theme("default")

# Available themes: default, disabled, result_only

Internationalization

The printer module also provides the _ function for internationalization:

from hexagon.support.output.printer import _

# Mark a string for translation
message = _("Hello, World!")
print(message)

Examples

Basic Output

from hexagon.support.output.printer import log

def my_function():
log.info("Starting operation...")

# Your logic here

log.success("Operation completed successfully")
return ["Result 1", "Result 2"]

Error Handling

from hexagon.support.output.printer import log

def risky_operation():
try:
# Your risky code here
log.success("Operation successful")
return ["Operation successful"]
except Exception as e:
log.error(f"Error: {str(e)}")
return [f"Error: {str(e)}"] # Return error message as output

Internationalization

from hexagon.support.output.printer import _, log

def greet(name):
greeting = _("Hello, {name}!")
message = greeting.format(name=name)
log.info(message)
return [message]

Best Practices

  • Use Appropriate Methods: Use the appropriate log method for the type of message
  • Be Consistent: Use a consistent style for your output messages
  • Use Internationalization: Mark user-facing strings for translation
  • Keep Messages Clear: Write clear, concise messages
  • Handle Errors Gracefully: Use error messages to provide helpful information when things go wrong