-
Notifications
You must be signed in to change notification settings - Fork 0
/
goatee_conf.go
338 lines (280 loc) · 7.67 KB
/
goatee_conf.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"fmt"
"io/ioutil"
"log"
"math"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/mattn/go-gtk/gdk"
"github.com/mattn/go-gtk/gtk"
gsv "github.com/mattn/go-gtk/gtksourceview"
"github.com/naoina/toml"
)
//Conf structure contains configuration
type Conf struct {
window *gtk.Window `toml:",omitempty"`
filename string `toml:",omitempty"`
schemeManager *gsv.SourceStyleSchemeManager `toml:",omitempty"`
UI struct {
MenuBarVisible bool `toml:"menubar-visible" wgt:"checkbox"`
StatusBarVisible bool `toml:"statusbar-visible" wgt:"checkbox"`
}
TextView struct {
Font string `toml:"font" wgt:"font"`
LineHightlight bool `toml:"line-hightlight" wgt:"checkbox"`
LineNumbers bool `toml:"line-numbers" wgt:"checkbox"`
WordWrap bool `toml:"word-wrap" wgt:"checkbox"`
IndentSpace bool `toml:"indent-space" wgt:"checkbox"`
IndentWidth int `toml:"indent-width" wgt:"int"`
StyleScheme string `toml:"style-scheme" wgt:"schemes"`
}
Tabs struct {
Homogeneous bool `toml:"homogeneous" wgt:"checkbox"`
CloseBtns bool `toml:"close-buttons" wgt:"checkbox"`
Height int `toml:"height" wgt:"int"`
FGNormal []int `toml:"fg-normal" wgt:"color"`
FGModified []int `toml:"fg-modified" wgt:"color"`
FGNew []int `toml:"fg-new" wgt:"color"`
}
Search struct {
MaxItems int `toml:"max-items" wgt:"int"`
}
Hex struct {
BytesInLine int `toml:"bytes-in-line" wgt:"int"`
}
}
//NewConf set default values for configuration and parse config file
func NewConf() *Conf {
confdir := os.Getenv("XDG_CONFIG_HOME")
if confdir == "" {
confdir = path.Join(os.Getenv("HOME"), ".config")
}
confdir = path.Join(confdir, "goatee")
configfiles := []string{
path.Join(confdir, "goatee.conf"),
"goatee.conf",
}
// default values
c := new(Conf)
c.filename = path.Join(confdir, "goatee.conf")
c.schemeManager = gsv.SourceStyleSchemeManagerGetDefault()
c.UI.MenuBarVisible = true
c.UI.StatusBarVisible = false
c.TextView.Font = "Liberation Mono 8"
c.TextView.LineHightlight = true
c.TextView.LineNumbers = true
c.TextView.WordWrap = true
c.TextView.IndentSpace = false
c.TextView.IndentWidth = 2
c.Tabs.Homogeneous = true
c.Tabs.CloseBtns = true
c.Tabs.Height = 16
c.Tabs.FGNormal = []int{200, 200, 200}
c.Tabs.FGModified = []int{220, 20, 20}
c.Tabs.FGNew = []int{250, 200, 10}
c.Search.MaxItems = 1024
c.Hex.BytesInLine = 16
//parse config files
for _, filename := range configfiles {
if err := c.readConfigFile(filename); err == nil {
c.filename = filename
break
}
}
return c
}
func (c *Conf) readConfigFile(filename string) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
if err = toml.Unmarshal(data, &c); err != nil {
err = fmt.Errorf("failed decode config file '%s', reason: %s", filename, err)
log.Println(err)
return err
}
return nil
}
func (c *Conf) Write() {
os.MkdirAll(filepath.Dir(c.filename), 0755)
if c.filename == "" {
return
}
data, err := toml.Marshal(&c)
if err != nil {
log.Println(err)
return
}
err = ioutil.WriteFile(c.filename, data, 0644)
if err != nil {
log.Println(err)
return
}
}
//OpenWindow open window configuration
func (c *Conf) OpenWindow() {
if c.window == nil {
c.CreateWindow()
}
c.window.ShowAll()
}
//CreateWindow create window configuration
func (c *Conf) CreateWindow() {
c.window = gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
c.window.SetName("Preferences")
c.window.SetTypeHint(gdk.WINDOW_TYPE_HINT_DIALOG)
c.window.SetDefaultSize(300, 300)
c.window.SetSizeRequest(300, 300)
vbox := gtk.NewVBox(false, 0)
notebook := gtk.NewNotebook()
c.readConfigFile(c.filename)
t := reflect.TypeOf(c).Elem()
v := reflect.ValueOf(c).Elem()
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Type.Kind() != reflect.Struct {
continue
}
fvbox := gtk.NewVBox(false, 0)
notebook.AppendPage(fvbox, gtk.NewLabel(t.Field(i).Name))
confStruct := t.Field(i).Type
for j := 0; j < confStruct.NumField(); j++ {
field := confStruct.Field(j)
val := v.Field(i).Field(j)
label, widget := c.newWidget(val, field)
hbox := gtk.NewHBox(false, 0)
hbox.PackStart(label, false, false, 5)
hbox.PackEnd(widget, false, false, 5)
fvbox.PackStart(hbox, false, false, 5)
}
}
closebtn := gtk.NewButtonFromStock(gtk.STOCK_CLOSE)
closebtn.Clicked(c.CloseWindow)
hbox := gtk.NewHBox(false, 0)
hbox.PackEnd(closebtn, false, false, 5)
vbox.Add(notebook)
vbox.PackEnd(hbox, false, false, 5)
c.window.Add(vbox)
}
func (c *Conf) newWidget(v reflect.Value, f reflect.StructField) (*gtk.Label, gtk.IWidget) {
name := c.getFieldName(f)
label := gtk.NewLabel(name)
tag, ok := f.Tag.Lookup("wgt")
if !ok {
log.Fatalf("tag `wgt` not set for field %s", name)
}
w := &ConfWidget{Field: v, conf: c}
switch tag {
case "checkbox":
w.chkbtn = gtk.NewCheckButton()
w.chkbtn.SetSizeRequest(150, -1)
w.chkbtn.SetActive(v.Bool())
w.chkbtn.Connect("clicked", w.UpdateValue)
case "string":
w.entry = gtk.NewEntry()
w.entry.SetSizeRequest(150, -1)
w.entry.SetText(v.String())
w.entry.Connect("changed", w.UpdateValue)
case "int":
w.spnbtn = gtk.NewSpinButtonWithRange(-1, 2048, 1)
w.spnbtn.SetSizeRequest(150, -1)
w.spnbtn.SetValue(float64(v.Int()))
w.spnbtn.Connect("changed", w.UpdateValue)
case "color":
color := convertColor(v.Interface().([]int))
w.colbtn = gtk.NewColorButtonWithColor(color)
w.colbtn.SetSizeRequest(150, -1)
w.colbtn.Connect("color-set", w.UpdateValue)
case "font":
w.fntbtn = gtk.NewFontButton()
w.fntbtn.SetSizeRequest(150, -1)
w.fntbtn.SetFontName(v.String())
w.fntbtn.Connect("font-set", w.UpdateValue)
case "schemes":
schemes := c.schemeManager.GetSchemeIds()
scheme := v.String()
w.cmbbox = gtk.NewComboBoxText()
w.cmbbox.SetSizeRequest(150, -1)
for i, s := range schemes {
w.cmbbox.AppendText(s)
if scheme == s {
w.cmbbox.SetActive(i)
}
}
w.cmbbox.Connect("changed", w.UpdateValue)
}
return label, w.GetWidget()
}
func (c *Conf) getFieldName(f reflect.StructField) string {
name := strings.Split(f.Tag.Get("toml"), ",")[0]
if len(name) == 0 {
name = f.Name
}
return c.FormatName(name)
}
func (c *Conf) CloseWindow() {
c.Write()
c.window.Hide()
}
type ConfWidget struct {
Field reflect.Value
conf *Conf
chkbtn *gtk.CheckButton
entry *gtk.Entry
spnbtn *gtk.SpinButton
colbtn *gtk.ColorButton
fntbtn *gtk.FontButton
cmbbox *gtk.ComboBoxText
}
func (w *ConfWidget) UpdateValue() {
switch {
case w.chkbtn != nil:
w.Field.SetBool(w.chkbtn.GetActive())
case w.entry != nil:
w.Field.SetString(w.entry.GetText())
case w.spnbtn != nil:
n, _ := strconv.Atoi(w.spnbtn.Entry.GetText())
w.Field.SetInt(int64(n))
case w.colbtn != nil:
col := w.colbtn.GetColor()
r := int(math.Sqrt(float64(col.Red())))
g := int(math.Sqrt(float64(col.Green())))
b := int(math.Sqrt(float64(col.Blue())))
w.Field.Set(reflect.ValueOf([]int{r, g, b}))
case w.fntbtn != nil:
w.Field.SetString(w.fntbtn.GetFontName())
case w.cmbbox != nil:
w.Field.SetString(w.cmbbox.GetActiveText())
}
ui.TabsUpdateConf()
}
func (w *ConfWidget) GetWidget() gtk.IWidget {
switch {
case w.chkbtn != nil:
return w.chkbtn
case w.entry != nil:
return w.entry
case w.spnbtn != nil:
return w.spnbtn
case w.colbtn != nil:
return w.colbtn
case w.fntbtn != nil:
return w.fntbtn
case w.cmbbox != nil:
return w.cmbbox
}
log.Println(w)
return nil
}
func (c *Conf) FormatName(name string) string {
name = strings.Replace(name, "-", " ", -1)
name = regexp.MustCompile("([A-Z])").ReplaceAllString(name, " $1")
name = strings.TrimSpace(name)
name = strings.Title(name)
return name
}