console

package
v1.19.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: MIT Imports: 14 Imported by: 0

README

Console Package

Go Version GoDoc

Production-ready terminal I/O toolkit for Go with colored output, interactive prompts, and UTF-8 text formatting.

AI Disclaimer (EU AI Act Article 50.4): AI assistance was used solely for testing, documentation, and bug resolution under human supervision.


Table of Contents


Overview

The console package provides a comprehensive toolkit for building professional command-line interfaces in Go. It wraps the popular fatih/color library with additional utilities for text formatting, user input, and structured output.

Design Philosophy
  1. Simple & Intuitive: Minimal API surface with clear, predictable behavior
  2. UTF-8 Native: Full support for international characters, emojis, and complex scripts
  3. Type-Safe: Strongly-typed color management and prompt functions
  4. Thread-Safe: Concurrent color operations with atomic storage
  5. Composable: Mix and match features for complex CLI applications
Why Use This Package?
  • Zero boilerplate for colored terminal output
  • Interactive prompts with type validation (string, int, bool, URL, password)
  • UTF-8 text padding that correctly handles multi-byte characters
  • Hierarchical output for structured data display
  • Buffer support for testing and non-terminal output
  • Thread-safe color management for concurrent applications
  • Production-ready error handling with structured errors

Key Features

Feature Description Use Case
Colored Output ANSI color support via fatih/color CLI branding, error highlighting
Color Types Multiple independent color schemes Separate colors for prompts vs output
Thread-Safe Storage Atomic color registry Concurrent CLI applications
Text Padding Left/right/center with UTF-8 support Tables, headers, formatted output
Interactive Prompts Type-safe user input (5 types) Configuration wizards, CLI tools
Password Masking Hidden terminal input Secure credential collection
Hierarchical Output Indented, structured display Configuration trees, nested data
Buffer Writing Colored output to any io.Writer Testing, logging, file output
Error Handling Structured error types Robust error management


Architecture

Package Structure
console/
├── interface.go    # ColorType definition and global functions
├── model.go        # ColorType methods (Print, Printf, etc.)
├── buff.go         # Buffer writing utilities
├── padding.go      # UTF-8 text padding functions
├── prompt.go       # Interactive user prompts
└── error.go        # Error definitions
Component Architecture
┌─────────────────────────────────────────────────────────┐
│                   Console Package                       │
│                                                          │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐ │
│  │    Color     │  │    Text      │  │    User      │ │
│  │  Management  │  │  Formatting  │  │   Prompts    │ │
│  └──────────────┘  └──────────────┘  └──────────────┘ │
│         │                  │                  │          │
│         ▼                  ▼                  ▼          │
│  ┌──────────────────────────────────────────────────┐  │
│  │         Thread-Safe Atomic Storage                │  │
│  │  (ColorType → color.Color mapping)               │  │
│  └──────────────────────────────────────────────────┘  │
│                          │                               │
│                          ▼                               │
│  ┌──────────────────────────────────────────────────┐  │
│  │           fatih/color Library                     │  │
│  │  (ANSI color codes, terminal detection)          │  │
│  └──────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────┘
                          │
                          ▼
                   Terminal / io.Writer
Color Type System
ColorType (uint8)
    │
    ├─ ColorPrint (0)   → Standard output colors
    └─ ColorPrompt (1)  → User prompt colors

Each ColorType maps to a color.Color instance
Stored in thread-safe atomic map

Installation

go get github.com/nabbar/golib/console

Quick Start

package main

import (
    "github.com/fatih/color"
    "github.com/nabbar/golib/console"
)

func main() {
    // Set color for print operations
    console.SetColor(console.ColorPrint, int(color.FgCyan), int(color.Bold))
    
    // Print colored text
    console.ColorPrint.Println("Hello, World!")
    console.ColorPrint.Printf("Formatted: %s = %d\n", "answer", 42)
    
    // Pad and center text
    title := console.PadCenter("My Application", 40, "=")
    console.ColorPrint.Println(title)
    
    // Interactive prompt
    name, _ := console.PromptString("Enter your name")
    console.ColorPrint.Printf("Welcome, %s!\n", name)
}

