/
dictsearch.go
194 lines (167 loc) · 3.93 KB
/
dictsearch.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
package main
import (
"fmt"
"github.com/conformal/gotk3/gtk"
"io/ioutil"
"log"
"path"
"runtime"
"strings"
// "sync"
)
const (
dict_path = "./dictionaries"
)
type Application struct {
active_dicts map[string]bool
ch chan string
done_flag chan int
// wg sync.WaitGroup
result string
// Widgets
win *gtk.Window
dict_list *gtk.Box
search_entry *gtk.Entry
search_button *gtk.Button
text_view *gtk.TextView
status_bar *gtk.Statusbar
}
func NewApplication() *Application {
app := new(Application)
app.ch = make(chan string)
app.done_flag = make(chan int)
return app
}
func (app *Application) GetDictList(path string) []string {
var dict_files []string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
dict_files = append(dict_files, f.Name())
}
return dict_files
}
func (app *Application) Search(text string, ch chan string, done_flag chan int) {
go func() {
searchstring := strings.TrimSpace(text)
if len(searchstring) < 1 || len(app.active_dicts) == 0 {
return
}
id := app.status_bar.GetContextId("Search status")
app.status_bar.Pop(id)
app.status_bar.Push(id, "Поиск...")
result := ""
for d, _ := range app.active_dicts {
data, err := ioutil.ReadFile(path.Join(dict_path, d))
if err != nil {
log.Fatal(err)
}
for _, entry := range strings.Split(string(data), "\n") {
if strings.HasPrefix(strings.ToLower(entry), strings.ToLower(searchstring)+" ") {
result += fmt.Sprintf("%s:\n%s\n\n", d, entry)
}
}
}
buf, err := app.text_view.GetBuffer()
if err != nil {
log.Println(err)
}
buf.SetText(result)
app.status_bar.Pop(id)
app.status_bar.Push(id, "Поиск завершен.")
}()
}
func (app *Application) Run() {
gtk.Init(nil)
app.active_dicts = make(map[string]bool)
builder, err := gtk.BuilderNew()
if err != nil {
log.Fatal(err)
}
builder.AddFromFile("dictsearch.glade")
w, err := builder.GetObject("win")
if err != nil {
log.Fatal(err)
}
app.win = w.(*gtk.Window)
app.win.Connect("destroy", func() {
gtk.MainQuit()
})
// Box, containing list of dictionary chaeckbuttons
dict_list_box, err := builder.GetObject("dict_list_box")
if err != nil {
log.Fatal(err)
}
app.dict_list = dict_list_box.(*gtk.Box)
// Search Entry
s_e, err := builder.GetObject("search_entry")
if err != nil {
log.Fatal(err)
}
app.search_entry = s_e.(*gtk.Entry)
app.search_entry.Connect("activate", func() {
text, err := app.search_entry.GetText()
if err != nil {
log.Fatal(err)
}
go app.Search(text, app.ch, app.done_flag)
})
// Search Button
s_b, err := builder.GetObject("search_button")
if err != nil {
log.Fatal(err)
}
app.search_button = s_b.(*gtk.Button)
app.search_button.Connect("clicked", func() {
text, err := app.search_entry.GetText()
if err != nil {
log.Fatal(err)
}
go app.Search(text, app.ch, app.done_flag)
})
// Text View
tv, err := builder.GetObject("textview")
if err != nil {
log.Fatal(err)
}
app.text_view = tv.(*gtk.TextView)
app.text_view.SetEditable(false)
app.text_view.SetWrapMode(gtk.WRAP_WORD)
// Adding dictionary checkbuttons
for i, dic := range app.GetDictList(dict_path) {
dict_button, err := gtk.CheckButtonNew()
if err != nil {
log.Fatal(err)
}
label := dic
dict_button.SetLabel(label)
name := fmt.Sprintf("checkbutton%d", i)
dict_button.SetName(name)
dict_button.SetFocusOnClick(false)
dict_button.Connect("toggled", func() {
if dict_button.GetActive() {
app.active_dicts[label] = true
} else {
delete(app.active_dicts, label)
}
})
app.dict_list.Add(dict_button)
}
// StatusBar
stb, err := builder.GetObject("statusbar")
if err != nil {
log.Fatal(err)
}
app.status_bar = stb.(*gtk.Statusbar)
app.search_entry.GrabFocus()
app.win.ShowAll()
gtk.Main()
}
func main() {
numcpu := runtime.NumCPU()
runtime.GOMAXPROCS(numcpu)
app := NewApplication()
app.Run()
}