forked from 4e554c4c/darkman.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (107 loc) · 2.3 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
package main
import (
"errors"
"fmt"
"github.com/neovim/go-client/nvim"
"github.com/neovim/go-client/nvim/plugin"
)
const (
UNKNOWN = iota
DARK
LIGHT
UNINITIALIZED
)
var currentMode uint32 = UNINITIALIZED
type setupArgs struct {
v *nvim.Nvim `msgpack:"-"`
ChangeBackground bool `msgpack:",array"`
SendUserEvent bool
Colorscheme *struct {
Dark string `msgpack:"dark"`
Light string `msgpack:"light"`
}
}
func getMode(args []string) (int, error) {
if currentMode == UNINITIALIZED {
return 0, errors.New("Mode not yet initialized, call `Setup`")
}
return int(currentMode), nil
}
func (args *setupArgs) handleNewMode() error {
var err error
var background, colorscheme, event string
switch currentMode {
case DARK:
background, event = "dark", "DarkMode"
if args.Colorscheme != nil {
colorscheme = args.Colorscheme.Dark
}
case LIGHT:
background, event = "light", "LightMode"
if args.Colorscheme != nil {
colorscheme = args.Colorscheme.Light
}
default:
return errors.New(fmt.Sprintf("Unexpected mode: %d", currentMode))
}
if c := args.Colorscheme; c != nil {
err = args.v.Command("colorscheme " + colorscheme)
if err != nil {
return err
}
}
if args.ChangeBackground {
err = args.v.SetOption("background", background)
if err != nil {
return err
}
}
if args.SendUserEvent {
err = args.v.Command("doautocmd User " + event)
if err != nil {
return err
}
}
return err
}
func setup(v *nvim.Nvim, args setupArgs) {
var err error
var p Portal
var ch <-chan uint32
if currentMode != UNINITIALIZED {
err = errors.New("setup() already called")
goto error
}
args.v = v
if p, err = setupPortal(); err != nil {
goto error
}
if currentMode, err = p.getMode(); err != nil {
goto error
}
if err = args.handleNewMode(); err != nil {
goto error
}
if ch, err = p.setupSignal(); err != nil {
goto error
}
go func() {
for {
if newMode := <-ch; newMode != currentMode {
currentMode = newMode
args.handleNewMode()
}
}
}()
return
error:
v.WriteErr(fmt.Sprintf("darkman: %v\n", err))
return
}
func main() {
plugin.Main(func(p *plugin.Plugin) error {
p.HandleFunction(&plugin.FunctionOptions{Name: "DarkmanGetMode"}, getMode)
p.HandleFunction(&plugin.FunctionOptions{Name: "DarkmanSetup"}, setup)
return nil
})
}