prompts

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: MIT Imports: 8 Imported by: 0

README

Prompts

Build command line prompts with ease, prompts provides several apis to help you create intuitive TUI faster,

Screenshot from 2023-09-30 19-38-45

Installation

go get github.com/yassinebenaid/prompts

API

Input

The input api allows you to prompt the user for an input field , it returns the input value

  • Usage:
// [...]

value, err := prompts.InputBox(prompts.InputOptions{
	Secure:      false, // hides the user input, very common for passwords
	Label:       "what is your name?",
	Placeholder: "what is your name",
	Required:    true,
	Validator:   func(value string) error {// will be called when user submit, and returned error will be displayed to the user below the input
		if len(value) < 3{
			return fmt.Errorf("minimum len is 3")
		}
		return nil
	},
})

if err != nil{
	log.Fatal(err)
}

fmt.Println("selected " + value)
  • Result:

image image

Password Input

The password input api is just normal input but with Secure option set to true ,

  • Usage:
// [...]

value, err := prompts.InputBox(prompts.InputOptions{
	Secure:      true, // set password mode
	Label:       "what is your password?",
	Placeholder: "what is your password",
	Required:    true,
	Validator:   func(value string) error {// will be called when user submit, and returned error will be displayed to the user below the input
		if len(value) < 3{
			return fmt.Errorf("minimum len is 3")
		}
		return nil
	},
})

if err != nil{
	log.Fatal(err)
}

fmt.Println("password : " + value)
  • Result:

image

Confirmation Input

The confirmation api can be used to prompt the user for confirmation , it returns a boolean ,

  • Usage:
// [...]

const DEFAULT = true
value, err := prompts.ConfirmBox("are you sure ?", DEFAULT)

if err != nil {
	log.Fatal(err)
}

fmt.Println("answer : ", value)
  • Result:

    image

Radio Input

The radio api can be used to prompt the user to choose one of several options , it returns a the index of the checked option ,

  • Usage:
// [...]

genders := []string{"male", "female"}
value, err := prompts.RadioBox("Choose your gender : ", genders)

if err != nil {
	log.Fatal(err)
}

fmt.Println("gender : ", genders[value])
  • Result:

    image

Select Box

The select box api can be used to prompt the user to choose between several options , it returns a slice of selected indexes,

  • Usage:
// [...]

hobbies := []string{"swimming", "coding", "gaming", "playing"}
value, err := prompts.SelectBox("Choose your hobbies : ", hobbies)

if err != nil {
	log.Fatal(err)
}

fmt.Println("gender : ", value)
  • Result:

    image

Alerts

these are helper apis you can use for better alerts and messages.

prompts.Info("Info alert")
prompts.Error("Error alert")
prompts.Warning("Warning alert")
prompts.Success("Success alert")
  • Result:

Screenshot from 2023-09-29 18-18-21

prompts.InfoMessage("Info message")
prompts.ErrorMessage("Error message")
prompts.WarningMessage("Warning message")
prompts.SuccessMessage("Success message")

Screenshot from 2023-09-29 18-19-17

Documentation

Overview

prompts offers several configurable, ituitive and intractive CLI components including inputs, selectbox, radio box and alerts formatter, just to name a few

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alert

func Alert(label string, color string, m string)

Prints the alert "m" labeled by "label" with the specified color

example :

prompts.Alert("WARNING", "#fca311", "hello world")

func ConfirmBox

func ConfirmBox(label string, def bool) (bool, error)

prompt the user for confirmation , it returns a boolean

example :

const DEFAULT = true
prompts.ConfirmBox("are you sure ?", DEFAULT)

func Error

func Error(m string)

Prints the message "m" using the "ERROR" theme for styling. Perfect for short messages

func ErrorMessage

func ErrorMessage(m string)

Prints the message "m" using the "ERROR" theme for styling. Perfect for long messages

func Info

func Info(m string)

Prints the message "m" using the "INFO" theme for styling. Perfect for short messages

func InfoMessage

func InfoMessage(m string)

Prints the message "m" using the "INFO" theme for styling. Perfect for long messages

func InputBox

func InputBox(options InputOptions) (string, error)

prompt user to fill an input, it returns the user input string

example :

prompts.InputBox(prompts.InputOptions{
	Secure:      false, // hides the user input, very common for passwords
	Label:       "what is your name?",
	Placeholder: "what is your name",
	Required:    true,
	Validator: func(value string) error { // will be called when user submit, and returned error will be displayed to the user below the input
		if len(value) < 3 {
			return fmt.Errorf("minimum len is 3")
		}
		return nil
	},
})

func Message

func Message(m string, color string)

Prints the message "m" labeled by "label" with the specified color

example :

prompts.Message("hello world", "#fca311")

func RadioBox

func RadioBox(label string, choices []string) (int, error)

prompt user to choose one of several choices , and return the checked indexe

example :

prompts.RadioBox("you are intersted at ", []string{"gaming", "coding"})

func SelectBox

func SelectBox(label string, choices []string) ([]int, error)

prompt user to select between choices , and return the selected indexes

example :

prompts.SelectBox("you are intersted at ", []string{"gaming", "coding"})

func Success

func Success(m string)

Prints m styled by SUCCESS theme

func SuccessMessage

func SuccessMessage(m string)

Prints the message "m" using the "SUCCESS" theme for styling. Perfect for long messages

func Warning

func Warning(m string)

Prints the message "m" using the "WARNING" theme for styling. Perfect for short messages

func WarningMessage

func WarningMessage(m string)

Prints the message "m" using the "WARNING" theme for styling. Perfect for long messages

Types

type InputOptions

type InputOptions struct {
	Secure      bool
	Label       string
	Placeholder string
	Required    bool
	Validator   func(value string) error
}

type SelectOptions

type SelectOptions struct {
	Choices []string
	Label   string
}

Jump to

Keyboard shortcuts

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