feat: handle more commands and properly implement older ones

* fix parser tests
* write logs to file
* gracefully terminate program
* make viewport follow output
* properly handle channel nicks and display them in the sidepanel
* general refactors and fixes
This commit is contained in:
2022-01-15 21:56:06 +02:00
parent 8a7e1568ae
commit 22d5e39899
15 changed files with 487 additions and 181 deletions
+4 -6
View File
@@ -30,7 +30,7 @@ import (
type Screen int
const (
Login = iota
Login Screen = iota
MainScreen
)
@@ -52,15 +52,13 @@ func initialUiState(client *client.Client) UI {
}
}
func InitialState() State {
func InitialState() *State {
client := &client.Client{}
s := State{
return &State{
Client: client,
UI: initialUiState(client),
}
return s
}
func (s State) Init() tea.Cmd {
@@ -94,7 +92,7 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
s.UI.CurrentScreen = MainScreen
// create new client with the provided host and port
(*s.Client) = client.NewClient(host, port, tlsEnabled)
s.Client.Initialize(host, port, tlsEnabled)
s.Client.Register(nickname, password, channel)
// 512 bytes as a base + 8192 additional bytes for tags
-5
View File
@@ -19,13 +19,8 @@ package app
import tea "github.com/charmbracelet/bubbletea"
func (s *State) Quit() tea.Msg {
if s.Client.Quit != nil {
close(s.Client.Quit)
}
if s.Client.TcpConn != nil {
s.Client.SendCommand("QUIT")
s.Client.TcpConn.Close()
}
return tea.Quit()
+23 -11
View File
@@ -17,6 +17,7 @@
package mainscreen
import (
"io"
"log"
"strings"
@@ -26,19 +27,18 @@ import (
func (s *State) ReadLoop() {
for {
select {
case <-s.Client.Quit:
msg, err := s.ConnReader.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Println(err)
}
s.Client.TcpConn.Close()
return
default:
msg, err := s.ConnReader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
}
msg = strings.Replace(msg, "\r\n", "", 1)
if ircMessage, valid := parser.ParseIRCMessage(msg); valid {
s.HandleCommand(ircMessage)
}
msg = strings.Replace(msg, "\r\n", "", 1)
if ircMessage, valid := parser.ParseIRCMessage(msg); valid {
s.HandleCommand(ircMessage)
}
}
}
@@ -52,3 +52,15 @@ func (s InputState) SendingPrivMsg(msg string) tea.Cmd {
return SendPrivMsg{Msg: msg}
}
}
type ReceivedIRCMsgMsg struct{}
func ReceivedIRCMsg() tea.Msg {
return ReceivedIRCMsgMsg{}
}
type SwitchChannelsMsg struct{}
func SwitchChannels() tea.Msg {
return SwitchChannelsMsg{}
}
+13
View File
@@ -62,7 +62,20 @@ func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) {
return s, cmd
}
func (s *InputState) Focus() {
s.Input.Focus()
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("105"))
}
func (s *InputState) Blur() {
s.Input.Blur()
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
}
func (s *InputState) SetSize(width int) {
// set a max width for the input field so it scrolls horizontally instead of wrapping to a newline
// -4 for input prompt char, cursor, and some extra magical padding
s.Input.Width = width - s.Style.GetHorizontalFrameSize() - 4
s.Style = s.Style.Width(width - s.Style.GetHorizontalBorderSize())
}
+124 -57
View File
@@ -19,6 +19,7 @@ package mainscreen
import (
"bufio"
"fmt"
"math"
"strings"
"github.com/charmbracelet/bubbles/textinput"
@@ -34,7 +35,8 @@ import (
type Window int
const (
Viewport = iota
Viewport Window = iota
SidePanel
InputBox
)
@@ -42,7 +44,6 @@ type State struct {
ConnReader *bufio.Reader
Client *client.Client
Viewport *viewport.Model
Style lipgloss.Style
FocusIndex Window
InputBox InputState
@@ -52,14 +53,14 @@ type State struct {
func NewMainScreen(client *client.Client) State {
newViewport := &viewport.Model{
HighPerformanceRendering: false,
Wrap: true,
Wrap: viewport.Wrap,
Style: MessagesStyle.Copy(),
}
return State{
ConnReader: nil,
Client: client,
Viewport: newViewport,
Style: MessagesStyle.Copy(),
FocusIndex: InputBox,
InputBox: NewInputBox(),
SidePanel: NewSidePanel(client),
@@ -71,6 +72,9 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case ReceivedIRCMsgMsg:
s.Viewport.GotoBottom()
return s, nil
case SendPrivMsg:
// for sending slash commands
// TODO: make this better
@@ -83,21 +87,28 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
}
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
if _, ok := s.Client.Channels[params[0]]; !ok {
s.Client.Channels[params[0]] = client.Channel{
Users: make(map[string]client.User),
}
}
s.Client.SendCommand(commands.JOIN, params...)
return s, SwitchChannels
}
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
if s.Client.ActiveChannel != s.Client.Host {
fullMsg := s.Client.Nickname + ": " + msg.Msg
if c, ok := s.Client.Channels[s.Client.ActiveChannel]; ok {
c.History += fullMsg + client.CRLF
s.Client.Channels[s.Client.ActiveChannel] = c
s.Client.Channels[s.Client.ActiveChannel] = c
}
// TODO: make sure to only append the message to the history if server sends back no errors
s.Client.SendCommand(commands.PRIVMSG, s.Client.ActiveChannel, msg.Msg)
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
s.Viewport.GotoBottom()
}
s.Client.SendCommand(commands.PRIVMSG, s.Client.ActiveChannel, msg.Msg)
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
}
return s, nil
@@ -111,35 +122,32 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
s.FocusIndex--
}
if s.FocusIndex > 1 {
if s.FocusIndex > 2 {
s.FocusIndex = 0
} else if s.FocusIndex < 0 {
s.FocusIndex = 1
s.FocusIndex = 2
}
switch s.FocusIndex {
case Viewport:
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("105"))
s.InputBox.Style = s.InputBox.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
s.InputBox.Input.Blur()
s.Focus()
s.InputBox.Blur()
s.SidePanel.Blur()
case InputBox:
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
s.InputBox.Style = s.InputBox.Style.Copy().BorderForeground(lipgloss.Color("105"))
s.InputBox, cmd = s.InputBox.Update(msg)
cmds = append(cmds, cmd)
cmds = append(cmds, textinput.Blink)
s.InputBox.Input.Focus()
s.Blur()
s.InputBox.Focus()
s.SidePanel.Blur()
case SidePanel:
s.Blur()
s.InputBox.Blur()
s.SidePanel.Focus()
}
return s, tea.Batch(cmds...)
}
}
// 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)
@@ -149,22 +157,35 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
cmds = append(cmds, cmd)
}
*s.SidePanel, cmd = s.SidePanel.Update(msg)
cmds = append(cmds, cmd)
return s, tea.Batch(cmds...)
}
func (s *State) Focus() {
s.Viewport.Style = s.Viewport.Style.Copy().BorderForeground(lipgloss.Color("105"))
}
func (s *State) Blur() {
s.Viewport.Style = s.Viewport.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
}
func (s *State) SetSize(width, height int) {
s.InputBox.SetSize(width)
s.SidePanel.SetSize(width, height, s.InputBox.Style.Copy().GetVerticalFrameSize())
s.SidePanel.SetSize(width, height, s.InputBox.Style.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
// We floor here because width is an int and some fractions are lost
// and also because we ceil the sidepanel's width
// -1 is for some extra invisible margin or padding between the main viewport and the inputbox
newWidth := int(math.Floor(float64(width)*8.0/10.0) - float64(s.Viewport.Style.GetHorizontalFrameSize()))
newHeight := height - s.InputBox.Style.GetVerticalFrameSize() - s.Viewport.Style.GetVerticalFrameSize() - 1
s.Viewport.Width = newWidth
s.Viewport.Height = newHeight
s.Viewport.Style = s.Viewport.Style.Width(newWidth)
s.Viewport.Style = s.Viewport.Style.Height(newHeight)
// copying the existing style is important here otherwise we'll end up with artifacting
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.Client.Channels[s.Client.ActiveChannel].History)
}
@@ -173,15 +194,16 @@ func (s *State) HandleCommand(msg parser.IRCMessage) {
// TODO: handle different commands
switch msg.Command {
case commands.PING:
s.Client.SendCommand(commands.PONG, msg.Parameters[0])
token := msg.Parameters[0]
s.Client.SendCommand(commands.PONG, token)
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)
privMsg := fmt.Sprintf("%s: %s", nick, msgContent)
if c, ok := s.Client.Channels[channel]; ok {
c.History += fullMsg + client.CRLF
c.History += privMsg + client.CRLF
s.Client.Channels[channel] = c
}
@@ -190,40 +212,81 @@ func (s *State) HandleCommand(msg parser.IRCMessage) {
nick := strings.SplitN(msg.Source, "!", 2)[0]
channel := msg.Parameters[0]
fullMsg := fmt.Sprintf("== %s has joined", nick)
joinMsg := fmt.Sprintf("== %s has joined", nick)
if c, ok := s.Client.Channels[channel]; ok {
c.History += fullMsg + client.CRLF
c.History += joinMsg + client.CRLF
if _, exists := c.Users[nick]; !exists {
c.Users[nick] = client.User{}
}
s.Client.Channels[channel] = c
}
if channel == s.Client.ActiveChannel {
s.SidePanel.UpdateNicks()
}
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
quitMsg := fmt.Sprintf("== %s has quit (%s)", nick, reason)
s.Client.Channels[s.Client.ActiveChannel] = c
for chanName, channel := range s.Client.Channels {
channel.History += quitMsg + client.CRLF
delete(channel.Users, nick)
s.Client.Channels[chanName] = channel
}
s.SidePanel.UpdateNicks()
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
case commands.PART:
nick := strings.SplitN(msg.Source, "!", 2)[0]
channel := msg.Parameters[0]
reason := ""
if len(msg.Parameters) > 1 {
reason = msg.Parameters[1]
}
partMsg := fmt.Sprintf("== %s has left %s (%s)", nick, channel, reason)
if c, ok := s.Client.Channels[channel]; ok {
c.History += partMsg + client.CRLF
delete(c.Users, nick)
s.Client.Channels[channel] = c
}
s.SidePanel.UpdateNicks()
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
case commands.RPL_NAMREPLY:
// TODO: put the users into their respective channels
// TODO: do i need these
// client := msg.Parameters[0]
// chanSymbol := msg.Parameters[1]
// channel := msg.Parameters[2]
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
if c, ok := s.Client.Channels[channel]; ok {
for _, nick := range nicks {
prefix := ""
_nick := nick
s.Client.Channels[s.Client.ActiveChannel] = c
if commands.UserPrefixes[string(nick[0])] {
prefix = string(nick[0])
_nick = nick[1:]
}
c.Users[_nick] = client.User{
Prefix: prefix,
}
}
}
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
if channel == s.Client.ActiveChannel {
s.SidePanel.UpdateNicks()
}
case commands.RPL_ENDOFNAMES:
// TODO: what do i do here lol
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 {
@@ -234,10 +297,14 @@ func (s *State) HandleCommand(msg parser.IRCMessage) {
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannel].History)
}
// send a receivedIRCmsg tea message so the ui can update
// we also use this tea message to scroll the viewport down
s.Client.Tea.Send(ReceivedIRCMsg())
}
func (s State) View() string {
top := lipgloss.JoinHorizontal(lipgloss.Right, s.Style.Render(s.Viewport.View()), s.SidePanel.View())
top := lipgloss.JoinHorizontal(lipgloss.Right, s.Viewport.View(), s.SidePanel.View())
screen := lipgloss.JoinVertical(0, top, s.InputBox.View())
return ui.MainStyle.Render(screen)
+73 -15
View File
@@ -18,6 +18,7 @@ package mainscreen
import (
"fmt"
"math"
"strings"
"github.com/charmbracelet/bubbles/viewport"
@@ -27,34 +28,91 @@ import (
)
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
Client *client.Client
Viewport viewport.Model
Content string
Focused bool
}
func (s *SidePanelState) getHeader() string {
separator := strings.Repeat("—", s.Viewport.Width) + "\n"
header := fmt.Sprintf("Users in this channel (%d)\n", len(s.Client.Channels[s.Client.ActiveChannel].Users)) + separator
return header
}
func (s *SidePanelState) getLatestNicks() []string {
nicks := make([]string, 0)
for nick, user := range s.Client.Channels[s.Client.ActiveChannel].Users {
_nick := user.Prefix + nick
nicks = append(nicks, _nick)
}
return nicks
}
func NewSidePanel(client *client.Client) *SidePanelState {
return &SidePanelState{
Style: SidePanelStyle.Copy(),
Client: client,
Viewport: viewport.Model{
HighPerformanceRendering: false,
Wrap: viewport.Wrap,
Style: SidePanelStyle.Copy(),
},
}
}
func (s SidePanelState) Update(msg tea.Msg) (SidePanelState, tea.Cmd) {
return s, nil
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg.(type) {
case SwitchChannelsMsg:
s.UpdateNicks()
return s, nil
}
if s.Focused {
s.Viewport, cmd = s.Viewport.Update(msg)
cmds = append(cmds, cmd)
}
return s, tea.Batch(cmds...)
}
func (s *SidePanelState) Focus() {
s.Focused = true
s.Viewport.Style = s.Viewport.Style.BorderForeground(lipgloss.Color("105"))
}
func (s *SidePanelState) Blur() {
s.Focused = false
s.Viewport.Style = s.Viewport.Style.BorderForeground(lipgloss.Color("#EEE"))
}
func (s *SidePanelState) UpdateNicks() {
header := s.getHeader()
nicks := s.getLatestNicks()
s.Viewport.SetContent(header + strings.Join(nicks, "\n"))
}
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)
// We ceil here because width is an int and some fractions are lost
// -1 is for some extra invisible margin or padding between the sidepanel and the inputbox
newWidth := int(math.Ceil(math.Ceil((float64(width) * 2.0 / 10.0)) - float64(s.Viewport.Style.GetHorizontalFrameSize())))
newHeight := height - inputboxHeight - s.Viewport.Style.GetVerticalFrameSize() - 1
s.Viewport.Style = s.Viewport.Style.Width(newWidth)
s.Viewport.Style = s.Viewport.Style.Height(newHeight)
s.Viewport.Width = newWidth
s.Viewport.Height = newHeight
// on resize recalculate the width and re-set the viewport contents
s.UpdateNicks()
}
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)
return s.Viewport.View()
}
-2
View File
@@ -35,6 +35,4 @@ var (
Border(lipgloss.NormalBorder(), true).
Width(ui.MainStyle.GetWidth() * 2 / 10).
BorderForeground(lipgloss.Color("#EEE"))
InputBoxHeight = 1
)
+2 -2
View File
@@ -20,8 +20,8 @@ import "github.com/charmbracelet/lipgloss"
var (
MainStyle = lipgloss.NewStyle().
Width(172).
Height(40)
Width(0).
Height(0)
DialogStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), true).