Files
gorc/ui/mainscreen/mainscreen.go
T

373 lines
10 KiB
Go
Raw Normal View History

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.
package mainscreen
import (
"bufio"
"math"
"strings"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/client"
2022-01-09 04:30:22 +02:00
"github.com/illusionman1212/gorc/commands"
"github.com/illusionman1212/gorc/ui"
)
type Window int
2022-01-20 05:08:39 +02:00
type TabDirection int
const (
Viewport Window = iota
SidePanel
InputBox
)
2022-01-20 05:08:39 +02:00
const (
Left TabDirection = iota
Right
)
// NOTE: I ABSOLUTELY HATE THIS
var FirstTabIndexInTabBar = 0
var LastTabIndexInTabBar = 0
type State struct {
2022-01-20 05:08:39 +02:00
ConnReader *bufio.Reader
Client *client.Client
Viewport *viewport.Model
FocusIndex Window
TabRenderingDirection TabDirection
2022-01-09 04:30:22 +02:00
InputBox InputState
SidePanel *SidePanelState
}
2022-01-05 00:54:54 +02:00
func NewMainScreen(client *client.Client) State {
newViewport := viewport.New(0, 0)
newViewport.Wrap = viewport.Wrap
newViewport.Style = MessagesStyle.Copy()
2022-01-09 04:30:22 +02:00
return State{
2022-01-20 05:08:39 +02:00
ConnReader: nil,
Client: client,
Viewport: &newViewport,
FocusIndex: InputBox,
InputBox: NewInputBox(),
SidePanel: NewSidePanel(client),
TabRenderingDirection: Right,
}
}
func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case ReceivedIRCMsgMsg:
2022-01-20 05:08:39 +02:00
wasAtBottom := s.Viewport.AtBottom()
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History)
2022-01-20 05:08:39 +02:00
if wasAtBottom {
s.Viewport.GotoBottom()
}
return s, nil
2022-01-05 00:54:54 +02:00
case SendPrivMsg:
2022-01-09 04:30:22 +02:00
// 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], " ")
}
2022-01-20 05:08:39 +02:00
2022-01-09 04:30:22 +02:00
if strings.ToUpper(command) == commands.JOIN {
2022-01-20 05:08:39 +02:00
// TODO: handle comma separated channels
channel := params[0]
s.Client.ActiveChannel = channel
for i, c := range s.Client.Channels {
if c.Name == channel {
s.Client.ActiveChannelIndex = i
2022-01-20 05:08:39 +02:00
break
} else if i == len(s.Client.Channels)-1 {
s.Client.Channels = append(s.Client.Channels, client.Channel{
Name: channel,
Users: make(map[string]client.User),
})
s.Client.ActiveChannelIndex = len(s.Client.Channels) - 1
2022-01-20 05:08:39 +02:00
LastTabIndexInTabBar = len(s.Client.Channels) - 1
s.TabRenderingDirection = Left
}
}
s.Client.SendCommand(commands.JOIN, params...)
return s, SwitchChannels
2022-01-09 04:30:22 +02:00
}
s.Client.SendCommand(command, params...)
} else {
if s.Client.ActiveChannel != s.Client.Host {
fullMsg := s.Client.Nickname + ": " + msg.Msg
s.Client.Channels[s.Client.ActiveChannelIndex].History += fullMsg + client.CRLF
// 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.ActiveChannelIndex].History)
s.Viewport.GotoBottom()
2022-01-09 04:30:22 +02:00
}
}
2022-01-09 04:30:22 +02:00
return s, nil
2022-01-20 05:08:39 +02:00
case SwitchChannelsMsg:
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History)
2022-01-20 05:08:39 +02:00
s.Viewport.GotoBottom()
*s.SidePanel, cmd = s.SidePanel.Update(msg)
cmds = append(cmds, cmd)
return s, tea.Batch(cmds...)
case tea.KeyMsg:
key := msg.String()
switch key {
case "tab", "shift+tab":
if key == "tab" {
s.FocusIndex++
} else {
s.FocusIndex--
}
if s.FocusIndex > 2 {
s.FocusIndex = 0
} else if s.FocusIndex < 0 {
s.FocusIndex = 2
}
switch s.FocusIndex {
case Viewport:
s.Focus()
s.InputBox.Blur()
s.SidePanel.Blur()
case InputBox:
cmds = append(cmds, textinput.Blink)
s.Blur()
s.InputBox.Focus()
s.SidePanel.Blur()
case SidePanel:
s.Blur()
s.InputBox.Blur()
s.SidePanel.Focus()
}
return s, tea.Batch(cmds...)
2022-01-20 05:08:39 +02:00
case "right", "left":
// we need this so that the viewport doesnt scroll to the bottom
if len(s.Client.Channels) == 1 {
return s, nil
}
if key == "right" {
s.Client.ActiveChannelIndex++
2022-01-20 05:08:39 +02:00
} else {
s.Client.ActiveChannelIndex--
2022-01-20 05:08:39 +02:00
}
if s.Client.ActiveChannelIndex >= len(s.Client.Channels) {
s.Client.ActiveChannelIndex = 0
} else if s.Client.ActiveChannelIndex < 0 {
s.Client.ActiveChannelIndex = len(s.Client.Channels) - 1
2022-01-20 05:08:39 +02:00
}
if s.Client.ActiveChannelIndex > LastTabIndexInTabBar {
2022-01-20 05:08:39 +02:00
s.TabRenderingDirection = Left
LastTabIndexInTabBar = s.Client.ActiveChannelIndex
} else if s.Client.ActiveChannelIndex < FirstTabIndexInTabBar {
FirstTabIndexInTabBar = s.Client.ActiveChannelIndex
2022-01-20 05:08:39 +02:00
s.TabRenderingDirection = Right
}
s.Client.ActiveChannel = s.Client.Channels[s.Client.ActiveChannelIndex].Name
2022-01-20 05:08:39 +02:00
return s, SwitchChannels
case "g":
if s.FocusIndex == Viewport {
s.Viewport.GotoTop()
}
case "G":
if s.FocusIndex == Viewport {
s.Viewport.GotoBottom()
}
}
}
switch s.FocusIndex {
case Viewport:
2022-01-09 04:30:22 +02:00
*s.Viewport, cmd = s.Viewport.Update(msg)
cmds = append(cmds, cmd)
case InputBox:
s.InputBox, cmd = s.InputBox.Update(msg)
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"))
2022-01-20 05:08:39 +02:00
tab = tab.Copy().BorderForeground(lipgloss.Color("105"))
leftArrowDim = leftArrowDim.Copy().BorderForeground(lipgloss.Color("105"))
rightArrowDim = rightArrowDim.Copy().BorderForeground(lipgloss.Color("105"))
leftArrowLit = leftArrowLit.Copy().BorderForeground(lipgloss.Color("105"))
rightArrowLit = rightArrowLit.Copy().BorderForeground(lipgloss.Color("105"))
tabLine = tabLine.Copy().Foreground(lipgloss.Color("105"))
}
func (s *State) Blur() {
s.Viewport.Style = s.Viewport.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
2022-01-20 05:08:39 +02:00
tab = tab.Copy().UnsetBorderForeground()
leftArrowDim = leftArrowDim.Copy().UnsetBorderForeground()
rightArrowDim = rightArrowDim.Copy().UnsetBorderForeground()
leftArrowLit = leftArrowLit.Copy().UnsetBorderForeground()
rightArrowLit = rightArrowLit.Copy().UnsetBorderForeground()
tabLine = tabLine.Copy().UnsetForeground()
}
func (s *State) SetSize(width, height int) {
s.InputBox.SetSize(width)
s.SidePanel.SetSize(width, height, s.InputBox.Style.GetVerticalFrameSize())
// We floor here because width is an int and some fractions are lost
// and also because we ceil the sidepanel's width
2022-01-20 05:08:39 +02:00
// -3 for the tab bar height
newWidth := int(math.Floor(float64(width)*8.0/10.0) - float64(s.Viewport.Style.GetHorizontalFrameSize()))
2022-01-20 05:08:39 +02:00
newHeight := height - s.InputBox.Style.GetVerticalFrameSize() - s.Viewport.Style.GetVerticalFrameSize() - 3
s.Viewport.Width = newWidth
s.Viewport.Height = newHeight
2022-01-09 04:30:22 +02:00
s.Viewport.Style = s.Viewport.Style.Width(newWidth)
s.Viewport.Style = s.Viewport.Style.Height(newHeight)
2022-01-09 04:30:22 +02:00
2022-01-20 05:08:39 +02:00
// we need this to render an empty viewport
history := ""
if len(s.Client.Channels) != 0 {
history = s.Client.Channels[s.Client.ActiveChannelIndex].History
2022-01-20 05:08:39 +02:00
}
// we need to re-set the content because words wrap differently on different sizes
2022-01-20 05:08:39 +02:00
s.Viewport.SetContent(history)
s.Viewport.SetYOffset(s.Viewport.YOffset)
}
2022-01-20 05:08:39 +02:00
func (s State) buildTabBar(rightArrow string, leftArrow string) string {
var renderedTabs []string
tabs := ""
2022-01-20 05:08:39 +02:00
switch s.TabRenderingDirection {
case Left:
for i := LastTabIndexInTabBar; i >= 0; i-- {
if s.Client.Channels[i].Name == s.Client.ActiveChannel {
renderedTabs = append([]string{activeTab.Render(s.Client.Channels[i].Name)}, renderedTabs...)
} else {
renderedTabs = append([]string{tab.Render(s.Client.Channels[i].Name)}, renderedTabs...)
}
2022-01-20 05:08:39 +02:00
tabs = lipgloss.JoinHorizontal(
lipgloss.Top,
renderedTabs...,
)
if lipgloss.Width(tabs) > lipgloss.Width(s.Viewport.View())-lipgloss.Width(leftArrow)-lipgloss.Width(rightArrow) {
// set the first tab to be displayed to the index of the previous tab in the loop
FirstTabIndexInTabBar = i + 1
// dont render the newly added tab
renderedTabs = renderedTabs[1:]
break
}
}
2022-01-20 05:08:39 +02:00
case Right:
for i := FirstTabIndexInTabBar; i < len(s.Client.Channels); i++ {
if s.Client.Channels[i].Name == s.Client.ActiveChannel {
renderedTabs = append(renderedTabs, activeTab.Render(s.Client.Channels[i].Name))
} else {
renderedTabs = append(renderedTabs, tab.Render(s.Client.Channels[i].Name))
}
2022-01-20 05:08:39 +02:00
tabs = lipgloss.JoinHorizontal(
lipgloss.Top,
renderedTabs...,
)
if lipgloss.Width(tabs) > lipgloss.Width(s.Viewport.View())-lipgloss.Width(leftArrow)-lipgloss.Width(rightArrow) {
// set the last tab to be displayed to the index of the previous tab in the loop
LastTabIndexInTabBar = i - 1
// dont render the newly added tab
renderedTabs = renderedTabs[:len(renderedTabs)-1]
break
}
2022-01-09 04:30:22 +02:00
}
2022-01-20 05:08:39 +02:00
}
2022-01-20 05:08:39 +02:00
tabs = lipgloss.JoinHorizontal(
lipgloss.Top,
renderedTabs...,
)
2022-01-20 05:08:39 +02:00
return tabs
}
2022-01-20 05:08:39 +02:00
func (s State) View() string {
leftArrow := leftArrowDim.Render("❰")
rightArrow := rightArrowDim.Render("❱")
2022-01-09 04:30:22 +02:00
2022-01-20 05:08:39 +02:00
tabs := s.buildTabBar(leftArrow, rightArrow)
tabBar := strings.Builder{}
2022-01-09 04:30:22 +02:00
2022-01-20 05:08:39 +02:00
if FirstTabIndexInTabBar != 0 {
leftArrow = leftArrowLit.Render("❰")
2022-01-05 00:54:54 +02:00
}
2022-01-20 05:08:39 +02:00
if LastTabIndexInTabBar != len(s.Client.Channels)-1 {
rightArrow = rightArrowLit.Render("❱")
}
2022-01-05 00:54:54 +02:00
2022-01-20 05:08:39 +02:00
tabBarLine := tabLine.Render(
strings.Repeat(
"─",
max(0, lipgloss.Width(s.Viewport.View())-lipgloss.Width(tabs)-lipgloss.Width(rightArrow)-lipgloss.Width(leftArrow)),
),
)
tabs = lipgloss.JoinHorizontal(lipgloss.Bottom, leftArrow, tabs, tabBarLine, rightArrow)
tabBar.WriteString(tabs)
leftSide := lipgloss.JoinVertical(0, tabBar.String(), s.Viewport.View())
top := lipgloss.JoinHorizontal(lipgloss.Right, leftSide, s.SidePanel.View())
2022-01-09 04:30:22 +02:00
screen := lipgloss.JoinVertical(0, top, s.InputBox.View())
return ui.MainStyle.Render(screen)
}
2022-01-20 05:08:39 +02:00
func max(a, b int) int {
if a > b {
return a
}
return b
}