refactor: cleanup initial impl and modularize everything

This commit is contained in:
2021-11-13 06:38:12 +02:00
parent 867e874ed3
commit 4bdb4a0fb3
10 changed files with 440 additions and 331 deletions
+110
View File
@@ -0,0 +1,110 @@
// gorc project
// Copyright (C) 2021 IllusionMan1212 and contributors
//
// 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 (
"log"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/ui"
"github.com/illusionman1212/gorc/ui/login"
"golang.org/x/term"
)
type Screen int
const (
Login = iota
MainScreen
)
// TODO: have a global State that holds a separate client State and separate tui State
type State struct {
host string
port string
nick string
password string
windowWidth int
windowHeight int
currentScreen Screen
login login.State
}
func InitialState() State {
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
log.Fatal(err)
}
s := State{
login: login.NewLogin(),
windowWidth: width,
windowHeight: height,
currentScreen: Login,
}
return s
}
func (s State) Init() tea.Cmd {
return nil
}
func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
// TODO: clean up and gracefully quit by leaving the IRC channel/server and then quit bubble.
return s, tea.Quit
}
case tea.WindowSizeMsg:
s.windowWidth = msg.Width
s.windowHeight = msg.Height
ui.MainStyle = ui.MainStyle.
Width(msg.Width).
Height(msg.Height)
return s, nil
}
// switch between which screen is currently active and update its state
switch s.currentScreen {
case Login:
loginState, loginCmd := s.login.Update(msg)
s.login = loginState
return s, loginCmd
}
return s, nil
}
func (s State) View() string {
// switch between which screen is currently active and render it.
switch s.currentScreen {
case Login:
return s.login.View()
}
return "Error: this screen shouldn't ever show"
}
+175
View File
@@ -0,0 +1,175 @@
// gorc project
// Copyright (C) 2021 IllusionMan1212 and contributors
//
// 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 login
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/ui"
)
type State struct {
FocusIndex int
Inputs []textinput.Model
TLS bool
}
func NewLogin() State {
ls := State{
Inputs: make([]textinput.Model, 5),
}
var t textinput.Model
for i := range ls.Inputs {
t = textinput.NewModel()
t.CursorStyle = ui.CursorStyle
switch i {
case 0:
t.Placeholder = "Host"
t.Focus()
t.CharLimit = 40
t.TextStyle = ui.FocusedStyle
case 1:
t.Placeholder = "Port"
t.CharLimit = 5
case 2:
t.Placeholder = "Channel"
t.CharLimit = 32
case 3:
t.Placeholder = "Nickname"
t.CharLimit = 32
case 4:
t.Placeholder = "Password"
t.CharLimit = 32
t.EchoMode = textinput.EchoPassword
t.EchoCharacter = '•'
}
ls.Inputs[i] = t
}
return ls
}
func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "tab", "shift+tab", "enter", "up", "down", "space":
str := msg.String()
// run the newClient cmd when pressing "enter" while focused on the connect button
if str == "enter" && s.FocusIndex == len(s.Inputs)+1 {
return s, tea.Quit // TODO: change this to create a newClient and connect using a tea.Cmd
}
// toggle tls state when pressing "enter" while focused on the checkbox
// TODO: figure out why space isn't working here. all other keys works just fine
if (str == "enter" || str == "space") && s.FocusIndex == len(s.Inputs) {
s.TLS = !s.TLS
return s, nil
}
if str == "up" || str == "shift+tab" {
s.FocusIndex--
} else {
s.FocusIndex++
}
if s.FocusIndex > len(s.Inputs)+1 {
s.FocusIndex = 0
} else if s.FocusIndex < 0 {
s.FocusIndex = len(s.Inputs) + 1
}
cmds := make([]tea.Cmd, len(s.Inputs))
for i := 0; i <= len(s.Inputs)-1; i++ {
if i == s.FocusIndex {
cmds[i] = s.Inputs[i].Focus()
s.Inputs[i].PromptStyle = ui.FocusedStyle
s.Inputs[i].TextStyle = ui.FocusedStyle
continue
}
s.Inputs[i].Blur()
s.Inputs[i].PromptStyle = ui.NoStyle
s.Inputs[i].TextStyle = ui.NoStyle
}
return s, tea.Batch(cmds...)
}
}
cmd := s.updateInputs(msg)
return s, cmd
}
func (s *State) updateInputs(msg tea.Msg) tea.Cmd {
var cmds = make([]tea.Cmd, len(s.Inputs))
for i := range s.Inputs {
s.Inputs[i], cmds[i] = s.Inputs[i].Update(msg)
}
return tea.Batch(cmds...)
}
func (s State) View() string {
var sb strings.Builder
for i := range s.Inputs {
sb.WriteString(s.Inputs[i].View())
if i < len(s.Inputs)-1 {
sb.WriteRune('\n')
}
}
checkbox := &blurredCheckbox
// if the checkbox is focused
if s.FocusIndex == len(s.Inputs) {
checkbox = &focusedCheckbox
// if tls is enabled
if s.TLS {
checkbox = &focusedCheckedCheckbox
}
}
// if the checkbox is not focused and tls is enabled
if s.FocusIndex != len(s.Inputs) && s.TLS {
checkbox = &blurredCheckedCheckbox
}
fmt.Fprintf(&sb, "\n%s\n", *checkbox)
button := &blurredButton
if s.FocusIndex == len(s.Inputs)+1 {
button = &focusedButton
}
fmt.Fprintf(&sb, "\n%s\n", *button)
screen := lipgloss.JoinVertical(0, welcomeMsgStyle.Render(WelcomeMsg), ui.DialogStyle.Render(sb.String()))
final := lipgloss.Place(ui.MainStyle.GetWidth(), ui.MainStyle.GetHeight(), lipgloss.Center, lipgloss.Top, screen)
return ui.MainStyle.Render(final)
}
+74
View File
@@ -0,0 +1,74 @@
// gorc project
// Copyright (C) 2021 IllusionMan1212 and contributors
//
// 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 login
import "github.com/charmbracelet/lipgloss"
const WelcomeMsg = `
/$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$
/$$__ $$ /$$__ $$ /$$__ $$ /$$_____/
| $$ \ $$| $$ \ $$| $$ \__/| $$
| $$ | $$| $$ | $$| $$ | $$
| $$$$$$$| $$$$$$/| $$ | $$$$$$$
\____ $$ \______/ |__/ \_______/
/$$ \ $$
| $$$$$$/
\______/
`
var (
welcomeMsgStyle = lipgloss.NewStyle().
Padding(0, 5).
MarginBottom(5)
focusedButton = lipgloss.NewStyle().
Background(lipgloss.Color("105")).
Foreground(lipgloss.Color("255")).
MarginTop(1).
Padding(0, 2).
Align(lipgloss.Center).
Render("Connect")
blurredButton = lipgloss.NewStyle().
Background(lipgloss.Color("#809070")).
Foreground(lipgloss.Color("#EEEEEE")).
MarginTop(1).
Padding(0, 2).
Align(lipgloss.Center).
Render("Connect")
focusedCheckbox = lipgloss.NewStyle().
Foreground(lipgloss.Color("105")).
MarginTop(1).
Render("[ ] Enable TLS")
blurredCheckbox = lipgloss.NewStyle().
Foreground(lipgloss.Color("#EEEEEE")).
MarginTop(1).
Render("[ ] Enable TLS")
focusedCheckedCheckbox = lipgloss.NewStyle().
Foreground(lipgloss.Color("105")).
MarginTop(1).
Render("[x] Enable TLS")
blurredCheckedCheckbox = lipgloss.NewStyle().
Foreground(lipgloss.Color("#EEEEEE")).
MarginTop(1).
Render("[x] Enable TLS")
)
+47
View File
@@ -0,0 +1,47 @@
// gorc project
// Copyright (C) 2021 IllusionMan1212 and contributors
//
// 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 ui
import "github.com/charmbracelet/lipgloss"
var (
Border = lipgloss.Border{
Top: "─",
Bottom: "─",
Left: "│",
Right: "│",
TopLeft: "╭",
TopRight: "╮",
BottomLeft: "╰",
BottomRight: "╯",
}
MainStyle = lipgloss.NewStyle().
Width(172).
Height(40)
DialogStyle = lipgloss.NewStyle().
Border(Border, true).
Align(lipgloss.Center).
Width(50).
Padding(3, 6).
BorderForeground(lipgloss.Color("105"))
CursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("105"))
FocusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("105"))
NoStyle = lipgloss.NewStyle()
)