Initial Commit
Internal Link Generation
This commit is contained in:
62
util/cli.go
Normal file
62
util/cli.go
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||
*/
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func PromptUser(text string) string {
|
||||
var resp string
|
||||
fmt.Println(text)
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
if scanner.Scan() {
|
||||
resp = scanner.Text()
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func PromptUserOptTrimmed(text string, def string) string {
|
||||
var resp string
|
||||
if def == "" {
|
||||
resp = strings.TrimSpace(PromptUser(text))
|
||||
} else {
|
||||
resp = strings.TrimSpace(PromptUser(fmt.Sprintf("%s [%s]:", text, def)))
|
||||
if resp == "" {
|
||||
resp = def
|
||||
}
|
||||
}
|
||||
for resp == "" {
|
||||
resp = strings.TrimSpace(PromptUser(text))
|
||||
if resp == "" {
|
||||
fmt.Println("Cannot be blank")
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
func PromptUserTrimmed(text string) string {
|
||||
return PromptUserOptTrimmed(text, "")
|
||||
}
|
||||
|
||||
func PromptUserInt(text string) int {
|
||||
var resp string
|
||||
var i int
|
||||
for resp == "" {
|
||||
resp = strings.TrimSpace(PromptUser(text))
|
||||
if resp == "" {
|
||||
fmt.Println("Cannot be blank")
|
||||
}
|
||||
var err error
|
||||
i, err = strconv.Atoi(resp)
|
||||
if err != nil {
|
||||
fmt.Println("Must be a number")
|
||||
resp = ""
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
Reference in New Issue
Block a user