gt/internal/prompt/prompt.go

63 lines
1.2 KiB
Go
Raw Normal View History

// Package prompt implements prompt functions for runes and strings.
2024-06-28 14:26:30 -04:00
package prompt
import (
"bytes"
"fmt"
"log"
2024-06-28 14:26:30 -04:00
"os"
"github.com/charmbracelet/huh"
2024-06-28 14:26:30 -04:00
"golang.org/x/term"
)
func YesNo(prompt string) bool {
return AskRune(prompt, "y/n") == 'y'
}
func AskRune(prompt, options string) byte {
// switch stdin into 'raw' mode
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
log.Fatal(err)
2024-06-28 14:26:30 -04:00
}
defer func() {
if err := term.Restore(int(os.Stdin.Fd()), oldState); err != nil {
log.Fatal(err)
2024-06-28 14:26:30 -04:00
}
}()
fmt.Fprintf(os.Stdout, "%s [%s]: ", prompt, options)
2024-06-28 14:26:30 -04:00
// read one byte from stdin
one := make([]byte, 1)
_, err = os.Stdin.Read(one)
2024-06-28 14:26:30 -04:00
if err != nil {
return 0
}
return bytes.ToLower(one)[0]
2024-06-28 14:26:30 -04:00
}
func NewPath(path string) (string, bool) {
for {
answer := AskRune(fmt.Sprintf("file %s exists, overwrite, rename, or cancel?", path), "o/r/c")
switch answer {
case 'o', 'O':
return path, false
case 'r', 'R':
if err := huh.NewInput().
Title("input a new filename").
Value(&path).
Run(); err != nil {
return path, false
}
if _, e := os.Stat(path); e != nil {
return path, false
}
default:
return path, true
}
}
}