diff options
| -rw-r--r-- | go-src/utils/utils.go | 12 | ||||
| -rw-r--r-- | go-src/windows.go | 48 |
2 files changed, 55 insertions, 5 deletions
diff --git a/go-src/utils/utils.go b/go-src/utils/utils.go index 754c7ed..c1bd8a4 100644 --- a/go-src/utils/utils.go +++ b/go-src/utils/utils.go @@ -1,6 +1,8 @@ package utils -import "os" +import ( + "os" +) // Repeats a rune given amount of times and returns the result as a string func RepeatRune(r rune, times int) (result string) { @@ -31,3 +33,11 @@ func AppendToFile(path string, value string) { allChatsFile.WriteString(value) allChatsFile.Close() } + +func TotalPages(maxSize int, messageAmount int) int { + return messageAmount / maxSize +} + +func Paginate(page int, maxSize int, messages ...string) []string { + return messages[len(messages)-maxSize*page : len(messages)-maxSize*(page-1)] +} diff --git a/go-src/windows.go b/go-src/windows.go index beac8ed..8e75aa7 100644 --- a/go-src/windows.go +++ b/go-src/windows.go @@ -7,8 +7,10 @@ import ( "runtime" "strconv" "strings" + "time" "gitlab.com/Syndamia/ctfc/go-src/ui" + "gitlab.com/Syndamia/ctfc/go-src/utils" ) type window func(...string) @@ -120,11 +122,35 @@ func chatWindow(values ...string) { 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 + ")") - ui.EmptyLine() - for _, v := range currChat.Messages { - ui.TextField(v) + + for i := 0; i <= 15; i++ { + ui.EmptyLine() } - ui.PageField(0, 0) + + 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) + } + }() input := ui.InputField("Message or [C/D/A/L/</>/H]") @@ -133,6 +159,7 @@ func chatWindow(values ...string) { currChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username) defer chatWindow(values...) } + lines = -1 } func logoutWindow(...string) { @@ -245,3 +272,16 @@ func clearScreen() { 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) +} |