Core Concepts

Color Management

ColorType Constants

The package provides predefined color types:

  • ColorPrint: For standard output
  • ColorPrompt: For user prompts and interactive input
Setting Colors
import "github.com/fatih/color"

// Set single attribute
console.SetColor(console.ColorPrint, int(color.FgRed))

// Set multiple attributes
console.SetColor(console.ColorPrint, int(color.FgYellow), int(color.Bold), int(color.Underline))

// Set using color.Color object
c := color.New(color.FgGreen, color.BgBlack)
console.ColorPrint.SetColor(c)
Using Colors
// Print methods (direct to stdout)
console.ColorPrint.Print("text")
console.ColorPrint.Println("text with newline")
console.ColorPrint.Printf("formatted %s", "text")
console.ColorPrint.PrintLnf("formatted %s with newline", "text")

// Format to string
formatted := console.ColorPrint.Sprintf("Hello %s", "World")

// Write to buffer
var buf bytes.Buffer
n, err := console.ColorPrint.BuffPrintf(&buf, "Buffered %s", "output")
Managing Colors
// Get current color
c := console.GetColor(console.ColorPrint)

// Convert uint8 to ColorType
ct := console.GetColorType(0) // Returns ColorPrint

// Remove color
console.DelColor(console.ColorPrint)

Text Formatting

String Padding

All padding functions support UTF-8 characters (emojis, Chinese, Arabic, etc.):

// Pad left (right-align)
result := console.PadLeft("text", 10, " ")
// Result: "      text"

// Pad right (left-align)
result := console.PadRight("text", 10, " ")
// Result: "text      "

// Pad center
result := console.PadCenter("text", 10, " ")
// Result: "   text   "

// Custom padding character
result := console.PadLeft("5", 5, "0")
// Result: "00005"

// UTF-8 support
result := console.PadCenter("你好", 10, " ")
// Correctly handles multi-byte characters
Hierarchical Output

Create indented, hierarchical output:

console.PrintTabf(0, "Root Level\n")
console.PrintTabf(1, "Child Level (2 spaces)\n")
console.PrintTabf(2, "Grandchild Level (4 spaces)\n")

// With formatting
console.PrintTabf(1, "Name: %s\n", "Alice")
console.PrintTabf(1, "Age: %d\n", 30)

Performance

Memory Characteristics

The console package maintains minimal memory overhead:

  • Color Storage: ~48 bytes per ColorType (atomic map entry)
  • String Operations: Zero allocations for direct Print operations
  • UTF-8 Padding: O(n) where n = string length in runes
  • Prompt Functions: ~100 bytes per call for bufio scanner
Thread Safety

All color operations are thread-safe through:

  • Atomic Maps: libatm.NewMapTyped for concurrent color access
  • Lock-Free Reads: Multiple goroutines can read colors simultaneously
  • Safe Updates: Color updates are atomic operations
  • No Global Locks: Independent color types don't block each other
Performance Benchmarks
Operation Time Allocations Notes
Print (no color) ~500ns 0 Direct stdout write
Print (with color) ~2µs 1 ANSI escape codes
BuffPrintf ~1.5µs 2 Buffer write + formatting
PadLeft/Right ~800ns 1 Per operation
PadCenter ~1µs 2 Two padding operations
PromptString ~10µs 3-4 Includes user input wait

Benchmarks on AMD64, Go 1.21

UTF-8 Performance
  • Rune Counting: O(n) scan using utf8.DecodeRuneInString
  • Multi-byte Handling: Correctly measures visual width
  • CJK Characters: Proper handling of wide characters (2 cells)
  • Emoji Support: Full support for multi-codepoint emojis

Use Cases

This package is designed for command-line interfaces requiring enhanced output and interactivity:

CLI Applications

  • Interactive configuration wizards
  • Database migration tools
  • Deployment automation scripts
  • Development task runners

