forked from aandrew-me/tgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
306 lines (269 loc) · 7.77 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"github.com/charmbracelet/bubbles/textarea"
"github.com/fatih/color"
"github.com/olekukonko/ts"
tea "github.com/charmbracelet/bubbletea"
)
const localVersion = "2.2.0"
var bold = color.New(color.Bold)
var boldBlue = color.New(color.Bold, color.FgBlue)
var boldViolet = color.New(color.Bold, color.FgMagenta)
var codeText = color.New(color.BgBlack, color.FgGreen, color.Bold)
var stopSpin = false
var programLoop = true
var configDir = ""
var userInput = ""
var executablePath = ""
func main() {
execPath, err := os.Executable()
if err == nil {
executablePath = execPath
}
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-terminate
os.Exit(0)
}()
args := os.Args
if len(args) > 1 && len(args[1]) > 1 {
input := args[1]
if input == "-v" || input == "--version" {
fmt.Println("tgpt", localVersion)
} else if input == "-cl" || input == "--changelog" {
getVersionHistory()
} else if input == "-w" || input == "--whole" {
if len(args) > 2 && len(args[2]) > 1 {
prompt := args[2]
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -w "What is encryption?"`)
os.Exit(0)
}
getWholeText(trimmedPrompt, configDir+"/tgpt")
} else {
formattedInput := getFormattedInputStdin()
fmt.Println()
getWholeText(formattedInput, configDir + "/tgpt")
}
} else if input == "-q" || input == "--quiet" {
if len(args) > 2 && len(args[2]) > 1 {
prompt := args[2]
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -q "What is encryption?"`)
os.Exit(0)
}
getSilentText(trimmedPrompt, configDir+"/tgpt")
} else {
formattedInput := getFormattedInputStdin()
fmt.Println()
getSilentText(formattedInput, configDir+"/tgpt")
}
} else if input == "-s" || input == "--shell" {
if len(args) > 2 && len(args[2]) > 1 {
prompt := args[2]
go loading(&stopSpin)
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -s "How to update system"`)
os.Exit(0)
}
shellCommand(trimmedPrompt)
} else {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -s "How to update system"`)
os.Exit(0)
}
} else if input == "-c" || input == "--code" {
if len(args) > 2 && len(args[2]) > 1 {
prompt := args[2]
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -c "Hello world in Python"`)
os.Exit(0)
}
codeGenerate(trimmedPrompt)
} else {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -c "Hello world in Python"`)
os.Exit(0)
}
} else if input == "-u" || input == "--update" {
update()
} else if input == "-i" || input == "--interactive" {
/////////////////////
// Normal interactive
/////////////////////
reader := bufio.NewReader(os.Stdin)
bold.Print("Interactive mode started. Press Ctrl + C or type exit to quit.\n\n")
previousMessages := ""
for {
boldBlue.Println("╭─ You")
boldBlue.Print("╰─> ")
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input:", err)
break
}
if len(input) > 1 {
input = strings.TrimSpace(input)
if len(input) > 1 {
if input == "exit" {
bold.Println("Exiting...")
return
}
responseTxt := getData(input, configDir+"/tgpt", true, previousMessages)
previousMessages += responseTxt
}
}
}
} else if input == "-m" || input == "--multiline" {
/////////////////////
// Multiline interactive
/////////////////////
fmt.Print("\nPress Tab to submit and Ctrl + C to exit.\n")
previousMessages := ""
for programLoop {
fmt.Print("\n")
p := tea.NewProgram(initialModel())
_, err := p.Run()
if err != nil {
fmt.Println(err)
os.Exit(0)
}
if len(userInput) > 0 {
responseTxt := getData(userInput, configDir+"/tgpt", true, previousMessages)
previousMessages += responseTxt
}
}
} else if input == "-img" || input == "--image" {
if len(args) > 2 && len(args[2]) > 1 {
prompt := args[2]
trimmedPrompt := strings.TrimSpace(prompt)
if len(trimmedPrompt) < 1 {
fmt.Println("You need to provide some text")
fmt.Println(`Example: tgpt -img "cat"`)
os.Exit(0)
}
generateImage(trimmedPrompt)
} else {
formattedInput := getFormattedInputStdin()
fmt.Println()
generateImage(formattedInput)
}
} else if strings.HasPrefix(input, "-") {
boldBlue.Println(`Usage: tgpt [Flag] [Prompt]`)
boldBlue.Println("\nFlags:")
fmt.Printf("%-50v Generate and Execute shell commands. (Experimental) \n", "-s, --shell")
fmt.Printf("%-50v Generate Code. (Experimental)\n", "-c, --code")
fmt.Printf("%-50v Gives response back without loading animation\n", "-q, --quiet")
fmt.Printf("%-50v Gives response back as a whole text\n", "-w, --whole")
fmt.Printf("%-50v Generate images from text\n", "-img, --image")
boldBlue.Println("\nOptions:")
fmt.Printf("%-50v Print version \n", "-v, --version")
fmt.Printf("%-50v Print help message \n", "-h, --help")
fmt.Printf("%-50v Start normal interactive mode \n", "-i, --interactive")
fmt.Printf("%-50v Start multi-line interactive mode \n", "-m, --multiline")
fmt.Printf("%-50v See changelog of versions \n", "-cl, --changelog")
if runtime.GOOS != "windows" {
fmt.Printf("%-50v Update program \n", "-u, --update")
}
boldBlue.Println("\nExamples:")
fmt.Println(`tgpt "What is internet?"`)
fmt.Println("tgpt -f")
fmt.Println(`tgpt -m`)
fmt.Println(`tgpt -s "How to update my system?"`)
} else {
go loading(&stopSpin)
formattedInput := strings.TrimSpace(input)
getData(formattedInput, configDir+"/tgpt", false, "")
}
} else {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
go loading(&stopSpin)
formattedInput := strings.TrimSpace(input)
getData(formattedInput, configDir+"/tgpt", false, "")
}
}
// Multiline input
type errMsg error
type model struct {
textarea textarea.Model
err error
}
func initialModel() model {
size, _ := ts.GetSize()
termWidth := size.Col()
ti := textarea.New()
ti.SetWidth(termWidth)
ti.CharLimit = 200000
ti.ShowLineNumbers = false
ti.Placeholder = "Enter your prompt"
ti.Focus()
return model{
textarea: ti,
err: nil,
}
}
func (m model) Init() tea.Cmd {
return textarea.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEsc:
if m.textarea.Focused() {
m.textarea.Blur()
}
case tea.KeyCtrlC:
programLoop = false
userInput = ""
return m, tea.Quit
case tea.KeyTab:
userInput = m.textarea.Value()
if len(userInput) > 1 {
m.textarea.Blur()
return m, tea.Quit
}
default:
if !m.textarea.Focused() {
cmd = m.textarea.Focus()
cmds = append(cmds, cmd)
}
}
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}
m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
return m.textarea.View()
}
func getFormattedInputStdin() (formattedInput string){
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
return strings.TrimSpace(input)
}