-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
142 lines (120 loc) · 3.12 KB
/
Copy pathsettings.go
File metadata and controls
142 lines (120 loc) · 3.12 KB
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
package gani
import (
"fmt"
"strings"
)
// Settings defines the Gani file settings
type Settings struct {
Looping bool
Continuous bool
SingleDirection bool
SetBackTo string
DefaultParams [3]string
DefaultAttrs [3]string
DefaultHead string
DefaultBody string
}
func (s *Settings) String() string {
builder := strings.Builder{}
if s.Looping {
builder.WriteString("LOOP")
builder.WriteString("\n")
}
if s.Continuous {
builder.WriteString("CONTINUOUS")
builder.WriteString("\n")
}
if s.SingleDirection {
builder.WriteString("SINGLEDIRECTION")
builder.WriteString("\n")
}
if !strings.EqualFold(s.SetBackTo, "") {
builder.WriteString(fmt.Sprintf("SETBACKTO %s", s.SetBackTo))
builder.WriteString("\n")
}
for i, param := range s.DefaultParams {
if !strings.EqualFold(param, "") {
builder.WriteString(fmt.Sprintf("DEFAULTPARAM%d %s", i+1, param))
builder.WriteString("\n")
}
}
for i, attr := range s.DefaultAttrs {
if !strings.EqualFold(attr, "") {
builder.WriteString(fmt.Sprintf("DEFAULTATTR%d %s", i+1, attr))
builder.WriteString("\n")
}
}
if !strings.EqualFold(s.DefaultHead, "") {
builder.WriteString(fmt.Sprintf("DEFAULTHEAD %s", s.DefaultHead))
builder.WriteString("\n")
}
if !strings.EqualFold(s.DefaultBody, "") {
builder.WriteString(fmt.Sprintf("DEFAULTBODY %s", s.DefaultBody))
builder.WriteString("\n")
}
return builder.String()
}
// Parse attempts to parse the given line to a Settings value
func (s *Settings) Parse(line string) error {
strs := strings.Split(line, " ")
if len(strs) == 1 {
return s.parseBool(strs[0])
}
return s.parseKeyValuePair(strs[0], strs[1])
}
// parseBool attempts to determine a boolean Settings value from the given string
func (s *Settings) parseBool(str string) error {
if strings.EqualFold(str, "LOOP") {
s.Looping = true
return nil
}
if strings.EqualFold(str, "CONTINUOUS") {
s.Continuous = true
return nil
}
if strings.EqualFold(str, "SINGLEDIRECTION") {
s.SingleDirection = true
return nil
}
return fmt.Errorf("unknown boolean field: %s", str)
}
// parseKeyValuePair attempts to determine a key value pair Settings value for the given
// key and value
func (s *Settings) parseKeyValuePair(key, value string) error {
switch key {
case "SETBACKTO":
s.SetBackTo = value
case "DEFAULTPARAM1":
s.DefaultParams[0] = value
case "DEFAULTPARAM2":
s.DefaultParams[1] = value
case "DEFAULTPARAM3":
s.DefaultParams[2] = value
case "DEFAULTATTR1":
s.DefaultAttrs[0] = value
case "DEFAULTATTR2":
s.DefaultAttrs[1] = value
case "DEFAULTATTR3":
s.DefaultAttrs[2] = value
case "DEFAULTHEAD":
s.DefaultHead = value
case "DEFAULTBODY":
s.DefaultBody = value
default:
return fmt.Errorf("unknown kvp")
}
return nil
}
// NewSettings returns the default settings object found when creating a new Gani
func NewSettings() *Settings {
return &Settings{
Looping: false,
Continuous: false,
SingleDirection: false,
SetBackTo: "",
DefaultParams: [3]string{},
DefaultAttrs: [3]string{"hat0.png"},
DefaultHead: "head19.png",
DefaultBody: "body.png",
}
}