DevOps Tools

  • Status dashboards in terminal
  • Log viewers with colored severity levels
  • CI/CD pipeline output formatting
  • Server health check displays

Data Display

  • Tabular data presentation
  • Tree-structured configuration
  • Progress indicators and spinners
  • Report generation

User Interaction

  • Setup and initialization wizards
  • Credential collection (with password masking)
  • Feature flag configuration
  • Environment setup confirmation

Testing & Development

  • Test output formatting
  • Debug information display
  • Build process visualization
  • Mock data generation for CLI

User Prompts

String Input
name, err := console.PromptString("Enter your name")
if err != nil {
    log.Fatal(err)
}
console.ColorPrint.Printf("Hello, %s!\n", name)
Integer Input
age, err := console.PromptInt("Enter your age")
if err != nil {
    log.Fatal(err)
}
console.ColorPrint.Printf("You are %d years old\n", age)
Boolean Input
// Accepts: true, false, 1, 0, t, f, T, F, TRUE, FALSE, True, False
confirm, err := console.PromptBool("Do you want to continue? (true/false)")
if err != nil {
    log.Fatal(err)
}
if confirm {
    console.ColorPrint.Println("Proceeding...")
}
URL Input
endpoint, err := console.PromptUrl("Enter API endpoint")
if err != nil {
    log.Fatal(err)
}
console.ColorPrint.Printf("Connecting to: %s\n", endpoint.String())
Password Input
// Input is hidden (no echo to terminal)
password, err := console.PromptPassword("Enter password")
if err != nil {
    log.Fatal(err)
}
// Use password securely...

Advanced Usage

Creating Formatted Tables
import "bytes"

var buf bytes.Buffer

// Set colors
console.SetColor(console.ColorPrint, int(color.FgCyan))

// Create header
header1 := console.PadRight("Name", 20, " ")
header2 := console.PadRight("Age", 10, " ")
header3 := console.PadRight("City", 15, " ")
console.ColorPrint.BuffPrintf(&buf, "%s%s%s\n", header1, header2, header3)

// Add separator
console.ColorPrint.BuffPrintf(&buf, "%s\n", strings.Repeat("-", 45))

// Add data rows
row1 := console.PadRight("Alice", 20, " ")
row2 := console.PadRight("30", 10, " ")
row3 := console.PadRight("NYC", 15, " ")
console.ColorPrint.BuffPrintf(&buf, "%s%s%s\n", row1, row2, row3)

fmt.Print(buf.String())
Progress Indicators
for i := 0; i <= 100; i += 10 {
    filled := i / 5
    empty := 20 - filled
    bar := strings.Repeat("█", filled) + strings.Repeat("░", empty)
    
    console.ColorPrint.Printf("\rProgress: [%s] %3d%%", bar, i)
    time.Sleep(100 * time.Millisecond)
}
console.ColorPrint.Println("\nComplete!")
Multi-Language Support
// Properly handles UTF-8, including emojis and non-Latin scripts
texts := map[string]string{
    "English":  "Hello World",
    "Chinese":  "你好世界",
    "Japanese": "こんにちは世界",
    "Emoji":    "Hello 🌍 World 🌎",
}

for lang, text := range texts {
    label := console.PadRight(lang, 12, " ")
    console.ColorPrint.Printf("%s: %s\n", label, text)
}
Configuration Display
console.PrintTabf(0, "Application Configuration\n")
console.PrintTabf(0, "%s\n", strings.Repeat("=", 50))
console.PrintTabf(0, "\n")

console.PrintTabf(0, "Server:\n")
console.PrintTabf(1, "Host: %s\n", "0.0.0.0")
console.PrintTabf(1, "Port: %d\n", 8080)
console.PrintTabf(1, "TLS:\n")
console.PrintTabf(2, "Enabled: %t\n", true)
console.PrintTabf(2, "CertFile: %s\n", "/etc/ssl/cert.pem")

