pardon

package module
v0.0.0-...-2556eee Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 7 Imported by: 0

README

go-pardon

Lightweight interactive CLI prompt library for Go

Import the package

import "github.com/engmtcdrm/pardon"

Usage

Confirm Prompt
continueFlag := true
confirm := pardon.NewConfirm(&continueFlag).
    Title("Are you sure you want to proceed?")

if err := confirm.Ask(); err != nil {
    fmt.Printf("Error: %v\n", err)
}
if continueFlag {
    fmt.Println("Proceeding!")
} else {
    fmt.Println("Stopping!")
}
Password Prompt
password := []byte{}
passwordPrompt := pardon.NewPassword(&password).
    Title("Enter your password:")

if err := passwordPrompt.Ask(); err != nil {
    fmt.Printf("Error: %v\n", err)
}
fmt.Printf("Entered password is '%s'\n", string(password))
Question Prompt
favColor := ""
question := pardon.NewQuestion(&favColor).
    Title("What is your favorite color?")

if err := question.Ask(); err != nil {
    fmt.Printf("Error: %v\n", err)
}
fmt.Printf("Entered favorite color is '%s'\n", favColor)
Select Prompt
package main

import (
    "fmt"
    "github.com/engmtcdrm/pardon"
)

func main() {
    var selectedColor int
    colors := []pardon.Option[int]{
        {Key: "Red", Value: 1},
        {Key: "Blue", Value: 2},
        {Key: "Green", Value: 3},
        {Key: "Yellow", Value: 4},
    }

    selectPrompt := pardon.NewSelect(&selectedColor).
        Title("Choose a color:").
        Options(colors...)

    if err := selectPrompt.Ask(); err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
    fmt.Printf("Selected option: %v\n", selectedColor)
}
Form (Multiple Prompts)
package main

import (
    "fmt"
    "github.com/engmtcdrm/pardon"
)

func main() {
    // Variables to store results
    var name string
    var password []byte
    var age int
    var confirmed bool

    // Create age options
    ageOptions := []pardon.Option[int]{
        {Key: "18-25", Value: 22},
        {Key: "26-35", Value: 30},
        {Key: "36-45", Value: 40},
        {Key: "46+", Value: 50},
    }

    // Create form with multiple prompts
    form := pardon.NewForm(
        pardon.NewQuestion(&name).
            Title("What is your name?"),

        pardon.NewPassword(&password).
            Title("Enter your password:"),

        pardon.NewSelect(&age).
            Title("Select your age group:").
            Options(ageOptions...),

        pardon.NewConfirm(&confirmed).
            Title("Do you agree to the terms?"),
    )

    // Execute all prompts in sequence
    if err := form.Ask(); err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    // Display results
    fmt.Printf("Name: %s\n", name)
    fmt.Printf("Password length: %d characters\n", len(password))
    fmt.Printf("Age group: %d\n", age)
    fmt.Printf("Agreed to terms: %t\n", confirmed)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserAborted     = errors.New("user aborted")
	ErrNoTitle         = errors.New("prompt requires a title")
	ErrNoSelectOptions = errors.New("select prompt requires at least one option")
	ErrNoValue         = errors.New("value must be set")
)
View Source
var Icons = icons{
	Alert:        "[!] ",
	QuestionMark: "[?] ",
	Password:     "🔒 ",
}

Icons contains default icons for different prompt types.

Functions

func SetDefaultAnswerFunc

func SetDefaultAnswerFunc(fn func(string) string)

SetDefaultAnswerFunc sets the global default answer transformation function.

func SetDefaultCursorFunc

func SetDefaultCursorFunc(fn func(string) string)

SetDefaultCursorFunc sets the global default cursor formatting function.

func SetDefaultIconFunc

func SetDefaultIconFunc(fn func(string) string)

SetDefaultIconFunc sets the global default icon transformation function.

func SetDefaultSelectFunc

func SetDefaultSelectFunc(fn func(string) string)

SetDefaultSelectFunc sets the global default selection formatting function.

func SetDefaultTitleFunc

func SetDefaultTitleFunc(fn func(string) string)

SetDefaultTitleFunc sets the global default title transformation function.

Types

type Confirm

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

Confirm represents a yes/no confirmation prompt for user decisions.

func NewConfirm

func NewConfirm(value *bool) *Confirm

NewConfirm creates a new Confirm prompt instance.

func (*Confirm) AnswerFunc

func (c *Confirm) AnswerFunc(fn func(string) string) *Confirm

AnswerFunc sets a function to transform the final answer before returning.

func (*Confirm) Ask

func (c *Confirm) Ask() error

Ask displays the confirmation prompt.

func (*Confirm) Icon

func (c *Confirm) Icon(s string) *Confirm

Icon sets a static icon for the confirmation prompt.

func (*Confirm) IconFunc

func (c *Confirm) IconFunc(fn func(string) string) *Confirm

IconFunc sets a dynamic icon function for the confirmation prompt.

func (*Confirm) Title

func (c *Confirm) Title(title string) *Confirm

Title sets a static title for the confirmation prompt.

func (*Confirm) TitleFunc

func (c *Confirm) TitleFunc(fn func(string) string) *Confirm

