aboutsummaryrefslogtreecommitdiff
path: root/go-src/windowsHelpers.go
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-07-22 14:02:11 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-07-22 14:02:11 +0300
commit3d3324e36311aadd02e25e8e11e5d98272c0aa0e (patch)
tree8e30ae5927148506d4644a8de1bbdc128fb53437 /go-src/windowsHelpers.go
parentffc2c1273183b488de9cc4fc282379c2a042bd6d (diff)
downloadctfc-3d3324e36311aadd02e25e8e11e5d98272c0aa0e.tar
ctfc-3d3324e36311aadd02e25e8e11e5d98272c0aa0e.tar.gz
ctfc-3d3324e36311aadd02e25e8e11e5d98272c0aa0e.zip
Moved some stuff from windows.go to windowsHelpers.go, implemented some consistency and code organization improvements
Diffstat (limited to 'go-src/windowsHelpers.go')
-rw-r--r--go-src/windowsHelpers.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go
index fb530c1..d4ff90c 100644
--- a/go-src/windowsHelpers.go
+++ b/go-src/windowsHelpers.go
@@ -1,6 +1,7 @@
package ctfc
import (
+ "strings"
"time"
"gitlab.com/Syndamia/ctfc/go-src/csi"
@@ -56,3 +57,93 @@ func routinePaginatedSubwindow(messages *[]string, updateMessages func(*[]string
time.Sleep(500 * time.Millisecond)
}
}
+
+func initPaginatedValues(defaultLength int, values []string) {
+ if len(values) == defaultLength {
+ values = append(append(values, "."), "")
+ }
+}
+
+/* Input actions */
+
+type inputAction struct {
+ value string
+ execute window
+ args []string
+}
+
+func handleInputActions(input string, handleNav bool, ia ...inputAction) func() {
+ if handleNav {
+ ia = append(ia,
+ inputAction{"C", chatsWindow, nil},
+ inputAction{"D", chatsWindow, nil},
+ inputAction{"A", chatsWindow, nil},
+ inputAction{"L", logoutWindow, nil},
+ )
+ }
+ for _, v := range ia {
+ if strings.ToLower(input) == strings.ToLower(v.value) {
+ return func() { v.execute(v.args...) }
+ }
+ }
+ return nil
+}
+
+/* Form */
+
+type formInput struct {
+ name string
+ specification string
+ validationFunc func(string) bool
+}
+
+func formWindow(boxTitle string, backWindow window, formInputs []formInput, currentValues ...string) (userInputs []string) {
+ userInputs = currentValues
+ // Go through all inputs
+ for i, v := range formInputs[len(currentValues):] {
+ csi.ClearScreen()
+
+ ui.NormalBox(true, boxTitle)
+
+ // Show filled input fields
+ for j, va := range userInputs {
+ ui.InputFieldFilled(formInputs[j].name, va)
+ }
+
+ // Wait for input on current input field
+ input := ui.InputField(v.name + v.specification)
+
+ // Go to the "backWindow", if we're on the first input, we are given a back window, and the letter B is given
+ if i == 0 && backWindow != nil && strings.ToLower(input) == "b" {
+ defer backWindow()
+ return
+ }
+
+ // If we're validating input data, try to validate
+ if v.validationFunc != nil {
+ // If input data isn't valid, show an error and the same screen
+ if !v.validationFunc(input) {
+ formCallback := func(...string) {
+ defer formWindow(boxTitle, backWindow, formInputs, userInputs...)
+ }
+ defer showError(invalidValueFor(v.name), formCallback)
+ return
+ }
+ }
+
+ // Add current input as a filled input
+ userInputs = append(userInputs, input)
+ }
+ return
+}
+
+/* Error */
+
+func showError(message string, callback window, callbackData ...string) {
+ csi.ClearScreen()
+
+ ui.ErrorBox(message)
+ csi.BlockClearScreenNextTime()
+
+ defer callback(callbackData...)
+}