feat: small refactor. basic PRIVMSG sending

This commit is contained in:
2022-01-05 00:54:54 +02:00
parent 327f59a14c
commit 1168a2a4dd
6 changed files with 134 additions and 66 deletions
+27 -12
View File
@@ -45,10 +45,14 @@ type State struct {
Style lipgloss.Style
FocusIndex Window
// a channel could be either a server channel
// or a username of a user
CurrentChannel string
InputBox InputState
}
func NewMainScreen() State {
func NewMainScreen(client *client.Client) State {
newViewport := viewport.Model{
HighPerformanceRendering: false,
Wrap: true,
@@ -57,11 +61,11 @@ func NewMainScreen() State {
state := State{
Content: "",
Reader: nil,
Client: nil,
Client: client,
Viewport: newViewport,
Style: MessagesStyle.Copy(),
FocusIndex: InputBox,
InputBox: NewInputBox(),
InputBox: NewInputBox(client),
}
return state
}
@@ -75,18 +79,15 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
return s, s.readFromServer
case ReceivedIRCCommandMsg:
message := parser.ParseIRCMessage(msg.Msg)
fullMsg := fmt.Sprintf("%s %s %s %s", message.Tags, message.Source, message.Command, strings.Join(message.Parameters, " "))
s.Content += fullMsg
s.HandleCommand(message)
return s, s.readFromServer
case SendPrivMsg:
s.Content += msg.Msg + client.CRLF
s.Client.SendCommand("PRIVMSG", s.CurrentChannel, msg.Msg)
s.Viewport.SetContent(s.Content)
// TODO: handle different commands
switch message.Command {
case "PING":
s.Client.SendCommand("PONG", message.Parameters[0])
break
}
return s, s.readFromServer
case tea.KeyMsg:
key := msg.String()
@@ -147,6 +148,20 @@ func (s *State) SetSize(width, height int) {
s.Viewport.SetContent(s.Content)
}
func (s *State) HandleCommand(msg parser.IRCMessage) {
fullMsg := fmt.Sprintf("%s %s %s %s", msg.Tags, msg.Source, msg.Command, strings.Join(msg.Parameters, " "))
s.Content += fullMsg
s.Viewport.SetContent(s.Content)
// TODO: handle different commands
switch msg.Command {
case "PING":
s.Client.SendCommand("PONG", msg.Parameters[0])
break
}
}
func (s State) View() string {
screen := lipgloss.JoinVertical(0, s.Style.Render(s.Viewport.View()), s.InputBox.View())