aboutsummaryrefslogtreecommitdiff
path: root/go-src/windowsHelpers.go
blob: 13cb3c19de7b833728a6988e3f37babfd6965f10 (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
59
60
61
62
63
64
65
66
67
68
69
70
package ctfc

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"time"

	"gitlab.com/Syndamia/ctfc/go-src/ui"
	"gitlab.com/Syndamia/ctfc/go-src/utils"
)

func routinePaginatedSubwindow(messages *[]string, updateMessages func(*[]string), lastLine *int, page int) {
	for *lastLine > -1 {
		if *lastLine == len(*messages)-1 {
			updateMessages(messages)
			continue
		}

		*lastLine = len(*messages) - 1
		fmt.Print("\033[s")

		moveCursorUp(utils.PageSize + 2)

		pageMessages := utils.Paginate(page, *messages...)
		for i := 0; i < utils.PageSize-len(pageMessages); i++ {
			ui.EmptyLine()
		}
		for _, v := range pageMessages {
			fmt.Print("\033[K")
			ui.TextField(v)
		}

		ui.PageField(page, utils.TotalPages(len(*messages)))

		fmt.Print("\033[u")
		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)
}