diff options
| -rw-r--r-- | go-src/go.mod | 2 | ||||
| -rw-r--r-- | go-src/main.go | 8 | ||||
| -rw-r--r-- | go-src/ui/ui.go (renamed from go-src/ui.go) | 45 | ||||
| -rw-r--r-- | go-src/utils/utils.go (renamed from go-src/utils.go) | 6 | ||||
| -rw-r--r-- | go-src/windows/windows.go | 20 |
5 files changed, 61 insertions, 20 deletions
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/ui.go index 77c6f90..70ffd67 100644 --- a/go-src/ui.go +++ b/go-src/ui/ui.go @@ -1,8 +1,10 @@ -package main +package ui import ( "fmt" "strings" + + "gitlab.com/Syndamia/go-src/utils" ) const ( @@ -27,19 +29,22 @@ const ( boxTopRightCorner = '┐' boxBottomLeftCorner = '└' boxBottomRightCorner = '┘' + + textRune = '%' + inputRune = '@' ) // Returns an error box -func errorBox(message string) (box string) { +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", boxErrTopLeftCorner, utils.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) + 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) { +func NormalBox(railed bool, messages ...string) (box string) { message := strings.Join(messages, " │ ") var messagesLengths []int @@ -55,9 +60,9 @@ func normalBox(railed bool, messages ...string) (box string) { */ 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", boxTopLeftCorner, utils.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) + 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 @@ -67,7 +72,7 @@ func normalBox(railed bool, messages ...string) (box string) { indBot := 2*(messageLength+2) + 2 if railed { - box = replaceAtIndex(box, boxVertRightLine, indBot) + box = utils.ReplaceAtIndex(box, boxVertRightLine, indBot) } for _, val := range messagesLengths { @@ -80,9 +85,29 @@ func normalBox(railed bool, messages ...string) (box string) { indBot += val + 3 // Replaces the border character on the top with one that connects to the middle border: ┌───────────┬───────────┐ - box = replaceAtIndex(box, boxHorDownLine, indTop) + box = utils.ReplaceAtIndex(box, boxHorDownLine, indTop) // Replaces the border character on the bottom with one that connects to the middle broder: └───────────┴───────────┘ - box = replaceAtIndex(box, boxHorUpLine, indBot) + 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/utils.go index 5d60ee9..673f749 100644 --- a/go-src/utils.go +++ b/go-src/utils/utils.go @@ -1,7 +1,7 @@ -package main +package utils // Repeats a rune given amount of times and returns the result as a string -func repeatRune(r rune, times int) (result string) { +func RepeatRune(r rune, times int) (result string) { for i := 0; i < times; i++ { result += string(r) } @@ -11,7 +11,7 @@ func repeatRune(r rune, times int) (result string) { // 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 { +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")) +} |
