aboutsummaryrefslogtreecommitdiff
path: root/go-src/ui
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-07-05 09:07:32 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-07-05 09:07:32 +0300
commite68bf6185aef85aae52cd2d9a86a9d8b241623e4 (patch)
tree6d38b0e90847345fc522a049d5cba93958a2bb75 /go-src/ui
parentd4f404e53fb0ab37ec0ffa95d69902410bfe0368 (diff)
downloadctfc-e68bf6185aef85aae52cd2d9a86a9d8b241623e4.tar
ctfc-e68bf6185aef85aae52cd2d9a86a9d8b241623e4.tar.gz
ctfc-e68bf6185aef85aae52cd2d9a86a9d8b241623e4.zip
Implemented text, input and numbered fields and improved structure
Diffstat (limited to 'go-src/ui')
-rw-r--r--go-src/ui/ui.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/go-src/ui/ui.go b/go-src/ui/ui.go
new file mode 100644
index 0000000..70ffd67
--- /dev/null
+++ b/go-src/ui/ui.go
@@ -0,0 +1,113 @@
+package ui
+
+import (
+ "fmt"
+ "strings"
+
+ "gitlab.com/Syndamia/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 = '@'
+)
+
+// Returns an error box
+func ErrorBox(message string) (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)
+ return
+}
+
+// Returns a normal box, most commonly used for the menu items
+func NormalBox(railed bool, messages ...string) (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)
+ }
+ return
+}
+
+func TextField(message string) string {
+ return fmt.Sprintf("%c%c %v", boxVertRightLine, textRune, message)
+}
+
+func InputField(specification string) string {
+ return fmt.Sprintf("%c%c %v : ", boxBottomLeftCorner, inputRune, specification)
+}
+
+func InputFieldFilled(specification string, input string) string {
+ return utils.ReplaceAtIndex(InputField(specification), boxVertRightLine, 0) + input
+}
+
+func NumberedFields(messages ...string) (result string) {
+ for i, v := range messages {
+ result += fmt.Sprintf("%c%v %v\n", boxVertRightLine, i+1, v)
+ }
+ result = result[:len(result)-1]
+ return
+}