diff options
Diffstat (limited to 'go-src')
| -rw-r--r-- | go-src/ui/ui.go | 45 | ||||
| -rw-r--r-- | go-src/windows.go | 85 | ||||
| -rw-r--r-- | go-src/windowsHelpers.go | 16 |
3 files changed, 106 insertions, 40 deletions
diff --git a/go-src/ui/ui.go b/go-src/ui/ui.go index bf684f0..976a349 100644 --- a/go-src/ui/ui.go +++ b/go-src/ui/ui.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "gitlab.com/Syndamia/ctfc/go-src/csi" "gitlab.com/Syndamia/ctfc/go-src/utils" ) @@ -34,6 +35,7 @@ const ( textRune = '%' inputRune = '@' + noRune = ' ' ) var scanner = bufio.NewScanner(os.Stdin) @@ -103,6 +105,10 @@ func TextField(message string) { fmt.Printf("%c%c %v\n", boxVertRightLine, textRune, message) } +func TextFields(messsages ...string) { + printMultiple(textRune, messsages...) +} + func InputField(specification string) string { fmt.Printf("%c%c %v : ", boxBottomLeftCorner, inputRune, specification) scanner.Scan() @@ -113,18 +119,12 @@ func InputFieldFilled(specification string, input string) { fmt.Printf("%c%c %v : %v\n", boxVertRightLine, inputRune, specification, input) } +func NumberedField(message string, number int) { + fmt.Printf("%c%v %v\n", boxVertRightLine, number, message) +} + func NumberedFields(messages ...string) { - if len(messages) == 0 { - return - } - var result string - for i, v := range messages { - result += fmt.Sprintf("%c%v %v", boxVertRightLine, i+1, v) - if i < len(messages)-1 { - result += "\n" - } - } - fmt.Println(result) + printMultiple(noRune, messages...) } func PageField(current int, max int) { @@ -134,3 +134,26 @@ func PageField(current int, max int) { func EmptyLine() { fmt.Println(string(boxVertLine)) } + +func EmptyLines(amount int) { + for i := 0; i < amount; i++ { + EmptyLine() + } +} + +func printMultiple(secondRune rune, messages ...string) { + if len(messages) == 0 { + return + } + + secondString := string(secondRune) + + for i, v := range messages { + csi.ClearLine() + + if secondRune == noRune { + secondString = fmt.Sprint(i + 1) + } + fmt.Printf("%c%v %v\n", boxVertRightLine, secondString, v) + } +} diff --git a/go-src/windows.go b/go-src/windows.go index b3af04e..744e672 100644 --- a/go-src/windows.go +++ b/go-src/windows.go @@ -20,7 +20,9 @@ func StartupWindow(...string) { inputAction{"L", loginWindow, nil}, inputAction{"R", registerWindow, nil}, ) - if !handled { + if handled != nil { + defer handled() + } else { defer showError(invalidCommand, StartupWindow) } } @@ -71,24 +73,43 @@ func registerWindow(values ...string) { func chatsWindow(values ...string) { csi.ClearScreen() + if len(values) == 0 { + values = append(append(values, ""), ".") + } + ui.NormalBox(true, "Direct Messages", "Account", "Logout") + allChats := getAllChats() - ui.NumberedFields(allChats...) - ui.PageField(0, 0) - ui.EmptyLine() - if len(values) > 0 { - if values[0] == "ShowHelp" { - ui.TextField("Chats page options: [(D)irect messages/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(C) for create chat/(name) for go to chat room by name/(number) for go to chat room by number/(H)elp]") - } + customLinesBelow := 2 + if values[0] == "ShowHelp" { + customLinesBelow++ + } + ui.EmptyLines(pageSize + 1 + customLinesBelow) + + lastLine := -1 + go routinePaginatedSubwindow(&allChats, + func(s *[]string) { *s = getAllChats() }, + &lastLine, + len(values[1]), + customLinesBelow, + true, + ) + + if values[0] == "ShowHelp" { + ui.TextField("Chats page options: [(D)irect messages/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(C) for create chat/(name) for go to chat room by name/(number) for go to chat room by number/(H)elp]") } + input := ui.InputField("[D/A/L/</>/C/E/name/number/H]") handled := handleInputActions(input, true, inputAction{"C", createChatWindow, nil}, - inputAction{"H", chatsWindow, []string{"ShowHelp"}}, + inputAction{"H", chatsWindow, []string{"ShowHelp", values[1]}}, ) - if !handled { + + if handled != nil { + defer handled() + } else { // If user input is number, navigate to chat of given number, else show error if chatI, err := strconv.Atoi(input); chatI >= 0 && chatI <= len(allChats) && err == nil { defer chatWindow(strings.Split(allChats[chatI-1], " : ")[0]) @@ -96,6 +117,7 @@ func chatsWindow(values ...string) { defer showError(invalidCommand, chatsWindow) } } + lastLine = -2 } func createChatWindow(values ...string) { @@ -124,31 +146,49 @@ func chatWindow(values ...string) { // We determine page number by length of a string // This method should be faster than having to cast to int all the time if len(values) == 1 { - values = append(values, ".") + values = append(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 + ")") + ui.TextFields( + currChat.Name+" : "+currChat.Description, + "Brought to you by "+currChat.Owner.Name+" ("+currChat.Owner.Username+")", + ) + customLinesBelow := 2 // One empty line to separate the "Brought to you by" line // with the chat messages, and then empty line for each chat message - for i := 0; i <= pageSize+1; i++ { - ui.EmptyLine() + ui.EmptyLines(pageSize + 1 + customLinesBelow) + + if values[2] == "ShowHelp" { + customLinesBelow++ } - lastLine := 0 + lastLine := -1 go routinePaginatedSubwindow(&currChat.Messages, func(s *[]string) { *s = getChat(values[0]).Messages }, &lastLine, len(values[1]), + customLinesBelow, + false, ) + if values[2] == "ShowHelp" { + ui.TextField("Help info") + values[2] = "" + } + input := ui.InputField("Message or [C/D/A/L/</>/H]") - handled := handleInputActions(input, true) - if !handled { + handled := handleInputActions(input, true, + inputAction{"H", chatWindow, []string{values[0], values[1], "ShowHelp"}}, + ) + + if handled != nil { + defer handled() + } else { switch input { case ">": // If possible, increment to the next page (adds a dot to the end of the string) @@ -168,7 +208,7 @@ func chatWindow(values ...string) { defer chatWindow(values...) } - lastLine = -1 // Practically stops execution of the paginated subwindow routine + lastLine = -2 // Practically stops execution of the paginated subwindow routine } func logoutWindow(...string) { @@ -252,7 +292,7 @@ type inputAction struct { args []string } -func handleInputActions(input string, handleNav bool, ia ...inputAction) bool { +func handleInputActions(input string, handleNav bool, ia ...inputAction) func() { if handleNav { ia = append(ia, inputAction{"C", chatsWindow, nil}, @@ -263,9 +303,8 @@ func handleInputActions(input string, handleNav bool, ia ...inputAction) bool { } for _, v := range ia { if strings.ToLower(input) == strings.ToLower(v.value) { - defer v.execute(v.args...) - return true + return func() { v.execute(v.args...) } } } - return false + return nil } diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go index 909a673..fb530c1 100644 --- a/go-src/windowsHelpers.go +++ b/go-src/windowsHelpers.go @@ -22,8 +22,8 @@ func paginate(page int, messages ...string) []string { // 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 { +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) @@ -33,17 +33,21 @@ func routinePaginatedSubwindow(messages *[]string, updateMessages func(*[]string *lastLine = len(*messages) - 1 csi.SaveCursorPosition() - csi.MoveCursorUpN(pageSize + 2) // +2 because of the input and pagination lines + 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() } - for _, v := range pageMessages { - csi.ClearLine() - ui.TextField(v) + + if numbered { + ui.NumberedFields(pageMessages...) + } else { + ui.TextFields(pageMessages...) } ui.PageField(page, totalPages(len(*messages))) |