console.PrintTabf(0, "\nDatabase:\n")
console.PrintTabf(1, "Driver: %s\n", "postgres")
console.PrintTabf(1, "MaxConnections: %d\n", 100)

Error Handling

Error Codes

The package defines the following error codes:

  • ErrorParamEmpty: Given parameters are empty
  • ErrorColorIOFprintf: Cannot write to IO
  • ErrorColorBufWrite: Cannot write to buffer
  • ErrorColorBufUndefined: Buffer is not defined (nil)
Handling Errors
n, err := console.ColorPrint.BuffPrintf(nil, "test")
if err != nil {
    // Check specific error
    if err.Error() == console.ErrorColorBufUndefined.Error().Error() {
        log.Println("Buffer was nil")
    }
    log.Fatal(err)
}

API Reference

Types
ColorType
type ColorType uint8

const (
    ColorPrint  ColorType = iota  // For standard output
    ColorPrompt                    // For user prompts
)
Functions
Color Management
// Get ColorType from uint8
func GetColorType(id uint8) ColorType

// Set color attributes for a ColorType
func SetColor(id ColorType, value ...int)

// Get color object for a ColorType
func GetColor(id ColorType) *color.Color

// Remove color for a ColorType
func DelColor(id ColorType)
ColorType Methods
// Set color using color.Color object
func (c ColorType) SetColor(col *color.Color)

// Print without newline
func (c ColorType) Print(text string)

// Print with newline
func (c ColorType) Println(text string)

// Print with formatting
func (c ColorType) Printf(format string, args ...interface{})

// Print with formatting and newline
func (c ColorType) PrintLnf(format string, args ...interface{})

// Format to string
func (c ColorType) Sprintf(format string, args ...interface{}) string

// Write formatted output to io.Writer
func (c ColorType) BuffPrintf(buff io.Writer, format string, args ...interface{}) (int, error)
Text Formatting
// Pad string on the left
func PadLeft(str string, len int, pad string) string

// Pad string on the right
func PadRight(str string, len int, pad string) string

// Center string with padding
func PadCenter(str string, len int, pad string) string

// Print with indentation levels (each level = 2 spaces)
func PrintTabf(tablLevel int, format string, args ...interface{})
User Prompts
// Prompt for string input
func PromptString(text string) (string, error)

// Prompt for integer input (base 10, 64-bit)
func PromptInt(text string) (int64, error)

// Prompt for boolean input
func PromptBool(text string) (bool, error)

// Prompt for URL input
func PromptUrl(text string) (*url.URL, error)

// Prompt for password (hidden input)
func PromptPassword(text string) (string, error)

Best Practices

  1. Always check errors from prompt functions in production code
  2. Use appropriate ColorTypes for different output purposes
  3. Clean up colors with DelColor() when changing application state
  4. Test with UTF-8 if your application supports internationalization
  5. Use BuffPrintf when you need to capture output or write to files
  6. Validate user input after using prompt functions

Examples

Complete CLI Application
package main

import (
    "fmt"
    "log"
    
    "github.com/fatih/color"
    "github.com/nabbar/golib/console"
)

func main() {
    // Setup colors
    console.SetColor(console.ColorPrint, int(color.FgCyan), int(color.Bold))
    console.SetColor(console.ColorPrompt, int(color.FgYellow))
    
    // Display title
    title := console.PadCenter("User Registration", 50, "=")
    console.ColorPrint.Println(title)
    console.ColorPrint.Println()
    
    // Collect user input
    name, err := console.PromptString("Enter your name")
    if err != nil {
        log.Fatal(err)
    }
    
    age, err := console.PromptInt("Enter your age")
    if err != nil {
        log.Fatal(err)
    }
    
    email, err := console.PromptString("Enter your email")
    if err != nil {
        log.Fatal(err)
    }
    
    // Display summary
    console.ColorPrint.Println()
    console.ColorPrint.Println("Registration Summary:")
    console.ColorPrint.Println(console.PadRight("", 50, "-"))
    
    console.PrintTabf(1, "Name:  %s\n", name)
    console.PrintTabf(1, "Age:   %d\n", age)
    console.PrintTabf(1, "Email: %s\n", email)
    
    // Confirm
    confirm, err := console.PromptBool("Confirm registration? (true/false)")
    if err != nil {
        log.Fatal(err)
    }
    
    if confirm {
        console.ColorPrint.Println("✓ Registration successful!")
    } else {
        console.ColorPrint.Println("✗ Registration cancelled")
    }
}

