-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
187 lines (163 loc) · 5.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/json"
"fmt"
"net/url"
"os"
"github.com/charmbracelet/bubbles/filepicker"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/gorilla/websocket"
)
var (
chatItems = make(map[string][]Message)
username = ""
serverConn *websocket.Conn
)
var (
titleStyle = lipgloss.NewStyle().PaddingLeft(0).PaddingTop(0).Foreground(lipgloss.Color("#458588")).Bold(true)
itemStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("#689d6a"))
noItemStyle = lipgloss.NewStyle().PaddingLeft(1).PaddingTop(2).Foreground(lipgloss.Color("#689d6a")).Bold(false)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("#fabd2f"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
)
func main() {
chatlist := list.New([]list.Item{}, chatItemDelegate{}, 0, 0)
chatlist.SetShowHelp(false)
chatlist.Styles = list.Styles{
TitleBar: titleStyle,
NoItems: noItemStyle,
PaginationStyle: paginationStyle,
}
chatlist.SetShowFilter(false)
chatlist.SetShowStatusBar(false)
input := textinput.New()
usernameInput := textinput.New()
passwordInput := textinput.New()
groupnameInput := textinput.New()
filenameInput := textinput.New()
messagelist := list.New([]list.Item{}, messageItemDelegate{}, 0, 0)
messagelist.Styles = list.Styles{
NoItems: lipgloss.NewStyle().PaddingLeft(2).PaddingTop(2),
}
messagelist.SetShowHelp(false)
messagelist.SetShowTitle(false)
messagelist.SetShowStatusBar(false)
messagelist.SetShowFilter(false)
fp := filepicker.New()
fp.AllowedTypes = []string{".mod", ".sum", ".go", ".txt", ".md"}
fp.CurrentDirectory, _ = os.UserHomeDir()
m := model{
chats: chatlist,
filepicker: fp,
messages: messagelist,
input: input,
usernameInput: usernameInput,
passwordInput: passwordInput,
groupnameInput: groupnameInput,
filenameInput: filenameInput,
}
m.chats.Title = "Chats"
m.chats.Styles.TitleBar.Align(lipgloss.Center)
p := tea.NewProgram(m, tea.WithAltScreen())
channel := make(chan *websocket.Conn)
go startConnection(p, channel)
serverConn = <-channel
if _, err := p.Run(); err != nil {
fmt.Println("Error creating bubble")
}
}
func userLogin() {
// Send username
err := serverConn.WriteJSON(Message{Type: "cmd", From: username, Content: "login"})
if err != nil {
fmt.Println("Error sending username:", err)
return
}
}
func startConnection(p *tea.Program, ch chan *websocket.Conn) {
u := url.URL{Scheme: "ws", Host: "localhost:8080", Path: "/ws"}
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
fmt.Println("Error connecting:", err)
return
}
defer conn.Close()
ch <- conn
for {
_, msgBytes, err := conn.ReadMessage()
if err != nil {
fmt.Println("Read error:", err)
return
}
var msg Message
err = json.Unmarshal(msgBytes, &msg)
if err != nil {
fmt.Println("Error decoding JSON:", err)
continue
}
p.Send(msg)
// chats[msg.From] = append(chats[msg.From], msg)
// fmt.Println(msg.From+":", msg.Content)
}
}
func writeMessages(conn *websocket.Conn, msg Message) {
err := conn.WriteJSON(msg)
if err != nil {
fmt.Println("Write error:", err)
return
}
}
// func createGroup(conn *websocket.Conn, msg Message) {
// // Send username
// err := serverConn.WriteJSON(msg)
// if err != nil {
// fmt.Println("Error sending username:", err)
// return
// }
// writeCommands(conn, reader, from, "", group, false, "create group")
// }
// func joinGroup(conn *websocket.Conn, from string, reader *bufio.Reader) {
// fmt.Println("Enter group name:")
// group, _ := reader.ReadString('\n')
// group = group[:len(group)-1]
// writeCommands(conn, reader, from, "", group, false, "join group")
// }
// func directMessage(conn *websocket.Conn, from string, reader *bufio.Reader) {
// fmt.Println("Enter senders username:")
// to, _ := reader.ReadString('\n')
// to = to[:len(to)-1]
// writeMessages(conn, reader, from, to, "", false)
// }
// func groupMessage(conn *websocket.Conn, from string, reader *bufio.Reader) {
// fmt.Println("Enter group name:")
// group, _ := reader.ReadString('\n')
// group = group[:len(group)-1]
// writeMessages(conn, reader, from, "", group, true)
// }
// func writeCommands(conn *websocket.Conn, reader *bufio.Reader, from string, to string, group string, isGroupMsg bool, content string) {
// err := conn.WriteJSON(Message{Type: "cmd", From: from, To: to, Group: group, IsGroupMsg: isGroupMsg, Content: content})
// if err != nil {
// fmt.Println("Write error:", err)
// return
// }
// }
// func readMessages(conn *websocket.Conn) {
// for {
// _, msgBytes, err := conn.ReadMessage()
// if err != nil {
// fmt.Println("Read error:", err)
// return
// }
// var msg Message
// err = json.Unmarshal(msgBytes, &msg)
// if err != nil {
// fmt.Println("Error decoding JSON:", err)
// continue
// }
// // chats[msg.From] = append(chats[msg.From], msg)
// fmt.Println(msg.From+":", msg.Content)
// }
// }