Skip to content

Commit

Permalink
settings: use a single underscore prefix for "private" methods.
Browse files Browse the repository at this point in the history
The double underscore is used to denote metamethods and could
potentially (though unlikely) cause problems with forward compatibility.
  • Loading branch information
torque committed Nov 13, 2017
1 parent d6ded6d commit 89a903b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/requires.moon
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ mp.get_osd_size = mp.get_osd_size or mp.get_screen_size
-- This is here instead of in settings.moon because it would conflict with the
-- default settings generator tool if it were placed in settings, and this is
-- the only file that goes before settings.
settings = { __defaults: { } }
settings = { _defaults: { } }

-- These methods are jammed in a metatable so that they can't be overridden from
-- the configuration file.
settingsMeta = {
__reload: =>
_reload: =>
-- Shallow copy should be good. This is necessary to reset values to the
-- defaults so that removing a configuration key has an effect.
for key, value in pairs @__defaults
for key, value in pairs @_defaults
settings[key] = value

options.read_options @, script_name .. '/main'
Expand All @@ -28,7 +28,7 @@ settingsMeta = {
-- about failing to stroke zero-height object.
@['bar-height-inactive'] = 1

__migrate: =>
_migrate: =>
oldConfig = mp.find_config_file 'lua-settings/%s.conf'\format script_name
newConfigFile = 'lua-settings/%s/main.conf'\format script_name
newConfig = mp.find_config_file newConfigFile
Expand Down Expand Up @@ -59,11 +59,11 @@ settingsMeta = {
log.info 'Configuration successfully migrated.'

__newindex: ( key, value ) =>
@__defaults[key] = value
@_defaults[key] = value
rawset @, key, value
}
settingsMeta.__index = settingsMeta

setmetatable settings, settingsMeta

settings\__migrate!
settings\_migrate!
2 changes: 1 addition & 1 deletion src/settings.moon
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,4 @@ will disappear without animating all the way off-screen. Positive values will
cause the display to animate the wrong direction.
]]

settings\__reload!
settings\_reload!
14 changes: 7 additions & 7 deletions tools/DefaultConfigGenerator.moon
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
convertToComment = ( text ) ->
result = { '' }
result = { }
text\gsub '(.-)\n', ( line ) ->
table.insert result, '# ' .. line

table.concat result, '\n'
return table.concat result, '\n'

convertToValue = ( value ) ->
switch type value
when 'boolean'
value and 'yes' or 'no'
return value and 'yes' or 'no'
when 'number'
tostring value
return tostring value
else
value
return value

combined = {}
for setting in *settings.__keys
for setting in *settings._keys
value = settings[setting]
comment = convertToComment helpText[setting]
settingString = setting .. '=' .. convertToValue value
settingString = setting .. '=' .. convertToValue( value ) .. '\n'
table.insert combined, comment
table.insert combined, settingString
table.insert combined, ''
Expand Down
7 changes: 4 additions & 3 deletions tools/ReadOptionsStub.moon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
options = { read_options: -> }
settings = { __keys: { }, __reload: => }
setmetatable settings, __newindex: ( key, value ) =>
table.insert @__keys, key
settings = { _keys: { }, _reload: => }
setmetatable settings, { __newindex: ( key, value ) =>
table.insert @_keys, key
rawset @, key, value
}

0 comments on commit 89a903b

Please sign in to comment.