-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
condition.go
69 lines (54 loc) · 1.23 KB
/
condition.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
package app
import (
"reflect"
)
// Condition represents a control structure that displays nodes depending on a
// given expression.
type Condition interface {
Node
// ElseIf sets the condition with the given nodes if previous expressions
// were not met and given expression is true.
ElseIf(expr bool, nodes ...Node) Condition
// Else sets the condition with the given UI elements if previous
// expressions were not met.
Else(nodes ...Node) Condition
isSatisfied() bool
nodes() []UI
}
// If returns a condition that contains the given nodes depending on the given
// expression.
func If(expr bool, nodes ...Node) Condition {
if !expr {
nodes = nil
}
return condition{
body: indirect(nodes...),
satisfied: !expr,
}
}
type condition struct {
body []UI
satisfied bool
}
func (c condition) nodeType() reflect.Type {
return reflect.TypeOf(c)
}
func (c condition) isSatisfied() bool {
return c.satisfied
}
func (c condition) nodes() []UI {
return c.body
}
func (c condition) ElseIf(expr bool, nodes ...Node) Condition {
if !c.satisfied {
return c
}
if expr {
c.body = indirect(nodes...)
c.satisfied = false
}
return c
}
func (c condition) Else(nodes ...Node) Condition {
return c.ElseIf(true, nodes...)
}