-
Notifications
You must be signed in to change notification settings - Fork 0
/
histyles.go
279 lines (252 loc) · 7.6 KB
/
histyles.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
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package histyle
import (
"embed"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"sort"
"github.com/goki/gi/gi"
"github.com/goki/gi/oswin"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
"github.com/goki/pi/pi"
)
//go:embed defaults.histys
var content embed.FS
// Styles is a collection of styles
type Styles map[string]*Style
var KiT_Styles = kit.Types.AddType(&Styles{}, StylesProps)
// StdStyles are the styles from chroma package
var StdStyles Styles
// CustomStyles are user's special styles
var CustomStyles = Styles{}
// AvailStyles are all highlighting styles
var AvailStyles Styles
// StyleDefault is the default highlighting style name -- can set this to whatever you want
var StyleDefault = gi.HiStyleName("emacs")
// StyleNames are all the names of all the available highlighting styles
var StyleNames []string
// AvailStyle returns a style by name from the AvailStyles list -- if not found
// default is used as a fallback
func AvailStyle(nm gi.HiStyleName) *Style {
if AvailStyles == nil {
Init()
}
if st, ok := AvailStyles[string(nm)]; ok {
return st
}
return AvailStyles[string(StyleDefault)]
}
// Add adds a new style to the list
func (hs *Styles) Add() *Style {
hse := &Style{}
nm := fmt.Sprintf("NewStyle_%v", len(*hs))
(*hs)[nm] = hse
return hse
}
// CopyFrom copies styles from another collection
func (hs *Styles) CopyFrom(os Styles) {
if *hs == nil {
*hs = make(Styles, len(os))
}
for nm, cse := range os {
(*hs)[nm] = cse
}
}
// MergeAvailStyles updates AvailStyles as combination of std and custom styles
func MergeAvailStyles() {
AvailStyles = make(Styles, len(CustomStyles)+len(StdStyles))
AvailStyles.CopyFrom(StdStyles)
AvailStyles.CopyFrom(CustomStyles)
StyleNames = AvailStyles.Names()
}
// Open hi styles from a JSON-formatted file.
func (hs *Styles) OpenJSON(filename gi.FileName) error {
b, err := ioutil.ReadFile(string(filename))
if err != nil {
// PromptDialog(nil, "File Not Found", err.Error(), true, false, nil, nil, nil)
// log.Println(err)
return err
}
return json.Unmarshal(b, hs)
}
// Save hi styles to a JSON-formatted file.
func (hs *Styles) SaveJSON(filename gi.FileName) error {
b, err := json.MarshalIndent(hs, "", " ")
if err != nil {
log.Println(err) // unlikely
return err
}
err = ioutil.WriteFile(string(filename), b, 0644)
if err != nil {
// PromptDialog(nil, "Could not Save to File", err.Error(), true, false, nil, nil, nil)
log.Println(err)
}
return err
}
// PrefsStylesFileName is the name of the preferences file in App prefs
// directory for saving / loading the custom styles
var PrefsStylesFileName = "hi_styles.json"
// StylesChanged is used for gui updating while editing
var StylesChanged = false
// OpenPrefs opens Styles from App standard prefs directory, using PrefsStylesFileName
func (hs *Styles) OpenPrefs() error {
pdir := oswin.TheApp.AppPrefsDir()
pnm := filepath.Join(pdir, PrefsStylesFileName)
StylesChanged = false
return hs.OpenJSON(gi.FileName(pnm))
}
// SavePrefs saves Styles to App standard prefs directory, using PrefsStylesFileName
func (hs *Styles) SavePrefs() error {
pdir := oswin.TheApp.AppPrefsDir()
pnm := filepath.Join(pdir, PrefsStylesFileName)
StylesChanged = false
MergeAvailStyles()
return hs.SaveJSON(gi.FileName(pnm))
}
// SaveAll saves all styles individually to chosen directory
func (hs *Styles) SaveAll(dir gi.FileName) {
for nm, st := range *hs {
fnm := filepath.Join(string(dir), nm+".histy")
st.SaveJSON(gi.FileName(fnm))
}
}
// OpenDefaults opens the default highlighting styles (from chroma originally)
// These are encoded as an embed from defaults.histys
func (hs *Styles) OpenDefaults() error {
defb, err := content.ReadFile("defaults.histys")
if err != nil {
log.Println(err)
return err
}
err = json.Unmarshal(defb, hs)
if err != nil {
log.Println(err)
return err
}
return err
}
// Names outputs names of styles in collection
func (hs *Styles) Names() []string {
nms := make([]string, len(*hs))
idx := 0
for nm := range *hs {
nms[idx] = nm
idx++
}
sort.StringSlice(nms).Sort()
return nms
}
// ViewStd shows the standard styles that are compiled into the program via
// chroma package
func (hs *Styles) ViewStd() {
gi.TheViewIFace.HiStylesView(true)
}
// Init must be called to initialize the hi styles -- post startup
// so chroma stuff is all in place, and loads custom styles
func Init() {
pi.LangSupport.OpenStd()
StdStyles.OpenDefaults()
CustomStyles.OpenPrefs()
if len(CustomStyles) == 0 {
cs := &Style{}
cs.CopyFrom(StdStyles[string(StyleDefault)])
CustomStyles["custom-sample"] = cs
}
MergeAvailStyles()
}
// StylesProps define the ToolBar and MenuBar for view
var StylesProps = ki.Props{
"MainMenu": ki.PropSlice{
{"AppMenu", ki.BlankProp{}},
{"File", ki.PropSlice{
{"OpenPrefs", ki.Props{}},
{"SavePrefs", ki.Props{
"shortcut": gi.KeyFunMenuSave,
"updtfunc": func(sti any, act *gi.Action) {
act.SetActiveStateUpdt(StylesChanged && sti.(*Styles) == &CustomStyles)
},
}},
{"sep-file", ki.BlankProp{}},
{"OpenJSON", ki.Props{
"label": "Open...",
"desc": "You can save and open styles to / from files to share, experiment, transfer, etc",
"shortcut": gi.KeyFunMenuOpen,
"Args": ki.PropSlice{
{"File Name", ki.Props{
"ext": ".json",
}},
},
}},
{"SaveJSON", ki.Props{
"label": "Save As...",
"desc": "You can save and open styles to / from files to share, experiment, transfer, etc",
"shortcut": gi.KeyFunMenuSaveAs,
"Args": ki.PropSlice{
{"File Name", ki.Props{
"ext": ".json",
}},
},
}},
{"SaveAll", ki.Props{
"label": "Save All...",
"desc": "Saves each style individually to selected directory (be sure to select a dir only!)",
"Args": ki.PropSlice{
{"Dir Name", ki.Props{}},
},
}},
}},
{"Edit", "Copy Cut Paste Dupe"},
{"Window", "Windows"},
},
"ToolBar": ki.PropSlice{
{"Add", ki.Props{ // note: overrides default Add
"desc": "Add a new style to the list.",
"icon": "plus",
"updtfunc": func(sti any, act *gi.Action) {
act.SetActiveStateUpdt(sti.(*Styles) == &CustomStyles)
},
}},
{"SavePrefs", ki.Props{
"desc": "saves styles to app prefs directory, in file hi_styles.json, which will be loaded automatically at startup into your CustomStyles.",
"icon": "file-save",
"updtfunc": func(sti any, act *gi.Action) {
act.SetActiveStateUpdt(StylesChanged && sti.(*Styles) == &CustomStyles)
},
}},
{"sep-file", ki.BlankProp{}},
{"OpenJSON", ki.Props{
"label": "Open from file",
"icon": "file-open",
"desc": "You can save and open styles to / from files to share, experiment, transfer, etc",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"ext": ".json",
}},
},
}},
{"SaveJSON", ki.Props{
"label": "Save to file",
"icon": "file-save",
"desc": "You can save and open styles to / from files to share, experiment, transfer, etc",
"Args": ki.PropSlice{
{"File Name", ki.Props{
"ext": ".json",
}},
},
}},
{"sep-std", ki.BlankProp{}},
{"ViewStd", ki.Props{
"desc": `Shows the standard styles that are compiled into the program (from <a href="https://github.com/alecthomas/chroma">github.com/alecthomas/chroma</a>). Save a style from there and load it into custom as a starting point for creating a variant of an existing style.`,
"confirm": true,
"updtfunc": func(sti any, act *gi.Action) {
act.SetActiveStateUpdt(sti.(*Styles) != &StdStyles)
},
}},
},
}