2024-07-07 21:45:35 +01:00
|
|
|
package textinput
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-07 21:59:27 +01:00
|
|
|
"git.shadowhosting.xyz/Eggactyl/tui"
|
2024-07-07 21:45:35 +01:00
|
|
|
"git.shadowhosting.xyz/Eggactyl/tui/validators"
|
|
|
|
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InputData struct {
|
|
|
|
Notice string
|
|
|
|
Question string
|
|
|
|
Validator validators.TextInputValidator
|
|
|
|
ValidationFunc func(input string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(data InputData) (*string, error) {
|
|
|
|
|
|
|
|
if data.Notice != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
tui.FmtItalic + data.Notice + tui.FmtReset + "\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
} else if data.Validator != nil && data.Validator.Notice() != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
tui.FmtItalic + data.Validator.Notice() + tui.FmtReset + "\n",
|
|
|
|
)
|
|
|
|
//fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;214m!\033[38;5;247m]\033[22m \033[3m%s\033[0m\n", data.Validator.Notice())
|
2024-07-07 21:45:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Question != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "?" + tui.FgColorGrey + "]" + tui.FmtReset + " " +
|
|
|
|
data.Question + " " + tui.FgColorGrey + ">>" + tui.Format(tui.FmtItalic, tui.FgColorGold) + "\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var input string
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
if _, err := fmt.Scanln(&input); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
width, _, err := term.GetSize(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.ValidationFunc != nil {
|
|
|
|
if !data.ValidationFunc(input) {
|
|
|
|
var lineNum int
|
|
|
|
|
|
|
|
if width == 0 {
|
|
|
|
if data.Notice != "" {
|
|
|
|
lineNum++
|
|
|
|
}
|
|
|
|
lineNum += 2
|
|
|
|
} else {
|
|
|
|
if data.Notice == "" {
|
|
|
|
lineNum = ((len(data.Question) + 5 + width - 1) / width) + 1
|
|
|
|
} else {
|
|
|
|
lineNum = ((len(data.Notice) + 5 + width) / width) + ((len(data.Question) + 5 + width - 1) / width) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < lineNum; i++ {
|
|
|
|
fmt.Printf("\033[A\033[K\033[0G")
|
|
|
|
}
|
2024-07-07 21:59:27 +01:00
|
|
|
|
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorRed + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
" Invalid input, please try again!\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
|
|
|
|
if data.Notice != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
tui.FmtItalic + data.Notice + tui.FmtReset + "\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
} else if data.Validator != nil {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
tui.FmtItalic + data.Validator.Notice() + tui.FmtReset + "\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Question != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "?" + tui.FgColorGrey + "]" + tui.FmtReset + " " +
|
|
|
|
data.Question + " " + tui.FgColorGrey + ">>" + tui.Format(tui.FmtItalic, tui.FgColorGold) + "\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if data.Validator != nil {
|
|
|
|
if !data.Validator.ValidationFunc(input) {
|
|
|
|
var lineNum int
|
|
|
|
|
|
|
|
if width == 0 {
|
|
|
|
if data.Notice != "" || data.Validator.Notice() != "" {
|
|
|
|
lineNum++
|
|
|
|
}
|
|
|
|
lineNum += 2
|
|
|
|
} else {
|
|
|
|
if data.Notice == "" && data.Validator.Notice() == "" {
|
|
|
|
lineNum = ((len(data.Question) + 5 + width - 1) / width) + 1
|
|
|
|
} else {
|
|
|
|
if data.Notice != "" {
|
|
|
|
lineNum = ((len(data.Notice) + 5 + width) / width) + ((len(data.Question) + 5 + width - 1) / width) + 1
|
|
|
|
} else if data.Validator.Notice() != "" {
|
|
|
|
lineNum = ((len(data.Validator.Notice()) + 5 + width - 1) / width) + ((len(data.Question) + 5 + width - 1) / width) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < lineNum; i++ {
|
|
|
|
fmt.Printf("\033[A\033[K\033[0G")
|
|
|
|
}
|
|
|
|
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorRed + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
" Invalid input, please try again!\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
|
|
|
|
if data.Notice != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
tui.FmtItalic + data.Notice + tui.FmtReset + "\n",
|
|
|
|
)
|
|
|
|
//fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;214m!\033[38;5;247m]\033[22m \033[3m%s\033[0m\n", data.Notice)
|
2024-07-07 21:45:35 +01:00
|
|
|
} else if data.Validator.Notice() != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
|
|
tui.FmtItalic + data.Validator.Notice() + tui.FmtReset + "\n",
|
|
|
|
)
|
2024-07-07 21:45:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if data.Question != "" {
|
2024-07-07 21:59:27 +01:00
|
|
|
fmt.Printf(
|
|
|
|
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
|
|
"[" + tui.FgColorGold + "?" + tui.FgColorGrey + "]" + tui.FmtReset + " " +
|
|
|
|
data.Question + " " + tui.FgColorGrey + ">>" + tui.Format(tui.FmtItalic, tui.FgColorGold) + "\n",
|
|
|
|
)
|
|
|
|
//fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;214m?\033[38;5;247m]\033[0m %s \033[38;5;247m>>\033[3m\033[38;5;214m\n", data.Question)
|
2024-07-07 21:45:35 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("\033[0m")
|
|
|
|
|
|
|
|
return &input, nil
|
|
|
|
|
|
|
|
}
|