2021-11-14 06:31:51 +02:00
|
|
|
// 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.
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
package mainscreen
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2021-12-13 09:38:07 +02:00
|
|
|
"github.com/charmbracelet/bubbles/viewport"
|
2021-11-14 06:19:46 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2021-12-13 09:38:07 +02:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2021-11-29 23:07:21 +02:00
|
|
|
"github.com/illusionman1212/gorc/client"
|
2021-11-14 06:19:46 +02:00
|
|
|
"github.com/illusionman1212/gorc/parser"
|
2021-12-13 09:38:07 +02:00
|
|
|
"github.com/illusionman1212/gorc/ui"
|
2021-11-14 06:19:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type State struct {
|
2021-12-13 09:38:07 +02:00
|
|
|
Content string
|
|
|
|
|
Reader *bufio.Reader
|
|
|
|
|
Client *client.Client
|
|
|
|
|
Viewport viewport.Model
|
2021-12-25 21:45:58 +02:00
|
|
|
Style lipgloss.Style
|
2021-12-13 09:38:07 +02:00
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
InputBox InputState
|
2021-12-13 09:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewMainScreen() State {
|
2021-12-25 21:45:58 +02:00
|
|
|
newViewport := viewport.Model{}
|
2021-12-13 09:38:07 +02:00
|
|
|
newViewport.HighPerformanceRendering = false
|
|
|
|
|
newViewport.SetContent("")
|
|
|
|
|
|
|
|
|
|
state := State{
|
|
|
|
|
Content: "",
|
|
|
|
|
Reader: nil,
|
|
|
|
|
Client: nil,
|
|
|
|
|
Viewport: newViewport,
|
2021-12-25 21:45:58 +02:00
|
|
|
Style: MessagesStyle.Copy(),
|
|
|
|
|
InputBox: NewInputBox(),
|
2021-12-13 09:38:07 +02:00
|
|
|
}
|
|
|
|
|
return state
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
|
2021-12-13 09:38:07 +02:00
|
|
|
var cmd tea.Cmd
|
2021-12-25 21:45:58 +02:00
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case InitialReadMsg:
|
|
|
|
|
return s, s.readFromServer
|
|
|
|
|
case ReceivedIRCCommandMsg:
|
|
|
|
|
message := parser.ParseIRCMessage(msg.Msg)
|
2021-12-09 05:14:20 +02:00
|
|
|
fullMsg := fmt.Sprintf("%s %s %s %s", message.Tags, message.Source, message.Command, strings.Join(message.Parameters, " "))
|
2021-12-13 09:38:07 +02:00
|
|
|
s.Content += fullMsg
|
|
|
|
|
|
|
|
|
|
s.Viewport.SetContent(s.Content)
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2021-11-29 23:07:21 +02:00
|
|
|
// TODO: handle different commands
|
|
|
|
|
switch message.Command {
|
|
|
|
|
case "PING":
|
|
|
|
|
s.Client.SendCommand("PONG")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
return s, s.readFromServer
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 09:38:07 +02:00
|
|
|
s.Viewport, cmd = s.Viewport.Update(msg)
|
2021-12-25 21:45:58 +02:00
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
|
|
|
|
s.InputBox, cmd = s.InputBox.Update(msg)
|
|
|
|
|
cmds = append(cmds, cmd)
|
2021-12-13 09:38:07 +02:00
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
return s, tea.Batch(cmds...)
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
func (s *State) SetSize(width, height int) {
|
|
|
|
|
s.InputBox.SetSize(width)
|
|
|
|
|
s.Viewport.Width = width - s.Style.GetVerticalFrameSize()
|
|
|
|
|
// 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())
|
|
|
|
|
// we need to re-set the content because words wrap differently on different sizes
|
|
|
|
|
s.Viewport.SetContent(s.Content)
|
|
|
|
|
}
|
2021-12-13 09:38:07 +02:00
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
func (s State) View() string {
|
|
|
|
|
screen := lipgloss.JoinVertical(0, s.Style.Render(s.Viewport.View()), s.InputBox.View())
|
2021-12-13 09:38:07 +02:00
|
|
|
|
2021-12-25 21:45:58 +02:00
|
|
|
return ui.MainStyle.Render(screen)
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|