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
+3 -4
View File
@@ -34,8 +34,7 @@ type Client struct {
const CRLF = "\r\n" const CRLF = "\r\n"
func NewClient(host string, port string, tlsEnabled bool) *Client { func NewClient(host string, port string, tlsEnabled bool) Client {
// TODO: distinguish between secure and insecure connections
addr := fmt.Sprintf("%s:%s", host, port) addr := fmt.Sprintf("%s:%s", host, port)
if tlsEnabled { if tlsEnabled {
@@ -46,7 +45,7 @@ func NewClient(host string, port string, tlsEnabled bool) *Client {
log.Fatal(err) log.Fatal(err)
} }
return &Client{ return Client{
TlsConn: conn, TlsConn: conn,
TcpConn: nil, TcpConn: nil,
} }
@@ -62,7 +61,7 @@ func NewClient(host string, port string, tlsEnabled bool) *Client {
log.Fatal(err) log.Fatal(err)
} }
return &Client{ return Client{
TlsConn: nil, TlsConn: nil,
TcpConn: conn, TcpConn: conn,
} }
+36 -42
View File
@@ -35,27 +35,29 @@ const (
) )
type UI struct { type UI struct {
currentScreen Screen CurrentScreen Screen
login login.State Login login.State
mainScreen mainscreen.State MainScreen mainscreen.State
} }
type State struct { type State struct {
ui UI UI UI
client *client.Client Client *client.Client
} }
func initialUiState() UI { func initialUiState(client *client.Client) UI {
return UI{ return UI{
login: login.NewLogin(), Login: login.NewLogin(),
mainScreen: mainscreen.NewMainScreen(), MainScreen: mainscreen.NewMainScreen(client),
} }
} }
func InitialState() State { func InitialState() State {
client := &client.Client{}
s := State{ s := State{
client: &client.Client{}, Client: client,
ui: initialUiState(), UI: initialUiState(client),
} }
return s return s
@@ -70,14 +72,7 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "ctrl+c": case "ctrl+c":
// TODO: clean up and gracefully quit by leaving the IRC channel/server and then quit bubble. return s, s.Quit
if s.client.TlsConn != nil {
s.client.TlsConn.Close()
}
if s.client.TcpConn != nil {
s.client.TcpConn.Close()
}
return s, tea.Quit
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
@@ -85,45 +80,44 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Width(msg.Width). Width(msg.Width).
Height(msg.Height) Height(msg.Height)
s.ui.mainScreen.SetSize(msg.Width, msg.Height) s.UI.MainScreen.SetSize(msg.Width, msg.Height)
return s, nil return s, nil
case login.ConnectingMsg: case login.ConnectingMsg:
host := s.ui.login.Inputs[0].Value() host := s.UI.Login.Inputs[0].Value()
port := s.ui.login.Inputs[1].Value() port := s.UI.Login.Inputs[1].Value()
channel := s.ui.login.Inputs[2].Value() channel := s.UI.Login.Inputs[2].Value()
nickname := s.ui.login.Inputs[3].Value() nickname := s.UI.Login.Inputs[3].Value()
password := s.ui.login.Inputs[4].Value() password := s.UI.Login.Inputs[4].Value()
tlsEnabled := s.ui.login.TLS tlsEnabled := s.UI.Login.TLS
s.ui.currentScreen = MainScreen s.UI.CurrentScreen = MainScreen
s.UI.MainScreen.CurrentChannel = channel
// create new client with the provided host and port // create new client with the provided host and port
client := client.NewClient(host, port, tlsEnabled) (*s.Client) = client.NewClient(host, port, tlsEnabled)
client.Register(nickname, password, channel) s.Client.Register(nickname, password, channel)
s.client = client
s.ui.mainScreen.Client = client
if tlsEnabled { if tlsEnabled {
r := bufio.NewReaderSize(client.TlsConn, 512) r := bufio.NewReaderSize(s.Client.TlsConn, 512)
s.ui.mainScreen.Reader = r s.UI.MainScreen.Reader = r
} else { } else {
r := bufio.NewReaderSize(client.TcpConn, 512) r := bufio.NewReaderSize(s.Client.TcpConn, 512)
s.ui.mainScreen.Reader = r s.UI.MainScreen.Reader = r
} }
return s, mainscreen.InitialRead return s, mainscreen.InitialRead
} }
// switch between which screen is currently active and update its state // switch between which screen is currently active and update its state
switch s.ui.currentScreen { switch s.UI.CurrentScreen {
case Login: case Login:
state, cmd := s.ui.login.Update(msg) state, cmd := s.UI.Login.Update(msg)
s.ui.login = state s.UI.Login = state
return s, cmd return s, cmd
case MainScreen: case MainScreen:
state, cmd := s.ui.mainScreen.Update(msg) state, cmd := s.UI.MainScreen.Update(msg)
s.ui.mainScreen = state s.UI.MainScreen = state
return s, cmd return s, cmd
} }
@@ -132,11 +126,11 @@ func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (s State) View() string { func (s State) View() string {
// switch between which screen is currently active and render it. // switch between which screen is currently active and render it.
switch s.ui.currentScreen { switch s.UI.CurrentScreen {
case Login: case Login:
return s.ui.login.View() return s.UI.Login.View()
case MainScreen: case MainScreen:
return s.ui.mainScreen.View() return s.UI.MainScreen.View()
} }
return "Error: this screen shouldn't ever show" return "Error: this screen shouldn't ever show"
+32
View File
@@ -0,0 +1,32 @@
// 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 app
import tea "github.com/charmbracelet/bubbletea"
func (s *State) Quit() tea.Msg {
if s.Client.TlsConn != nil {
s.Client.SendCommand("QUIT")
s.Client.TlsConn.Close()
}
if s.Client.TcpConn != nil {
s.Client.SendCommand("QUIT")
s.Client.TcpConn.Close()
}
return tea.Quit()
}
+14 -4
View File
@@ -22,16 +22,16 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
type ReceivedIRCCommandMsg struct {
Msg string
}
type InitialReadMsg struct{} type InitialReadMsg struct{}
func InitialRead() tea.Msg { func InitialRead() tea.Msg {
return InitialReadMsg{} return InitialReadMsg{}
} }
type ReceivedIRCCommandMsg struct {
Msg string
}
func (s State) readFromServer() tea.Msg { func (s State) readFromServer() tea.Msg {
msg, err := s.Reader.ReadString('\n') msg, err := s.Reader.ReadString('\n')
if err != nil { if err != nil {
@@ -40,3 +40,13 @@ func (s State) readFromServer() tea.Msg {
return ReceivedIRCCommandMsg{Msg: msg} return ReceivedIRCCommandMsg{Msg: msg}
} }
type SendPrivMsg struct {
Msg string
}
func (s InputState) SendingPrivMsg(msg string) tea.Cmd {
return func() tea.Msg {
return SendPrivMsg{Msg: msg}
}
}
+20 -2
View File
@@ -20,23 +20,41 @@ import (
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/client"
) )
type InputState struct { type InputState struct {
Input textinput.Model Input textinput.Model
Style lipgloss.Style Style lipgloss.Style
Client *client.Client
} }
func NewInputBox() InputState { func NewInputBox(client *client.Client) InputState {
input := textinput.NewModel() input := textinput.NewModel()
input.Placeholder = "Send a message..." input.Placeholder = "Send a message..."
input.Focus() input.Focus()
state := InputState{input, InputboxStyle.Copy()} state := InputState{
Input: input,
Style: InputboxStyle.Copy(),
Client: client,
}
return state return state
} }
func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) { func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
key := msg.String()
switch key {
case "enter":
value := s.Input.Value()
s.Input.Reset()
return s, s.SendingPrivMsg(value)
}
}
var cmd tea.Cmd var cmd tea.Cmd
s.Input, cmd = s.Input.Update(msg) s.Input, cmd = s.Input.Update(msg)
return s, cmd return s, cmd
+27 -12
View File
@@ -45,10 +45,14 @@ type State struct {
Style lipgloss.Style Style lipgloss.Style
FocusIndex Window FocusIndex Window
// a channel could be either a server channel
// or a username of a user
CurrentChannel string
InputBox InputState InputBox InputState
} }
func NewMainScreen() State { func NewMainScreen(client *client.Client) State {
newViewport := viewport.Model{ newViewport := viewport.Model{
HighPerformanceRendering: false, HighPerformanceRendering: false,
Wrap: true, Wrap: true,
@@ -57,11 +61,11 @@ func NewMainScreen() State {
state := State{ state := State{
Content: "", Content: "",
Reader: nil, Reader: nil,
Client: nil, Client: client,
Viewport: newViewport, Viewport: newViewport,
Style: MessagesStyle.Copy(), Style: MessagesStyle.Copy(),
FocusIndex: InputBox, FocusIndex: InputBox,
InputBox: NewInputBox(), InputBox: NewInputBox(client),
} }
return state return state
} }
@@ -75,18 +79,15 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
return s, s.readFromServer return s, s.readFromServer
case ReceivedIRCCommandMsg: case ReceivedIRCCommandMsg:
message := parser.ParseIRCMessage(msg.Msg) message := parser.ParseIRCMessage(msg.Msg)
fullMsg := fmt.Sprintf("%s %s %s %s", message.Tags, message.Source, message.Command, strings.Join(message.Parameters, " ")) s.HandleCommand(message)
s.Content += fullMsg
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) 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 return s, s.readFromServer
case tea.KeyMsg: case tea.KeyMsg:
key := msg.String() key := msg.String()
@@ -147,6 +148,20 @@ func (s *State) SetSize(width, height int) {
s.Viewport.SetContent(s.Content) 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 { func (s State) View() string {
screen := lipgloss.JoinVertical(0, s.Style.Render(s.Viewport.View()), s.InputBox.View()) screen := lipgloss.JoinVertical(0, s.Style.Render(s.Viewport.View()), s.InputBox.View())