Testing

Comprehensive testing documentation is available in TESTING.md.

Quick Test:

cd console
go test -v -cover

With Race Detection:

CGO_ENABLED=1 go test -race -v

Test Metrics:

  • 70+ test specifications
  • 60.9% code coverage (prompt functions require interactive input)
  • Ginkgo v2 + Gomega framework
  • Full UTF-8 edge case coverage

Contributing

Contributions are welcome! Please follow these guidelines:

Code Contributions

  • Do not use AI to generate package implementation code
  • AI may assist with tests, documentation, and bug fixing
  • All contributions must pass existing tests
  • Maintain or improve test coverage
  • Follow existing code style and patterns

Documentation

  • Update README.md for new features
  • Add examples for common use cases
  • Keep TESTING.md synchronized with test changes
  • Document all public APIs with GoDoc comments

Testing

  • Write tests for all new features
  • Test edge cases and error conditions
  • Include UTF-8 test cases for text operations
  • Add comments explaining complex test scenarios

Pull Requests

  • Provide clear description of changes
  • Reference related issues
  • Include test results
  • Update documentation

See CONTRIBUTING.md for detailed guidelines.


Future Enhancements

Potential improvements for future versions:

Enhanced Prompts

  • Multi-select lists with arrow key navigation
  • Autocomplete for string inputs
  • Input validation with custom validators
  • Default value display and handling
  • Prompt history (arrow up/down)

Color Management

  • Color themes (light/dark/custom)
  • Automatic color scheme detection from terminal
  • 256-color and true-color support
  • Gradient text rendering
  • RGB color specification

Text Formatting

  • Table builder with column alignment
  • Box drawing characters for borders
  • Markdown-like formatting (bold, italic, underline)
  • Progress bar component
  • Spinner/loading animations

Advanced Features

  • Terminal size detection and adaptive formatting
  • Pager integration for long output
  • Mouse interaction support
  • Split-screen output
  • Status bar component

Performance

  • Lazy color initialization
  • Output batching for high-throughput scenarios
  • Zero-allocation mode for performance-critical paths
  • Custom buffer pool for BuffPrintf

Suggestions and contributions are welcome via GitHub issues.


External Libraries
  • logger - Structured logging (may use console for output)
  • atomic - Atomic map used for color storage
  • shell - Shell command integration
Standards

License

MIT License - See LICENSE file for details.

Copyright (c) 2020 Nicolas JUHEL


Resources


This package is part of the golib project.

Documentation

Overview

Package console provides enhanced terminal I/O utilities for Go applications. It offers colored output, interactive prompts, text formatting, and UTF-8 text padding.

The package wraps fatih/color for ANSI color support and adds:

  • Thread-safe color management with multiple color types
  • UTF-8 aware text padding (left, right, center)
  • Interactive user prompts with type validation
  • Hierarchical output with indentation
  • Buffer writing for testing and non-terminal output

Example usage:

import (
    "github.com/fatih/color"
    "github.com/nabbar/golib/console"
)

// Set colors
console.SetColor(console.ColorPrint, int(color.FgCyan), int(color.Bold))

// Print colored text
console.ColorPrint.Println("Hello, World!")

// Get user input
name, _ := console.PromptString("Enter your name")

// Format text
title := console.PadCenter("My App", 40, "=")

Index

Constants

