Skip to content

yassinebenaid/prompts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prompts

Build command line prompts with ease, prompts provides several TUI components to create intuitive CLI applications 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 , 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