-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (149 loc) · 4.23 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
package main
import (
"context"
"fmt"
"os/exec"
"time"
"github.com/mattn/go-gtk/gtk"
"golang.org/x/text/unicode/norm"
)
func getTextViewContent(textView *gtk.TextView) string {
buffer := textView.GetBuffer()
var startIter, endIter gtk.TextIter
buffer.GetStartIter(&startIter)
buffer.GetEndIter(&endIter)
text := buffer.GetText(&startIter, &endIter, true)
return text
}
func updateTextViewContent(textView *gtk.TextView, text string) {
buffer := textView.GetBuffer()
var startIter, endIter gtk.TextIter
buffer.GetStartIter(&startIter)
buffer.GetEndIter(&endIter)
buffer.Delete(&startIter, &endIter)
buffer.Insert(&startIter, text)
}
func openTlsBtn(text string) *gtk.Button {
openGgtlstBtn := gtk.NewButtonWithLabel("Open Google Translate")
openGgtlstBtn.SetSizeRequest(20, 10)
openGgtlstBtn.Connect("clicked", func() {
url := fmt.Sprintf("https://translate.google.com/?sl=en&tl=vi&text=%s&op=translate", text)
cmd := exec.Command("google-chrome", url)
err := cmd.Start()
if err != nil {
fmt.Println("Failed to execute commandeclaredd:", err)
}
})
return openGgtlstBtn
}
func getSpeakBtn(text string) *gtk.Button {
btn := gtk.NewButtonWithLabel("Speak")
btn.SetSizeRequest(20, 10)
btn.Connect("clicked", func() {
cmd := exec.Command("google_speech", text)
err := cmd.Start()
if err != nil {
fmt.Println("Failed to execute command:", err)
}
})
return btn
}
func getWindow(orgText, transText string) *gtk.Window {
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle("Translate GUI")
window.SetIconName("textview")
window.Connect("destroy", gtk.MainQuit)
window.Connect("focus-out-event", gtk.MainQuit)
orgTextView := gtk.NewTextView()
orgTextView.SetWrapMode(gtk.WRAP_WORD)
orgTextView.SetEditable(true)
orgTextView.SetCursorVisible(true)
orgTextView.SetBorderWidth(2)
orgBuffer := orgTextView.GetBuffer()
var orgTextIter gtk.TextIter
orgBuffer.GetStartIter(&orgTextIter)
orgBuffer.Insert(&orgTextIter, orgText)
translatedTextView := gtk.NewTextView()
translatedTextView.SetWrapMode(gtk.WRAP_WORD)
translatedTextView.SetCursorVisible(false)
var (
inputTextcancel context.CancelFunc
inputTextCtx context.Context
)
orgBuffer.Connect("changed", func() {
text := getTextViewContent(orgTextView)
if inputTextcancel != nil {
inputTextcancel()
}
inputTextCtx, inputTextcancel = context.WithCancel(context.Background())
go func() {
for {
select {
case <-inputTextCtx.Done():
return
case <-time.After(1 * time.Second):
transText := trans(text)
// reset cancel function when user typed new input
updateTextViewContent(translatedTextView, transText)
}
}
}()
})
var iter gtk.TextIter
buffer := translatedTextView.GetBuffer()
buffer.GetStartIter(&iter)
buffer.Insert(&iter, transText)
topVbox := gtk.NewVBox(true, 0)
topVbox.PackStart(orgTextView, true, true, 0)
topVbox.SetSpacing(5)
topVbox.PackStart(translatedTextView, true, true, 0)
topVbox.SetSizeRequest(400, 200)
// Add the button to a vertical box
vbox := gtk.NewVBox(false, 1)
vbox.PackStart(topVbox, true, true, 0)
// button group
bottomHbox := gtk.NewHBox(true, 1)
bottomHbox.SetSizeRequest(400, 50)
openGgtlstBtn := openTlsBtn(orgText)
speakBtn := getSpeakBtn(orgText)
bottomHbox.PackStart(openGgtlstBtn, true, true, 0)
bottomHbox.PackStart(speakBtn, true, true, 0)
vbox.PackStart(bottomHbox, false, false, 0)
// vbox.PackEnd(openGgtlstBtn, true, true, 0)
window.Add(vbox)
window.SetSizeRequest(400, 300)
window.ShowAll()
return window
}
func trans(text string) string {
cmd := exec.Command("trans", ":vi", "-b", text)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Failed to execute command:", err)
return ""
} else {
res := norm.NFC.String(string(out))
return string(res)
}
}
func getSelectedText() string {
cmd := exec.Command("xclip", "-out", "-selection", "primary")
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Failed to execute command:", err)
panic(err)
} else {
return string(out)
}
}
func showWindow() {
selectedText := getSelectedText()
transText := trans(selectedText)
gtk.Init(nil)
_ = getWindow(selectedText, transText)
gtk.Main()
}
func main() {
showWindow()
}