View Source
const (
	// ErrorParamEmpty indicates that required parameters were not provided or are empty.
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgConsole

	// ErrorColorIOFprintf indicates a failure to write formatted output to an io.Writer.
	ErrorColorIOFprintf

	// ErrorColorBufWrite indicates a failure to write data to a buffer.
	ErrorColorBufWrite

	// ErrorColorBufUndefined indicates that a required buffer was nil or not defined.
	// This occurs when BuffPrintf is called with a nil io.Writer.
	ErrorColorBufUndefined
)

Error codes for the console package. These codes are registered with the golib/errors package for structured error handling.

Variables

This section is empty.

Functions

func DelColor added in v1.19.0

func DelColor(id ColorType)

DelColor removes the color configuration for a ColorType. After deletion, the ColorType will use an empty color (no coloring).

Parameters:

  • id: The ColorType to remove

Thread-safe: Can be called concurrently from multiple goroutines.

Example:

console.DelColor(console.ColorPrint)

func GetColor added in v1.19.0

func GetColor(id ColorType) *color.Color

GetColor retrieves the color.Color instance for a ColorType. Returns an empty color.Color if the ColorType hasn't been configured.

Parameters:

  • id: The ColorType to retrieve

Returns:

  • Pointer to color.Color instance (never nil)

Thread-safe: Can be called concurrently from multiple goroutines.

Example:

c := console.GetColor(console.ColorPrint)
c.Println("Colored text")

func PadCenter added in v1.10.4

func PadCenter(str string, len int, pad string) string

PadCenter centers a string with padding on both sides. Uses UTF-8 rune counting to correctly handle multi-byte characters. If padding cannot be distributed evenly, the right side gets one extra pad character.

Parameters:

  • str: The string to center
  • len: The desired total length in runes (not bytes)
  • pad: The padding string (typically " ", "=", or "-")

Returns:

  • Centered string with length 'len' runes

UTF-8 Support: Correctly handles emojis, CJK characters, and multi-byte Unicode.

Example:

PadCenter("text", 10, " ")    // Returns "   text   "
PadCenter("Title", 20, "=")   // Returns "=======Title========"
PadCenter("你好", 10, " ")     // Returns "    你好    " (correctly counts 2 runes)

func PadLeft

func PadLeft(str string, len int, pad string) string

PadLeft pads a string on the left (right-aligns the text). Uses UTF-8 rune counting to correctly handle multi-byte characters.

Parameters:

  • str: The string to pad
  • len: The desired total length in runes (not bytes)
  • pad: The padding string (typically " " or "0")

Returns:

  • Padded string with length 'len' runes

UTF-8 Support: Correctly handles emojis, CJK characters, and multi-byte Unicode.

Example:

PadLeft("text", 10, " ")      // Returns "      text"
PadLeft("5", 5, "0")          // Returns "00005"
PadLeft("你好", 10, " ")       // Returns "        你好" (correctly counts 2 runes)

func PadRight

func PadRight(str string, len int, pad string) string

PadRight pads a string on the right (left-aligns the text). Uses UTF-8 rune counting to correctly handle multi-byte characters.

Parameters:

  • str: The string to pad
  • len: The desired total length in runes (not bytes)
  • pad: The padding string (typically " ")

Returns:

  • Padded string with length 'len' runes

UTF-8 Support: Correctly handles emojis, CJK characters, and multi-byte Unicode.

Example:

PadRight("text", 10, " ")     // Returns "text      "
PadRight("Name", 20, " ")     // Returns "Name                "
PadRight("🌍", 5, " ")         // Returns "🌍    " (correctly counts 1 rune)

func PrintTabf

func PrintTabf(tablLevel int, format string, args ...interface{})

PrintTabf prints formatted text with hierarchical indentation. Each indentation level adds 2 spaces. Uses ColorPrint for output.

Parameters:

  • tablLevel: The indentation level (0 = no indent, 1 = 2 spaces, 2 = 4 spaces, etc.)
  • format: Printf-style format string
  • args: Arguments for format string

Output: Writes directly to stdout with ColorPrint colors.

Use Cases:

  • Configuration display
  • Hierarchical data structures
  • Nested lists
  • Tree output

Example:

