aboutsummaryrefslogtreecommitdiff
path: root/go-src/ui/ui.go
blob: 976a3496b51d685df24f95663255a7a973bea075 (plain) (blame)
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
package ui

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"gitlab.com/Syndamia/ctfc/go-src/csi"
	"gitlab.com/Syndamia/ctfc/go-src/utils"
)

const (
	boxErrHorLine           = '═'
	boxErrHorDownLine       = '╦'
	boxErrHorUpLine         = '╩'
	boxErrVertLine          = '║'
	boxErrVertRightLine     = '╠'
	boxErrVertLeftLine      = '╣'
	boxErrTopLeftCorner     = '╔'
	boxErrTopRightCorner    = '╗'
	boxErrBottomLeftCorner  = '╚'
	boxErrBottomRightCorner = '╝'

	boxHorLine           = '─'
	boxHorDownLine       = '┬'
	boxHorUpLine         = '┴'
	boxVertLine          = '│'
	boxVertRightLine     = '├'
	boxVertLeftLine      = '┤'
	boxTopLeftCorner     = '┌'
	boxTopRightCorner    = '┐'
	boxBottomLeftCorner  = '└'
	boxBottomRightCorner = '┘'

	textRune  = '%'
	inputRune = '@'
	noRune    = ' '
)

var scanner = bufio.NewScanner(os.Stdin)

// Returns an error box
func ErrorBox(message string) {
	var box string
	// 2+len because message length doesn't accomodate the spaces between the left and right border
	box += fmt.Sprintf("%c%v%c\n", boxErrTopLeftCorner, utils.RepeatRune(boxErrHorLine, 2+len(message)), boxErrTopRightCorner)
	box += fmt.Sprintf("%c %v %c\n", boxErrVertLine, message, boxErrVertLine)
	box += fmt.Sprintf("%c%v%c", boxErrBottomLeftCorner, utils.RepeatRune(boxErrHorLine, 2+len(message)), boxErrBottomRightCorner)
	fmt.Println(box)
}

// Returns a normal box, most commonly used for the menu items
func NormalBox(railed bool, messages ...string) {
	var box string

	message := strings.Join(messages, " │ ")
	var messagesLengths []int

	for i := 0; i < len(messages)-1; i++ {
		messagesLengths = append(messagesLengths, len(messages[i]))
	}

	/* The message is the middle part between the left and right border,
	   so you have to add 2 for the spaces between it and the borders.
	   Since each "│" character adds 3 to the length (and not 1),
	   get the amount of "│" character from number of messages, double the value
	   and finally subtract (so, "││" is len 6, 2 "│" characters * 2 = 4, 6 - 4 = 2).
	*/
	messageLength := 2 + len(message) - 2*(len(messages)-1)

	box += fmt.Sprintf("%c%v%c\n", boxTopLeftCorner, utils.RepeatRune(boxHorLine, messageLength), boxTopRightCorner)
	box += fmt.Sprintf("%c %v %c\n", boxVertLine, message, boxVertLine)
	box += fmt.Sprintf("%c%v%c", boxBottomLeftCorner, utils.RepeatRune(boxHorLine, messageLength), boxBottomRightCorner)

	indTop := 0
	/* messageLength doesn't include the left and right spaces that are in middle part
	   and on each line there are \n characters (hence adding 2 at the end).
	   With this variable, we're practically at index 0 on the third line.
	*/
	indBot := 2*(messageLength+2) + 2

	if railed {
		box = utils.ReplaceAtIndex(box, boxVertRightLine, indBot)
	}

	for _, val := range messagesLengths {
		/* +1 Because message length doesn't include the " " in the beginning
		   +2 because we're now at the index of the last letter, and need to go to
		   the right border index
		   And that in total means we add 3 to length
		*/
		indTop += val + 3
		indBot += val + 3

		// Replaces the border character on the top with one that connects to the middle border: ┌───────────┬───────────┐
		box = utils.ReplaceAtIndex(box, boxHorDownLine, indTop)
		// Replaces the border character on the bottom with one that connects to the middle broder: └───────────┴───────────┘
		box = utils.ReplaceAtIndex(box, boxHorUpLine, indBot)
	}
	fmt.Println(box)
}

func TextField(message string) {
	fmt.Printf("%c%c %v\n", boxVertRightLine, textRune, message)
}

func TextFields(messsages ...string) {
	printMultiple(textRune, messsages...)
}

func InputField(specification string) string {
	fmt.Printf("%c%c %v : ", boxBottomLeftCorner, inputRune, specification)
	scanner.Scan()
	return scanner.Text()
}

func InputFieldFilled(specification string, input string) {
	fmt.Printf("%c%c %v : %v\n", boxVertRightLine, inputRune, specification, input)
}

func NumberedField(message string, number int) {
	fmt.Printf("%c%v %v\n", boxVertRightLine, number, message)
}

func NumberedFields(messages ...string) {
	printMultiple(noRune, messages...)
}

func PageField(current int, max int) {
	fmt.Printf("├─Page %v/%v─┘\n", current, max)
}

func EmptyLine() {
	fmt.Println(string(boxVertLine))
}

func EmptyLines(amount int) {
	for i := 0; i < amount; i++ {
		EmptyLine()
	}
}

func printMultiple(secondRune rune, messages ...string) {
	if len(messages) == 0 {
		return
	}

	secondString := string(secondRune)

	for i, v := range messages {
		csi.ClearLine()

		if secondRune == noRune {
			secondString = fmt.Sprint(i + 1)
		}
		fmt.Printf("%c%v %v\n", boxVertRightLine, secondString, v)
	}
}