feat: BIG changes

* use commands constants
* use a separate goroutine to read from connection
* refactor connection reading and ui updating
* add sidepanel for users in channel
* other cleanups
This commit is contained in:
2022-01-09 04:30:22 +02:00
parent 1168a2a4dd
commit 8a7e1568ae
11 changed files with 280 additions and 97 deletions
+17 -15
View File
@@ -18,27 +18,29 @@ package mainscreen
import (
"log"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/parser"
)
type InitialReadMsg struct{}
func (s *State) ReadLoop() {
for {
select {
case <-s.Client.Quit:
return
default:
msg, err := s.ConnReader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
func InitialRead() tea.Msg {
return InitialReadMsg{}
}
type ReceivedIRCCommandMsg struct {
Msg string
}
func (s State) readFromServer() tea.Msg {
msg, err := s.Reader.ReadString('\n')
if err != nil {
log.Print(err)
msg = strings.Replace(msg, "\r\n", "", 1)
if ircMessage, valid := parser.ParseIRCMessage(msg); valid {
s.HandleCommand(ircMessage)
}
}
}
return ReceivedIRCCommandMsg{Msg: msg}
}
type SendPrivMsg struct {
+9 -7
View File
@@ -29,17 +29,15 @@ type InputState struct {
Client *client.Client
}
func NewInputBox(client *client.Client) InputState {
func NewInputBox() InputState {
input := textinput.NewModel()
input.Placeholder = "Send a message..."
input.Focus()
state := InputState{
Input: input,
Style: InputboxStyle.Copy(),
Client: client,
}
return state
return InputState{
Input: input,
Style: InputboxStyle.Copy(),
}
}
func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) {
@@ -49,6 +47,10 @@ func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) {
switch key {
case "enter":
value := s.Input.Value()
if len(value) == 0 {
return s, nil
}
s.Input.Reset()
return s, s.SendingPrivMsg(value)
+115 -40
View File
@@ -26,6 +26,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/client"
"github.com/illusionman1212/gorc/commands"
"github.com/illusionman1212/gorc/parser"
"github.com/illusionman1212/gorc/ui"
)
@@ -38,36 +39,31 @@ const (
)
type State struct {
Content string
Reader *bufio.Reader
ConnReader *bufio.Reader
Client *client.Client
Viewport viewport.Model
Viewport *viewport.Model
Style lipgloss.Style
FocusIndex Window
// a channel could be either a server channel
// or a username of a user
CurrentChannel string
InputBox InputState
InputBox InputState
SidePanel *SidePanelState
}
func NewMainScreen(client *client.Client) State {
newViewport := viewport.Model{
newViewport := &viewport.Model{
HighPerformanceRendering: false,
Wrap: true,
}
state := State{
Content: "",
Reader: nil,
return State{
ConnReader: nil,
Client: client,
Viewport: newViewport,
Style: MessagesStyle.Copy(),
FocusIndex: InputBox,
InputBox: NewInputBox(client),
InputBox: NewInputBox(),
SidePanel: NewSidePanel(client),
}
return state
}
func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
@@ -75,20 +71,36 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case InitialReadMsg:
return s, s.readFromServer
case ReceivedIRCCommandMsg:
message := parser.ParseIRCMessage(msg.Msg)
s.HandleCommand(message)
return s, s.readFromServer
case SendPrivMsg:
s.Content += msg.Msg + client.CRLF
s.Client.SendCommand("PRIVMSG", s.CurrentChannel, msg.Msg)
// for sending slash commands
// TODO: make this better
if msg.Msg[0] == '/' {
substrs := strings.SplitN(msg.Msg[1:], " ", 2)
command := substrs[0]
var params []string
if len(substrs) > 1 {
params = strings.Split(substrs[1], " ")
}
if strings.ToUpper(command) == commands.JOIN {
s.Client.ActiveChannel = params[0]
s.Client.Channels[params[0]] = client.Channel{}
s.Client.SendCommand(command, params...)
return s, nil
}
s.Client.SendCommand(command, params...)
} else {
// TODO: dont send command when activechannel == c.host
fullMsg := s.Client.Nickname + ": " + msg.Msg
if c, ok := s.Client.Channels[s.Client.ActiveChannel]; ok {
c.History += fullMsg + client.CRLF
s.Viewport.SetContent(s.Content)
s.Client.Channels[s.Client.ActiveChannel] = c
}
s.Client.SendCommand(commands.PRIVMSG, s.Client.ActiveChannel, msg.Msg)
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
}
return s, s.readFromServer
return s, nil
case tea.KeyMsg:
key := msg.String()
switch key {
@@ -125,9 +137,12 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
}
}
// TODO: maybe we still need this, not sure
// s.Viewport.SetContent(s.Client.ActiveChannel.History)
switch s.FocusIndex {
case Viewport:
s.Viewport, cmd = s.Viewport.Update(msg)
*s.Viewport, cmd = s.Viewport.Update(msg)
cmds = append(cmds, cmd)
case InputBox:
s.InputBox, cmd = s.InputBox.Update(msg)
@@ -139,31 +154,91 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
func (s *State) SetSize(width, height int) {
s.InputBox.SetSize(width)
s.Viewport.Width = width - s.Style.GetVerticalFrameSize()
s.SidePanel.SetSize(width, height, s.InputBox.Style.Copy().GetVerticalFrameSize())
// -1 is for some extra invisible margin or padding or whatever (idk what it is tbh)
newViewportWidth := (width * 8 / 10) - s.Style.GetHorizontalBorderSize() - 1
newViewportHeight := height - s.InputBox.Style.GetVerticalFrameSize() - s.Style.GetVerticalBorderSize() - 1
// copying the existing style is important here otherwise we'll end up with artifacting
s.Style = s.Style.Copy().Width(width - s.Style.GetVerticalFrameSize())
s.Viewport.Height = height - s.InputBox.Style.GetHorizontalFrameSize() - s.Style.GetHorizontalFrameSize()
s.Style = s.Style.Copy().Height(height - s.InputBox.Style.GetHorizontalFrameSize() - s.Style.GetHorizontalFrameSize())
s.Viewport.Width = newViewportWidth
s.Style = s.Style.Width(newViewportWidth)
s.Viewport.Height = newViewportHeight
s.Style = s.Style.Height(newViewportHeight)
// we need to re-set the content because words wrap differently on different sizes
s.Viewport.SetContent(s.Content)
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
}
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
case commands.PING:
s.Client.SendCommand(commands.PONG, msg.Parameters[0])
case commands.PRIVMSG:
nick := strings.SplitN(msg.Source, "!", 2)[0]
channel := msg.Parameters[0]
msgContent := msg.Parameters[1]
fullMsg := fmt.Sprintf("%s: %s", nick, msgContent)
if c, ok := s.Client.Channels[channel]; ok {
c.History += fullMsg + client.CRLF
s.Client.Channels[channel] = c
}
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
case commands.JOIN:
nick := strings.SplitN(msg.Source, "!", 2)[0]
channel := msg.Parameters[0]
fullMsg := fmt.Sprintf("== %s has joined", nick)
if c, ok := s.Client.Channels[channel]; ok {
c.History += fullMsg + client.CRLF
s.Client.Channels[channel] = c
}
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
case commands.QUIT:
nick := strings.SplitN(msg.Source, "!", 2)[0]
reason := msg.Parameters[0]
fullMsg := fmt.Sprintf("== %s has quit (%s)", nick, reason)
if c, ok := s.Client.Channels[s.Client.ActiveChannel]; ok {
c.History += fullMsg + client.CRLF
s.Client.Channels[s.Client.ActiveChannel] = c
}
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
case commands.RPL_NAMREPLY:
// TODO: put the users into their respective channels
// client := msg.Parameters[0]
// chanSymbol := msg.Parameters[1]
// channel := msg.Parameters[2]
nicks := strings.Split(msg.Parameters[3], " ")
s.SidePanel.Nicks = append(s.SidePanel.Nicks, nicks...)
case commands.RPL_ENDOFNAMES:
fullMsg := fmt.Sprintf("%s %s %s %s", msg.Tags, msg.Source, msg.Command, strings.Join(msg.Parameters, " "))
if c, ok := s.Client.Channels[s.Client.ActiveChannel]; ok {
c.History += fullMsg + client.CRLF
s.Client.Channels[s.Client.ActiveChannel] = c
}
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
default:
fullMsg := fmt.Sprintf("%s %s %s %s", msg.Tags, msg.Source, msg.Command, strings.Join(msg.Parameters, " "))
if c, ok := s.Client.Channels[s.Client.ActiveChannel]; ok {
c.History += fullMsg + client.CRLF
s.Client.Channels[s.Client.ActiveChannel] = c
}
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
}
}
func (s State) View() string {
screen := lipgloss.JoinVertical(0, s.Style.Render(s.Viewport.View()), s.InputBox.View())
top := lipgloss.JoinHorizontal(lipgloss.Right, s.Style.Render(s.Viewport.View()), s.SidePanel.View())
screen := lipgloss.JoinVertical(0, top, s.InputBox.View())
return ui.MainStyle.Render(screen)
}
+60
View File
@@ -0,0 +1,60 @@
// gorc project
// Copyright (C) 2021 IllusionMan1212
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see https://www.gnu.org/licenses.
package mainscreen
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/client"
)
type SidePanelState struct {
// TODO: make a viewport and use that to display the names
// OR: we could use a list
Style lipgloss.Style
Nicks []string
Viewport viewport.Model
Content string
}
func NewSidePanel(client *client.Client) *SidePanelState {
return &SidePanelState{
Style: SidePanelStyle.Copy(),
}
}
func (s SidePanelState) Update(msg tea.Msg) (SidePanelState, tea.Cmd) {
return s, nil
}
func (s *SidePanelState) SetSize(width, height, inputboxHeight int) {
s.Style = s.Style.Copy().Width(width * 2 / 10)
// -1 is for some extra invisible margin or padding or whatever (idk what it is tbh)
s.Style = s.Style.Copy().Height(height - inputboxHeight - s.Style.GetVerticalBorderSize() - 1)
}
func (s SidePanelState) View() string {
separator := strings.Repeat("-", s.Style.GetWidth()) + "\n"
header := fmt.Sprintf("Users in this channel (%d)\n", len(s.Nicks)) + separator
nicks := strings.Join(s.Nicks, "\n")
return s.Style.Render(header + nicks)
}
+5 -1
View File
@@ -29,7 +29,11 @@ var (
BorderForeground(lipgloss.Color("105"))
MessagesStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), true).
Width(ui.MainStyle.GetWidth()).
Width(ui.MainStyle.GetWidth() * 8 / 10).
BorderForeground(lipgloss.Color("#EEE"))
SidePanelStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), true).
Width(ui.MainStyle.GetWidth() * 2 / 10).
BorderForeground(lipgloss.Color("#EEE"))
InputBoxHeight = 1