This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.go
213 lines (198 loc) · 6.21 KB
/
tui.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/fatih/color"
)
// emoji stuff
const (
SUCCESS = "✅"
ERROR = "❌"
EXTRASPIN = 4
)
// colours
var (
PromptColour = color.New(color.FgBlue)
OptionColour = color.New(color.FgCyan)
BackColour = color.New(color.FgHiBlack)
SuccessColour = color.New(color.FgHiGreen)
ErrorColour = color.New(color.FgRed)
FatalColour = color.New(color.FgRed, color.Underline)
MessageColour = color.New(color.FgHiMagenta)
)
func title() string {
return "\n ▄▄█████▓ ██▀███ ▒█████ ██▓ ██▓ ▄████▄ ▒█████ ██▀███ ▓█████▄ \n ▓ ██▒ ▓▒▓██ ▒ ██▒▒██▒ ██▒▓██▒ ▓██▒ ▒██▀ ▀█ ▒██▒ ██▒▓██ ▒ ██▒▒██▀ ██▌\n ▒ ▓██░ ▒░▓██ ░▄█ ▒▒██░ ██▒▒██░ ▒██░ ▒▓█ ▄ ▒██░ ██▒▓██ ░▄█ ▒░██ █▌\n ░ ▓██▓ ░ ▒██▀▀█▄ ▒██ ██░▒██░ ▒██░ ▒▓▓▄ ▄██▒▒██ ██░▒██▀▀█▄ ░▓█▄ ▌\n ▒██▒ ░ ░██▓ ▒██▒░ ████▓▒░░██████▒░██████▒▒ ▓███▀ ░░ ████▓▒░░██▓ ▒██▒░▒████▓ \n ▒ ░░ ░ ▒▓ ░▒▓░░ ▒░▒░▒░ ░ ▒░▓ ░░ ▒░▓ ░░ ░▒ ▒ ░░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ▒▒▓ ▒ \n ░ ░▒ ░ ▒░ ░ ▒ ▒░ ░ ░ ▒ ░░ ░ ▒ ░ ░ ▒ ░ ▒ ▒░ ░▒ ░ ▒░ ░ ▒ ▒ \n ░ ░░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ▒ ░░ ░ ░ ░ ░ \n ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ░ \n ░ ░ \n https://github.com/jibstack64/trollcord v0.0.4"
}
// prompts the user with 'prompt' and returns their response.
// if 'retry' is true, then the user will be prompted until a response is given.
func getInput(prompt string, retry bool, retryString *string) string {
PromptColour.Println("\n" + prompt)
for {
BackColour.Print("> ")
var input string
inputReader := bufio.NewReader(os.Stdin)
input, _ = inputReader.ReadString('\n')
input = strings.ReplaceAll(input, "\n", "")
if retry && input == "" {
if retryString != nil {
ErrorColour.Println(*retryString)
}
continue
} else {
return input
}
}
}
// clears 'n' lines.
func clearLine(n int) {
for c := 0; c < n; c++ {
fmt.Print("\033[1A\033[K")
}
}
// creates a loading string.
// when finished becomes true, it stops.
func loading(text string, fn func(finished *bool, err *error)) error {
fmt.Print("\n")
chars := []string{
"\\", "/", "-", "\\", "/", "-",
}
c := 0
extra := 0
// track progress
var finished bool
var err error
go fn(&finished, &err)
for {
fmt.Printf("%s %s\n", MessageColour.Sprint(chars[c]), text)
time.Sleep(time.Second / 4)
clearLine(1) // clear line
c++
if c == len(chars) {
c = 0
}
if finished {
if extra <= EXTRASPIN {
extra++
} else {
break
}
}
}
// cool!
if err != nil {
fmt.Printf("%s %s\n", ERROR, ErrorColour.Sprint(text))
} else {
fmt.Printf("%s %s\n", SUCCESS, SuccessColour.Sprint(text))
}
return err
}
// prompts the user with 'prompt' and awaits their input.
// returns a boolean, yes being true and no being false.
func yesOrNo(prompt string) bool {
inputString := fmt.Sprintf("%s%s%s%s%s",
BackColour.Sprint("("), SuccessColour.Sprint("yes"), BackColour.Sprint("/"),
ErrorColour.Sprint("no"), BackColour.Sprint(")"))
PromptColour.Printf("\n%s %s\n", prompt, inputString)
for {
BackColour.Print("> ")
var input string
fmt.Scanln(&input)
input = strings.ToLower(input) // lower
if input == "yes" {
return true
} else if input == "no" {
return false
} else {
continue
}
}
}
// creates a progress bar that is updated according to 'length' and 'done'.
func progressBar(prompt string, fn func(length *int, done *int, err *error)) error {
fmt.Printf("\n%s\n", MessageColour.Sprint(prompt))
progress := "[%s]"
check := "#"
back := "~"
// track progress
var length int
var done int
var err error
go fn(&length, &done, &err)
for {
var checks string
donevl := 0
if done > 0 {
donevl = done / length * 20
}
for i := 0; i < donevl; i++ {
checks += check
}
for i := 0; i < 20-donevl; i++ {
checks += back
}
fmt.Printf("%s\n", fmt.Sprintf(progress, checks))
time.Sleep(time.Second / 4)
clearLine(1) // clear line
if done <= length || err != nil {
break
}
}
// cool!
if err != nil {
fmt.Printf("[%s]\n", ErrorColour.Sprint("~~~~~~~~~"+ERROR+"~~~~~~~~~"))
} else {
fmt.Printf("[%s]\n", SuccessColour.Sprint("~~~~~~~~~"+SUCCESS+"~~~~~~~~~"))
}
return err
}
// prompts the user with the given 'prompt' string.
// lists all options in 'options' as 'index+1: option'.
// returns selected option's index.
func fromSelection(prompt string, options []string) int {
PromptColour.Println("\n" + prompt)
for p, v := range options {
if v == "" {
continue
}
fmt.Printf("%s%s %s\n", OptionColour.Sprint(p+1), BackColour.Sprint("."), v)
}
for {
BackColour.Print("> ")
var input string
fmt.Scanln(&input)
index, err := strconv.Atoi(input)
index -= 1
if err != nil {
ErrorColour.Printf("'%s' is not an integer.\n", input)
continue
}
for p, v := range options {
if index == p && v != "" {
return p
}
}
if input != "" {
ErrorColour.Printf("%s is not a valid option.\n", input)
}
}
}
// prints 'text' in an underlined, red error text.
func fatal(text string) {
FatalColour.Println(text)
}
// prints 'text' in a red error text.
func errorPr(text string) {
ErrorColour.Println(text)
}
// prints 'text' in a light magenta tone.
func message(text string) {
MessageColour.Println(text)
}
// prints 'text' in a light green tone.
func success(text string) {
SuccessColour.Println(text)
}