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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package menu
import (
"bufio"
"fmt"
"github.com/charmbracelet/lipgloss"
"io"
"os"
"pik/flags"
"pik/menu/style"
"pik/model"
"slices"
)
var confirmations = []rune{
'y',
'Y',
' ',
'\n',
}
var (
PromptColor = lipgloss.Color("1")
PromptStyle = style.New(func() lipgloss.Style {
st := lipgloss.NewStyle()
if !*flags.Yes {
st.Foreground(PromptColor)
}
return st
})
ConfirmStyle = style.New(func() lipgloss.Style {
return lipgloss.NewStyle()
})
AnswerStyle = style.New(func() lipgloss.Style {
return lipgloss.NewStyle()
})
YesStyle = style.New(func() lipgloss.Style {
return lipgloss.NewStyle().Faint(true)
})
)
func Confirm(r io.Reader, source *model.Source, target model.Target, args ...string) bool {
parts := []string{
ConfirmStyle.Render(PromptStyle.Render("[Y/n]")),
Banner(source, target, args...),
"? ",
}
banner := BannerStyle.Render(parts...)
_, _ = fmt.Fprint(os.Stderr, banner)
if *flags.Yes {
_, _ = fmt.Fprintln(os.Stderr, AnswerStyle.Render("Y", YesStyle.Render("(--yes)")))
return true
}
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanRunes)
scanner.Scan()
if slices.Contains(confirmations, []rune(scanner.Text())[0]) {
return true
} else {
_, _ = fmt.Fprint(os.Stderr, "confirmation was not given.")
}
return false
}
|