diff options
| -rw-r--r-- | go-src/chat.go | 17 | ||||
| -rw-r--r-- | go-src/folderPaths/folderPaths.go | 31 | ||||
| -rw-r--r-- | go-src/main/main.go | 2 | ||||
| -rw-r--r-- | go-src/ui/ui.go | 9 | ||||
| -rw-r--r-- | go-src/windows.go | 15 |
5 files changed, 62 insertions, 12 deletions
diff --git a/go-src/chat.go b/go-src/chat.go index 18a9200..c7ea5f1 100644 --- a/go-src/chat.go +++ b/go-src/chat.go @@ -21,9 +21,13 @@ func (ch Chat) addMessage(value string) { } func createChat(data ...string) { - f, _ := os.Create(folderPaths.FileAtChatsFolder(data[0])) - f.WriteString(data[0] + "\n" + data[1] + "\n" + data[2]) - f.Close() + chatFile, _ := os.Create(folderPaths.FileAtChatsFolder(data[0])) + chatFile.WriteString(data[0] + "\n" + data[1] + "\n" + data[2]) + chatFile.Close() + + allChatsFile, _ := os.OpenFile(folderPaths.AllChatsFilePath(), os.O_APPEND|os.O_WRONLY, 0644) + allChatsFile.WriteString(data[0] + " : " + data[1] + "\n") + allChatsFile.Close() } func getChat(name string) Chat { @@ -31,3 +35,10 @@ func getChat(name string) Chat { values := bytes.Split(f, []byte("\n")) return Chat{string(values[0]), string(values[1]), getUser(string(values[2])), utils.TwoDByteArrayToStringArray(values[3:])} } + +func getAllChats() []string { + f, _ := os.ReadFile(folderPaths.AllChatsFilePath()) + values := bytes.Split(f, []byte("\n")) + chats := utils.TwoDByteArrayToStringArray(values) + return chats[:len(chats)-1] +} diff --git a/go-src/folderPaths/folderPaths.go b/go-src/folderPaths/folderPaths.go index 67bb92b..b39ce87 100644 --- a/go-src/folderPaths/folderPaths.go +++ b/go-src/folderPaths/folderPaths.go @@ -5,25 +5,46 @@ import ( "runtime" ) -func InitFolders() { - dirs := []string{rootFolder(), buildPath(rootFolder(), "Chats"), buildPath(rootFolder(), "DirectMessages"), buildPath(rootFolder(), "Users")} +func InitFoldersAndFiles() { + dirs := []string{rootFolder(), FileAtChatsFolder(""), FileAtDirectMessagesFolder(""), FileAtUsersFolder("")} for _, v := range dirs { if _, err := os.Stat(v); os.IsNotExist(err) { os.Mkdir(v, 0775) } } + + files := []string{AllChatsFilePath()} + for _, v := range files { + if _, err := os.Stat(v); os.IsNotExist(err) { + f, _ := os.Create(v) + f.Close() + } + } +} + +func AllChatsFilePath() string { + return buildPath(rootFolder(), "AllChats.txt") } func FileAtChatsFolder(fileName string) string { - return buildPath(rootFolder(), "Chats", fileName+".txt") + if fileName != "" { + fileName += ".txt" + } + return buildPath(rootFolder(), "Chats", fileName) } func FileAtDirectMessagesFolder(fileName string) string { - return buildPath(rootFolder(), "DirectMessages", fileName+".txt") + if fileName != "" { + fileName += ".txt" + } + return buildPath(rootFolder(), "DirectMessages", fileName) } func FileAtUsersFolder(fileName string) string { - return buildPath(rootFolder(), "Users", fileName+".txt") + if fileName != "" { + fileName += ".txt" + } + return buildPath(rootFolder(), "Users", fileName) } func rootFolder() string { diff --git a/go-src/main/main.go b/go-src/main/main.go index d1161dc..dbb3045 100644 --- a/go-src/main/main.go +++ b/go-src/main/main.go @@ -6,6 +6,6 @@ import ( ) func main() { - folderPaths.InitFolders() + folderPaths.InitFoldersAndFiles() ctfc.StartupWindow() } diff --git a/go-src/ui/ui.go b/go-src/ui/ui.go index 1a362e6..bf684f0 100644 --- a/go-src/ui/ui.go +++ b/go-src/ui/ui.go @@ -114,11 +114,16 @@ func InputFieldFilled(specification string, input string) { } func NumberedFields(messages ...string) { + if len(messages) == 0 { + return + } var result string for i, v := range messages { - result += fmt.Sprintf("%c%v %v\n", boxVertRightLine, i+1, v) + result += fmt.Sprintf("%c%v %v", boxVertRightLine, i+1, v) + if i < len(messages)-1 { + result += "\n" + } } - result = result[:len(result)-1] fmt.Println(result) } diff --git a/go-src/windows.go b/go-src/windows.go index 3bc621c..95478c6 100644 --- a/go-src/windows.go +++ b/go-src/windows.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "runtime" + "strconv" "strings" "gitlab.com/Syndamia/ctfc/go-src/ui" @@ -68,6 +69,8 @@ func chatsWindow(values ...string) { clearScreen() ui.NormalBox(true, "Direct Messages", "Account", "Logout") + allChats := getAllChats() + ui.NumberedFields(allChats...) ui.PageField(0, 0) ui.EmptyLine() @@ -83,7 +86,12 @@ func chatsWindow(values ...string) { inputAction{"H", chatsWindow, []string{"ShowHelp"}}, ) if !handled { - defer showError(invalidCommand, chatsWindow) + // if is number + if chatI, err := strconv.Atoi(input); chatI >= 0 && chatI <= len(allChats) && err == nil { + defer chatWindow(strings.Split(allChats[chatI-1], " : ")[0]) + } else { + defer showError(invalidCommand, chatsWindow) + } } } @@ -106,8 +114,13 @@ func createChatWindow(values ...string) { } func chatWindow(values ...string) { + clearScreen() + currChat := getChat(values[0]) fmt.Println(currChat) + + ui.InputField("Enter anything") + defer chatsWindow() } func logoutWindow(...string) { |
