Categorygithub.com/manifoldco/promptui
modulepackage
0.9.0
Repository: https://github.com/manifoldco/promptui.git
Documentation: pkg.go.dev

# README

promptui

Interactive prompt for command-line applications.

We built Promptui because we wanted to make it easy and fun to explore cloud services with manifold cli.

Code of Conduct | Contribution Guidelines

GitHub release GoDoc Travis Go Report Card License

Overview

promptui

Promptui is a library providing a simple interface to create command-line prompts for go. It can be easily integrated into spf13/cobra, urfave/cli or any cli go application.

Promptui has two main input modes:

  • Prompt provides a single line for user input. Prompt supports optional live validation, confirmation and masking the input.

  • Select provides a list of options to choose from. Select supports pagination, search, detailed view and custom templates.

For a full list of options check GoDoc.

Basic Usage

Prompt

package main

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/manifoldco/promptui"
)

func main() {
	validate := func(input string) error {
		_, err := strconv.ParseFloat(input, 64)
		if err != nil {
			return errors.New("Invalid number")
		}
		return nil
	}

	prompt := promptui.Prompt{
		Label:    "Number",
		Validate: validate,
	}

	result, err := prompt.Run()

	if err != nil {
		fmt.Printf("Prompt failed %v\n", err)
		return
	}

	fmt.Printf("You choose %q\n", result)
}

Select

package main

import (
	"fmt"

	"github.com/manifoldco/promptui"
)

func main() {
	prompt := promptui.Select{
		Label: "Select Day",
		Items: []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
			"Saturday", "Sunday"},
	}

	_, result, err := prompt.Run()

	if err != nil {
		fmt.Printf("Prompt failed %v\n", err)
		return
	}

	fmt.Printf("You choose %q\n", result)
}

More Examples

See full list of examples

# Packages

# Functions

NewCursor create a new cursor, with the DefaultCursor, the specified input, and position at the end of the specified starting input.
Styler is a function that accepts multiple possible styling transforms from the state, color and background colors constants and transforms them into a templated string to apply those styles in the CLI.

# Constants

The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible background colors of text inside the application.
The possible colors of text inside the application.
The possible colors of text inside the application.
The possible state of text inside the application, either Bold, faint, italic or underline.
The possible colors of text inside the application.
The possible state of text inside the application, either Bold, faint, italic or underline.
The possible colors of text inside the application.
The possible state of text inside the application, either Bold, faint, italic or underline.
The possible colors of text inside the application.
The possible colors of text inside the application.
The possible state of text inside the application, either Bold, faint, italic or underline.
The possible colors of text inside the application.
The possible colors of text inside the application.
SelectedAdd is used internally inside SelectWithAdd when the add option is selected in select mode.

# Variables

BlockCursor is a cursor which highlights a character by inverting colors on it.
DefaultCursor is a big square block character.
ErrAbort is the error returned when confirm prompts are supplied "n".
ErrEOF is the error returned from prompts when EOF is encountered.
ErrInterrupt is the error returned from prompts when an interrupt (ctrl-c) is encountered.
FuncMap defines template helpers for the output.
IconBad is the icon used when a bad answer is entered in prompt mode.
IconGood is the icon used when a good answer is entered in prompt mode.
IconInitial is the icon used when starting in prompt mode and the icon next to the label when starting in select mode.
IconSelect is the icon used to identify the currently selected item in select mode.
IconWarn is the icon used when a good, but potentially invalid answer is entered in prompt mode.
KeyBackspace is the default key for deleting input text.
KeyBackward is the default key to page up during selection.
These runes are used to identify the commands entered by the user in the command prompt.
KeyCtrlH is the key for deleting input text.
KeyEnter is the default key for submission/selection.
KeyForward is the default key to page down during selection.
These runes are used to identify the commands entered by the user in the command prompt.
KeyNext is the default key to go down during selection.
These runes are used to identify the commands entered by the user in the command prompt.
KeyPrev is the default key to go up during selection.
These runes are used to identify the commands entered by the user in the command prompt.
PipeCursor is a pipe character "|" which appears before the input character.
ResetCode is the character code used to reset the terminal formatting.
SearchPrompt is the prompt displayed in search mode.

# Structs

Cursor tracks the state associated with the movable cursor The strategy is to keep the prompt, input pristine except for requested modifications.
Key defines a keyboard code and a display representation for the help menu.
Prompt represents a single line text field input with options for validation and input masks.
PromptTemplates allow a prompt to be customized following stdlib text/template syntax.
Select represents a list of items used to enable selections, they can be used as search engines, menus or as a list of items in a cli based prompt.
SelectKeys defines the available keys used by select mode to enable the user to move around the list and trigger search mode.
SelectTemplates allow a select list to be customized following stdlib text/template syntax.
SelectWithAdd represents a list for selecting a single item inside a list of items with the possibility to add new items to the list.

# Type aliases

Pointer is A specific type that translates a given set of runes into a given set of runes pointed at by the cursor.
ValidateFunc is a placeholder type for any validation functions that validates a given input.