-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.go
190 lines (169 loc) · 3.42 KB
/
rule.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
package rules
import (
"fmt"
"strconv"
)
type Apply int
const (
SplitDirection Apply = iota
SplitRatio
MinWidth
MaxWidth
MinHeight
MaxHeight
Layout
Layer
IsPseudoTiled
IsFloating
IsLocked
IsSticky
IsPrivate
IsBordered
IsCentered
IsManaged
IsFocused
ClientSelector
DesktopSelector
MonitorSelector
)
var stringApply map[string]Apply = map[string]Apply{
"direction": SplitDirection, //string
"ratio": SplitRatio, //float64
"minwidth": MinWidth, //uint16
"maxwidth": MaxWidth, //uint16
"minheight": MinHeight, //uint16
"maxheight": MaxHeight, //uint16
"layout": Layout, //string
"layer": Layer, //string
"pseudotiled": IsPseudoTiled, //bool
"floating": IsFloating, //bool
"lock": IsLocked, //bool
"sticky": IsSticky, //bool
"private": IsPrivate, //bool
"bordered": IsBordered, //bool
"centered": IsCentered, //bool
"manage": IsManaged, //bool
"focus": IsFocused, //bool
"client": ClientSelector, //string
"desktop": DesktopSelector, //string
"monitor": MonitorSelector, //string
}
func (a Apply) String() string {
switch a {
case SplitDirection:
return "split direction"
case SplitRatio:
return "split ratio"
case MinWidth:
return "minimum width"
case MaxWidth:
return "maximum width"
case MinHeight:
return "minimum height"
case MaxHeight:
return "maximum height"
case IsPseudoTiled:
return "pseudo tiled"
case IsFloating:
return "floating"
case IsLocked:
return "locked"
case IsSticky:
return "sticky"
case IsPrivate:
return "private"
case IsBordered:
return "bordered"
case IsCentered:
return "centered"
case IsManaged:
return "managed"
case IsFocused:
return "focused"
case ClientSelector:
return "select client"
case DesktopSelector:
return "select desktop"
case MonitorSelector:
return "select monitor"
}
return ""
}
type Component interface {
Applies() Apply
Implementr
}
type Implementr interface {
String() string
Uint16() uint16
Float() float64
Bool() bool
}
type component struct {
applies Apply
raw string
}
func newComponent(apply, to string) Component {
if a, ok := stringApply[apply]; ok {
return &component{
applies: a,
raw: to,
}
}
return nil
}
func (c *component) Applies() Apply {
return c.applies
}
func (c *component) String() string {
return c.raw
}
func (c *component) Uint16() uint16 {
if i, err := strconv.ParseUint(c.raw, 10, 16); err == nil {
return uint16(i)
}
return 0
}
func (c *component) Float() float64 {
if f, err := strconv.ParseFloat(c.raw, 64); err == nil {
return f
}
return 0.0
}
func (c *component) Bool() bool {
if b, err := strconv.ParseBool(c.raw); err == nil {
return b
}
return false
}
type Rule interface {
Cause() Component
Effect() Component
String() string
Reuse() bool
}
type rule struct {
cause Component
effect Component
reuse bool
}
func newRule(c, k, e, v string, reuse bool) Rule {
if cause := newComponent(c, k); cause != nil {
if effect := newComponent(e, v); effect != nil {
return &rule{cause, effect, reuse}
}
}
return nil
}
func (r *rule) Cause() Component {
return r.cause
}
func (r *rule) Effect() Component {
return r.effect
}
func (r *rule) String() string {
return fmt.Sprintf("%s %s", r.cause.String(), r.effect.String())
}
func (r *rule) Reuse() bool {
return r.reuse
}