aboutsummaryrefslogtreecommitdiff
path: root/go-src/windowsHelpers.go
diff options
context:
space:
mode:
Diffstat (limited to 'go-src/windowsHelpers.go')
-rw-r--r--go-src/windowsHelpers.go70
1 files changed, 27 insertions, 43 deletions
diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go
index 13cb3c1..909a673 100644
--- a/go-src/windowsHelpers.go
+++ b/go-src/windowsHelpers.go
@@ -1,70 +1,54 @@
package ctfc
import (
- "fmt"
- "os"
- "os/exec"
- "runtime"
"time"
+ "gitlab.com/Syndamia/ctfc/go-src/csi"
+ ctfcmath "gitlab.com/Syndamia/ctfc/go-src/ctfcMath"
"gitlab.com/Syndamia/ctfc/go-src/ui"
- "gitlab.com/Syndamia/ctfc/go-src/utils"
)
+/* Pagination */
+
+const pageSize = 15
+
+func totalPages(messageAmount int) int {
+ return ctfcmath.CeilDivInt(messageAmount, pageSize)
+}
+
+func paginate(page int, messages ...string) []string {
+ return messages[ctfcmath.MaxInt(len(messages)-pageSize*page, 0) : len(messages)-pageSize*(page-1)]
+}
+
+// Must be run in a routine ( go routinePaginatedSubwindow(...) )
+// There must be empty lines for the subwindow
func routinePaginatedSubwindow(messages *[]string, updateMessages func(*[]string), lastLine *int, page int) {
for *lastLine > -1 {
+ // Update messages, if we've already shown the last message
if *lastLine == len(*messages)-1 {
updateMessages(messages)
continue
}
*lastLine = len(*messages) - 1
- fmt.Print("\033[s")
+ csi.SaveCursorPosition()
- moveCursorUp(utils.PageSize + 2)
+ csi.MoveCursorUpN(pageSize + 2) // +2 because of the input and pagination lines
+ csi.MoveCursorToBeginningOfLine()
- pageMessages := utils.Paginate(page, *messages...)
- for i := 0; i < utils.PageSize-len(pageMessages); i++ {
- ui.EmptyLine()
+ pageMessages := paginate(page, *messages...)
+ // Leaves empty lines at the top, if there aren't enough messages to fill a full page
+ for i := 0; i < pageSize-len(pageMessages); i++ {
+ csi.MoveCursorDown()
}
for _, v := range pageMessages {
- fmt.Print("\033[K")
+ csi.ClearLine()
ui.TextField(v)
}
- ui.PageField(page, utils.TotalPages(len(*messages)))
+ ui.PageField(page, totalPages(len(*messages)))
- fmt.Print("\033[u")
+ csi.RestoreCursorPosition()
time.Sleep(500 * time.Millisecond)
}
}
-
-var clearNext = true
-
-func clearScreen() {
- if !clearNext {
- clearNext = true
- return
- }
-
- if runtime.GOOS == "windows" {
- cmd := exec.Command("cmd", "/c", "cls")
- cmd.Stdout = os.Stdout
- cmd.Run()
- } else {
- fmt.Println("\033[2J")
- }
-}
-
-func moveCursorUp(times int) {
- fmt.Printf("\033[%vA", times)
- fmt.Print("\033[E")
-}
-
-func moveCursorDown(times int) {
- fmt.Printf("\033[%vB", times)
-}
-
-func moveCursorRight(times int) {
- fmt.Printf("\033[%vC", times)
-}