From e68bf6185aef85aae52cd2d9a86a9d8b241623e4 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 5 Jul 2021 09:07:32 +0300 Subject: Implemented text, input and numbered fields and improved structure --- go-src/go.mod | 2 +- go-src/main.go | 8 +--- go-src/ui.go | 88 ------------------------------------ go-src/ui/ui.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++ go-src/utils.go | 18 -------- go-src/utils/utils.go | 18 ++++++++ go-src/windows/windows.go | 20 ++++++++ 7 files changed, 154 insertions(+), 113 deletions(-) delete mode 100644 go-src/ui.go create mode 100644 go-src/ui/ui.go delete mode 100644 go-src/utils.go create mode 100644 go-src/utils/utils.go create mode 100644 go-src/windows/windows.go diff --git a/go-src/go.mod b/go-src/go.mod index 06254fb..e949e73 100644 --- a/go-src/go.mod +++ b/go-src/go.mod @@ -1,3 +1,3 @@ -module gitlab.com/Syndamia/ctfc/-/tree/main/go-src +module gitlab.com/Syndamia/go-src go 1.16 diff --git a/go-src/main.go b/go-src/main.go index f06b1ff..b038900 100644 --- a/go-src/main.go +++ b/go-src/main.go @@ -1,11 +1,7 @@ package main -import "fmt" +import "gitlab.com/Syndamia/go-src/windows" 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")) + windows.Test() } diff --git a/go-src/ui.go b/go-src/ui.go deleted file mode 100644 index 77c6f90..0000000 --- a/go-src/ui.go +++ /dev/null @@ -1,88 +0,0 @@ -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/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 +} diff --git a/go-src/utils.go b/go-src/utils.go deleted file mode 100644 index 5d60ee9..0000000 --- a/go-src/utils.go +++ /dev/null @@ -1,18 +0,0 @@ -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) -} diff --git a/go-src/utils/utils.go b/go-src/utils/utils.go new file mode 100644 index 0000000..673f749 --- /dev/null +++ b/go-src/utils/utils.go @@ -0,0 +1,18 @@ +package utils + +// 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) +} diff --git a/go-src/windows/windows.go b/go-src/windows/windows.go new file mode 100644 index 0000000..f32b2d5 --- /dev/null +++ b/go-src/windows/windows.go @@ -0,0 +1,20 @@ +package windows + +import ( + "fmt" + + "gitlab.com/Syndamia/go-src/ui" +) + +func Test() { + // Temporary values for testing purposes + fmt.Println(ui.ErrorBox("Test error")) + fmt.Println(ui.NormalBox(true, "Page 1")) + fmt.Println(ui.NormalBox(false, "Page 1", "Page 2")) + fmt.Println(ui.NormalBox(true, "af", "Page dfsdf", "fd ffdfs dfsdf sd Page 3", "A")) + fmt.Println(ui.NormalBox(true, "Login")) + fmt.Println(ui.TextField("Login to your account")) + fmt.Println(ui.NumberedFields("Option 1", "Option 2", "Option 3")) + fmt.Println(ui.InputFieldFilled("Username", "John")) + fmt.Println(ui.InputField("Password")) +} -- cgit v1.2.3