From 636ba9a01f7f4c7bbd4c40f65d2d8d00d27d3564 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 11 Sep 2021 15:30:28 +0300 Subject: Abstracted if-else logic for changing page and entering number/text input value in chats page --- go-src/windows.go | 57 +++++++++++++++--------------------------------- go-src/windowsHelpers.go | 24 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 40 deletions(-) (limited to 'go-src') diff --git a/go-src/windows.go b/go-src/windows.go index c8e7234..31c2fb0 100644 --- a/go-src/windows.go +++ b/go-src/windows.go @@ -111,26 +111,14 @@ func chatsWindow(values ...string) { ) if nextWindow == nil { - // 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[utils.MaxInt(len(allChats)-pageSize*len(values[0]), 0)+chatI-1], " : ")[0]) - } else if input == ">" { - // If possible, increment to the next page (adds a dot to the end of the string) - if len(values[0]) < totalPages(len(allChats)) { - values[0] += "." - } - defer chatsWindow(values...) - } else if input == "<" { - // If possible, decrement to the previous page (removes a dot from the string) - if len(values[0]) > 1 { - utils.StrShortenRight(&values[0], 1) - } - defer chatsWindow(values...) - } else if chatNameExists(input) { - defer chatWindow(input) - } else { - defer showError(invalidCommand, chatsWindow) - } + defer handleComplexInputActions(input, + allChats, + " : ", + &values[0], + chatsWindow, + chatWindow, + chatNameExists, + ) } else { defer nextWindow() } @@ -253,26 +241,15 @@ func directMessagesWindow(values ...string) { ) if nextWindow == nil { - // If user input is number, navigate to chat of given number, else show error - if chatI, err := strconv.Atoi(input); chatI >= 0 && chatI <= len(allDirectMessageChats) && err == nil { - defer directMessageWindow(strings.Trim(strings.Split(allDirectMessageChats[utils.MaxInt(len(allDirectMessageChats)-pageSize*len(values[0]), 0)+chatI-1], " (")[1], ")")) - } else if input == ">" { - // If possible, increment to the next page (adds a dot to the end of the string) - if len(values[0]) < totalPages(len(allDirectMessageChats)) { - values[0] += "." - } - defer directMessagesWindow(values...) - } else if input == "<" { - // If possible, decrement to the previous page (removes a dot from the string) - if len(values[0]) > 1 { - utils.StrShortenRight(&values[0], 1) - } - defer directMessagesWindow(values...) - } else if usernameExists(input) { - defer directMessageWindow(input) - } else { - defer showError(invalidCommand, chatsWindow) - } + values[0] = values[0][:len(values[0])-1] + defer handleComplexInputActions(input, + allDirectMessageChats, + " (", + &values[0], + directMessagesWindow, + directMessageWindow, + usernameExists, + ) } else { defer nextWindow() } diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go index 9b1d2d9..761e0d2 100644 --- a/go-src/windowsHelpers.go +++ b/go-src/windowsHelpers.go @@ -1,6 +1,7 @@ package ctfc import ( + "strconv" "strings" "time" @@ -89,6 +90,29 @@ func handleInputActions(input string, handleNav bool, ia ...inputAction) func() return nil } +func handleComplexInputActions(input string, allValues []string, nameSep string, pageArgument *string, currentWindow window, singleValueWindow window, existsCheck func(string) bool) { + // If user input is number, navigate to chat of given number, else show error + if chatI, err := strconv.Atoi(input); chatI >= 0 && chatI <= len(allValues) && err == nil { + defer singleValueWindow(strings.Split(allValues[utils.MaxInt(len(allValues)-pageSize*len(*pageArgument), 0)+chatI-1], " : ")[0]) + } else if input == ">" && pageArgument != nil { + // If possible, increment to the next page (adds a dot to the end of the string) + if len(*pageArgument) < totalPages(len(allValues)) { + *pageArgument += "." + } + defer currentWindow(allValues...) + } else if input == "<" { + // If possible, decrement to the previous page (removes a dot from the string) + if len(*pageArgument) > 1 { + utils.StrShortenRight(*&pageArgument, 1) + } + defer currentWindow(allValues...) + } else if existsCheck(input) { + defer singleValueWindow(input) + } else { + defer showError(invalidCommand, currentWindow) + } +} + /* Form */ type formInput struct { -- cgit v1.2.3