mirror of
https://github.com/IllusionMan1212/gorc.git
synced 2026-10-24 02:12:33 +00:00
feat: small refactor. basic PRIVMSG sending
This commit is contained in:
+14
-4
@@ -22,16 +22,16 @@ import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
type ReceivedIRCCommandMsg struct {
|
||||
Msg string
|
||||
}
|
||||
|
||||
type InitialReadMsg struct{}
|
||||
|
||||
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 {
|
||||
@@ -40,3 +40,13 @@ func (s State) readFromServer() tea.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}
|
||||
}
|
||||
}
|
||||
|
||||
+22
-4
@@ -20,23 +20,41 @@ import (
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/illusionman1212/gorc/client"
|
||||
)
|
||||
|
||||
type InputState struct {
|
||||
Input textinput.Model
|
||||
Style lipgloss.Style
|
||||
Input textinput.Model
|
||||
Style lipgloss.Style
|
||||
Client *client.Client
|
||||
}
|
||||
|
||||
func NewInputBox() InputState {
|
||||
func NewInputBox(client *client.Client) InputState {
|
||||
input := textinput.NewModel()
|
||||
input.Placeholder = "Send a message..."
|
||||
input.Focus()
|
||||
state := InputState{input, InputboxStyle.Copy()}
|
||||
state := InputState{
|
||||
Input: input,
|
||||
Style: InputboxStyle.Copy(),
|
||||
Client: client,
|
||||
}
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
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
|
||||
s.Input, cmd = s.Input.Update(msg)
|
||||
return s, cmd
|
||||
|
||||
+27
-12
@@ -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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user