2021-11-13 06:38:12 +02:00
|
|
|
// gorc project
|
2022-01-24 04:24:58 +02:00
|
|
|
// Copyright (C) 2022 IllusionMan1212
|
2021-11-13 06:38:12 +02:00
|
|
|
//
|
|
|
|
|
// 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 (
|
2021-11-14 06:19:46 +02:00
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
2021-11-13 06:38:12 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-01-24 04:23:02 +02:00
|
|
|
"github.com/illusionman1212/gorc/cmds"
|
2022-01-24 02:26:24 +02:00
|
|
|
"github.com/illusionman1212/gorc/irc"
|
|
|
|
|
"github.com/illusionman1212/gorc/irc/handler"
|
2021-11-13 06:38:12 +02:00
|
|
|
"github.com/illusionman1212/gorc/ui"
|
|
|
|
|
"github.com/illusionman1212/gorc/ui/login"
|
2021-11-14 06:19:46 +02:00
|
|
|
"github.com/illusionman1212/gorc/ui/mainscreen"
|
2021-11-13 06:38:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Screen int
|
|
|
|
|
|
|
|
|
|
const (
|
2022-01-15 21:56:06 +02:00
|
|
|
Login Screen = iota
|
2021-11-13 06:38:12 +02:00
|
|
|
MainScreen
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
type UI struct {
|
2022-01-05 00:54:54 +02:00
|
|
|
CurrentScreen Screen
|
|
|
|
|
Login login.State
|
|
|
|
|
MainScreen mainscreen.State
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
2021-11-13 06:38:12 +02:00
|
|
|
|
2021-11-14 06:19:46 +02:00
|
|
|
type State struct {
|
2022-01-05 00:54:54 +02:00
|
|
|
UI UI
|
2022-01-24 02:26:24 +02:00
|
|
|
Client *irc.Client
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 02:26:24 +02:00
|
|
|
func initialUiState(client *irc.Client) UI {
|
2021-11-14 06:19:46 +02:00
|
|
|
return UI{
|
2022-01-05 00:54:54 +02:00
|
|
|
Login: login.NewLogin(),
|
|
|
|
|
MainScreen: mainscreen.NewMainScreen(client),
|
2021-11-14 06:19:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 21:56:06 +02:00
|
|
|
func InitialState() *State {
|
2022-01-24 02:26:24 +02:00
|
|
|
client := &irc.Client{}
|
2022-01-05 00:54:54 +02:00
|
|
|
|
2022-01-15 21:56:06 +02:00
|
|
|
return &State{
|
2022-01-05 00:54:54 +02:00
|
|
|
Client: client,
|
|
|
|
|
UI: initialUiState(client),
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s State) Init() tea.Cmd {
|
2021-11-14 06:19:46 +02:00
|
|
|
return textinput.Blink
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
|
switch msg.String() {
|
|
|
|
|
case "ctrl+c":
|
2022-01-24 04:23:02 +02:00
|
|
|
return s, cmds.Quit(s.Client)
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
|
ui.MainStyle = ui.MainStyle.
|
|
|
|
|
Width(msg.Width).
|
|
|
|
|
Height(msg.Height)
|
|
|
|
|
|
2022-01-25 00:05:34 +02:00
|
|
|
s.UI.Login.SetSize(msg.Width, msg.Height)
|
2022-01-05 00:54:54 +02:00
|
|
|
s.UI.MainScreen.SetSize(msg.Width, msg.Height)
|
2021-12-13 09:38:07 +02:00
|
|
|
|
2021-11-13 06:38:12 +02:00
|
|
|
return s, nil
|
2022-01-24 04:23:02 +02:00
|
|
|
case cmds.ConnectMsg:
|
2022-01-05 00:54:54 +02:00
|
|
|
host := s.UI.Login.Inputs[0].Value()
|
|
|
|
|
port := s.UI.Login.Inputs[1].Value()
|
|
|
|
|
channel := s.UI.Login.Inputs[2].Value()
|
|
|
|
|
nickname := s.UI.Login.Inputs[3].Value()
|
|
|
|
|
password := s.UI.Login.Inputs[4].Value()
|
|
|
|
|
tlsEnabled := s.UI.Login.TLS
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2022-01-05 00:54:54 +02:00
|
|
|
s.UI.CurrentScreen = MainScreen
|
2021-11-14 06:19:46 +02:00
|
|
|
|
|
|
|
|
// create new client with the provided host and port
|
2022-01-15 21:56:06 +02:00
|
|
|
s.Client.Initialize(host, port, tlsEnabled)
|
2022-01-05 00:54:54 +02:00
|
|
|
s.Client.Register(nickname, password, channel)
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2022-01-24 04:23:02 +02:00
|
|
|
s.Client.LastTabIndexInTabBar = len(s.Client.Channels) - 1
|
2022-01-20 05:08:39 +02:00
|
|
|
|
2022-01-24 02:26:24 +02:00
|
|
|
go handler.ReadLoop(s.Client)
|
2021-11-14 06:19:46 +02:00
|
|
|
|
2022-01-09 04:30:22 +02:00
|
|
|
return s, textinput.Blink
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// switch between which screen is currently active and update its state
|
2022-01-05 00:54:54 +02:00
|
|
|
switch s.UI.CurrentScreen {
|
2021-11-13 06:38:12 +02:00
|
|
|
case Login:
|
2022-01-05 00:54:54 +02:00
|
|
|
state, cmd := s.UI.Login.Update(msg)
|
|
|
|
|
s.UI.Login = state
|
2021-11-14 06:19:46 +02:00
|
|
|
return s, cmd
|
|
|
|
|
case MainScreen:
|
2022-01-05 00:54:54 +02:00
|
|
|
state, cmd := s.UI.MainScreen.Update(msg)
|
|
|
|
|
s.UI.MainScreen = state
|
2021-11-14 06:19:46 +02:00
|
|
|
return s, cmd
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s State) View() string {
|
|
|
|
|
// switch between which screen is currently active and render it.
|
2022-01-05 00:54:54 +02:00
|
|
|
switch s.UI.CurrentScreen {
|
2021-11-13 06:38:12 +02:00
|
|
|
case Login:
|
2022-01-05 00:54:54 +02:00
|
|
|
return s.UI.Login.View()
|
2021-11-14 06:19:46 +02:00
|
|
|
case MainScreen:
|
2022-01-05 00:54:54 +02:00
|
|
|
return s.UI.MainScreen.View()
|
2021-11-13 06:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "Error: this screen shouldn't ever show"
|
|
|
|
|
}
|