Skip to content

Commit 509c009

Browse files
committed
Reworked Global_Option_Updater plugin
1 parent a720c1c commit 509c009

File tree

1 file changed

+111
-73
lines changed

1 file changed

+111
-73
lines changed

plugins/Global_Option_Updater.xml

Lines changed: 111 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
language="Lua"
1010
purpose="Shows / changes global options"
1111
date_written="2010-09-17 08:28:51"
12+
date_modified="2018-03-28 11:06"
1213
requires="4.50"
13-
version="1.0"
14+
version="2.0"
1415
>
1516
<description trim="y">
1617
<![CDATA[
@@ -42,100 +43,137 @@ if you use the GUI interface (global preferences) it may put the option back aga
4243
<alias
4344
match="change_global_option * *"
4445
enabled="y"
45-
send_to="12"
4646
sequence="100"
47+
script="ChangeOption"
4748
>
48-
<send>
49-
50-
-- open preferences database
51-
local db = assert (sqlite3.open (GetInfo (82)))
52-
local value = nil
53-
54-
-- see if this key already exists (if not, probably spelling error)
55-
for a in db:nrows "SELECT * FROM prefs WHERE name = '%1'" do
56-
value = a.value
57-
end -- for
58-
59-
-- if found, modify it
60-
if value then
61-
62-
if db:execute ("UPDATE prefs SET value = '%2' WHERE name = '%1'") ~= sqlite3.OK then
63-
ColourNote ("red", "", "Unable to modify preferences: " .. db:errmsg ())
64-
else
65-
if value ~= "%2" then
66-
ColourNote ("cyan", "", "Item '%1' changed from '" .. value .. "' to '%2'")
67-
else
68-
ColourNote ("cyan", "", "No change required, value of '%1' is already '%2'")
69-
end -- if
70-
end -- if updated OK
71-
72-
else
73-
ColourNote ("red", "", "Item '%1' is not in preferences database")
74-
end -- does not exist
75-
76-
db:close()
77-
78-
</send>
7949
</alias>
80-
50+
8151
<alias
8252
match="show_global_option *"
8353
enabled="y"
84-
send_to="12"
8554
sequence="100"
55+
script="ShowOption"
8656
>
87-
<send>
88-
89-
-- open preferences database
90-
local db = assert (sqlite3.open (GetInfo (82)))
91-
local value = nil
92-
93-
-- find the item
94-
for a in db:nrows "SELECT * FROM prefs WHERE name = '%1'" do
95-
value = a.value
96-
end -- for
97-
98-
-- if found, display it
99-
if value then
100-
ColourNote ("cyan", "", "Item '%1' has value '" .. value .. "'")
101-
else
102-
ColourNote ("red", "", "Item '%1' is not in preferences database")
103-
end -- does not exist
104-
105-
db:close()
106-
107-
</send>
10857
</alias>
10958

11059
<alias
11160
match="list_global_options"
11261
enabled="y"
113-
send_to="12"
11462
sequence="100"
63+
script="ListOptions"
11564
>
116-
<send>
117-
65+
</alias>
66+
67+
</aliases>
68+
69+
<script>
70+
71+
function ListOptions (name, line, wildcards)
72+
73+
ColourNote ("cyan", "", "Global Options")
74+
ColourNote ("cyan", "", string.rep ("-", 40))
75+
76+
-- show all
77+
78+
local t = GetGlobalOptionList ()
79+
table.sort (t)
80+
81+
for k, v in ipairs (t) do
82+
ColourNote ("cyan", "", v)
83+
end -- for
84+
85+
ColourNote ("cyan", "", string.rep ("-", 40))
86+
end -- ListOptions
87+
88+
function ShowOption (name, line, wildcards)
89+
90+
local whichOption = wildcards [1]
91+
92+
-- find all possible keys
93+
local t = { }
94+
for k, v in ipairs (GetGlobalOptionList ()) do
95+
t [v] = true
96+
end -- for
97+
98+
if not t [whichOption] then
99+
ColourNote ("red", "", "Item '" .. whichOption .. "' is not a global option. Try: list_global_options")
100+
ColourNote ("deepskyblue", "", "Option names are case-sensitive.")
101+
return
102+
end -- if option not found
103+
118104
-- open preferences database
119-
local db = assert (sqlite3.open (GetInfo (82)))
105+
local db = assert (sqlite3.open (GetInfo (82)))
106+
local value = nil
107+
108+
-- find the item
109+
for a in db:nrows ("SELECT * FROM prefs WHERE name = '" .. whichOption .. "'") do
110+
value = a.value
111+
end -- for
112+
113+
-- if found, display it
114+
if value then
115+
ColourTell ("cyan", "", "Item '" .. whichOption .. "' has value '")
116+
ColourTell ("orange", "", value)
117+
ColourNote ("cyan", "", "'")
118+
else
119+
ColourNote ("red", "", "Item '" .. whichOption .. "' is not in preferences database")
120+
end -- does not exist
120121

121-
ColourNote ("cyan", "", "Global Options")
122-
ColourNote ("cyan", "", string.rep ("-", 40))
122+
db:close()
123123

124-
-- show all
125-
for a in db:nrows "SELECT * FROM prefs ORDER BY name" do
126-
ColourNote ("cyan", "", a.name)
127-
end -- for
124+
end -- ShowOption
128125

129-
ColourNote ("cyan", "", string.rep ("-", 40))
126+
function ChangeOption (name, line, wildcards)
130127

131-
db:close()
128+
local whichOption = wildcards [1]
129+
local newValue = wildcards [2]
132130

133-
</send>
134-
</alias>
135-
136-
</aliases>
131+
-- find all possible keys
132+
local t = { }
133+
for k, v in ipairs (GetGlobalOptionList ()) do
134+
t [v] = true
135+
end -- for
137136

138-
<script>
137+
if not t [whichOption] then
138+
ColourNote ("red", "", "Item '" .. whichOption .. "' is not a global option. Try: list_global_options")
139+
ColourNote ("deepskyblue", "", "Option names are case-sensitive.")
140+
return
141+
end -- if
142+
143+
-- open preferences database
144+
local db = assert (sqlite3.open (GetInfo (82)))
145+
local value = '(nothing)'
146+
147+
-- see if this key already exists and if so, what its value is
148+
for a in db:nrows ("SELECT * FROM prefs WHERE name = '" .. whichOption .. "'") do
149+
value = a.value
150+
end -- for
151+
152+
-- fix up quotes
153+
newValueFixed = string.gsub (newValue, "'", "''")
154+
155+
if db:execute ("INSERT OR REPLACE INTO prefs " ..
156+
"(name, value) VALUES ('" .. whichOption .. "', '"
157+
.. newValue .. "')") ~= sqlite3.OK then
158+
ColourNote ("red", "", "Unable to modify preferences: " .. db:errmsg ())
159+
else
160+
if value ~= "%2" then
161+
ColourNote ("cyan", "", "Item '" .. whichOption .. "' changed from '"
162+
.. value .. "' to '" .. newValue .. "'")
163+
ColourNote ("orange", "", [[
164+
After changing an option you should exit the client and re-open it, otherwise
165+
if you use the GUI interface (global preferences) it may put the option back again
166+
to its old value because the options are kept in memory.]])
167+
168+
else
169+
ColourNote ("cyan", "", "No change required, value of '"
170+
.. whichOption .. "' is already '" .. newValue .. "'")
171+
end -- if
172+
end -- if updated OK
173+
174+
db:close()
175+
176+
end -- ChangeOption
139177

140178
-- show help
141179
ColourNote ("cyan", "", GetPluginInfo (GetPluginID (), 3))

0 commit comments

Comments
 (0)