/
Options.lua
236 lines (203 loc) · 6.81 KB
/
Options.lua
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
Scroll = {
Position = 1,
ItemHeight = 0,
Folders = {},
}
Variables = {
Style = 'Default',
DayLabels = '',
HideLastWeek = 0,
EventFile = '#**@**#Calendars\\Holidays.xml',
LabelText = '',
LeadingZeroes = 0,
MonthLabels = '',
StartOnMonday = 0,
UseLocalMonths = 1,
NextFormat = '',
ShowMoonPhases = 1,
MoonColor = '',
}
function Initialize()
if SKIN:GetVariable('MoonColor') == '' then
SKIN:Bang('!SetOption', 'MoonColorFill', 'LineColor', '0,0,0,1')
end
end -- Initialize
function Update()
local fullpath = SKIN:GetVariable('EventFile'):match('^([^|]+)')
if not fullpath then -- No EventFile
return ''
elseif fullpath:match('%.') then -- With file extension
return fullpath:match('(.-)[^\\/]-%.[^%.]*$')
else -- No file extension
return fullpath:match('(.+)[/\\]$') .. '\\'
end
end -- Update
function SetColor(name, color)
if name == 'MoonColor' then
SKIN:Bang('!SetOption', 'MoonColorFill', 'LineColor', '#*MoonColor*#')
end
SKIN:Bang('!SetVariable', name, color)
SKIN:Bang('!Update')
end -- SetColor
-- ========== START SCROLLBOX ==========
function DrawItems()
local cpos = TablePosition(Scroll.Folders, SKIN:GetVariable('Style'), function(input) return input.folder end)
for i = 1, 4 do
local tpos = i - 1 + Scroll.Position
if tpos >= cpos then tpos = tpos + 1 end -- Skip the current style
for k, v in pairs{
Text = Scroll.Folders[tpos].name,
ToolTipTitle = Scroll.Folders[tpos].author,
ToolTipText = Scroll.Folders[tpos].info,
} do SKIN:Bang('!SetOption', 'ScrollLine' .. i, k, v) end
end
SKIN:Bang('!SetOption', 'StyleCurrent', 'Text', Scroll.Folders[cpos].name)
SKIN:Bang('!SetOption', 'TopBar', 'H', Scroll.ItemHeight * (Scroll.Position - 1))
SKIN:Bang('!SetOption', 'BotBar', 'H', Scroll.ItemHeight * (#Scroll.Folders - Scroll.Position - 4))
SKIN:Bang('!Update')
end -- DrawItems
function UnpackList(input)
for _, word in ipairs(delim(input)) do
local temp = {folder = word, name = word, author = '', info = '',}
local ini = ReadIni(SKIN:ReplaceVariables(('#@#Styles\\%s\\meta.txt'):format(word)))
if ini.metadata then
temp = {
folder = word,
name = ini.metadata.name or word,
author = ini.metadata.author and ('Created by ' .. ini.metadata.author) or '',
info = ini.metadata.info or '',
}
end
table.insert(Scroll.Folders, temp)
end
Scroll.ItemHeight = 60 / (#Scroll.Folders - 1)
SKIN:Bang('!SetOption', 'MidBar', 'H', 4 * Scroll.ItemHeight)
DrawItems()
end -- UnpackList
function ScrollUp()
Scroll.Position = Scroll.Position == 1 and 1 or (Scroll.Position - 1)
DrawItems()
end -- ScrollUp
function ScrollDown()
if #Scroll.Folders > 4 then
local max = #Scroll.Folders - 4
Scroll.Position = Scroll.Position == max and max or (Scroll.Position + 1)
end
DrawItems()
end -- ScrollDown
function SetStyle(input)
SKIN:Bang('!HideMeterGroup', 'Scroll')
local npos = TablePosition(Scroll.Folders, SKIN:GetMeter(input):GetOption('Text'), function(temp) return temp.name end)
if npos then
MessageOutput()
SKIN:Bang('!SetVariable', 'Style', Scroll.Folders[npos].folder)
DrawItems()
end
end -- SetStyle
-- ========== END SCROLLBOX ==========
function Save()
MessageOutput()
file = SKIN:ReplaceVariables('#@#Settings.inc')
for k, _ in pairs(Variables) do SKIN:Bang('!WriteKeyValue', 'Variables', k, SKIN:GetVariable(k), file) end
SKIN:Bang('!Refresh', 'LuaCalendar')
end -- Save
function Defaults()
for k, v in pairs(Variables) do SKIN:Bang('!SetVariable', k, v) end
MessageOutput('Click Save to confirm Defaults')
DrawItems()
end -- Defaults
function CheckUpdate()
local ini = ReadIni(SKIN:ReplaceVariables('#ROOTCONFIGPATH#LuaCalendar.ini'))
local CurrentVersion = tonumber(SKIN:GetMeasure('UpdateVersion'):GetStringValue():match('<version>(.+)</version>'))
if ini.metadata and CurrentVersion then
local LocalVersion = tonumber(ini.metadata.version)
if LocalVersion < CurrentVersion then
SKIN:Bang('!SetOption', 'UpdateMeter', 'Text', 'An Update is Available: v' .. CurrentVersion)
elseif CurrentVersion < LocalVersion then
SKIN:Bang('!SetOption', 'UpdateMeter', 'Text', 'Thanks for testing the Beta!')
end
end
end -- CheckUpdate
function CheckDayLabels(input)
if lengthcheck(input, 7, 'DayLabels must have seven days.') then
SKIN:Bang('!SetVariable', 'DayLabels', input)
end
end -- CheckDayLabels
function CheckMonthLabels(input)
if lengthcheck(input, 12, 'MonthLabels must have twelve months.') then
SKIN:Bang('!SetVariable', 'MonthLabels', input)
end
end -- CheckMonthLabels
function CheckMLFormat(input)
if varcheck(input, {'mname', 'year', 'today', 'month'}, 'Invalid LabelText variable {$%s}') then
SKIN:Bang('!SetVariable', 'LabelText', input)
end
end -- CheckMLFormat
function CheckELFormat(input)
if varcheck(input, {'day', 'desc'}, 'Invalid NextFormat variable {$%s}') then
SKIN:Bang('!SetVariable', 'NextFormat', input)
end
end -- CheckELFormat
-- ========== HELPER FUNCTIONS ==========
function lengthcheck(line, num, msg)
MessageOutput()
SKIN:Bang('!HideMeterGroup', 'Scroll')
local temp = delim(line)
if #temp == num or input == '' then
MessageOutput()
return true
else
MessageOutput(msg)
end
end -- lengthcheck
function varcheck(input, list, msg)
MessageOutput()
SKIN:Bang('!HideMeterGroup', 'Scroll')
for word in input:gmatch('{%$([^}]+)}') do
if not TablePosition(list, word:lower(), function(temp) return temp:lower() end) then
MessageOutput(msg:format(word))
break
end
end
return true
end -- varcheck
function MessageOutput(input)
SKIN:Bang('!SetOption', 'Message', 'Text', input or ' ')
end -- MessageOutput
function TablePosition(tbl, key, func)
for i, v in ipairs(tbl) do
if (func and func(v) or v) == key then
return i
end
end
return nil
end -- TablePosition
function delim(input)
local temp = {}
for word in input:gmatch('[^|]+') do table.insert(temp, word) end
return temp
end -- delim
function ReadIni(inputfile)
local file = io.open(inputfile, 'r')
local tbl, section = {}
if file then
local num = 0
for line in file:lines() do
num, line = (num + 1), line:gsub('\t', ' ')
if not line:match('^%s-;') then
local key, command = line:match('^([^=]+)=(.+)')
if line:match('^%s-%[.+%]') then
section = line:lower():match('^%s-%[([^%]]+)')
if not tbl[section] then tbl[section] = {} end
elseif key and command and section then
tbl[section][key:lower():match('^%s*(%S*)%s*$')] = command:match('^%s*(.-)%s*$')
elseif #line > 0 and section and not key or command then
print(num .. ': Invalid property or value.')
end
end
end
if not section then print('No sections found in ' .. inputfile) end
file:close()
end
return tbl
end -- ReadIni