https://github.com/tdewolff/prompt
Command line prompter
https://github.com/tdewolff/prompt
Last synced: 2 months ago
JSON representation
Command line prompter
- Host: GitHub
- URL: https://github.com/tdewolff/prompt
- Owner: tdewolff
- License: mit
- Created: 2023-03-20T13:06:08.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-03-14T09:15:08.000Z (3 months ago)
- Last Synced: 2025-04-09T21:53:04.959Z (2 months ago)
- Language: Go
- Size: 106 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://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](https://git-scm.com/) and [Go](https://golang.org/dl/) (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
``` go
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.```go
package mainimport "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.```go
package mainimport "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`.```go
package mainimport "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.```go
package mainimport "github.com/tdewolff/prompt"
func main() {
prompt.Enter("Are you done?") // waits for enter
}
```### Validators
```go
Not(Validator) // logical NOT
And(Validator...) // logical AND
Or(Validator...) // logical ORIs(any) // is exact match
In([]any) // in list
NotIn([]any) // not in listStrLength(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](LICENSE.md).