2021-01-13 10:08:08 +08:00
|
|
|
package stario
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
2023-02-11 16:55:57 +08:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2021-01-13 10:08:08 +08:00
|
|
|
"os"
|
2023-02-11 16:55:57 +08:00
|
|
|
"runtime"
|
2021-01-13 10:08:08 +08:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type InputMsg struct {
|
2022-03-18 13:49:38 +08:00
|
|
|
msg string
|
|
|
|
|
err error
|
|
|
|
|
skipSliceSigErr bool
|
2021-01-13 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 19:15:43 +08:00
|
|
|
type rawTerminalSession struct {
|
|
|
|
|
fd int
|
|
|
|
|
state *terminal.State
|
|
|
|
|
reader *bufio.Reader
|
|
|
|
|
redrawHint string
|
|
|
|
|
printNewline bool
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 14:25:56 +08:00
|
|
|
func Passwd(hint string, defaultVal string) InputMsg {
|
2026-03-21 19:15:43 +08:00
|
|
|
return passwd(hint, defaultVal, "", true)
|
2021-01-13 14:25:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PasswdWithMask(hint string, defaultVal string, mask string) InputMsg {
|
2026-03-21 19:15:43 +08:00
|
|
|
return passwd(hint, defaultVal, mask, true)
|
2023-02-11 16:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PasswdResponseSignal(hint string, defaultVal string) InputMsg {
|
|
|
|
|
return passwd(hint, defaultVal, "", true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PasswdResponseSignalWithMask(hint string, defaultVal string, mask string) InputMsg {
|
|
|
|
|
return passwd(hint, defaultVal, mask, true)
|
2021-01-13 14:25:56 +08:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 11:08:09 +08:00
|
|
|
func MessageBoxRaw(hint string, defaultVal string) InputMsg {
|
|
|
|
|
return messageBox(hint, defaultVal)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 19:15:43 +08:00
|
|
|
func newRawTerminalSession(hint string, printNewline bool) (*rawTerminalSession, error) {
|
2021-04-23 11:08:09 +08:00
|
|
|
if hint != "" {
|
|
|
|
|
fmt.Print(hint)
|
|
|
|
|
}
|
|
|
|
|
fd := int(os.Stdin.Fd())
|
|
|
|
|
state, err := terminal.MakeRaw(fd)
|
|
|
|
|
if err != nil {
|
2026-03-21 19:15:43 +08:00
|
|
|
return nil, err
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
return &rawTerminalSession{
|
|
|
|
|
fd: fd,
|
|
|
|
|
state: state,
|
|
|
|
|
reader: bufio.NewReader(os.Stdin),
|
|
|
|
|
redrawHint: promptRedrawHint(hint),
|
|
|
|
|
printNewline: printNewline,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (session *rawTerminalSession) Close() {
|
|
|
|
|
if session == nil || session.state == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_ = terminal.Restore(session.fd, session.state)
|
|
|
|
|
if session.printNewline {
|
|
|
|
|
fmt.Println()
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 19:15:43 +08:00
|
|
|
func (session *rawTerminalSession) Restore() error {
|
|
|
|
|
if session == nil || session.state == nil {
|
|
|
|
|
return nil
|
2023-02-11 16:55:57 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
return terminal.Restore(session.fd, session.state)
|
2023-02-11 16:55:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 19:15:43 +08:00
|
|
|
func promptRedrawHint(hint string) string {
|
|
|
|
|
if strings.Index(hint, "\n") >= 0 {
|
|
|
|
|
hint = hint[strings.LastIndex(hint, "\n")+1:]
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(hint)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func finalizeInputValue(raw string, defaultVal string) string {
|
|
|
|
|
raw = strings.TrimSpace(raw)
|
|
|
|
|
if len(raw) == 0 {
|
|
|
|
|
return defaultVal
|
|
|
|
|
}
|
|
|
|
|
return raw
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func renderRawEcho(ioBuf []rune, mask string) string {
|
|
|
|
|
if mask == "" {
|
|
|
|
|
return string(ioBuf)
|
|
|
|
|
}
|
|
|
|
|
return strings.Repeat(mask, len(ioBuf))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func redrawPromptLine(hint string, echo string, lastWidth int) int {
|
|
|
|
|
nowWidth := stringDisplayWidth(hint) + stringDisplayWidth(echo)
|
|
|
|
|
clearWidth := lastWidth
|
|
|
|
|
if nowWidth > clearWidth {
|
|
|
|
|
clearWidth = nowWidth
|
|
|
|
|
}
|
|
|
|
|
fmt.Print("\r")
|
|
|
|
|
if clearWidth > 0 {
|
|
|
|
|
fmt.Print(strings.Repeat(" ", clearWidth))
|
|
|
|
|
fmt.Print("\r")
|
|
|
|
|
}
|
2021-01-13 14:25:56 +08:00
|
|
|
if hint != "" {
|
|
|
|
|
fmt.Print(hint)
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
if echo != "" {
|
|
|
|
|
fmt.Print(echo)
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
return nowWidth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func stringDisplayWidth(text string) int {
|
|
|
|
|
width := 0
|
|
|
|
|
for _, r := range text {
|
|
|
|
|
width += runeDisplayWidth(r)
|
|
|
|
|
}
|
|
|
|
|
return width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runeDisplayWidth(r rune) int {
|
|
|
|
|
switch {
|
|
|
|
|
case r == '\t':
|
|
|
|
|
return 4
|
|
|
|
|
case r < 0x20 || (r >= 0x7f && r < 0xa0):
|
|
|
|
|
return 0
|
|
|
|
|
case isWideRune(r):
|
|
|
|
|
return 2
|
|
|
|
|
default:
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isWideRune(r rune) bool {
|
|
|
|
|
return r >= 0x1100 && (r <= 0x115f ||
|
|
|
|
|
r == 0x2329 || r == 0x232a ||
|
|
|
|
|
(r >= 0x2e80 && r <= 0xa4cf && r != 0x303f) ||
|
|
|
|
|
(r >= 0xac00 && r <= 0xd7a3) ||
|
|
|
|
|
(r >= 0xf900 && r <= 0xfaff) ||
|
|
|
|
|
(r >= 0xfe10 && r <= 0xfe19) ||
|
|
|
|
|
(r >= 0xfe30 && r <= 0xfe6f) ||
|
|
|
|
|
(r >= 0xff00 && r <= 0xff60) ||
|
|
|
|
|
(r >= 0xffe0 && r <= 0xffe6) ||
|
|
|
|
|
(r >= 0x1f300 && r <= 0x1f64f) ||
|
|
|
|
|
(r >= 0x1f900 && r <= 0x1f9ff) ||
|
|
|
|
|
(r >= 0x20000 && r <= 0x3fffd))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rawLineInput(hint string, defaultVal string, mask string, handleSignal bool) InputMsg {
|
|
|
|
|
session, err := newRawTerminalSession(hint, true)
|
2021-01-13 14:25:56 +08:00
|
|
|
if err != nil {
|
2022-03-18 13:49:38 +08:00
|
|
|
return InputMsg{msg: "", err: err}
|
2021-01-13 14:25:56 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
defer session.Close()
|
|
|
|
|
ioBuf := make([]rune, 0, 16)
|
|
|
|
|
lastWidth := 0
|
2021-01-13 14:25:56 +08:00
|
|
|
for {
|
2026-03-21 19:15:43 +08:00
|
|
|
b, _, err := session.reader.ReadRune()
|
2021-01-13 14:25:56 +08:00
|
|
|
if err != nil {
|
2022-03-18 13:49:38 +08:00
|
|
|
return InputMsg{msg: "", err: err}
|
2021-01-13 14:25:56 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
if handleSignal && isSignal(b) {
|
2023-02-11 16:55:57 +08:00
|
|
|
if runtime.GOOS != "windows" {
|
2026-03-21 19:15:43 +08:00
|
|
|
if err := session.Restore(); err != nil {
|
|
|
|
|
return InputMsg{msg: "", err: err}
|
2023-02-11 16:55:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
if err := signal(b); err != nil {
|
|
|
|
|
return InputMsg{msg: "", err: err}
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
continue
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
switch b {
|
|
|
|
|
case 0x0d, 0x0a:
|
|
|
|
|
return InputMsg{msg: finalizeInputValue(string(ioBuf), defaultVal), err: nil}
|
|
|
|
|
case 0x08, 0x7F:
|
2021-04-23 11:08:09 +08:00
|
|
|
if len(ioBuf) > 0 {
|
|
|
|
|
ioBuf = ioBuf[:len(ioBuf)-1]
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
default:
|
2021-04-23 11:08:09 +08:00
|
|
|
ioBuf = append(ioBuf, b)
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
lastWidth = redrawPromptLine(session.redrawHint, renderRawEcho(ioBuf, mask), lastWidth)
|
2021-01-13 14:25:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-21 19:15:43 +08:00
|
|
|
func messageBox(hint string, defaultVal string) InputMsg {
|
|
|
|
|
return rawLineInput(hint, defaultVal, "", false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isSignal(s rune) bool {
|
|
|
|
|
switch s {
|
|
|
|
|
case 0x03, 0x1a, 0x1c:
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func passwd(hint string, defaultVal string, mask string, handleSignal bool) InputMsg {
|
|
|
|
|
return rawLineInput(hint, defaultVal, mask, handleSignal)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func MessageBox(hint string, defaultVal string) InputMsg {
|
|
|
|
|
if hint != "" {
|
|
|
|
|
fmt.Print(hint)
|
|
|
|
|
}
|
|
|
|
|
inputReader := bufio.NewReader(os.Stdin)
|
|
|
|
|
str, err := inputReader.ReadString('\n')
|
|
|
|
|
if err != nil {
|
2022-03-18 14:51:57 +08:00
|
|
|
return InputMsg{msg: str, err: err}
|
2021-01-13 10:08:08 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
str = finalizeInputValue(str, defaultVal)
|
2022-03-18 14:51:57 +08:00
|
|
|
return InputMsg{msg: str, err: err}
|
2022-03-18 13:49:38 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 14:51:51 +08:00
|
|
|
func (im *InputMsg) IgnoreSliceParseError(i bool) *InputMsg {
|
2022-03-18 13:49:38 +08:00
|
|
|
im.skipSliceSigErr = i
|
|
|
|
|
return im
|
2021-01-13 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) String() (string, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return "", im.err
|
|
|
|
|
}
|
|
|
|
|
return im.msg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustString() string {
|
|
|
|
|
res, _ := im.String()
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceString(sep string) ([]string, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return nil, im.err
|
|
|
|
|
}
|
2024-08-30 14:51:51 +08:00
|
|
|
if len(strings.TrimSpace(im.msg)) == 0 {
|
|
|
|
|
return []string{}, nil
|
|
|
|
|
}
|
2022-03-18 13:49:38 +08:00
|
|
|
return strings.Split(im.msg, sep), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceString(sep string) []string {
|
|
|
|
|
res, _ := im.SliceString(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) sliceFn(sep string, fn func(string) (interface{}, error)) ([]interface{}, error) {
|
|
|
|
|
var res []interface{}
|
|
|
|
|
data, err := im.SliceString(sep)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
code, err := fn(v)
|
|
|
|
|
if err != nil && !im.skipSliceSigErr {
|
|
|
|
|
return nil, err
|
|
|
|
|
} else if err == nil {
|
|
|
|
|
res = append(res, code)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func (im InputMsg) Int() (int, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return 0, im.err
|
|
|
|
|
}
|
|
|
|
|
return strconv.Atoi(im.msg)
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceInt(sep string) ([]int, error) {
|
|
|
|
|
data, err := im.sliceFn(sep, func(v string) (interface{}, error) {
|
|
|
|
|
return strconv.Atoi(v)
|
|
|
|
|
})
|
|
|
|
|
var res []int
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
res = append(res, v.(int))
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceInt(sep string) []int {
|
|
|
|
|
res, _ := im.SliceInt(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func (im InputMsg) MustInt() int {
|
|
|
|
|
res, _ := im.Int()
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) Int64() (int64, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return 0, im.err
|
|
|
|
|
}
|
|
|
|
|
return strconv.ParseInt(im.msg, 10, 64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustInt64() int64 {
|
|
|
|
|
res, _ := im.Int64()
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceInt64(sep string) ([]int64, error) {
|
|
|
|
|
data, err := im.sliceFn(sep, func(v string) (interface{}, error) {
|
|
|
|
|
return strconv.ParseInt(v, 10, 64)
|
|
|
|
|
})
|
|
|
|
|
var res []int64
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
res = append(res, v.(int64))
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceInt64(sep string) []int64 {
|
|
|
|
|
res, _ := im.SliceInt64(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func (im InputMsg) Uint64() (uint64, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return 0, im.err
|
|
|
|
|
}
|
|
|
|
|
return strconv.ParseUint(im.msg, 10, 64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustUint64() uint64 {
|
|
|
|
|
res, _ := im.Uint64()
|
|
|
|
|
return res
|
|
|
|
|
}
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceUint64(sep string) ([]uint64, error) {
|
|
|
|
|
data, err := im.sliceFn(sep, func(v string) (interface{}, error) {
|
|
|
|
|
return strconv.ParseUint(v, 10, 64)
|
|
|
|
|
})
|
|
|
|
|
var res []uint64
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
res = append(res, v.(uint64))
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceUint64(sep string) []uint64 {
|
|
|
|
|
res, _ := im.SliceUint64(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
2021-01-13 10:08:08 +08:00
|
|
|
|
|
|
|
|
func (im InputMsg) Bool() (bool, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return false, im.err
|
|
|
|
|
}
|
|
|
|
|
return strconv.ParseBool(im.msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustBool() bool {
|
|
|
|
|
res, _ := im.Bool()
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceBool(sep string) ([]bool, error) {
|
|
|
|
|
data, err := im.sliceFn(sep, func(v string) (interface{}, error) {
|
|
|
|
|
return strconv.ParseBool(v)
|
|
|
|
|
})
|
|
|
|
|
var res []bool
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
res = append(res, v.(bool))
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceBool(sep string) []bool {
|
|
|
|
|
res, _ := im.SliceBool(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func (im InputMsg) Float64() (float64, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return 0, im.err
|
|
|
|
|
}
|
|
|
|
|
return strconv.ParseFloat(im.msg, 64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustFloat64() float64 {
|
|
|
|
|
res, _ := im.Float64()
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceFloat64(sep string) ([]float64, error) {
|
|
|
|
|
data, err := im.sliceFn(sep, func(v string) (interface{}, error) {
|
|
|
|
|
return strconv.ParseFloat(v, 64)
|
|
|
|
|
})
|
|
|
|
|
var res []float64
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
res = append(res, v.(float64))
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceFloat64(sep string) []float64 {
|
|
|
|
|
res, _ := im.SliceFloat64(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func (im InputMsg) Float32() (float32, error) {
|
|
|
|
|
if im.err != nil {
|
|
|
|
|
return 0, im.err
|
|
|
|
|
}
|
|
|
|
|
res, err := strconv.ParseFloat(im.msg, 32)
|
|
|
|
|
return float32(res), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustFloat32() float32 {
|
|
|
|
|
res, _ := im.Float32()
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 13:49:38 +08:00
|
|
|
func (im InputMsg) SliceFloat32(sep string) ([]float32, error) {
|
|
|
|
|
data, err := im.sliceFn(sep, func(v string) (interface{}, error) {
|
|
|
|
|
return strconv.ParseFloat(v, 32)
|
|
|
|
|
})
|
|
|
|
|
var res []float32
|
|
|
|
|
for _, v := range data {
|
|
|
|
|
res = append(res, v.(float32))
|
|
|
|
|
}
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (im InputMsg) MustSliceFloat32(sep string) []float32 {
|
|
|
|
|
res, _ := im.SliceFloat32(sep)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 10:08:08 +08:00
|
|
|
func YesNo(hint string, defaults bool) bool {
|
2026-03-21 19:15:43 +08:00
|
|
|
res, err := YesNoE(hint, defaults)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return defaults
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseYesNoValue(raw string, defaults bool) (bool, bool) {
|
|
|
|
|
raw = strings.TrimSpace(strings.ToUpper(raw))
|
|
|
|
|
if raw == "" {
|
|
|
|
|
return defaults, true
|
|
|
|
|
}
|
|
|
|
|
switch []rune(raw)[0] {
|
|
|
|
|
case 'Y':
|
|
|
|
|
return true, true
|
|
|
|
|
case 'N':
|
|
|
|
|
return false, true
|
|
|
|
|
default:
|
|
|
|
|
return false, false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func YesNoE(hint string, defaults bool) (bool, error) {
|
2021-04-23 11:08:09 +08:00
|
|
|
for {
|
2026-03-21 19:15:43 +08:00
|
|
|
res, err := MessageBox(hint, "").String()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
if answer, ok := parseYesNoValue(res, defaults); ok {
|
|
|
|
|
return answer, nil
|
2021-04-23 11:08:09 +08:00
|
|
|
}
|
2021-01-13 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 14:25:56 +08:00
|
|
|
func StopUntil(hint string, trigger string, repeat bool) error {
|
2026-03-21 19:15:43 +08:00
|
|
|
triggerRunes := []rune(trigger)
|
|
|
|
|
pressLen := len(triggerRunes)
|
2021-01-13 10:08:08 +08:00
|
|
|
if trigger == "" {
|
|
|
|
|
pressLen = 1
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
session, err := newRawTerminalSession(hint, false)
|
2021-01-13 14:25:56 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
defer session.Close()
|
2021-01-13 14:25:56 +08:00
|
|
|
i := 0
|
2021-01-13 10:08:08 +08:00
|
|
|
for {
|
2026-03-21 19:15:43 +08:00
|
|
|
b, _, err := session.reader.ReadRune()
|
2021-01-13 10:08:08 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-01-13 14:25:56 +08:00
|
|
|
if trigger == "" {
|
2021-01-13 10:08:08 +08:00
|
|
|
break
|
|
|
|
|
}
|
2026-03-21 19:15:43 +08:00
|
|
|
if b == triggerRunes[i] {
|
2021-01-13 14:25:56 +08:00
|
|
|
i++
|
|
|
|
|
if i == pressLen {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
i = 0
|
|
|
|
|
if hint != "" && repeat {
|
2021-04-23 11:08:09 +08:00
|
|
|
fmt.Print("\r\n")
|
2021-01-13 14:25:56 +08:00
|
|
|
fmt.Print(hint)
|
|
|
|
|
}
|
2021-01-13 10:08:08 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|