console.PrintTabf(0, "Root\n")
console.PrintTabf(1, "Child 1\n")
console.PrintTabf(2, "Grandchild\n")
console.PrintTabf(1, "Child 2\n")

Output:

Root
  Child 1
    Grandchild
  Child 2

func PromptBool

func PromptBool(text string) (bool, error)

PromptBool displays a prompt and reads a boolean value from stdin. Accepts multiple formats for true/false values.

Parameters:

  • text: The prompt message to display (e.g., "Continue? (true/false)")

Returns:

  • bool: The parsed boolean value
  • error: Input error or parse error (invalid boolean format)

Accepted values:

  • True: "true", "TRUE", "True", "t", "T", "1"
  • False: "false", "FALSE", "False", "f", "F", "0"

Example:

confirm, err := console.PromptBool("Do you want to continue? (true/false)")
if err != nil {
    log.Fatal(err)
}
if confirm {
    fmt.Println("Proceeding...")
}

func PromptInt

func PromptInt(text string) (int64, error)

PromptInt displays a prompt and reads an integer from stdin. The input is parsed as a base-10, 64-bit signed integer.

Parameters:

  • text: The prompt message to display (e.g., "Enter your age")

Returns:

  • int64: The parsed integer value
  • error: Input error or parse error (invalid integer format)

Accepts: Any valid base-10 integer (-9223372036854775808 to 9223372036854775807)

Example:

age, err := console.PromptInt("Enter your age")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("You are %d years old\n", age)

func PromptPassword

func PromptPassword(text string) (string, error)

PromptPassword displays a prompt and reads a password from stdin with hidden input. The input is not echoed to the terminal for security. A newline is printed after input to maintain proper terminal formatting.

Parameters:

  • text: The prompt message to display (e.g., "Enter password")

Returns:

  • string: The password entered by the user
  • error: Any error during reading (e.g., terminal not available)

Security Notes:

  • Input is not displayed on screen (no echo)
  • Password remains in memory as a string (consider using []byte and zeroing)
  • Use HTTPS/TLS when transmitting passwords over network

Example:

password, err := console.PromptPassword("Enter password")
if err != nil {
    log.Fatal(err)
}
// Use password securely...
// Consider zeroing the password when done:
// for i := range password { password[i] = 0 }

func PromptString

func PromptString(text string) (string, error)

PromptString displays a prompt and reads a line of text from stdin. The prompt text is displayed using ColorPrompt colors.

Parameters:

  • text: The prompt message to display (e.g., "Enter your name")

Returns:

  • string: The user's input (trimmed of newline)
  • error: Any error during reading (e.g., EOF, I/O error)

Example:

name, err := console.PromptString("Enter your name")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Hello, %s!\n", name)

func PromptUrl

func PromptUrl(text string) (*url.URL, error)

PromptUrl displays a prompt and reads a URL from stdin. The input is parsed and validated as a URL.

Parameters:

  • text: The prompt message to display (e.g., "Enter API endpoint")

Returns:

  • *url.URL: The parsed URL object
  • error: Input error or parse error (invalid URL format)

