github.com/blend/go-sdk@v1.20220411.3/sh/prompt.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package sh 9 10 import ( 11 "bufio" 12 "fmt" 13 "io" 14 "os" 15 16 "github.com/blend/go-sdk/ex" 17 ) 18 19 // ErrUnexpectedNewLine is returned from scan.go when you just hit enter with nothing in the prompt 20 const ErrUnexpectedNewLine ex.Class = "unexpected newline" 21 22 // Prompt gives a prompt and reads input until newlines. 23 func Prompt(prompt string) string { 24 return PromptFrom(os.Stdout, os.Stdin, prompt) 25 } 26 27 // Promptf gives a prompt of a given format and args and reads input until newlines. 28 func Promptf(format string, args ...interface{}) string { 29 return PromptFrom(os.Stdout, os.Stdin, fmt.Sprintf(format, args...)) 30 } 31 32 // PromptFrom gives a prompt and reads input until newlines from a given set of streams. 33 func PromptFrom(stdout io.Writer, stdin io.Reader, prompt string) string { 34 fmt.Fprint(stdout, prompt) 35 36 scanner := bufio.NewScanner(stdin) 37 var output string 38 if scanner.Scan() { 39 output = scanner.Text() 40 } 41 return output 42 }