-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (168 loc) · 5.16 KB
/
main.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
/*
* Copyright (C) 2024 Nicholas Popovic
*
* 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 2
* 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/>.
*/
import (
"fmt"
"log"
"strings"
"main/helpers"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/mistakenelf/teacup/statusbar"
ui "main/ui-components"
)
// model represents the properties of the UI.
type model struct {
tabs ui.TabModel
loading bool
spinner spinner.Model
viewport viewport.Model
textInput textinput.Model
statusbar statusbar.Model
input_mode string
}
// New creates a new instance of the UI.
func New() model {
tabs := ui.TabModel{}
ti := textinput.New()
ti.Placeholder = "Type here..."
ti.Focus()
s := spinner.New()
s.Spinner = spinner.Dot
sb := statusbar.New(
statusbar.ColorConfig{
Foreground: lipgloss.AdaptiveColor{Dark: "#ffffff", Light: "#ffffff"},
Background: lipgloss.AdaptiveColor{Light: "#F25D94", Dark: "#F25D94"},
},
statusbar.ColorConfig{
Foreground: lipgloss.AdaptiveColor{Light: "#ffffff", Dark: "#ffffff"},
Background: lipgloss.AdaptiveColor{Light: "#3c3836", Dark: "#3c3836"},
},
statusbar.ColorConfig{
Foreground: lipgloss.AdaptiveColor{Light: "#ffffff", Dark: "#ffffff"},
Background: lipgloss.AdaptiveColor{Light: "#A550DF", Dark: "#A550DF"},
},
statusbar.ColorConfig{
Foreground: lipgloss.AdaptiveColor{Light: "#ffffff", Dark: "#ffffff"},
Background: lipgloss.AdaptiveColor{Light: "#6124DF", Dark: "#6124DF"},
},
)
return model{
tabs: tabs,
loading: false,
spinner: s,
statusbar: sb,
viewport: viewport.Model{},
textInput: ti,
input_mode: "INSERT",
}
}
// Init intializes the UI.
func (m model) Init() tea.Cmd {
return textinput.Blink
}
// Update handles all UI interactions.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
switch msg := msg.(type) {
// Handle window resize events
case tea.WindowSizeMsg:
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - 3
m.statusbar.SetSize(msg.Width)
m.statusbar.SetContent(m.input_mode, "~/.config/nvim", "1/23", "SB")
// Handle key presses
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
cmds = append(cmds, tea.Quit)
case "esc":
if m.input_mode == "COMMAND" {
m.input_mode = "INSERT"
m.statusbar.SetContent(m.input_mode, "~/.config/nvim", "1/23", "SB")
m.textInput.Focus()
} else if m.input_mode == "INSERT" {
m.input_mode = "COMMAND"
m.statusbar.SetContent(m.input_mode, "~/.config/nvim", "1/23", "SB")
m.textInput.Blur()
}
case "enter":
// Handle enter key pressed
if m.input_mode == "INSERT" {
// Perform action on enter key pressed in INSERT mode
//log.Println("Enter key pressed: ", m.textInput.Value())
m.loading = true
cmds = append(cmds, m.spinner.Tick, helpers.GetChatCompletion(m.textInput.Value()))
m.textInput.SetValue("")
}
}
case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd)
case helpers.FetchedDataMsg:
m.loading = false
if msg.Err == nil {
r, _ := glamour.NewTermRenderer(
glamour.WithStandardStyle("dark"),
glamour.WithWordWrap(m.viewport.Width),
)
out, _ := r.Render(msg.Data)
m.viewport.SetContent(out)
} else {
m.viewport.SetContent(msg.Err.Error())
}
}
tabsModel, cmd := m.tabs.Update(msg)
m.tabs = tabsModel.(ui.TabModel)
cmds = append(cmds, cmd)
// Handle keyboard and mouse events in the viewport
m.viewport, cmd = m.viewport.Update(msg)
cmds = append(cmds, cmd)
m.textInput, cmd = m.textInput.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
// View returns a string representation of the UI.
func (m model) View() string {
renderedTabs := m.tabs.View()
output := fmt.Sprintf("%s", renderedTabs)
// Display loading message (horiz/vert centered)
if m.loading {
loadingMessage := m.spinner.View() + " Loading..."
viewportWidth := m.viewport.Width
viewportHeight := m.viewport.Height
paddingLeft := (viewportWidth - lipgloss.Width(loadingMessage)) / 2
paddingTop := viewportHeight / 2
m.viewport.SetContent(fmt.Sprintf("%s%s", strings.Repeat("\n", paddingTop), strings.Repeat(" ", paddingLeft)+loadingMessage))
}
output += "\n" + m.viewport.View() + "\n" + m.textInput.View() + "\n" + m.statusbar.View()
return output
}
func main() {
b := New()
p := tea.NewProgram(b, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}