summaryrefslogtreecommitdiff
path: root/pkg/ui/views/views.go
blob: fa7003513def624aa3b6f89c41950c3c4e66efd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package views

import (
	"errors"
	"os"

	"os/exec"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	"golang.org/x/term"
)

var logo = `
▗▄▄▖  ▗▄▖ ▗▖ ▗▖ ▗▄▖ ▗▖  ▗▖▗▖  ▗▖ ▗▄▖ 
▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▛▚▖▐▌▐▛▚▖▐▌▐▌ ▐▌
▐▛▀▚▖▐▛▀▜▌▐▛▀▜▌▐▛▀▜▌▐▌ ▝▜▌▐▌ ▝▜▌▐▛▀▜▌
▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌ ▐▌▐▌  ▐▌▐▌  ▐▌▐▌ ▐▌
`

var (
	highlightColor = lipgloss.Color("#7ee2a8")
	errorStyle     = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000"))
	altCodeStyle   = lipgloss.NewStyle().Foreground(lipgloss.Color("#666666")).Bold(true)
	windowStyle    = lipgloss.NewStyle().BorderForeground(highlightColor).Padding(2, 0).Align(lipgloss.Center).Border(lipgloss.RoundedBorder())
	inputStyle     = lipgloss.NewStyle().Foreground(highlightColor)
)

// Get terminal size dynamically
func GetTerminalSize() (width, height int) {
	fd := int(os.Stdin.Fd())
	if w, h, err := term.GetSize(fd); err == nil {
		return w, h
	}
	return 80, 24 // Default size if detection fails
}

// Clear terminal screen
func ClearScreen() {
	if len(os.Getenv("DEBUG")) == 0 {
		cmd := exec.Command("clear")
		if os.Getenv("OS") == "Windows_NT" {
			cmd = exec.Command("cmd", "/c", "cls")
		}
		cmd.Stdout = os.Stdout
		cmd.Run()
	}
}

func getFormWidth(width int) int {
	formWidth := width * 2 / 3
	if formWidth > 80 {
		formWidth = 80 // Cap at 80 chars for readability
	} else if formWidth < 40 {
		formWidth = width - 4 // For small terminals
	}

	return formWidth
}

type RahannaModel struct {
	width        int
	height       int
	currentModel tea.Model
	auth         AuthModel
	play         PlayModel
}

func NewRahannaModel() RahannaModel {
	width, height := GetTerminalSize()

	auth := NewAuthModel(width, height)
	play := NewPlayModel(width, height)

	var currentModel tea.Model = auth

	if _, err := os.Stat(".rahannarc"); !errors.Is(err, os.ErrNotExist) {
		currentModel = play
	}

	return RahannaModel{
		width:        width,
		height:       height,
		currentModel: currentModel,
		auth:         auth,
		play:         play,
	}
}

func (m RahannaModel) Init() tea.Cmd {
	return m.currentModel.Init()
}

type switchModel struct {
	model tea.Model
}

func SwitchModelCmd(model tea.Model) tea.Cmd {
	s := switchModel{
		model: model,
	}

	return func() tea.Msg {
		return s
	}
}

func (m RahannaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case switchModel:
		m.currentModel = msg.model
		return m, nil
	}
	var cmd tea.Cmd
	m.currentModel, cmd = m.currentModel.Update(msg)
	return m, cmd
}

func (m RahannaModel) View() string {
	return m.currentModel.View()
}

func handleExit(msg tea.Msg) tea.Cmd {
	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case "ctrl+c":
			return tea.Quit
		}
	}

	return nil
}

func getLogo(width int) string {
	logoStyle := lipgloss.NewStyle().
		Foreground(lipgloss.Color("#7ee2a8")).
		Bold(true).
		Align(lipgloss.Center).
		Width(width)

	return logoStyle.Render(logo)

}