Accepts: Any valid URL format (http://example.com, https://api.example.com/v1, etc.)

Example:

endpoint, err := console.PromptUrl("Enter API endpoint")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Connecting to: %s\n", endpoint.String())

func SetColor

func SetColor(id ColorType, value ...int)

SetColor configures the color attributes for a ColorType. Multiple attributes can be combined (e.g., color + bold + underline).

Parameters:

  • id: The ColorType to configure
  • value: Variadic list of color attributes (from fatih/color package)

Thread-safe: Can be called concurrently from multiple goroutines.

Example:

// Set single attribute
console.SetColor(console.ColorPrint, int(color.FgRed))

// Set multiple attributes
console.SetColor(console.ColorPrint,
    int(color.FgYellow),
    int(color.Bold),
    int(color.Underline))

Types

type ColorType added in v1.19.0

type ColorType uint8

ColorType represents a color scheme identifier. Each ColorType can have its own color configuration (foreground, background, attributes). This allows different parts of the application to use different color schemes.

const (
	// ColorPrint is the default color type for standard output.
	// Use this for general application output, messages, and formatted text.
	ColorPrint ColorType = iota

	// ColorPrompt is the color type for user prompts and interactive input.
	// Use this for questions, input requests, and interactive elements.
	ColorPrompt
)

func GetColorType

func GetColorType(id uint8) ColorType

GetColorType converts a uint8 to a ColorType. This is useful when loading color types from configuration files or external sources.

Parameters:

  • id: The numeric identifier (0 = ColorPrint, 1 = ColorPrompt, etc.)

Returns:

  • ColorType corresponding to the given id

Example:

ct := console.GetColorType(0) // Returns ColorPrint

func (ColorType) BuffPrintf added in v1.19.0

func (c ColorType) BuffPrintf(buff io.Writer, format string, args ...interface{}) (int, error)

BuffPrintf writes formatted, colored text to an io.Writer. This is useful for:

  • Writing colored output to buffers for testing
  • Writing colored text to files
  • Capturing colored output for processing
  • Non-terminal output that supports ANSI codes

Parameters:

  • buff: The io.Writer to write to (returns error if nil)
  • format: Printf-style format string
  • args: Arguments for format string

Returns:

  • int: Number of bytes written
  • error: ErrorColorBufUndefined if buff is nil, or write error

Example:

var buf bytes.Buffer
n, err := console.ColorPrint.BuffPrintf(&buf, "Hello %s", "World")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Wrote %d bytes: %s\n", n, buf.String())

func (ColorType) Print added in v1.19.0

func (c ColorType) Print(text string)

Print prints the text to stdout with the ColorType's color, without a newline. Output goes directly to os.Stdout.

Parameters:

  • text: The text to print

Example:

console.ColorPrint.Print("Hello")
console.ColorPrint.Print(" World")

func (ColorType) PrintLnf added in v1.19.0

func (c ColorType) PrintLnf(format string, args ...interface{})

PrintLnf prints formatted text to stdout with the ColorType's color, followed by a newline. Equivalent to Println(fmt.Sprintf(format, args...)).

Parameters:

  • format: Printf-style format string
  • args: Arguments for format string

Example:

console.ColorPrint.PrintLnf("Hello %s!", "World")

func (ColorType) Printf added in v1.19.0

func (c ColorType) Printf(format string, args ...interface{})

Printf prints formatted text to stdout with the ColorType's color, without a newline. Equivalent to Print(fmt.Sprintf(format, args...)).

Parameters:

  • format: Printf-style format string
  • args: Arguments for format string

Example:

console.ColorPrint.Printf("Hello %s", "World")

func (ColorType) Println added in v1.19.0

func (c ColorType) Println(text string)

Println prints the text to stdout with the ColorType's color, followed by a newline. Output goes directly to os.Stdout.

Parameters:

  • text: The text to print

Example:

console.ColorPrint.Println("Hello, World!")

func (ColorType) SetColor added in v1.19.0

func (c ColorType) SetColor(col *color.Color)

SetColor sets the color configuration for this ColorType using a color.Color object. This method provides an alternative to the package-level SetColor function.

Parameters:

  • col: The color.Color instance to use (nil resets to no coloring)

Thread-safe: Can be called concurrently.

Example:

c := color.New(color.FgGreen, color.BgBlack)
console.ColorPrint.SetColor(c)

func (ColorType) Sprintf added in v1.19.0

func (c ColorType) Sprintf(format string, args ...interface{}) string

Sprintf formats the string with the ColorType's color and returns it. Does not print to stdout - returns the formatted string with ANSI color codes.

Parameters:

  • format: Printf-style format string
  • args: Arguments for format string

Returns:

  • Formatted string with color codes

Example:

colored := console.ColorPrint.Sprintf("Hello %s", "World")
fmt.Println(colored) // Prints colored text

Jump to

Keyboard shortcuts

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