-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (153 loc) · 4.88 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
// Copyright 2022 The golang.de#itiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"golang.design/x/clipboard"
"golang.design/x/hotkey"
zmq "github.com/go-zeromq/zmq4"
)
func main() {
err := clipboard.Init()
if err != nil {
panic(err)
}
ctx := context.Background()
socket := zmq.NewReq(ctx, zmq.WithDialerRetry(time.Second))
defer socket.Close()
fmt.Printf("Connecting to hello world server...")
if err := socket.Dial("tcp://localhost:5555"); err != nil {
fmt.Errorf("dialing: %w", err)
}
sendToGPT := func(str string) string {
// Send hello.
m := zmq.NewMsgString(str)
fmt.Println("sending ", m)
if err := socket.Send(m); err != nil {
return "{\"error\": \"client send error\"}"
}
// Wait for reply.
r, err := socket.Recv()
if err != nil {
return "{\"error\": \"client receive error\"}"
}
ret := string(r.Bytes())
fmt.Println("received ", ret)
return ret
}
w, before_content := ui(sendToGPT)
go func() {
// Register a desired hotkey.
hk := hotkey.New([]hotkey.Modifier{hotkey.ModCmd}, hotkey.KeyL)
if err := hk.Register(); err != nil {
panic("hotkey registration failed")
}
// Start listen hotkey event whenever it is ready.
for range hk.Keydown() {
data := clipboard.Read(clipboard.FmtText)
if data != nil {
content := string(data)
before_content.SetText(content)
w.Show()
}
}
}()
w.ShowAndRun()
}
func ui(sendToGPT func(str string) string) (fyne.Window, *widget.Label) {
myApp := app.New()
myWindow := myApp.NewWindow("laoxian")
myWindow.Resize(fyne.NewSize(400, 300))
//text
before_label := widget.NewLabel("Before")
before_content := widget.NewLabel("...")
before_content.Wrapping = fyne.TextWrapWord
after_label := widget.NewLabel("After")
after_content := widget.NewLabel("...")
after_content.Wrapping = fyne.TextWrapWord
//feature list
reply_style_list := []string{"professional", "casual", "formal", "friendly", "diplomatic"}
context_list := []string{"Email", "Slack", "Discord", "Telegram", "Facebook", "Instagram"}
rewrite_style_list := []string{"native", "professional", "casual"}
//gpt features
keyword_label := widget.NewLabel("Keyword")
keyword_value := widget.NewEntry()
context_label := widget.NewLabel("Context")
context_value := widget.NewSelect(context_list, func(value string) {
log.Println("Select context to", value)
})
style_label := widget.NewLabel("Style")
reply_style_value := widget.NewSelect(reply_style_list, func(value string) {
log.Println("Select style to", value)
})
rewrite_style_value := widget.NewSelectEntry(rewrite_style_list)
// fetch result
async_process := func(msg map[string]interface{}) {
str, _ := json.Marshal(msg)
response := sendToGPT(string(str))
fmt.Println("done")
var dat map[string]string
json.Unmarshal([]byte(response), &dat)
result := dat["completion"]
after_content.SetText(result)
clipboard.Write(clipboard.FmtText, []byte(result))
fyne.CurrentApp().SendNotification(fyne.NewNotification("Laoxian", "Complete, content in clipboard"))
}
//button
reply_button := widget.NewButton("Submit", func() {
msg := map[string]interface{}{
"template": "reply",
"params": map[string]interface{}{
"keyword": keyword_value.Text,
"style": reply_style_value.Selected,
"content": before_content.Text,
"context": context_value.Selected,
},
}
go async_process(msg)
})
rewrite_button := widget.NewButton("Submit", func() {
msg := map[string]interface{}{
"template": "rewrite",
"params": map[string]interface{}{
"style": rewrite_style_value.Text,
"content": before_content.Text,
},
}
go async_process(msg)
})
//grid
grid_reply := container.New(layout.NewVBoxLayout(), keyword_label, keyword_value, style_label, reply_style_value, context_label, context_value, reply_button)
grid_rewrite := container.New(layout.NewVBoxLayout(), style_label, rewrite_style_value, rewrite_button)
grid_content := container.New(layout.NewVBoxLayout(), before_label, before_content, after_label, after_content)
//mode
modes := container.NewGridWithColumns(2,
widget.NewButton("Dark", func() {
myApp.Settings().SetTheme(theme.DarkTheme())
}),
widget.NewButton("Light", func() {
myApp.Settings().SetTheme(theme.LightTheme())
}),
)
// //tabs
tabs := container.NewAppTabs(
container.NewTabItemWithIcon("", theme.HomeIcon(), grid_content),
container.NewTabItem("Reply", grid_reply),
container.NewTabItem("Rewrite", grid_rewrite),
)
tabs.SetTabLocation(container.TabLocationLeading)
myWindow.SetContent(container.NewBorder(tabs, modes, nil, nil))
// myWindow.ShowAndRun()
return myWindow, before_content
}