-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (134 loc) · 3.96 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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/noboruma/prompt-ai/ais"
"github.com/noboruma/prompt-ai/clipboards"
"github.com/noboruma/prompt-ai/prompt"
str "github.com/noboruma/prompt-ai/strings"
"github.com/rivo/tview"
)
func prepareChatViewSection() *tview.TextView {
chatView := tview.NewTextView().
SetTextAlign(tview.AlignLeft).
SetText("...").
SetDynamicColors(true).
SetRegions(true).
SetWordWrap(true)
chatView.SetBorder(true).SetTitle("Chat")
return chatView
}
func prepareInputSection(app *tview.Application, chatView *tview.TextView, copy_clipboards *clipboards.Clipboards) *tview.Flex {
errorText := tview.NewTextView().
SetScrollable(false).
SetToggleHighlights(false).
SetWordWrap(true)
errorText.SetTextColor(tcell.ColorRed)
errorLog := func(msg string) {
app.QueueUpdateDraw(func() {
errorText.SetText(msg)
})
}
statusText := tview.NewTextView().
SetScrollable(false).
SetToggleHighlights(false).
SetDynamicColors(true).
SetRegions(true).
SetWordWrap(true)
updateQuota := func() {
usage, err := ais.GetOpenAIQuotaUsage()
if err != nil {
errorLog(err.Error())
}
statusText.SetText(fmt.Sprintf("[yellow]Usage: $%v", usage.Used))
}
updateQuota()
inputTextArea := tview.NewInputField().
SetLabel(prompt.DefaultPrompt).
SetPlaceholder("E.g. why is 42 the answer")
prev_ans := ""
var sb strings.Builder
inputTextArea.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
inputTextArea.SetDisabled(true)
spinctl := prompt.StartSpinner(app, inputTextArea)
go func() {
defer inputTextArea.SetDisabled(false)
defer func() { spinctl <- struct{}{} }()
responses, err := ais.SendPrompt(inputTextArea.GetText(), prev_ans, 100)
if err != nil {
errorLog(err.Error())
return
}
errorLog("")
sb.WriteString("[yellow]>>")
sb.WriteString(inputTextArea.GetText())
sb.WriteString("\n")
prev_ans = strings.Join(responses, "\n")
sections := str.ExtractMarkdownSections(prev_ans)
for i := range sections {
if sections[i].Markdown {
id := copy_clipboards.Append(sections[i].Content)
sb.WriteString("[green]")
sb.WriteString("[F")
sb.WriteString(strconv.Itoa(id + 1))
sb.WriteString("]")
sb.WriteString("\n---[blue]")
sb.WriteString(tview.Escape(sections[i].Content))
sb.WriteString("[green]---")
} else {
sb.WriteString("[white]")
sb.WriteString(sections[i].Content)
}
}
sb.WriteString("\n")
chatView.SetText(sb.String())
inputTextArea.SetText("")
updateQuota()
}()
}
})
inputField := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(inputTextArea, 0, 1, true).
AddItem(tview.NewTextView().
SetDynamicColors(true).
SetText("[green]F1-F8[white]: copy to system clipboard"), 0, 1, false).
AddItem(tview.NewTextView().
SetDynamicColors(true).
SetText("[green]TAB[white]: switch panes"), 0, 1, false).
AddItem(statusText, 0, 1, false).
AddItem(errorText, 0, 1, false)
inputField.SetBorder(true).SetTitle("Input")
return inputField
}
func main() {
clipboards := clipboards.NewClipboards()
app := tview.NewApplication()
chatView := prepareChatViewSection()
inputField := prepareInputSection(app, chatView, &clipboards)
inputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
if key >= tcell.KeyF1 && key <= tcell.KeyF8 {
clipboards.Fetch(int(key - tcell.KeyF1))
} else if key == tcell.KeyTab {
app.SetFocus(chatView)
}
return event
})
chatView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
if key == tcell.KeyTab {
app.SetFocus(inputField)
}
return event
})
mainLayout := tview.NewFlex().
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(chatView, 0, 2, false).
AddItem(inputField, 7, 1, true), 0, 1, true)
if err := app.SetRoot(mainLayout, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}