TitleFunc sets a dynamic title function for the confirmation prompt.

func (*Confirm) Value

func (c *Confirm) Value(value *bool) *Confirm

Value sets a default value for the confirmation prompt.

type Form

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

Form represents a collection of prompts executed sequentially.

func NewForm

func NewForm(prompts ...Prompt) *Form

NewForm creates a new Form with the given prompts.

func (*Form) Ask

func (f *Form) Ask() error

Ask executes all prompts in sequence, stopping on the first error.

type Option

type Option[T comparable] struct {
	Key   string // Display label
	Value T      // Associated value
}

Option represents a selectable key-value pair for use in selection prompts.

func NewOption

func NewOption[T comparable](key string, value T) Option[T]

NewOption creates a new Option with the given key and value.

type Password

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

Password represents a password input prompt that securely collects sensitive information.

func NewPassword

func NewPassword(value *[]byte) *Password

NewPassword creates a new Password prompt instance.

func (*Password) AnswerFunc

func (p *Password) AnswerFunc(fn func(string) string) *Password

AnswerFunc sets a function to transform the final answer before returning.

func (*Password) Ask

func (p *Password) Ask() error

Ask displays the password prompt.

func (*Password) Icon

func (p *Password) Icon(s string) *Password

Icon sets a static icon for the password prompt.

func (*Password) IconFunc

func (p *Password) IconFunc(fn func(string) string) *Password

IconFunc sets a dynamic icon function for the password prompt.

func (*Password) Title

func (p *Password) Title(title string) *Password

Title sets a static title for the password prompt.

func (*Password) TitleFunc

func (p *Password) TitleFunc(fn func(string) string) *Password

TitleFunc sets a dynamic title function for the password prompt.

func (*Password) Validate

func (p *Password) Validate(fn func([]byte) error) *Password

Validate sets a validation function for the password prompt.

func (*Password) Value

func (p *Password) Value(value *[]byte) *Password

Value sets a default value for the password prompt.

type Prompt

type Prompt interface {
	// Ask displays the prompt and waits for user input.
	Ask() error
}

Prompt is the interface implemented by all prompt types.

type Question

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

Question represents a text input prompt for user questions.

func NewQuestion

func NewQuestion(value *string) *Question

NewQuestion creates a new Question prompt instance.

func (*Question) AnswerFunc

func (q *Question) AnswerFunc(fn func(string) string) *Question

AnswerFunc sets a function to transform the final answer.

func (*Question) Ask

func (q *Question) Ask() error

Ask displays the question prompt and waits for input.

func (*Question) Icon

func (q *Question) Icon(s string) *Question

Icon sets the prompt icon.

func (*Question) IconFunc

func (q *Question) IconFunc(fn func(string) string) *Question

IconFunc sets a dynamic icon function.

func (*Question) Title

func (q *Question) Title(title string) *Question

Title sets the question text.

func (*Question) TitleFunc

func (q *Question) TitleFunc(fn func(string) string) *Question

TitleFunc sets a dynamic title function.

func (*Question) Validate

func (q *Question) Validate(fn func(string) error) *Question

Validate sets input validation.

func (*Question) Value

func (q *Question) Value(value *string) *Question

Value sets a default input value.

type Select

type Select[T comparable] struct {
	// contains filtered or unexported fields
}

Select represents a multiple-choice selection prompt.

func NewSelect

func NewSelect[T comparable](value *T) *Select[T]

NewSelect creates a new Select prompt instance.

func (*Select[T]) AnswerFunc

func (sel *Select[T]) AnswerFunc(fn func(string) string) *Select[T]

AnswerFunc sets a function to format the final answer display.

func (*Select[T]) Ask

func (sel *Select[T]) Ask() error

Ask displays the select prompt and waits for user selection.

func (*Select[T]) Cursor

func (sel *Select[T]) Cursor(cursor string) *Select[T]

Cursor sets the cursor symbol displayed next to the selected option.

func (*Select[T]) CursorFunc

func (sel *Select[T]) CursorFunc(fn func(string) string) *Select[T]

CursorFunc sets a function to dynamically format the cursor symbol.

func (*Select[T]) Icon

func (sel *Select[T]) Icon(icon string) *Select[T]

Icon sets the icon displayed before the prompt title.

func (*Select[T]) IconFunc

func (sel *Select[T]) IconFunc(fn func(string) string) *Select[T]

IconFunc sets a function to dynamically format the prompt icon.

func (*Select[T]) Options

func (sel *Select[T]) Options(options ...Option[T]) *Select[T]

Options sets the list of available options for selection.

func (*Select[T]) SelectFunc

func (sel *Select[T]) SelectFunc(fn func(string) string) *Select[T]

SelectFunc sets a function to format option text during selection.

func (*Select[T]) Title

func (sel *Select[T]) Title(title string) *Select[T]

Title sets the prompt title text that will be displayed to the user.

func (*Select[T]) TitleFunc

func (sel *Select[T]) TitleFunc(fn func(string) string) *Select[T]

TitleFunc sets a function to dynamically format the prompt title.

func (*Select[T]) Value

func (sel *Select[T]) Value(value *T) *Select[T]

Value sets the pointer where the selected option's value will be stored.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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