You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Prompt function from PR #199 could be a bit expanded to support message formatting and default values:
// Prompt creates a pipe that reads user input from stdin after displaying the// specified prompt.funcPrompt(promptstring) *Pipe {
returnPromptf(prompt)
}
// Promptf displays a formatted prompt to the user and reads a line of input.// Additional arguments can be provided for the format specifier like Printf. // The last argument will be used as the default value.funcPromptf(formatstring, args...any) *Pipe {
fmt.Printf(format, args...)
p:=Stdin().First(1)
input, err:=p.String()
iferr!=nil {
returnp
}
input=strings.TrimSpace(input)
ifinput==""&&len(args) >0 {
input=fmt.Sprint(args[len(args)-1])
}
returnEcho(input)
}
Example:
input, _:=script.Prompt("Input: ").String()
fmt.Printf("Input was %s\n", input)
input, _=script.Promptf("Input [%s]: ", "John").String()
fmt.Printf("Input was %s\n", input)
Output:
Input: Jane
Input was Jane
Input [John]:
Input was John
The text was updated successfully, but these errors were encountered:
The Prompt function from PR #199 could be a bit expanded to support message formatting and default values:
Example:
Output:
The text was updated successfully, but these errors were encountered: