diff options
| -rw-r--r-- | go-src/go.mod | 3 | ||||
| -rw-r--r-- | go-src/main.go | 11 | ||||
| -rw-r--r-- | go-src/ui.go | 88 | ||||
| -rw-r--r-- | go-src/utils.go | 18 |
4 files changed, 120 insertions, 0 deletions
diff --git a/go-src/go.mod b/go-src/go.mod new file mode 100644 index 0000000..06254fb --- /dev/null +++ b/go-src/go.mod @@ -0,0 +1,3 @@ +module gitlab.com/Syndamia/ctfc/-/tree/main/go-src + +go 1.16 diff --git a/go-src/main.go b/go-src/main.go new file mode 100644 index 0000000..f06b1ff --- /dev/null +++ b/go-src/main.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + // Temporary values for testing purposes + fmt.Println(errorBox("Test error")) + fmt.Println(normalBox(true, "Page 1")) + fmt.Println(normalBox(false, "Page 1", "Page 2")) + fmt.Println(normalBox(true, "af", "Page dfsdf", "fd ffdfs dfsdf sd Page 3", "A")) +} diff --git a/go-src/ui.go b/go-src/ui.go new file mode 100644 index 0000000..77c6f90 --- /dev/null +++ b/go-src/ui.go @@ -0,0 +1,88 @@ +package main + +import ( + "fmt" + "strings" +) + +const ( + boxErrHorLine = '═' + boxErrHorDownLine = '╦' + boxErrHorUpLine = '╩' + boxErrVertLine = '║' + boxErrVertRightLine = '╠' + boxErrVertLeftLine = '╣' + boxErrTopLeftCorner = '╔' + boxErrTopRightCorner = '╗' + boxErrBottomLeftCorner = '╚' + boxErrBottomRightCorner = '╝' + + boxHorLine = '─' + boxHorDownLine = '┬' + boxHorUpLine = '┴' + boxVertLine = '│' + boxVertRightLine = '├' + boxVertLeftLine = '┤' + boxTopLeftCorner = '┌' + boxTopRightCorner = '┐' + boxBottomLeftCorner = '└' + boxBottomRightCorner = '┘' +) + +// 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, repeatRune(boxErrHorLine, 2+len(message)), boxErrTopRightCorner) + box += fmt.Sprintf("%c %v %c\n", boxErrVertLine, message, boxErrVertLine) + box += fmt.Sprintf("%c%v%c\n", boxErrBottomLeftCorner, 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, repeatRune(boxHorLine, messageLength), boxTopRightCorner) + box += fmt.Sprintf("%c %v %c\n", boxVertLine, message, boxVertLine) + box += fmt.Sprintf("%c%v%c\n", boxBottomLeftCorner, 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 = 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 = replaceAtIndex(box, boxHorDownLine, indTop) + // Replaces the border character on the bottom with one that connects to the middle broder: └───────────┴───────────┘ + box = replaceAtIndex(box, boxHorUpLine, indBot) + } + return +} diff --git a/go-src/utils.go b/go-src/utils.go new file mode 100644 index 0000000..5d60ee9 --- /dev/null +++ b/go-src/utils.go @@ -0,0 +1,18 @@ +package main + +// Repeats a rune given amount of times and returns the result as a string +func repeatRune(r rune, times int) (result string) { + for i := 0; i < times; i++ { + result += string(r) + } + return +} + +// Replaces a character inside a string with a given rune at index +// +// Thanks https://stackoverflow.com/a/24894202/12036073 +func replaceAtIndex(in string, r rune, i int) string { + out := []rune(in) + out[i] = r + return string(out) +} |
