aboutsummaryrefslogtreecommitdiff
path: root/go-src/windowsHelpers.go
blob: fb530c15b3a4890a56123302703841e9e899b0cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ctfc

import (
	"time"

	"gitlab.com/Syndamia/ctfc/go-src/csi"
	ctfcmath "gitlab.com/Syndamia/ctfc/go-src/ctfcMath"
	"gitlab.com/Syndamia/ctfc/go-src/ui"
)

/* 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, customLinesBelow int, numbered bool) {
	for *lastLine > -2 {
		// Update messages, if we've already shown the last message
		if *lastLine == len(*messages)-1 {
			updateMessages(messages)
			continue
		}

		*lastLine = len(*messages) - 1
		csi.SaveCursorPosition()

		csi.MoveCursorUpN(pageSize + 1 + customLinesBelow)
		csi.MoveCursorToBeginningOfLine()

		pageMessages := paginate(page, *messages...)

		// Leaves empty lines at the top, if there aren't enough messages to fill a full page
		// Works on the assumption that there are ui.EmptyLine(), where messages would be printed
		for i := 0; i < pageSize-len(pageMessages); i++ {
			csi.MoveCursorDown()
		}

		if numbered {
			ui.NumberedFields(pageMessages...)
		} else {
			ui.TextFields(pageMessages...)
		}

		ui.PageField(page, totalPages(len(*messages)))

		csi.RestoreCursorPosition()
		time.Sleep(500 * time.Millisecond)
	}
}