console

package
v1.18.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: MIT Imports: 13 Imported by: 0

README

console Package

The console package provides utilities for enhanced console input/output in Go applications. It offers colored output, user prompts, and string formatting helpers to improve CLI user experience.

Features

  • Colored printing and formatting using fatih/color
  • Customizable color types for standard and prompt outputs
  • User input prompts for strings, integers, booleans, URLs, and passwords (with hidden input)
  • String padding and tabulated printing helpers
  • Error handling with custom error codes

Main Types & Functions

Color Output
  • SetColor(col colorType, value ...int): Set color attributes for a color type.
  • ColorPrint / ColorPrompt: Predefined color types for standard and prompt outputs.
  • Print, Println, Printf, Sprintf: Print text with or without color.
  • BuffPrintf: Print formatted text to an io.Writer with color support.
User Prompts
  • PromptString(text string) (string, error): Prompt user for a string.
  • PromptInt(text string) (int64, error): Prompt user for an integer.
  • PromptBool(text string) (bool, error): Prompt user for a boolean.
  • PromptUrl(text string) (*url.URL, error): Prompt user for a URL.
  • PromptPassword(text string) (string, error): Prompt user for a password (input hidden).
String Formatting Helpers
  • PadLeft/PadRight/PadCenter(str string, len int, pad string) string: Pad a string to a given length.
  • PrintTabf(tablLevel int, format string, args ...interface{}): Print formatted text with indentation.
Error Handling
  • Custom error codes for parameter validation and I/O errors.
  • Errors are wrapped using the github.com/nabbar/golib/errors package.

Example Usage

import "github.com/nabbar/golib/console"

func main() {
    // Set prompt color to green
    console.SetColor(console.ColorPrompt, 32)
    name, _ := console.PromptString("Enter your name")
    console.ColorPrint.Printf("Hello, %s!\n", name)

    // Print padded and colored output
    padded := console.PadCenter("Welcome", 20, "-")
    console.ColorPrint.Println(padded)
}

Notes

  • Colors can be customized using ANSI attributes.
  • Password prompts use hidden input (no echo).
  • All prompt functions return errors; always check them in production code.
  • Deprecated functions are marked and will be removed in future versions.

Documentation

Index

Constants

View Source
const (
	ColorPrint colorType = iota
	ColorPrompt
)
View Source
const (
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgConsole
	ErrorColorIOFprintf
	ErrorColorBufWrite
	ErrorColorBufUndefined
)

Variables

This section is empty.

Functions

func GetColorType

func GetColorType(colId uint8) colorType

func PadCenter added in v1.10.4

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

func PadLeft

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

func PadRight

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

func PrintTab

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

@TODO : remove function deprecated: replaced by PrintTabf nolint: goprintffuncname

func PrintTabf

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

func PromptBool

func PromptBool(text string) (bool, error)

func PromptInt

func PromptInt(text string) (int64, error)

func PromptPassword

func PromptPassword(text string) (string, error)

func PromptString

func PromptString(text string) (string, error)

func PromptUrl

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

func SetColor

func SetColor(col colorType, value ...int)

Types

This section is empty.

Jump to

Keyboard shortcuts

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