diff options
| -rw-r--r-- | go-src/ctfcMath/ctfcMath.go | 14 | ||||
| -rw-r--r-- | go-src/utils/utils.go | 14 | ||||
| -rw-r--r-- | go-src/windows.go | 86 | ||||
| -rw-r--r-- | go-src/windowsHelpers.go | 70 |
4 files changed, 119 insertions, 65 deletions
diff --git a/go-src/ctfcMath/ctfcMath.go b/go-src/ctfcMath/ctfcMath.go new file mode 100644 index 0000000..5dfc613 --- /dev/null +++ b/go-src/ctfcMath/ctfcMath.go @@ -0,0 +1,14 @@ +package ctfcmath + +import "math" + +func MaxInt(x int, y int) int { + if x > y { + return x + } + return y +} + +func CeilDivInt(x int, y int) int { + return int(math.Ceil(float64(x) / float64(y))) +} diff --git a/go-src/utils/utils.go b/go-src/utils/utils.go index c1bd8a4..8d0c1c7 100644 --- a/go-src/utils/utils.go +++ b/go-src/utils/utils.go @@ -2,6 +2,8 @@ package utils import ( "os" + + ctfcmath "gitlab.com/Syndamia/ctfc/go-src/ctfcMath" ) // Repeats a rune given amount of times and returns the result as a string @@ -34,10 +36,14 @@ func AppendToFile(path string, value string) { allChatsFile.Close() } -func TotalPages(maxSize int, messageAmount int) int { - return messageAmount / maxSize +/* Pagination */ + +const PageSize = 15 + +func TotalPages(messageAmount int) int { + return ctfcmath.CeilDivInt(messageAmount, PageSize) } -func Paginate(page int, maxSize int, messages ...string) []string { - return messages[len(messages)-maxSize*page : len(messages)-maxSize*(page-1)] +func Paginate(page int, messages ...string) []string { + return messages[ctfcmath.MaxInt(len(messages)-PageSize*page, 0) : len(messages)-PageSize*(page-1)] } diff --git a/go-src/windows.go b/go-src/windows.go index 8e75aa7..584058a 100644 --- a/go-src/windows.go +++ b/go-src/windows.go @@ -1,13 +1,8 @@ package ctfc import ( - "fmt" - "os" - "os/exec" - "runtime" "strconv" "strings" - "time" "gitlab.com/Syndamia/ctfc/go-src/ui" "gitlab.com/Syndamia/ctfc/go-src/utils" @@ -118,48 +113,47 @@ func createChatWindow(values ...string) { func chatWindow(values ...string) { clearScreen() + if len(values) == 1 { + values = append(values, ".") + } + currChat := getChat(values[0]) ui.NormalBox(true, "Chats", "Direct Messages", "Account", "Logout") ui.TextField(currChat.Name + " : " + currChat.Description) ui.TextField("Brought to you by " + currChat.Owner.Name + " (" + currChat.Owner.Username + ")") - for i := 0; i <= 15; i++ { + for i := 0; i <= 16; i++ { ui.EmptyLine() } - lines := 0 lastLine := 0 - go func() { - for lines > -1 { - if lastLine == len(currChat.Messages)-1 { - currChat = getChat(values[0]) - continue - } - - lastLine = len(currChat.Messages) - 1 - fmt.Print("\033[s") - - moveCursorUp(15 + 2) - - for _, v := range utils.Paginate(1, 15, currChat.Messages...) { - fmt.Print("\033[K") - ui.TextField(v) - } - ui.PageField(1, utils.TotalPages(15, len(currChat.Messages))) - - fmt.Print("\033[u") - time.Sleep(500 * time.Millisecond) - } - }() + go routinePaginatedSubwindow(&currChat.Messages, + func(s *[]string) { *s = getChat(values[0]).Messages }, + &lastLine, + len(values[1]), + ) input := ui.InputField("Message or [C/D/A/L/</>/H]") handled := handleInputActions(input, true) if !handled { - currChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username) + switch input { + case ">": + if len(values[1]) < utils.TotalPages(len(currChat.Messages)) { + values[1] += "." + } + case "<": + if len(values[1]) > 1 { + values[1] = values[1][:len(values[1])-1] + } + default: + values[1] = "." + currChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username) + } + defer chatWindow(values...) } - lines = -1 + lastLine = -1 } func logoutWindow(...string) { @@ -247,8 +241,6 @@ func handleInputActions(input string, handleNav bool, ia ...inputAction) bool { /* Errors and Clear */ -var clearNext = true - func showError(message string, callback window, callbackData ...string) { clearScreen() @@ -257,31 +249,3 @@ func showError(message string, callback window, callbackData ...string) { defer callback(callbackData...) } - -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) -} diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go new file mode 100644 index 0000000..13cb3c1 --- /dev/null +++ b/go-src/windowsHelpers.go @@ -0,0 +1,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) +} |
