-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
80 lines (74 loc) · 1.51 KB
/
io.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
package gilc
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/mattn/go-colorable"
)
func YesNoDialog(prompt string, acceptEnter bool) (bool, error) {
for {
fmt.Printf("%s [y/n]: ", prompt)
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return false, err
}
switch strings.TrimSpace(strings.ToLower(answer)) {
case "y", "yes":
return true, nil
case "n", "no":
return false, nil
default:
fmt.Printf("'%s' is not a valid answer. Type 'y' or 'n'\n", answer)
}
}
}
func ReadLine(prompt string) (string, error) {
fmt.Print(prompt)
return bufio.NewReader(os.Stdin).ReadString('\n')
}
func WriteLine(msg string) {
Write(msg)
fmt.Print("\n")
}
func Write(msg string) {
out := colorable.NewColorableStdout()
indicator := false
for _, c := range msg {
// number after the "§"
if indicator {
indicator = false
switch c {
case '0', '8':
fmt.Fprint(out, "\033[30m")
case '1', '9':
fmt.Fprint(out, "\033[34")
case '2', 'a':
fmt.Fprintf(out, "\033[32m")
case '3', 'b':
fmt.Fprintf(out, "\033[36m")
case '4', 'c':
fmt.Fprintf(out, "\033[31m")
case '5', 'd':
fmt.Fprintf(out, "\033[35m")
case '6', 'e':
fmt.Fprintf(out, "\033[33m")
case '7', 'f':
fmt.Fprintf(out, "\033[37m")
case 'r':
fmt.Fprint(out, "\033[0m")
default:
break
}
continue
}
// § -> next is indicator
if c == '§' {
indicator = true
continue
}
// just normal letter
fmt.Print(string(c))
}
fmt.Fprint(out, "\033[0m")
}