-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththeme_shuffler.go
91 lines (76 loc) · 1.88 KB
/
theme_shuffler.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
package ui
import (
"sort"
"github.com/solidiquis/alacpretty/internal/themes"
"github.com/solidiquis/alacpretty/internal/yamlconf"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
type ThemeShuffler struct {
// public
Widget *widgets.List
// private
x1, y1, x2, y2 int
yamlConfig *string
}
func NewThemeShuffler(x1, y1, x2, y2 int) *ThemeShuffler {
return &ThemeShuffler{
Widget: widgets.NewList(),
x1: x1,
y1: y1,
x2: x2,
y2: y2,
}
}
func (ts *ThemeShuffler) GetWidget() ui.Drawable {
var widget ui.Drawable = ts.Widget
return widget
}
func (ts *ThemeShuffler) ToggleActive() {
if fgColor := ts.Widget.BorderStyle.Fg; fgColor == ui.ColorYellow {
ts.Widget.BorderStyle.Fg = ui.ColorWhite
} else {
ts.Widget.BorderStyle.Fg = ui.ColorYellow
}
ui.Render(ts.Widget)
}
func (ts *ThemeShuffler) InitWidget(fileContent *string) {
ts.yamlConfig = fileContent
rows := make([]string, len(themes.ThemeNames))
for i, theme := range themes.ThemeNames {
rows[i] = theme
}
sort.Strings(rows)
var selectedRow int
currentTheme := yamlconf.CurrentTheme(fileContent)
for i, theme := range rows {
if theme == currentTheme {
selectedRow = i
}
}
ts.Widget.Title = "Themes"
ts.Widget.Rows = rows
ts.Widget.SelectedRow = selectedRow
ts.Widget.TextStyle = ui.NewStyle(ui.ColorYellow)
ts.Widget.WrapText = false
ts.Widget.SetRect(ts.x1, ts.y1, ts.x2, ts.y2)
ts.Widget.BorderStyle.Fg = ui.ColorWhite
}
func (ts *ThemeShuffler) SetState() string {
for {
uiEvents := ui.PollEvents()
e := <-uiEvents
switch e.ID {
case "<C-c>", "q", "H", "J", "K", "L":
return e.ID
case "j", "<Down>":
ts.Widget.ScrollDown()
case "k", "<Up>":
ts.Widget.ScrollUp()
}
newTheme := ts.Widget.Rows[ts.Widget.SelectedRow]
yamlconf.ChangeTheme(ts.yamlConfig, newTheme)
yamlconf.ApplyChanges(*(ts.yamlConfig))
ui.Render(ts.Widget)
}
}