-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
45 lines (39 loc) · 895 Bytes
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func promptText() (string, error) {
f, err := os.CreateTemp("", "")
if err != nil {
panic(fmt.Errorf("failed to open default text editor: %w", err))
}
f.Close()
cmd := exec.Command("nano", f.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return "", fmt.Errorf("failed to start editing command: %w", err)
}
err = cmd.Wait()
if err != nil {
return "", fmt.Errorf("error while editing: %w", err)
}
b, err := os.ReadFile(f.Name())
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
return strings.TrimSpace(string(b)), nil
}
var reader = bufio.NewReader(os.Stdin)
func yesNo(prompt string) bool {
fmt.Print(prompt, " Y/n: ")
yesNo, _ := reader.ReadString('\n')
yesNo = yesNo[:len(yesNo)-1]
return yesNo != "n"
}