output

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 5 Imported by: 0

README

Output Package

The output package provides a consistent way to handle user feedback, error messages, and progress indicators in the ccmd CLI tool.

Features

  • Color-coded output: Success (green), Error (red), Warning (yellow), Info (blue)
  • User-friendly error messages: Wrap technical errors with user-friendly explanations
  • Progress indicators: Spinner for indeterminate progress, progress bar for measurable operations
  • Debug output: Conditional debug messages controlled by environment variable

Usage

Basic Output
import "github.com/gifflet/ccmd/internal/output"

// Print different types of messages
output.PrintSuccess("Operation completed successfully")
output.PrintError("Operation failed")
output.PrintWarning("This might cause issues")
output.PrintInfo("Processing files...")

// Fatal error (exits with code 1)
output.Fatal("Critical error: %s", err)
User-Friendly Errors
// Create user-friendly errors
err := output.NewUserError("Failed to load configuration", originalErr)
output.PrintUserError(err)

// Wrap existing errors
wrappedErr := output.WrapError(err, "Failed to initialize application")

// Check if error is user-friendly
if output.IsUserError(err) {
    output.PrintUserError(err)
} else {
    output.PrintError("Unexpected error: %v", err)
}
Progress Indicators
Spinner (for indeterminate progress)
spinner := output.NewSpinner("Processing...")
spinner.Start()

// Do work...

spinner.Success("Processing complete")
// or
spinner.Error("Processing failed")
Progress Bar (for measurable progress)
items := []string{"file1", "file2", "file3"}
progress := output.NewProgressBar(len(items), "Processing files")

for _, item := range items {
    // Process item
    progress.Increment()
}

progress.Complete()
Interactive Input
// Get user input
name := output.Prompt("Enter your name")
Debug Output
// Set CCMD_DEBUG=1 to enable debug output
output.Debug("Detailed information: %v", details)

Environment Variables

  • CCMD_DEBUG=1: Enable debug output

Testing

Run tests with:

go test ./internal/output/...

Documentation

Overview

Package output provides colored output utilities for command-line interface.

Package output provides colored output utilities for command-line interface.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Success = color.New(color.FgGreen).SprintFunc()
	Error   = color.New(color.FgRed).SprintFunc()
	Warning = color.New(color.FgYellow).SprintFunc()
	Info    = color.New(color.FgBlue).SprintFunc()
	Bold    = color.New(color.Bold).SprintFunc()
)

Color functions for different message types

Functions

func Debugf

func Debugf(format string, a ...interface{})

Debugf prints a debug message if debug mode is enabled.

func Fatalf

func Fatalf(format string, a ...interface{})

Fatalf prints an error message and exits with code 1.

func IsUserError

func IsUserError(err error) bool

IsUserError checks if an error is a UserError

func PrintErrorf

func PrintErrorf(format string, a ...interface{})

PrintErrorf prints a formatted error message.

func PrintInfof

func PrintInfof(format string, a ...interface{})

PrintInfof prints a formatted info message.

func PrintSuccessf

func PrintSuccessf(format string, a ...interface{})

PrintSuccessf prints a formatted success message.

func PrintUserError

func PrintUserError(err error)

PrintUserError prints a UserError in a friendly format

func PrintWarningf

func PrintWarningf(format string, a ...interface{})

PrintWarningf prints a formatted warning message.

func Printf

func Printf(format string, a ...interface{})

Printf prints a formatted message.

Example
package main

import (
	"github.com/gifflet/ccmd/internal/output"
)

func main() {
	// Basic output examples
	output.Printf("Regular message")
	output.PrintInfof("Information message")
	output.PrintSuccessf("Operation completed successfully")
	output.PrintWarningf("This is a warning")
	output.PrintErrorf("Something went wrong")
}

func Prompt

func Prompt(prompt string) string

Prompt asks the user for input with a colored prompt

func WrapError

func WrapError(err error, message string) error

WrapError wraps an error with a user-friendly message

Types

type ProgressBar

type ProgressBar struct {
	// contains filtered or unexported fields
}

ProgressBar provides a simple progress indicator

Example
package main

import (
	"time"

	"github.com/gifflet/ccmd/internal/output"
)

func main() {
	// Using progress bar for measurable operations
	items := []string{"file1.txt", "file2.txt", "file3.txt"}
	progress := output.NewProgressBar(len(items), "Processing files")

	for _, item := range items {
		// Process item
		_ = item
		time.Sleep(500 * time.Millisecond)
		progress.Increment()
	}

	progress.Complete()
}

func NewProgressBar

func NewProgressBar(total int, message string) *ProgressBar

NewProgressBar creates a new progress bar

func (*ProgressBar) Complete

func (p *ProgressBar) Complete()

Complete marks the progress as complete

func (*ProgressBar) Increment

func (p *ProgressBar) Increment()

Increment increments the progress by 1

func (*ProgressBar) Update

func (p *ProgressBar) Update(current int)

Update updates the progress bar with the current value

type Spinner

type Spinner struct {
	// contains filtered or unexported fields
}

Spinner provides a simple loading indicator

Example
package main

import (
	"time"

	"github.com/gifflet/ccmd/internal/output"
)

func main() {
	// Using spinner for long operations
	spinner := output.NewSpinner("Processing files...")
	spinner.Start()

	// Simulate work
	time.Sleep(2 * time.Second)

	spinner.Success("Files processed successfully")

	// Or show error
	spinner2 := output.NewSpinner("Downloading...")
	spinner2.Start()
	time.Sleep(1 * time.Second)
	spinner2.Error("Download failed")
}

func NewSpinner

func NewSpinner(message string) *Spinner

NewSpinner creates a new spinner with the given message

func (*Spinner) Error

func (s *Spinner) Error(message string)

Error stops the spinner and shows an error message

func (*Spinner) Start

func (s *Spinner) Start()

Start begins the spinner animation

func (*Spinner) Stop

func (s *Spinner) Stop()

Stop stops the spinner and clears the line

func (*Spinner) Success

func (s *Spinner) Success(message string)

Success stops the spinner and shows a success message

type UserError

type UserError struct {
	Message string
	Details string
	Err     error
}

UserError represents an error that should be displayed to the user with a friendly message

Example
package main

import (
	"errors"

	"github.com/gifflet/ccmd/internal/output"
)

func main() {
	// Creating user-friendly errors
	err := output.NewUserError("Failed to load configuration", errors.New("file not found"))
	output.PrintUserError(err)

	// Using formatted error
	err2 := output.NewUserErrorf("Invalid command: %s", "unknown-cmd")
	output.PrintUserError(err2)
}

func NewUserError

func NewUserError(message string, err error) *UserError

NewUserError creates a new user-friendly error

func NewUserErrorf

func NewUserErrorf(format string, a ...interface{}) *UserError

NewUserErrorf creates a new user-friendly error with formatted message

func (*UserError) Error

func (e *UserError) Error() string

func (*UserError) Unwrap

func (e *UserError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL