/
cli.go
81 lines (72 loc) · 2 KB
/
cli.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
/*
* Copyright 2018 Information Systems Engineering, TU Berlin, Germany
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This is being developed for the DITAS Project: https://www.ditas-project.eu/
*/
package kcc
import (
"bufio"
"fmt"
"os"
"strconv"
"gopkg.in/gookit/color.v1"
)
var scanner = bufio.NewScanner(os.Stdin)
func SimpleQuestion(question string, style color.Color) bool {
style.Println(question + "[y/n]")
scanner.Scan()
value := scanner.Text()
if value == "y" || value == "Y" || value == "yes" || value == "YES" {
return true
}
return false
}
func ReadString(question string, style color.Color, repeats int, validate func(string) bool) (*string, error) {
var value string
asked := -1
for {
if repeats < asked {
break
}
asked++
style.Println(question)
scanner.Scan()
value = scanner.Text()
if validate != nil && validate(value) {
return &value, nil
} else {
return &value, nil
}
}
return nil, fmt.Errorf("no awnser recived")
}
func Menu(question string, style color.Color, options []string) int {
selector := ""
for i, s := range options {
selector += fmt.Sprintf("\t%d: %s\n", i, s)
}
for {
num, err := ReadString(fmt.Sprintf("%s:\n %s", question, selector), style, 1, func(input string) bool {
num, err := strconv.Atoi(input)
return err == nil && num >= 0 && num < len(options)
})
if err == nil {
i, _ := strconv.Atoi(*num)
return i
} else {
color.LightRed.Println("Wrong Selection, please try again.")
}
}
}