Compare commits
No commits in common. "main" and "0.0.1" have entirely different histories.
7 changed files with 25 additions and 350 deletions
64
ansi.go
64
ansi.go
|
@ -1,64 +0,0 @@
|
||||||
package tui
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"golang.org/x/term"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Colors = string
|
|
||||||
type Fmt = string
|
|
||||||
|
|
||||||
// Formatting
|
|
||||||
const (
|
|
||||||
FmtReset Colors = "\033[0m"
|
|
||||||
FmtBold = "\033[1m"
|
|
||||||
FmtBoldReset = "\033[22m"
|
|
||||||
FmtDim = "\033[2m"
|
|
||||||
FmtDimReset = FmtBoldReset
|
|
||||||
FmtItalic = "\033[3m"
|
|
||||||
FmtItalicReset = "\033[23m"
|
|
||||||
FmtUnderline = "\033[4m"
|
|
||||||
FmtUnderlineReset = "\033[24m"
|
|
||||||
FmtBlink = "\033[5m"
|
|
||||||
FmtBlinkReset = "\033[25m"
|
|
||||||
FmtReverse = "\033[7m"
|
|
||||||
FmtReverseReset = "\033[27m"
|
|
||||||
FmtHidden = "\033[8m"
|
|
||||||
FmtHiddenReset = "\033[28m"
|
|
||||||
FmtStrikethrough = "\033[9m"
|
|
||||||
FmtStrikethroughReset = "\033[29m"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Foreground Colors
|
|
||||||
const (
|
|
||||||
FgColorGrey Fmt = "\033[38;5;247m"
|
|
||||||
FgColorGold = "\033[38;5;214m"
|
|
||||||
FgColorGreen = "\033[38;5;34m"
|
|
||||||
FgColorRed = "\033[38;5;167m"
|
|
||||||
)
|
|
||||||
|
|
||||||
func BlankLine() {
|
|
||||||
|
|
||||||
width, _, _ := term.GetSize(int(os.Stdin.Fd()))
|
|
||||||
|
|
||||||
// Assume ptero terminal if no width.
|
|
||||||
if width == 0 {
|
|
||||||
fmt.Print("\033[1B")
|
|
||||||
} else {
|
|
||||||
fmt.Println("")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func Format(codes ...string) string {
|
|
||||||
|
|
||||||
var str strings.Builder
|
|
||||||
|
|
||||||
for _, code := range codes {
|
|
||||||
str.WriteString(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
return str.String()
|
|
||||||
}
|
|
|
@ -2,7 +2,6 @@ package confirmation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.eggactyl.cloud/Eggactyl/tui"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
@ -16,23 +15,11 @@ type InputData struct {
|
||||||
func New(data InputData) (*bool, error) {
|
func New(data InputData) (*bool, error) {
|
||||||
|
|
||||||
if data.Notice != "" {
|
if data.Notice != "" {
|
||||||
fmt.Printf(
|
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)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.Question != "" {
|
if data.Question != "" {
|
||||||
fmt.Printf(
|
fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;214m?\033[38;5;247m]\033[0m %s (\033[38;5;34my\033[0m/\033[38;5;167mn\033[0m) \033[38;5;247m>>\033[3m\033[38;5;214m\n", data.Question)
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtReset + " " +
|
|
||||||
data.Question + " " +
|
|
||||||
"(" + tui.FgColorGreen + "y" + tui.FmtReset + "/" + tui.FgColorRed + "n" + tui.FmtReset + ") " +
|
|
||||||
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;34my\033[0m/\033[38;5;167mn\033[0m) \033[38;5;247m>>\033[3m\033[38;5;214m\n", data.Question)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var input string
|
var input string
|
||||||
|
@ -76,31 +63,14 @@ inputLoop:
|
||||||
for i := 0; i < lineNum; i++ {
|
for i := 0; i < lineNum; i++ {
|
||||||
fmt.Printf("\033[A\033[K\033[0G")
|
fmt.Printf("\033[A\033[K\033[0G")
|
||||||
}
|
}
|
||||||
fmt.Printf(
|
fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;167m!\033[38;5;247m]\033[0m Invalid input, please try again!\n")
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtReset + " " +
|
|
||||||
"Invalid input, please try again!\n",
|
|
||||||
)
|
|
||||||
//fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;167m!\033[38;5;247m]\033[0m Invalid input, please try again!\n")
|
|
||||||
|
|
||||||
if data.Notice != "" {
|
if data.Notice != "" {
|
||||||
fmt.Printf(
|
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)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if data.Question != "" {
|
if data.Question != "" {
|
||||||
fmt.Printf(
|
fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;214m?\033[38;5;247m]\033[0m %s (\033[38;5;34my\033[0m/\033[38;5;167mn\033[0m) \033[38;5;247m>>\033[3m\033[38;5;214m\n", data.Question)
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtReset + " " +
|
|
||||||
data.Question + " " +
|
|
||||||
"(" + tui.FgColorGreen + "y" + tui.FmtReset + "/" + tui.FgColorRed + "n" + tui.FmtReset + ") " +
|
|
||||||
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;34my\033[0m/\033[38;5;167mn\033[0m) \033[38;5;247m>>\033[3m\033[38;5;214m\n", data.Question)
|
|
||||||
}
|
}
|
||||||
continue inputLoop
|
continue inputLoop
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
package displaylist
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.eggactyl.cloud/Eggactyl/tui"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ListOptions struct {
|
|
||||||
Title string
|
|
||||||
Items []Item
|
|
||||||
}
|
|
||||||
|
|
||||||
type Item struct {
|
|
||||||
Key string
|
|
||||||
Value string
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(opts ListOptions) {
|
|
||||||
|
|
||||||
fmt.Println(tui.FgColorGrey + "[ " + tui.Format(tui.FgColorGold, tui.FmtUnderline) + opts.Title + tui.Format(tui.FmtUnderlineReset, tui.FgColorGrey) + " ]" + tui.FmtReset)
|
|
||||||
|
|
||||||
for _, item := range opts.Items {
|
|
||||||
fmt.Println(itemString(item.Key, item.Value))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func itemString(key string, value string) string {
|
|
||||||
return tui.FgColorGrey + "[ " + tui.FgColorGold + key + " " + tui.FgColorGrey + value + tui.FgColorGrey + " ]" + tui.FmtReset
|
|
||||||
}
|
|
4
go.mod
4
go.mod
|
@ -1,6 +1,6 @@
|
||||||
module git.eggactyl.cloud/Eggactyl/tui
|
module git.shadowhosting.xyz/Eggactyl/tui
|
||||||
|
|
||||||
go 1.22.5
|
go 1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/nicksnyder/go-i18n/v2 v2.4.0
|
github.com/nicksnyder/go-i18n/v2 v2.4.0
|
||||||
|
|
41
list/list.go
41
list/list.go
|
@ -3,7 +3,6 @@ package list
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.eggactyl.cloud/Eggactyl/tui"
|
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -210,15 +209,11 @@ func (l *ListData) renderList() {
|
||||||
|
|
||||||
currentPage := l.pages[l.currentPage]
|
currentPage := l.pages[l.currentPage]
|
||||||
|
|
||||||
listNotice := tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
listNotice := fmt.Sprintf("\033[1m\033[38;5;247m[\033[38;5;214m!\033[38;5;247m]\033[22m \033[3mPlease choose an option from 1 - %d\033[0m\n", len(currentPage.Cache))
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
tui.FmtItalic + fmt.Sprintf("Please choose an option from 1 - %d", len(currentPage.Cache)) + "\n"
|
|
||||||
|
|
||||||
//listNotice := fmt.Sprintf("\033[1m\033[38;5;247m[\033[38;5;214m!\033[38;5;247m]\033[22m \033[3mPlease choose an option from 1 - %d\033[0m\n", len(currentPage.Cache))
|
|
||||||
l.strLengths = append(l.strLengths, len(removeANSIEscapeCodes(listNotice)))
|
l.strLengths = append(l.strLengths, len(removeANSIEscapeCodes(listNotice)))
|
||||||
fmt.Print(listNotice)
|
fmt.Print(listNotice)
|
||||||
|
|
||||||
listTitle := tui.FgColorGrey + "[ " + tui.Format(tui.FgColorGold, tui.FmtUnderline) + currentPage.Title + ":" + tui.Format(tui.FmtUnderlineReset, tui.FgColorGrey) + " ]" + tui.FmtReset + "\n"
|
listTitle := fmt.Sprintf("\033[1m\033[38;5;247m\033[4m%s:\033[0m\n", currentPage.Title)
|
||||||
l.strLengths = append(l.strLengths, len(removeANSIEscapeCodes(listTitle)))
|
l.strLengths = append(l.strLengths, len(removeANSIEscapeCodes(listTitle)))
|
||||||
fmt.Print(listTitle)
|
fmt.Print(listTitle)
|
||||||
|
|
||||||
|
@ -236,23 +231,17 @@ func (l *ListData) renderList() {
|
||||||
|
|
||||||
var userInputColor string
|
var userInputColor string
|
||||||
if index == len(currentPage.Cache)-1 {
|
if index == len(currentPage.Cache)-1 {
|
||||||
userInputColor = tui.Format(tui.FmtItalic, tui.FgColorGrey)
|
userInputColor = "\033[3m\033[38;5;214m"
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := item.Value.(string); ok {
|
if _, ok := item.Value.(string); ok {
|
||||||
if item.Value == "action_back" {
|
if item.Value == "action_back" {
|
||||||
listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorRed) + strconv.Itoa(index+1) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset +
|
listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;167m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
|
||||||
fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n"
|
|
||||||
//listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;167m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
|
|
||||||
} else {
|
} else {
|
||||||
listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorGold) + strconv.Itoa(index+1) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset +
|
listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
|
||||||
fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n"
|
|
||||||
//listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
listItem = " " + tui.FgColorGrey + "[" + tui.Format(tui.FmtBold, tui.FgColorGold) + strconv.Itoa(index+1) + tui.Format(tui.FmtBoldReset, tui.FgColorGrey) + "]" + tui.FmtReset +
|
listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
|
||||||
fmt.Sprintf(" %-*s ", longestStrLength, item.Label) + tui.Format(tui.FmtItalic, tui.FgColorGrey) + item.Notice + tui.FmtReset + userInputColor + "\n"
|
|
||||||
//listItem = fmt.Sprintf(" \033[38;5;247m[\033[1m\033[38;5;214m%d\033[22m\033[38;5;247m]\033[0m %-*s \033[3m\033[38;5;247m%s\033[0m%s\n", index+1, longestStrLength, item.Label, item.Notice, userInputColor)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
l.strLengths = append(l.strLengths, len(removeANSIEscapeCodes(listItem)))
|
l.strLengths = append(l.strLengths, len(removeANSIEscapeCodes(listItem)))
|
||||||
|
@ -282,7 +271,7 @@ func (l *ListData) inputHandler(items []ListItem) ListItem {
|
||||||
totalLineNum = len(l.strLengths)
|
totalLineNum = len(l.strLengths)
|
||||||
} else {
|
} else {
|
||||||
for _, strLength := range l.strLengths {
|
for _, strLength := range l.strLengths {
|
||||||
totalLineNum += (strLength) / width
|
totalLineNum += ((strLength) / width)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalLineNum++ //User input line
|
totalLineNum++ //User input line
|
||||||
|
@ -291,12 +280,7 @@ func (l *ListData) inputHandler(items []ListItem) ListItem {
|
||||||
fmt.Printf("\033[0m\033[A\033[K\033[0G")
|
fmt.Printf("\033[0m\033[A\033[K\033[0G")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;167m!\033[38;5;247m]\033[0m Invalid input, please try again!\n")
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorRed + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
" Invalid input, please try again!\n",
|
|
||||||
)
|
|
||||||
//fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;167m!\033[38;5;247m]\033[0m Invalid input, please try again!\n")
|
|
||||||
l.strLengths = []int{}
|
l.strLengths = []int{}
|
||||||
|
|
||||||
l.renderList()
|
l.renderList()
|
||||||
|
@ -312,7 +296,7 @@ func (l *ListData) inputHandler(items []ListItem) ListItem {
|
||||||
totalLineNum = len(l.strLengths)
|
totalLineNum = len(l.strLengths)
|
||||||
} else {
|
} else {
|
||||||
for _, strLength := range l.strLengths {
|
for _, strLength := range l.strLengths {
|
||||||
totalLineNum += (strLength) / width
|
totalLineNum += ((strLength) / width)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalLineNum++ //User input line
|
totalLineNum++ //User input line
|
||||||
|
@ -321,12 +305,7 @@ func (l *ListData) inputHandler(items []ListItem) ListItem {
|
||||||
fmt.Printf("\033[A\033[K\033[0G")
|
fmt.Printf("\033[A\033[K\033[0G")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;167m!\033[38;5;247m]\033[0m Invalid input, please try again!\n")
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorRed + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
" Invalid input, please try again!\n",
|
|
||||||
)
|
|
||||||
//fmt.Printf("\033[1m\033[38;5;247m[\033[38;5;167m!\033[38;5;247m]\033[0m Invalid input, please try again!\n")
|
|
||||||
l.strLengths = []int{}
|
l.strLengths = []int{}
|
||||||
|
|
||||||
l.renderList()
|
l.renderList()
|
||||||
|
|
|
@ -2,7 +2,6 @@ package progress
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.eggactyl.cloud/Eggactyl/tui"
|
|
||||||
"math"
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -60,9 +59,9 @@ func (p *ProgressBar) render(final bool) {
|
||||||
var blockColor string
|
var blockColor string
|
||||||
|
|
||||||
if final {
|
if final {
|
||||||
blockColor = tui.FgColorGreen
|
blockColor = "34"
|
||||||
} else {
|
} else {
|
||||||
blockColor = tui.FgColorGold
|
blockColor = "214"
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(p.desc)
|
sb.WriteString(p.desc)
|
||||||
|
@ -77,13 +76,11 @@ func (p *ProgressBar) render(final bool) {
|
||||||
|
|
||||||
sb.WriteString(percentString)
|
sb.WriteString(percentString)
|
||||||
|
|
||||||
sb.WriteString(tui.Format(tui.FmtBold, tui.FgColorGrey) + " [" + tui.FmtReset)
|
sb.WriteString("\033[1m\033[38;5;247m [\033[0m")
|
||||||
//sb.WriteString("\033[1m\033[38;5;247m [\033[0m")
|
|
||||||
|
|
||||||
for i := 0; i < int(numFilled); i++ {
|
for i := 0; i < int(numFilled); i++ {
|
||||||
|
|
||||||
sb.WriteString(blockColor + "█" + tui.FmtReset)
|
sb.WriteString(fmt.Sprintf("\033[38;5;%sm█\033[0m", blockColor))
|
||||||
//sb.WriteString(fmt.Sprintf("%sm█\033[0m", blockColor))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,25 +88,21 @@ func (p *ProgressBar) render(final bool) {
|
||||||
|
|
||||||
numBlank = numBlank - 1
|
numBlank = numBlank - 1
|
||||||
|
|
||||||
sb.WriteString(tui.FgColorGold + "▒" + tui.FmtReset)
|
sb.WriteString("\033[38;5;214m▒\033[0m")
|
||||||
//sb.WriteString("\033[38;5;214m▒\033[0m")
|
|
||||||
|
|
||||||
for i := 0; i < int(numBlank); i++ {
|
for i := 0; i < int(numBlank); i++ {
|
||||||
sb.WriteString(tui.FgColorGrey + "░" + tui.FmtReset)
|
sb.WriteString("\033[38;5;247m░\033[0m")
|
||||||
//sb.WriteString("\033[38;5;247m░\033[0m")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
for i := 0; i < int(numBlank); i++ {
|
for i := 0; i < int(numBlank); i++ {
|
||||||
sb.WriteString(tui.FgColorGrey + "░" + tui.FmtReset)
|
sb.WriteString("\033[38;5;247m░\033[0m")
|
||||||
//sb.WriteString("\033[38;5;247m░\033[0m")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.WriteString(tui.Format(tui.FmtBold, tui.FgColorGrey) + "]" + tui.FmtReset)
|
sb.WriteString("\033[1m\033[38;5;247m]\033[0m")
|
||||||
//sb.WriteString("\033[1m\033[38;5;247m]\033[0m")
|
|
||||||
|
|
||||||
width, _, err := term.GetSize(0)
|
width, _, err := term.GetSize(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,172 +0,0 @@
|
||||||
package textinput
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"git.eggactyl.cloud/Eggactyl/tui"
|
|
||||||
"git.eggactyl.cloud/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 != "" {
|
|
||||||
fmt.Printf(
|
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
tui.FmtItalic + data.Notice + tui.FmtReset + "\n",
|
|
||||||
)
|
|
||||||
} else if data.Validator != nil && data.Validator.Notice() != "" {
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
if data.Question != "" {
|
|
||||||
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",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf(
|
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorRed + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
" Invalid input, please try again!\n",
|
|
||||||
)
|
|
||||||
|
|
||||||
if data.Notice != "" {
|
|
||||||
fmt.Printf(
|
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
tui.FmtItalic + data.Notice + tui.FmtReset + "\n",
|
|
||||||
)
|
|
||||||
} else if data.Validator != nil {
|
|
||||||
fmt.Printf(
|
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
tui.FmtItalic + data.Validator.Notice() + tui.FmtReset + "\n",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if data.Question != "" {
|
|
||||||
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",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf(
|
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorRed + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
" Invalid input, please try again!\n",
|
|
||||||
)
|
|
||||||
|
|
||||||
if data.Notice != "" {
|
|
||||||
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)
|
|
||||||
} else if data.Validator.Notice() != "" {
|
|
||||||
fmt.Printf(
|
|
||||||
tui.Format(tui.FmtBold, tui.FgColorGrey) +
|
|
||||||
"[" + tui.FgColorGold + "!" + tui.FgColorGrey + "]" + tui.FmtBoldReset + " " +
|
|
||||||
tui.FmtItalic + data.Validator.Notice() + tui.FmtReset + "\n",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if data.Question != "" {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("\033[0m")
|
|
||||||
|
|
||||||
return &input, nil
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in a new issue