Skip to content

Repository files navigation

Strumt codecovGo Report CardGoDocGitHub tag

Strumt is a library to create prompt chain. It provides multiline prompt, input validation, retry on error, ability to create typesafe prompt, ability to customize prompt and error display, a recording of prompt session and the ability to easily test your prompt scenario.

Example

Checkout godoc to have more examples : https://godoc.org/github.com/antham/strumt


asciicast

package main
import (
"bufio""fmt""os""strconv""github.com/antham/strumt"
)
funcmain() {
user:=User{}
p:=strumt.NewPromptsFromReaderAndWriter(bufio.NewReader(os.Stdin), os.Stdout)
p.AddLinePrompter(&StringPrompt{&user.FirstName, "Enter your first name", "userName", "lastName", "userName"})
p.AddLinePrompter(&StringPrompt{&user.LastName, "Enter your last name", "lastName", "age", "lastName"})
p.AddLinePrompter(&IntPrompt{&user.Age, "Enter your age", "age", "", "age"})
p.SetFirst("userName")
p.Run()
for_, step:=rangep.Scenario() {
fmt.Println(step.PromptString())
fmt.Println(step.Inputs()[0])
ifstep.Error() !=nil {
fmt.Println(step.Error())
}
}
fmt.Println()
fmt.Printf("User datas : %#v", user)
}
typeStringPromptstruct {
store*stringpromptstringcurrentIDstringnextPromptstringnextPromptOnErrorstring
}
func (s*StringPrompt) ID() string {
returns.currentID
}
func (s*StringPrompt) PromptString() string {
returns.prompt
}
func (s*StringPrompt) Parse(valuestring) error {
ifvalue=="" {
returnfmt.Errorf("Empty value given")
}
*(s.store) =valuereturnnil
}
func (s*StringPrompt) NextOnSuccess(valuestring) string {
returns.nextPrompt
}
func (s*StringPrompt) NextOnError(errerror) string {
returns.nextPromptOnError
}
typeIntPromptstruct {
store*intpromptstringcurrentIDstringnextPromptstringnextPromptOnErrorstring
}
func (i*IntPrompt) ID() string {
returni.currentID
}
func (i*IntPrompt) PromptString() string {
returni.prompt
}
func (i*IntPrompt) Parse(valuestring) error {
age, err:=strconv.Atoi(value)
iferr!=nil {
returnfmt.Errorf("%s is not a valid number", value)
}
ifage<=0 {
returnfmt.Errorf("Give a valid age")
}
*(i.store) =agereturnnil
}
func (i*IntPrompt) NextOnSuccess(valuestring) string {
returni.nextPrompt
}
func (i*IntPrompt) NextOnError(errerror) string {
returni.nextPromptOnError
}
typeUserstruct {
FirstNamestringLastNamestringAgeint
}

About

Strumt is a library to create prompt chain

Topics

Resources

Code of conduct

Contributing

Stars

63 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages