Skip to content

tdewolff/prompt

Repository files navigation

Command line prompter

Command line prompter for terminal user interaction with the following features:

  • input prompt scans into any variable type (string, bool, int, time.Time, ..., or custom types)
  • input is editable in-place
  • select prompt with options
  • enter and yes/no prompt
  • input validation

See also github.com/tdewolff/argp for a command line argument parser.

TODO: capture terminal resize event TODO: handle long labels for prompt/select

Installation

Make sure you have Git and Go (1.13 or higher) installed, run

mkdir Project
cd Project
go mod init
go get -u github.com/tdewolff/prompt

Then add the following import

import (
    "github.com/tdewolff/prompt"
)

Examples

Input prompt

A regular prompt requesting user input. When the target is a primary type (except boolean) or implements the Stringer interface, it will be editable in-place.

package main

import "github.com/tdewolff/prompt"

func main() {
    // Validators verify the user input to match conditions.
    validators := []prompt.Validator{prompt.StrLength(5, 10), prompt.Suffix("suffix")}

    var val string
    deflt := prompt.Default("value", 3)  // set text caret to the 3rd character
    if err := prompt.Prompt(&val, "Label", deflt, validators...); err != nil {
        panic(err)
    }
    fmt.Println("Result:", val)
}

where val can be of any primary type, such as string, []byte, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, or time.Time.

When the value is editable it allowd users to use keys such as: Left, Ctrl + B to move left; Right, Ctrl + F to move right; Home, Ctrl + A to go to start; End, Ctrl + E to go to end; Backspace and Delete to delete a character; Ctrl + K and Ctrl + U to delete from the caret to the start and end of the input respectively; Enter, Ctrl + D to confirm input; and Ctrl + C, Esc to quit.

Select prompt

A list selection prompt that allows the user to select amongst predetermined options.

package main

import "github.com/tdewolff/prompt"

func main() {
    val, deflt := "", "Yellow"  // can be ints if you need the index into options
    options := []string{"Red", "Orange", "Green", "Yellow", "Blue", "Purple"}
    if err := prompt.Select(&val, "Label", options, deflt); err != nil {
        panic(err)
    }
    fmt.Println("Selected:", val)
}

The select prompt allows users to use keys such as: Up, Shift + Tab to go up; Down, Tab to go down; Enter, Ctrl + D to select option; Ctrl + C to quit; and Esc to cancel the selection.

When there are many options, it is possible to enter a query to filter options.

Yes/No prompt

A yes or no prompt returning true or false.

package main

import "github.com/tdewolff/prompt"

func main() {
    deflt := false
    if prompt.YesNo("Label", deflt) {
        fmt.Println("Yes")
    } else {
        fmt.Println("No")
    }
}

true is any of 1, y, yes, t, true and is case-insensitive.

false is any of 0, n, no, f, false and is case-insensitive.

Enter prompt

A prompt that waits for Enter to be pressed.

package main

import "github.com/tdewolff/prompt"

func main() {
    prompt.Enter("Are you done?")  // waits for enter
}

Validators

Not(Validator)     // logical NOT
And(Validator...)  // logical AND
Or(Validator...)   // logical OR

Is(any)       // is exact match
In([]any)     // in list
NotIn([]any)  // not in list

StrLength(min, max int)           // limit string length (inclusive)
NumRange(min, max float64)        // limit int/uint/float range (inclusive)
DateRange(min, max time.Time)     // limit time.Time range (inclusive)
Prefix(afix string)
Suffix(afix string)
Pattern(pattern, message string)  // pattern match and error message
EmailAddress()
IPAddress()                       // valid IPv4 or IPv6 address
IPv4Address()
IPv6Address()
Port()                            // server port
Path()                            // Unix path
AbsolutePath()                    // Unix absolute path
UserName()                        // valid Unix user name
TopDomainName()                   // such as example.com
DomainName()                      // such as sub.example.com
FQDN()                            // such as sub.example.com.
Dir()                             // existing directory
File()                            // existing file

License

Released under the MIT license.

About

Command line prompter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages